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> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 35 | #include <linux/bit_spinlock.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 36 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 37 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 38 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 40 | #include <linux/ratelimit.h> |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 41 | #include <linux/mount.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 42 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 43 | #include <linux/blkdev.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 44 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 45 | #include "ctree.h" |
| 46 | #include "disk-io.h" |
| 47 | #include "transaction.h" |
| 48 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 49 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 50 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 51 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 52 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 53 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 54 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 55 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 56 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 57 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 58 | #include "backref.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 59 | |
| 60 | struct btrfs_iget_args { |
| 61 | u64 ino; |
| 62 | struct btrfs_root *root; |
| 63 | }; |
| 64 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 65 | static const struct inode_operations btrfs_dir_inode_operations; |
| 66 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 67 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 68 | static const struct inode_operations btrfs_special_inode_operations; |
| 69 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 70 | static const struct address_space_operations btrfs_aops; |
| 71 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 72 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 73 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 74 | |
| 75 | static struct kmem_cache *btrfs_inode_cachep; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 76 | static struct kmem_cache *btrfs_delalloc_work_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 77 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 78 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 79 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 80 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 81 | |
| 82 | #define S_SHIFT 12 |
| 83 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 84 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 85 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 86 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 87 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 88 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 89 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 90 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 91 | }; |
| 92 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 93 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 94 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 95 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 96 | static noinline int cow_file_range(struct inode *inode, |
| 97 | struct page *locked_page, |
| 98 | u64 start, u64 end, int *page_started, |
| 99 | unsigned long *nr_written, int unlock); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 100 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 101 | u64 len, u64 orig_start, |
| 102 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 103 | u64 orig_block_len, u64 ram_bytes, |
| 104 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 105 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 106 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 107 | struct inode *inode, struct inode *dir, |
| 108 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 109 | { |
| 110 | int err; |
| 111 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 112 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 113 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 114 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 115 | return err; |
| 116 | } |
| 117 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 118 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 119 | * this does all the hard work for inserting an inline extent into |
| 120 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 121 | * no overlapping inline items exist in the btree |
| 122 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 123 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 124 | struct btrfs_root *root, struct inode *inode, |
| 125 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 126 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 127 | struct page **compressed_pages) |
| 128 | { |
| 129 | struct btrfs_key key; |
| 130 | struct btrfs_path *path; |
| 131 | struct extent_buffer *leaf; |
| 132 | struct page *page = NULL; |
| 133 | char *kaddr; |
| 134 | unsigned long ptr; |
| 135 | struct btrfs_file_extent_item *ei; |
| 136 | int err = 0; |
| 137 | int ret; |
| 138 | size_t cur_size = size; |
| 139 | size_t datasize; |
| 140 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 141 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 142 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 143 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 144 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 145 | path = btrfs_alloc_path(); |
| 146 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 147 | return -ENOMEM; |
| 148 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 149 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 150 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 151 | key.objectid = btrfs_ino(inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 152 | key.offset = start; |
| 153 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 154 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 155 | |
| 156 | inode_add_bytes(inode, size); |
| 157 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 158 | datasize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 159 | if (ret) { |
| 160 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 161 | goto fail; |
| 162 | } |
| 163 | leaf = path->nodes[0]; |
| 164 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 165 | struct btrfs_file_extent_item); |
| 166 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 167 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 168 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 169 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 170 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 171 | ptr = btrfs_file_extent_inline_start(ei); |
| 172 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 173 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 174 | struct page *cpage; |
| 175 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 176 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 177 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 178 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 179 | PAGE_CACHE_SIZE); |
| 180 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 181 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 182 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 183 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 184 | |
| 185 | i++; |
| 186 | ptr += cur_size; |
| 187 | compressed_size -= cur_size; |
| 188 | } |
| 189 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 190 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 191 | } else { |
| 192 | page = find_get_page(inode->i_mapping, |
| 193 | start >> PAGE_CACHE_SHIFT); |
| 194 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 195 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 196 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 197 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 198 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 199 | page_cache_release(page); |
| 200 | } |
| 201 | btrfs_mark_buffer_dirty(leaf); |
| 202 | btrfs_free_path(path); |
| 203 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 204 | /* |
| 205 | * we're an inline extent, so nobody can |
| 206 | * extend the file past i_size without locking |
| 207 | * a page we already have locked. |
| 208 | * |
| 209 | * We must do any isize and inode updates |
| 210 | * before we unlock the pages. Otherwise we |
| 211 | * could end up racing with unlink. |
| 212 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 213 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 214 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 215 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 216 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 217 | fail: |
| 218 | btrfs_free_path(path); |
| 219 | return err; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /* |
| 224 | * conditionally insert an inline extent into the file. This |
| 225 | * does the checks required to make sure the data is small enough |
| 226 | * to fit as an inline extent. |
| 227 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 228 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 229 | struct btrfs_root *root, |
| 230 | struct inode *inode, u64 start, u64 end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 231 | size_t compressed_size, int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 232 | struct page **compressed_pages) |
| 233 | { |
| 234 | u64 isize = i_size_read(inode); |
| 235 | u64 actual_end = min(end + 1, isize); |
| 236 | u64 inline_len = actual_end - start; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 237 | u64 aligned_end = ALIGN(end, root->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 238 | u64 data_len = inline_len; |
| 239 | int ret; |
| 240 | |
| 241 | if (compressed_size) |
| 242 | data_len = compressed_size; |
| 243 | |
| 244 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 245 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 246 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 247 | (!compressed_size && |
| 248 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 249 | end + 1 < isize || |
| 250 | data_len > root->fs_info->max_inline) { |
| 251 | return 1; |
| 252 | } |
| 253 | |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 254 | ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 255 | if (ret) |
| 256 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 257 | |
| 258 | if (isize > actual_end) |
| 259 | inline_len = min_t(u64, isize, actual_end); |
| 260 | ret = insert_inline_extent(trans, root, inode, start, |
| 261 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 262 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 263 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 264 | btrfs_abort_transaction(trans, root, ret); |
| 265 | return ret; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 266 | } else if (ret == -ENOSPC) { |
| 267 | return 1; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 268 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 269 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 270 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 271 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 272 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 276 | struct async_extent { |
| 277 | u64 start; |
| 278 | u64 ram_size; |
| 279 | u64 compressed_size; |
| 280 | struct page **pages; |
| 281 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 282 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 283 | struct list_head list; |
| 284 | }; |
| 285 | |
| 286 | struct async_cow { |
| 287 | struct inode *inode; |
| 288 | struct btrfs_root *root; |
| 289 | struct page *locked_page; |
| 290 | u64 start; |
| 291 | u64 end; |
| 292 | struct list_head extents; |
| 293 | struct btrfs_work work; |
| 294 | }; |
| 295 | |
| 296 | static noinline int add_async_extent(struct async_cow *cow, |
| 297 | u64 start, u64 ram_size, |
| 298 | u64 compressed_size, |
| 299 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 300 | unsigned long nr_pages, |
| 301 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 302 | { |
| 303 | struct async_extent *async_extent; |
| 304 | |
| 305 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 306 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 307 | async_extent->start = start; |
| 308 | async_extent->ram_size = ram_size; |
| 309 | async_extent->compressed_size = compressed_size; |
| 310 | async_extent->pages = pages; |
| 311 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 312 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 313 | list_add_tail(&async_extent->list, &cow->extents); |
| 314 | return 0; |
| 315 | } |
| 316 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 317 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 318 | * we create compressed extents in two phases. The first |
| 319 | * phase compresses a range of pages that have already been |
| 320 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 321 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 322 | * This is done inside an ordered work queue, and the compression |
| 323 | * is spread across many cpus. The actual IO submission is step |
| 324 | * two, and the ordered work queue takes care of making sure that |
| 325 | * happens in the same order things were put onto the queue by |
| 326 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 327 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 328 | * If this code finds it can't get good compression, it puts an |
| 329 | * entry onto the work queue to write the uncompressed bytes. This |
| 330 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 331 | * are written in the same order that the flusher thread sent them |
| 332 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 333 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 334 | static noinline int compress_file_range(struct inode *inode, |
| 335 | struct page *locked_page, |
| 336 | u64 start, u64 end, |
| 337 | struct async_cow *async_cow, |
| 338 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 339 | { |
| 340 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 341 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 342 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 343 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 344 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 345 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 346 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 347 | struct page **pages = NULL; |
| 348 | unsigned long nr_pages; |
| 349 | unsigned long nr_pages_ret = 0; |
| 350 | unsigned long total_compressed = 0; |
| 351 | unsigned long total_in = 0; |
| 352 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 353 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 354 | int i; |
| 355 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 356 | int compress_type = root->fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 357 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 358 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 359 | /* if this is a small write inside eof, kick off a defrag */ |
| 360 | if ((end - start + 1) < 16 * 1024 && |
| 361 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 362 | btrfs_add_inode_defrag(NULL, inode); |
| 363 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 364 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 365 | again: |
| 366 | will_compress = 0; |
| 367 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 368 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 369 | |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 370 | /* |
| 371 | * we don't want to send crud past the end of i_size through |
| 372 | * compression, that's just a waste of CPU time. So, if the |
| 373 | * end of the file is before the start of our current |
| 374 | * requested range of bytes, we bail out to the uncompressed |
| 375 | * cleanup code that can deal with all of this. |
| 376 | * |
| 377 | * It isn't really the fastest way to fix things, but this is a |
| 378 | * very uncommon corner. |
| 379 | */ |
| 380 | if (actual_end <= start) |
| 381 | goto cleanup_and_bail_uncompressed; |
| 382 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 383 | total_compressed = actual_end - start; |
| 384 | |
| 385 | /* we want to make sure that amount of ram required to uncompress |
| 386 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 387 | * of a compressed extent to 128k. This is a crucial number |
| 388 | * because it also controls how easily we can spread reads across |
| 389 | * cpus for decompression. |
| 390 | * |
| 391 | * We also want to make sure the amount of IO required to do |
| 392 | * a random read is reasonably small, so we limit the size of |
| 393 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 394 | */ |
| 395 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 396 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 397 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 398 | total_in = 0; |
| 399 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 400 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 401 | /* |
| 402 | * we do compression for mount -o compress and when the |
| 403 | * inode has not been flagged as nocompress. This flag can |
| 404 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 405 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 406 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 407 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 408 | (BTRFS_I(inode)->force_compress) || |
| 409 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 410 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 411 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 412 | if (!pages) { |
| 413 | /* just bail out to the uncompressed code */ |
| 414 | goto cont; |
| 415 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 416 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 417 | if (BTRFS_I(inode)->force_compress) |
| 418 | compress_type = BTRFS_I(inode)->force_compress; |
| 419 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 420 | /* |
| 421 | * we need to call clear_page_dirty_for_io on each |
| 422 | * page in the range. Otherwise applications with the file |
| 423 | * mmap'd can wander in and change the page contents while |
| 424 | * we are compressing them. |
| 425 | * |
| 426 | * If the compression fails for any reason, we set the pages |
| 427 | * dirty again later on. |
| 428 | */ |
| 429 | extent_range_clear_dirty_for_io(inode, start, end); |
| 430 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 431 | ret = btrfs_compress_pages(compress_type, |
| 432 | inode->i_mapping, start, |
| 433 | total_compressed, pages, |
| 434 | nr_pages, &nr_pages_ret, |
| 435 | &total_in, |
| 436 | &total_compressed, |
| 437 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 438 | |
| 439 | if (!ret) { |
| 440 | unsigned long offset = total_compressed & |
| 441 | (PAGE_CACHE_SIZE - 1); |
| 442 | struct page *page = pages[nr_pages_ret - 1]; |
| 443 | char *kaddr; |
| 444 | |
| 445 | /* zero the tail end of the last page, we might be |
| 446 | * sending it down to disk |
| 447 | */ |
| 448 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 449 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 450 | memset(kaddr + offset, 0, |
| 451 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 452 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 453 | } |
| 454 | will_compress = 1; |
| 455 | } |
| 456 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 457 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 458 | if (start == 0) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 459 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 460 | if (IS_ERR(trans)) { |
| 461 | ret = PTR_ERR(trans); |
| 462 | trans = NULL; |
| 463 | goto cleanup_and_out; |
| 464 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 465 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 466 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 467 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 468 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 469 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 470 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 471 | */ |
| 472 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 473 | start, end, 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 474 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 475 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 476 | ret = cow_file_range_inline(trans, root, inode, |
| 477 | start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 478 | total_compressed, |
| 479 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 480 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 481 | if (ret <= 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 482 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 483 | * inline extent creation worked or returned error, |
| 484 | * we don't need to create any more async work items. |
| 485 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 486 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 487 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 488 | &BTRFS_I(inode)->io_tree, |
| 489 | start, end, NULL, |
| 490 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 491 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 492 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 493 | |
| 494 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 495 | goto free_pages_out; |
| 496 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 497 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | if (will_compress) { |
| 501 | /* |
| 502 | * we aren't doing an inline extent round the compressed size |
| 503 | * up to a block size boundary so the allocator does sane |
| 504 | * things |
| 505 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 506 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 507 | |
| 508 | /* |
| 509 | * one last check to make sure the compression is really a |
| 510 | * win, compare the page count read with the blocks on disk |
| 511 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 512 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 513 | if (total_compressed >= total_in) { |
| 514 | will_compress = 0; |
| 515 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 516 | num_bytes = total_in; |
| 517 | } |
| 518 | } |
| 519 | if (!will_compress && pages) { |
| 520 | /* |
| 521 | * the compression code ran but failed to make things smaller, |
| 522 | * free any pages it allocated and our page pointer array |
| 523 | */ |
| 524 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 525 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 526 | page_cache_release(pages[i]); |
| 527 | } |
| 528 | kfree(pages); |
| 529 | pages = NULL; |
| 530 | total_compressed = 0; |
| 531 | nr_pages_ret = 0; |
| 532 | |
| 533 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 534 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 535 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 536 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 537 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 538 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 539 | if (will_compress) { |
| 540 | *num_added += 1; |
| 541 | |
| 542 | /* the async work queues will take care of doing actual |
| 543 | * allocation on disk for these compressed pages, |
| 544 | * and will submit them to the elevator. |
| 545 | */ |
| 546 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 547 | total_compressed, pages, nr_pages_ret, |
| 548 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 549 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 550 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 551 | start += num_bytes; |
| 552 | pages = NULL; |
| 553 | cond_resched(); |
| 554 | goto again; |
| 555 | } |
| 556 | } else { |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 557 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 558 | /* |
| 559 | * No compression, but we still need to write the pages in |
| 560 | * the file we've been given so far. redirty the locked |
| 561 | * page if it corresponds to our extent and set things up |
| 562 | * for the async work queue to run cow_file_range to do |
| 563 | * the normal delalloc dance |
| 564 | */ |
| 565 | if (page_offset(locked_page) >= start && |
| 566 | page_offset(locked_page) <= end) { |
| 567 | __set_page_dirty_nobuffers(locked_page); |
| 568 | /* unlocked later on in the async handlers */ |
| 569 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 570 | if (redirty) |
| 571 | extent_range_redirty_for_io(inode, start, end); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 572 | add_async_extent(async_cow, start, end - start + 1, |
| 573 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 574 | *num_added += 1; |
| 575 | } |
| 576 | |
| 577 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 578 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 579 | |
| 580 | free_pages_out: |
| 581 | for (i = 0; i < nr_pages_ret; i++) { |
| 582 | WARN_ON(pages[i]->mapping); |
| 583 | page_cache_release(pages[i]); |
| 584 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 585 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 586 | |
| 587 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 588 | |
| 589 | cleanup_and_out: |
| 590 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 591 | start, end, NULL, |
| 592 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 593 | EXTENT_CLEAR_DIRTY | |
| 594 | EXTENT_CLEAR_DELALLOC | |
| 595 | EXTENT_SET_WRITEBACK | |
| 596 | EXTENT_END_WRITEBACK); |
| 597 | if (!trans || IS_ERR(trans)) |
| 598 | btrfs_error(root->fs_info, ret, "Failed to join transaction"); |
| 599 | else |
| 600 | btrfs_abort_transaction(trans, root, ret); |
| 601 | goto free_pages_out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* |
| 605 | * phase two of compressed writeback. This is the ordered portion |
| 606 | * of the code, which only gets called in the order the work was |
| 607 | * queued. We walk all the async extents created by compress_file_range |
| 608 | * and send them down to the disk. |
| 609 | */ |
| 610 | static noinline int submit_compressed_extents(struct inode *inode, |
| 611 | struct async_cow *async_cow) |
| 612 | { |
| 613 | struct async_extent *async_extent; |
| 614 | u64 alloc_hint = 0; |
| 615 | struct btrfs_trans_handle *trans; |
| 616 | struct btrfs_key ins; |
| 617 | struct extent_map *em; |
| 618 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 619 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 620 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 621 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 622 | |
| 623 | if (list_empty(&async_cow->extents)) |
| 624 | return 0; |
| 625 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 626 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 627 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 628 | async_extent = list_entry(async_cow->extents.next, |
| 629 | struct async_extent, list); |
| 630 | list_del(&async_extent->list); |
| 631 | |
| 632 | io_tree = &BTRFS_I(inode)->io_tree; |
| 633 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 634 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 635 | /* did the compression code fall back to uncompressed IO? */ |
| 636 | if (!async_extent->pages) { |
| 637 | int page_started = 0; |
| 638 | unsigned long nr_written = 0; |
| 639 | |
| 640 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 641 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 642 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 643 | |
| 644 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 645 | ret = cow_file_range(inode, async_cow->locked_page, |
| 646 | async_extent->start, |
| 647 | async_extent->start + |
| 648 | async_extent->ram_size - 1, |
| 649 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 650 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 651 | /* JDM XXX */ |
| 652 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 653 | /* |
| 654 | * if page_started, cow_file_range inserted an |
| 655 | * inline extent and took care of all the unlocking |
| 656 | * and IO for us. Otherwise, we need to submit |
| 657 | * all those pages down to the drive. |
| 658 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 659 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 660 | extent_write_locked_range(io_tree, |
| 661 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 662 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 663 | async_extent->ram_size - 1, |
| 664 | btrfs_get_extent, |
| 665 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 666 | else if (ret) |
| 667 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 668 | kfree(async_extent); |
| 669 | cond_resched(); |
| 670 | continue; |
| 671 | } |
| 672 | |
| 673 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 674 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 675 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 676 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 677 | if (IS_ERR(trans)) { |
| 678 | ret = PTR_ERR(trans); |
| 679 | } else { |
| 680 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 681 | ret = btrfs_reserve_extent(trans, root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 682 | async_extent->compressed_size, |
| 683 | async_extent->compressed_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 684 | 0, alloc_hint, &ins, 1); |
Miao Xie | 962197b | 2012-09-11 23:27:35 -0600 | [diff] [blame] | 685 | if (ret && ret != -ENOSPC) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 686 | btrfs_abort_transaction(trans, root, ret); |
| 687 | btrfs_end_transaction(trans, root); |
| 688 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 689 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 690 | if (ret) { |
| 691 | int i; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 692 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 693 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 694 | WARN_ON(async_extent->pages[i]->mapping); |
| 695 | page_cache_release(async_extent->pages[i]); |
| 696 | } |
| 697 | kfree(async_extent->pages); |
| 698 | async_extent->nr_pages = 0; |
| 699 | async_extent->pages = NULL; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 700 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 701 | if (ret == -ENOSPC) |
| 702 | goto retry; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 703 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 704 | } |
| 705 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 706 | /* |
| 707 | * here we're doing allocation and writeback of the |
| 708 | * compressed pages |
| 709 | */ |
| 710 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 711 | async_extent->start + |
| 712 | async_extent->ram_size - 1, 0); |
| 713 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 714 | em = alloc_extent_map(); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 715 | if (!em) |
| 716 | goto out_free_reserve; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 717 | em->start = async_extent->start; |
| 718 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 719 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 720 | em->mod_start = em->start; |
| 721 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 722 | |
| 723 | em->block_start = ins.objectid; |
| 724 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 725 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 726 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 727 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 728 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 729 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 730 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 731 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 732 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 733 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 734 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 735 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 736 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 737 | if (ret != -EEXIST) { |
| 738 | free_extent_map(em); |
| 739 | break; |
| 740 | } |
| 741 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 742 | async_extent->start + |
| 743 | async_extent->ram_size - 1, 0); |
| 744 | } |
| 745 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 746 | if (ret) |
| 747 | goto out_free_reserve; |
| 748 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 749 | ret = btrfs_add_ordered_extent_compress(inode, |
| 750 | async_extent->start, |
| 751 | ins.objectid, |
| 752 | async_extent->ram_size, |
| 753 | ins.offset, |
| 754 | BTRFS_ORDERED_COMPRESSED, |
| 755 | async_extent->compress_type); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 756 | if (ret) |
| 757 | goto out_free_reserve; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 758 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 759 | /* |
| 760 | * clear dirty, set writeback and unlock the pages. |
| 761 | */ |
| 762 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 763 | &BTRFS_I(inode)->io_tree, |
| 764 | async_extent->start, |
| 765 | async_extent->start + |
| 766 | async_extent->ram_size - 1, |
| 767 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 768 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 769 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 770 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 771 | |
| 772 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 773 | async_extent->start, |
| 774 | async_extent->ram_size, |
| 775 | ins.objectid, |
| 776 | ins.offset, async_extent->pages, |
| 777 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 778 | alloc_hint = ins.objectid + ins.offset; |
| 779 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 780 | if (ret) |
| 781 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 782 | cond_resched(); |
| 783 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 784 | ret = 0; |
| 785 | out: |
| 786 | return ret; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 787 | out_free_reserve: |
| 788 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 789 | out_free: |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 790 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 791 | async_extent->start, |
| 792 | async_extent->start + |
| 793 | async_extent->ram_size - 1, |
| 794 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 795 | EXTENT_CLEAR_UNLOCK | |
| 796 | EXTENT_CLEAR_DELALLOC | |
| 797 | EXTENT_CLEAR_DIRTY | |
| 798 | EXTENT_SET_WRITEBACK | |
| 799 | EXTENT_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 800 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 801 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 802 | } |
| 803 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 804 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 805 | u64 num_bytes) |
| 806 | { |
| 807 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 808 | struct extent_map *em; |
| 809 | u64 alloc_hint = 0; |
| 810 | |
| 811 | read_lock(&em_tree->lock); |
| 812 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 813 | if (em) { |
| 814 | /* |
| 815 | * if block start isn't an actual block number then find the |
| 816 | * first block in this inode and use that as a hint. If that |
| 817 | * block is also bogus then just don't worry about it. |
| 818 | */ |
| 819 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 820 | free_extent_map(em); |
| 821 | em = search_extent_mapping(em_tree, 0, 0); |
| 822 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 823 | alloc_hint = em->block_start; |
| 824 | if (em) |
| 825 | free_extent_map(em); |
| 826 | } else { |
| 827 | alloc_hint = em->block_start; |
| 828 | free_extent_map(em); |
| 829 | } |
| 830 | } |
| 831 | read_unlock(&em_tree->lock); |
| 832 | |
| 833 | return alloc_hint; |
| 834 | } |
| 835 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 836 | /* |
| 837 | * when extent_io.c finds a delayed allocation range in the file, |
| 838 | * the call backs end up in this code. The basic idea is to |
| 839 | * allocate extents on disk for the range, and create ordered data structs |
| 840 | * in ram to track those extents. |
| 841 | * |
| 842 | * locked_page is the page that writepage had locked already. We use |
| 843 | * it to make sure we don't do extra locks or unlocks. |
| 844 | * |
| 845 | * *page_started is set to one if we unlock locked_page and do everything |
| 846 | * required to start IO on it. It may be clean and already done with |
| 847 | * IO when we return. |
| 848 | */ |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 849 | static noinline int __cow_file_range(struct btrfs_trans_handle *trans, |
| 850 | struct inode *inode, |
| 851 | struct btrfs_root *root, |
| 852 | struct page *locked_page, |
| 853 | u64 start, u64 end, int *page_started, |
| 854 | unsigned long *nr_written, |
| 855 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 856 | { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 857 | u64 alloc_hint = 0; |
| 858 | u64 num_bytes; |
| 859 | unsigned long ram_size; |
| 860 | u64 disk_num_bytes; |
| 861 | u64 cur_alloc_size; |
| 862 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 863 | struct btrfs_key ins; |
| 864 | struct extent_map *em; |
| 865 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 866 | int ret = 0; |
| 867 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 868 | BUG_ON(btrfs_is_free_space_inode(inode)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 869 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 870 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 871 | num_bytes = max(blocksize, num_bytes); |
| 872 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 873 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 874 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 875 | if (num_bytes < 64 * 1024 && |
| 876 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 877 | btrfs_add_inode_defrag(trans, inode); |
| 878 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 879 | if (start == 0) { |
| 880 | /* lets try to make an inline extent */ |
| 881 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 882 | start, end, 0, 0, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 883 | if (ret == 0) { |
| 884 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 885 | &BTRFS_I(inode)->io_tree, |
| 886 | start, end, NULL, |
| 887 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 888 | EXTENT_CLEAR_UNLOCK | |
| 889 | EXTENT_CLEAR_DELALLOC | |
| 890 | EXTENT_CLEAR_DIRTY | |
| 891 | EXTENT_SET_WRITEBACK | |
| 892 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 893 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 894 | *nr_written = *nr_written + |
| 895 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 896 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 897 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 898 | } else if (ret < 0) { |
| 899 | btrfs_abort_transaction(trans, root, ret); |
| 900 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 901 | } |
| 902 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 903 | |
| 904 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 905 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 906 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 907 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 908 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 909 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 910 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 911 | unsigned long op; |
| 912 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 913 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 914 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 915 | root->sectorsize, 0, alloc_hint, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 916 | &ins, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 917 | if (ret < 0) { |
| 918 | btrfs_abort_transaction(trans, root, ret); |
| 919 | goto out_unlock; |
| 920 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 921 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 922 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 923 | BUG_ON(!em); /* -ENOMEM */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 924 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 925 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 926 | ram_size = ins.offset; |
| 927 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 928 | em->mod_start = em->start; |
| 929 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 930 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 931 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 932 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 933 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 934 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 935 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 936 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 937 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 938 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 939 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 940 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 941 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 942 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 943 | if (ret != -EEXIST) { |
| 944 | free_extent_map(em); |
| 945 | break; |
| 946 | } |
| 947 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 948 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 949 | } |
| 950 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 951 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 952 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 953 | ram_size, cur_alloc_size, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 954 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 955 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 956 | if (root->root_key.objectid == |
| 957 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 958 | ret = btrfs_reloc_clone_csums(inode, start, |
| 959 | cur_alloc_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 960 | if (ret) { |
| 961 | btrfs_abort_transaction(trans, root, ret); |
| 962 | goto out_unlock; |
| 963 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 964 | } |
| 965 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 966 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 967 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 968 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 969 | /* we're not doing compressed IO, don't unlock the first |
| 970 | * page (which the caller expects to stay locked), don't |
| 971 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 972 | * |
| 973 | * Do set the Private2 bit so we know this page was properly |
| 974 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 975 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 976 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 977 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 978 | EXTENT_SET_PRIVATE2; |
| 979 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 980 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 981 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 982 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 983 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 984 | num_bytes -= cur_alloc_size; |
| 985 | alloc_hint = ins.objectid + ins.offset; |
| 986 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 987 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 988 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 989 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 990 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 991 | out_unlock: |
| 992 | extent_clear_unlock_delalloc(inode, |
| 993 | &BTRFS_I(inode)->io_tree, |
Josef Bacik | beb42dd | 2012-05-30 15:35:17 -0400 | [diff] [blame] | 994 | start, end, locked_page, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 995 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 996 | EXTENT_CLEAR_UNLOCK | |
| 997 | EXTENT_CLEAR_DELALLOC | |
| 998 | EXTENT_CLEAR_DIRTY | |
| 999 | EXTENT_SET_WRITEBACK | |
| 1000 | EXTENT_END_WRITEBACK); |
| 1001 | |
| 1002 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1003 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1004 | |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1005 | static noinline int cow_file_range(struct inode *inode, |
| 1006 | struct page *locked_page, |
| 1007 | u64 start, u64 end, int *page_started, |
| 1008 | unsigned long *nr_written, |
| 1009 | int unlock) |
| 1010 | { |
| 1011 | struct btrfs_trans_handle *trans; |
| 1012 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1013 | int ret; |
| 1014 | |
| 1015 | trans = btrfs_join_transaction(root); |
| 1016 | if (IS_ERR(trans)) { |
| 1017 | extent_clear_unlock_delalloc(inode, |
| 1018 | &BTRFS_I(inode)->io_tree, |
| 1019 | start, end, locked_page, |
| 1020 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1021 | EXTENT_CLEAR_UNLOCK | |
| 1022 | EXTENT_CLEAR_DELALLOC | |
| 1023 | EXTENT_CLEAR_DIRTY | |
| 1024 | EXTENT_SET_WRITEBACK | |
| 1025 | EXTENT_END_WRITEBACK); |
| 1026 | return PTR_ERR(trans); |
| 1027 | } |
| 1028 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 1029 | |
| 1030 | ret = __cow_file_range(trans, inode, root, locked_page, start, end, |
| 1031 | page_started, nr_written, unlock); |
| 1032 | |
| 1033 | btrfs_end_transaction(trans, root); |
| 1034 | |
| 1035 | return ret; |
| 1036 | } |
| 1037 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1038 | /* |
| 1039 | * work queue call back to started compression on a file and pages |
| 1040 | */ |
| 1041 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1042 | { |
| 1043 | struct async_cow *async_cow; |
| 1044 | int num_added = 0; |
| 1045 | async_cow = container_of(work, struct async_cow, work); |
| 1046 | |
| 1047 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1048 | async_cow->start, async_cow->end, async_cow, |
| 1049 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1050 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1051 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1052 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1053 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | /* |
| 1057 | * work queue call back to submit previously compressed pages |
| 1058 | */ |
| 1059 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1060 | { |
| 1061 | struct async_cow *async_cow; |
| 1062 | struct btrfs_root *root; |
| 1063 | unsigned long nr_pages; |
| 1064 | |
| 1065 | async_cow = container_of(work, struct async_cow, work); |
| 1066 | |
| 1067 | root = async_cow->root; |
| 1068 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1069 | PAGE_CACHE_SHIFT; |
| 1070 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1071 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1072 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1073 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1074 | wake_up(&root->fs_info->async_submit_wait); |
| 1075 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1076 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1077 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1078 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1079 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1080 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1081 | { |
| 1082 | struct async_cow *async_cow; |
| 1083 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1084 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1085 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1086 | kfree(async_cow); |
| 1087 | } |
| 1088 | |
| 1089 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1090 | u64 start, u64 end, int *page_started, |
| 1091 | unsigned long *nr_written) |
| 1092 | { |
| 1093 | struct async_cow *async_cow; |
| 1094 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1095 | unsigned long nr_pages; |
| 1096 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1097 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1098 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1099 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1100 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1101 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1102 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1103 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1104 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1105 | async_cow->root = root; |
| 1106 | async_cow->locked_page = locked_page; |
| 1107 | async_cow->start = start; |
| 1108 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1109 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1110 | cur_end = end; |
| 1111 | else |
| 1112 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1113 | |
| 1114 | async_cow->end = cur_end; |
| 1115 | INIT_LIST_HEAD(&async_cow->extents); |
| 1116 | |
| 1117 | async_cow->work.func = async_cow_start; |
| 1118 | async_cow->work.ordered_func = async_cow_submit; |
| 1119 | async_cow->work.ordered_free = async_cow_free; |
| 1120 | async_cow->work.flags = 0; |
| 1121 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1122 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1123 | PAGE_CACHE_SHIFT; |
| 1124 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1125 | |
| 1126 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 1127 | &async_cow->work); |
| 1128 | |
| 1129 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1130 | wait_event(root->fs_info->async_submit_wait, |
| 1131 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1132 | limit)); |
| 1133 | } |
| 1134 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1135 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1136 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1137 | wait_event(root->fs_info->async_submit_wait, |
| 1138 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1139 | 0)); |
| 1140 | } |
| 1141 | |
| 1142 | *nr_written += nr_pages; |
| 1143 | start = cur_end + 1; |
| 1144 | } |
| 1145 | *page_started = 1; |
| 1146 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1147 | } |
| 1148 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1149 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1150 | u64 bytenr, u64 num_bytes) |
| 1151 | { |
| 1152 | int ret; |
| 1153 | struct btrfs_ordered_sum *sums; |
| 1154 | LIST_HEAD(list); |
| 1155 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1156 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1157 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1158 | if (ret == 0 && list_empty(&list)) |
| 1159 | return 0; |
| 1160 | |
| 1161 | while (!list_empty(&list)) { |
| 1162 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1163 | list_del(&sums->list); |
| 1164 | kfree(sums); |
| 1165 | } |
| 1166 | return 1; |
| 1167 | } |
| 1168 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1169 | /* |
| 1170 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1171 | * of the extents that exist in the file, and COWs the file as required. |
| 1172 | * |
| 1173 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1174 | * blocks on disk |
| 1175 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1176 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1177 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1178 | u64 start, u64 end, int *page_started, int force, |
| 1179 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1180 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1181 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1182 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1183 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1184 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1185 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1186 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1187 | u64 cow_start; |
| 1188 | u64 cur_offset; |
| 1189 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1190 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1191 | u64 disk_bytenr; |
| 1192 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1193 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1194 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1195 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1196 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1197 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1198 | int nocow; |
| 1199 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1200 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1201 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1202 | |
| 1203 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1204 | if (!path) { |
| 1205 | extent_clear_unlock_delalloc(inode, |
| 1206 | &BTRFS_I(inode)->io_tree, |
| 1207 | start, end, locked_page, |
| 1208 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1209 | EXTENT_CLEAR_UNLOCK | |
| 1210 | EXTENT_CLEAR_DELALLOC | |
| 1211 | EXTENT_CLEAR_DIRTY | |
| 1212 | EXTENT_SET_WRITEBACK | |
| 1213 | EXTENT_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1214 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1215 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1216 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1217 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1218 | |
| 1219 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1220 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1221 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1222 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1223 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1224 | if (IS_ERR(trans)) { |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1225 | extent_clear_unlock_delalloc(inode, |
| 1226 | &BTRFS_I(inode)->io_tree, |
| 1227 | start, end, locked_page, |
| 1228 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1229 | EXTENT_CLEAR_UNLOCK | |
| 1230 | EXTENT_CLEAR_DELALLOC | |
| 1231 | EXTENT_CLEAR_DIRTY | |
| 1232 | EXTENT_SET_WRITEBACK | |
| 1233 | EXTENT_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1234 | btrfs_free_path(path); |
| 1235 | return PTR_ERR(trans); |
| 1236 | } |
| 1237 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1238 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1239 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1240 | cow_start = (u64)-1; |
| 1241 | cur_offset = start; |
| 1242 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1243 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1244 | cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1245 | if (ret < 0) { |
| 1246 | btrfs_abort_transaction(trans, root, ret); |
| 1247 | goto error; |
| 1248 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1249 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1250 | leaf = path->nodes[0]; |
| 1251 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1252 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1253 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1254 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1255 | path->slots[0]--; |
| 1256 | } |
| 1257 | check_prev = 0; |
| 1258 | next_slot: |
| 1259 | leaf = path->nodes[0]; |
| 1260 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1261 | ret = btrfs_next_leaf(root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1262 | if (ret < 0) { |
| 1263 | btrfs_abort_transaction(trans, root, ret); |
| 1264 | goto error; |
| 1265 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1266 | if (ret > 0) |
| 1267 | break; |
| 1268 | leaf = path->nodes[0]; |
| 1269 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1270 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1271 | nocow = 0; |
| 1272 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1273 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1274 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1275 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1276 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1277 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1278 | found_key.offset > end) |
| 1279 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1280 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1281 | if (found_key.offset > cur_offset) { |
| 1282 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1283 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1284 | goto out_check; |
| 1285 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1286 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1287 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1288 | struct btrfs_file_extent_item); |
| 1289 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1290 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1291 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1292 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1293 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1294 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1295 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1296 | extent_end = found_key.offset + |
| 1297 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1298 | disk_num_bytes = |
| 1299 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1300 | if (extent_end <= start) { |
| 1301 | path->slots[0]++; |
| 1302 | goto next_slot; |
| 1303 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1304 | if (disk_bytenr == 0) |
| 1305 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1306 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1307 | btrfs_file_extent_encryption(leaf, fi) || |
| 1308 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1309 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1310 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1311 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1312 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1313 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1314 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1315 | found_key.offset - |
| 1316 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1317 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1318 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1319 | disk_bytenr += cur_offset - found_key.offset; |
| 1320 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1321 | /* |
| 1322 | * force cow if csum exists in the range. |
| 1323 | * this ensure that csum for a given extent are |
| 1324 | * either valid or do not exist. |
| 1325 | */ |
| 1326 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1327 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1328 | nocow = 1; |
| 1329 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1330 | extent_end = found_key.offset + |
| 1331 | btrfs_file_extent_inline_len(leaf, fi); |
| 1332 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1333 | } else { |
| 1334 | BUG_ON(1); |
| 1335 | } |
| 1336 | out_check: |
| 1337 | if (extent_end <= start) { |
| 1338 | path->slots[0]++; |
| 1339 | goto next_slot; |
| 1340 | } |
| 1341 | if (!nocow) { |
| 1342 | if (cow_start == (u64)-1) |
| 1343 | cow_start = cur_offset; |
| 1344 | cur_offset = extent_end; |
| 1345 | if (cur_offset > end) |
| 1346 | break; |
| 1347 | path->slots[0]++; |
| 1348 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1349 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1350 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1351 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1352 | if (cow_start != (u64)-1) { |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1353 | ret = __cow_file_range(trans, inode, root, locked_page, |
| 1354 | cow_start, found_key.offset - 1, |
| 1355 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1356 | if (ret) { |
| 1357 | btrfs_abort_transaction(trans, root, ret); |
| 1358 | goto error; |
| 1359 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1360 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1361 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1362 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1363 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1364 | struct extent_map *em; |
| 1365 | struct extent_map_tree *em_tree; |
| 1366 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1367 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1368 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1369 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1370 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1371 | em->len = num_bytes; |
| 1372 | em->block_len = num_bytes; |
| 1373 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1374 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1375 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1376 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1377 | em->mod_start = em->start; |
| 1378 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1379 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1380 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1381 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1382 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1383 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1384 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1385 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1386 | if (ret != -EEXIST) { |
| 1387 | free_extent_map(em); |
| 1388 | break; |
| 1389 | } |
| 1390 | btrfs_drop_extent_cache(inode, em->start, |
| 1391 | em->start + em->len - 1, 0); |
| 1392 | } |
| 1393 | type = BTRFS_ORDERED_PREALLOC; |
| 1394 | } else { |
| 1395 | type = BTRFS_ORDERED_NOCOW; |
| 1396 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1397 | |
| 1398 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1399 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1400 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1401 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1402 | if (root->root_key.objectid == |
| 1403 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1404 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1405 | num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1406 | if (ret) { |
| 1407 | btrfs_abort_transaction(trans, root, ret); |
| 1408 | goto error; |
| 1409 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1410 | } |
| 1411 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1412 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1413 | cur_offset, cur_offset + num_bytes - 1, |
| 1414 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1415 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1416 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1417 | cur_offset = extent_end; |
| 1418 | if (cur_offset > end) |
| 1419 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1420 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1421 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1422 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1423 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1424 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1425 | cur_offset = end; |
| 1426 | } |
| 1427 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1428 | if (cow_start != (u64)-1) { |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1429 | ret = __cow_file_range(trans, inode, root, locked_page, |
| 1430 | cow_start, end, |
| 1431 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1432 | if (ret) { |
| 1433 | btrfs_abort_transaction(trans, root, ret); |
| 1434 | goto error; |
| 1435 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1436 | } |
| 1437 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1438 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1439 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1440 | if (!ret) |
| 1441 | ret = err; |
| 1442 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1443 | if (ret && cur_offset < end) |
| 1444 | extent_clear_unlock_delalloc(inode, |
| 1445 | &BTRFS_I(inode)->io_tree, |
| 1446 | cur_offset, end, locked_page, |
| 1447 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1448 | EXTENT_CLEAR_UNLOCK | |
| 1449 | EXTENT_CLEAR_DELALLOC | |
| 1450 | EXTENT_CLEAR_DIRTY | |
| 1451 | EXTENT_SET_WRITEBACK | |
| 1452 | EXTENT_END_WRITEBACK); |
| 1453 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1454 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1455 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1456 | } |
| 1457 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1458 | /* |
| 1459 | * extent_io.c call back to do delayed allocation processing |
| 1460 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1461 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1462 | u64 start, u64 end, int *page_started, |
| 1463 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1464 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1465 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1466 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1467 | |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1468 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1469 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1470 | page_started, 1, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1471 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1472 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1473 | page_started, 0, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1474 | } else if (!btrfs_test_opt(root, COMPRESS) && |
| 1475 | !(BTRFS_I(inode)->force_compress) && |
| 1476 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1477 | ret = cow_file_range(inode, locked_page, start, end, |
| 1478 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1479 | } else { |
| 1480 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1481 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1482 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1483 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1484 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1485 | return ret; |
| 1486 | } |
| 1487 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1488 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1489 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1490 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1491 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1492 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1493 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1494 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1495 | spin_lock(&BTRFS_I(inode)->lock); |
| 1496 | BTRFS_I(inode)->outstanding_extents++; |
| 1497 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1502 | * extents so we can keep track of new extents that are just merged onto old |
| 1503 | * extents, such as when we are doing sequential writes, so we can properly |
| 1504 | * account for the metadata space we'll need. |
| 1505 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1506 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1507 | struct extent_state *new, |
| 1508 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1509 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1510 | /* not delalloc, ignore it */ |
| 1511 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1512 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1513 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1514 | spin_lock(&BTRFS_I(inode)->lock); |
| 1515 | BTRFS_I(inode)->outstanding_extents--; |
| 1516 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1517 | } |
| 1518 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1519 | /* |
| 1520 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1521 | * bytes in this file, and to maintain the list of inodes that |
| 1522 | * have pending delalloc work to be done. |
| 1523 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1524 | static void btrfs_set_bit_hook(struct inode *inode, |
| 1525 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1526 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1527 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1528 | /* |
| 1529 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1530 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1531 | * bit, which is only set or cleared with irqs on |
| 1532 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1533 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1534 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1535 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1536 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1537 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1538 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1539 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1540 | } else { |
| 1541 | spin_lock(&BTRFS_I(inode)->lock); |
| 1542 | BTRFS_I(inode)->outstanding_extents++; |
| 1543 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1544 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1545 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1546 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1547 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1548 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1549 | BTRFS_I(inode)->delalloc_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1550 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1551 | &BTRFS_I(inode)->runtime_flags)) { |
| 1552 | spin_lock(&root->fs_info->delalloc_lock); |
| 1553 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1554 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1555 | &root->fs_info->delalloc_inodes); |
| 1556 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1557 | &BTRFS_I(inode)->runtime_flags); |
| 1558 | } |
| 1559 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1560 | } |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1561 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1562 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1563 | } |
| 1564 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1565 | /* |
| 1566 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1567 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1568 | static void btrfs_clear_bit_hook(struct inode *inode, |
| 1569 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1570 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1571 | /* |
| 1572 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1573 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1574 | * bit, which is only set or cleared with irqs on |
| 1575 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1576 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1577 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1578 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1579 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1580 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1581 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1582 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1583 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1584 | spin_lock(&BTRFS_I(inode)->lock); |
| 1585 | BTRFS_I(inode)->outstanding_extents--; |
| 1586 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1587 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1588 | |
| 1589 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1590 | btrfs_delalloc_release_metadata(inode, len); |
| 1591 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1592 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 1593 | && do_list) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1594 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1595 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1596 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1597 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1598 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1599 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1600 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1601 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1602 | &BTRFS_I(inode)->runtime_flags)) { |
| 1603 | spin_lock(&root->fs_info->delalloc_lock); |
| 1604 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1605 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1606 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1607 | &BTRFS_I(inode)->runtime_flags); |
| 1608 | } |
| 1609 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1610 | } |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1611 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1612 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1613 | } |
| 1614 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1615 | /* |
| 1616 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1617 | * we don't create bios that span stripes or chunks |
| 1618 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1619 | 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] | 1620 | size_t size, struct bio *bio, |
| 1621 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1622 | { |
| 1623 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1624 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1625 | u64 length = 0; |
| 1626 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1627 | int ret; |
| 1628 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1629 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1630 | return 0; |
| 1631 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1632 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1633 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1634 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1635 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1636 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1637 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1638 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1639 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1640 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1641 | } |
| 1642 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1643 | /* |
| 1644 | * in order to insert checksums into the metadata in large chunks, |
| 1645 | * we wait until bio submission time. All the pages in the bio are |
| 1646 | * checksummed and sums are attached onto the ordered extent record. |
| 1647 | * |
| 1648 | * At IO completion time the cums attached on the ordered extent record |
| 1649 | * are inserted into the btree |
| 1650 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1651 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1652 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1653 | unsigned long bio_flags, |
| 1654 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1655 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1656 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1657 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1658 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1659 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1660 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1661 | return 0; |
| 1662 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1663 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1664 | /* |
| 1665 | * in order to insert checksums into the metadata in large chunks, |
| 1666 | * we wait until bio submission time. All the pages in the bio are |
| 1667 | * checksummed and sums are attached onto the ordered extent record. |
| 1668 | * |
| 1669 | * At IO completion time the cums attached on the ordered extent record |
| 1670 | * are inserted into the btree |
| 1671 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1672 | 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] | 1673 | int mirror_num, unsigned long bio_flags, |
| 1674 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1675 | { |
| 1676 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1677 | int ret; |
| 1678 | |
| 1679 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1680 | if (ret) |
| 1681 | bio_endio(bio, ret); |
| 1682 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1683 | } |
| 1684 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1685 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1686 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1687 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1688 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1689 | 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] | 1690 | int mirror_num, unsigned long bio_flags, |
| 1691 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1692 | { |
| 1693 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1694 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1695 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1696 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1697 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1698 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1699 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1700 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1701 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1702 | metadata = 2; |
| 1703 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1704 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1705 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1706 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1707 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1708 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1709 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1710 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1711 | mirror_num, |
| 1712 | bio_flags); |
| 1713 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1714 | } else if (!skip_sum) { |
| 1715 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1716 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1717 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1718 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1719 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1720 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1721 | /* csum items have already been cloned */ |
| 1722 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1723 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1724 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1725 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1726 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1727 | bio_flags, bio_offset, |
| 1728 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1729 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1730 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1731 | } else if (!skip_sum) { |
| 1732 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1733 | if (ret) |
| 1734 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1735 | } |
| 1736 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1737 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1738 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1739 | |
| 1740 | out: |
| 1741 | if (ret < 0) |
| 1742 | bio_endio(bio, ret); |
| 1743 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1744 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1745 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1746 | /* |
| 1747 | * given a list of ordered sums record them in the inode. This happens |
| 1748 | * at IO completion time based on sums calculated at bio submission time. |
| 1749 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1750 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1751 | struct inode *inode, u64 file_offset, |
| 1752 | struct list_head *list) |
| 1753 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1754 | struct btrfs_ordered_sum *sum; |
| 1755 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1756 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1757 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1758 | btrfs_csum_file_blocks(trans, |
| 1759 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1760 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1761 | } |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1765 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1766 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1767 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1768 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1769 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1770 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1771 | } |
| 1772 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1773 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1774 | struct btrfs_writepage_fixup { |
| 1775 | struct page *page; |
| 1776 | struct btrfs_work work; |
| 1777 | }; |
| 1778 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1779 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1780 | { |
| 1781 | struct btrfs_writepage_fixup *fixup; |
| 1782 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1783 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1784 | struct page *page; |
| 1785 | struct inode *inode; |
| 1786 | u64 page_start; |
| 1787 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1788 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1789 | |
| 1790 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1791 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1792 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1793 | lock_page(page); |
| 1794 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1795 | ClearPageChecked(page); |
| 1796 | goto out_page; |
| 1797 | } |
| 1798 | |
| 1799 | inode = page->mapping->host; |
| 1800 | page_start = page_offset(page); |
| 1801 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1802 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1803 | 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] | 1804 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1805 | |
| 1806 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1807 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1808 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1809 | |
| 1810 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1811 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1812 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1813 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1814 | unlock_page(page); |
| 1815 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1816 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1817 | goto again; |
| 1818 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1819 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1820 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1821 | if (ret) { |
| 1822 | mapping_set_error(page->mapping, ret); |
| 1823 | end_extent_writepage(page, ret, page_start, page_end); |
| 1824 | ClearPageChecked(page); |
| 1825 | goto out; |
| 1826 | } |
| 1827 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1828 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1829 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1830 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1831 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1832 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1833 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1834 | out_page: |
| 1835 | unlock_page(page); |
| 1836 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1837 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | /* |
| 1841 | * There are a few paths in the higher layers of the kernel that directly |
| 1842 | * set the page dirty bit without asking the filesystem if it is a |
| 1843 | * good idea. This causes problems because we want to make sure COW |
| 1844 | * properly happens and the data=ordered rules are followed. |
| 1845 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1846 | * 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] | 1847 | * hasn't been properly setup for IO. We kick off an async process |
| 1848 | * to fix it up. The async helper will wait for ordered extents, set |
| 1849 | * the delalloc bit and make it safe to write the page. |
| 1850 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1851 | 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] | 1852 | { |
| 1853 | struct inode *inode = page->mapping->host; |
| 1854 | struct btrfs_writepage_fixup *fixup; |
| 1855 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1856 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1857 | /* this page is properly in the ordered list */ |
| 1858 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1859 | return 0; |
| 1860 | |
| 1861 | if (PageChecked(page)) |
| 1862 | return -EAGAIN; |
| 1863 | |
| 1864 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1865 | if (!fixup) |
| 1866 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1867 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1868 | SetPageChecked(page); |
| 1869 | page_cache_get(page); |
| 1870 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1871 | fixup->page = page; |
| 1872 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1873 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1874 | } |
| 1875 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1876 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1877 | struct inode *inode, u64 file_pos, |
| 1878 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1879 | u64 num_bytes, u64 ram_bytes, |
| 1880 | u8 compression, u8 encryption, |
| 1881 | u16 other_encoding, int extent_type) |
| 1882 | { |
| 1883 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1884 | struct btrfs_file_extent_item *fi; |
| 1885 | struct btrfs_path *path; |
| 1886 | struct extent_buffer *leaf; |
| 1887 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1888 | int ret; |
| 1889 | |
| 1890 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1891 | if (!path) |
| 1892 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1893 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1894 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1895 | |
| 1896 | /* |
| 1897 | * we may be replacing one extent in the tree with another. |
| 1898 | * The new extent is pinned in the extent map, and we don't want |
| 1899 | * to drop it from the cache until it is completely in the btree. |
| 1900 | * |
| 1901 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1902 | * the caller is expected to unpin it and allow it to be merged |
| 1903 | * with the others. |
| 1904 | */ |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1905 | ret = btrfs_drop_extents(trans, root, inode, file_pos, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 1906 | file_pos + num_bytes, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1907 | if (ret) |
| 1908 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1909 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1910 | ins.objectid = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1911 | ins.offset = file_pos; |
| 1912 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1913 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1914 | if (ret) |
| 1915 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1916 | leaf = path->nodes[0]; |
| 1917 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1918 | struct btrfs_file_extent_item); |
| 1919 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1920 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1921 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1922 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1923 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1924 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1925 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1926 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1927 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1928 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1929 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1930 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 1931 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1932 | |
| 1933 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1934 | |
| 1935 | ins.objectid = disk_bytenr; |
| 1936 | ins.offset = disk_num_bytes; |
| 1937 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1938 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1939 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1940 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1941 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1942 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1943 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1944 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1945 | } |
| 1946 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 1947 | /* snapshot-aware defrag */ |
| 1948 | struct sa_defrag_extent_backref { |
| 1949 | struct rb_node node; |
| 1950 | struct old_sa_defrag_extent *old; |
| 1951 | u64 root_id; |
| 1952 | u64 inum; |
| 1953 | u64 file_pos; |
| 1954 | u64 extent_offset; |
| 1955 | u64 num_bytes; |
| 1956 | u64 generation; |
| 1957 | }; |
| 1958 | |
| 1959 | struct old_sa_defrag_extent { |
| 1960 | struct list_head list; |
| 1961 | struct new_sa_defrag_extent *new; |
| 1962 | |
| 1963 | u64 extent_offset; |
| 1964 | u64 bytenr; |
| 1965 | u64 offset; |
| 1966 | u64 len; |
| 1967 | int count; |
| 1968 | }; |
| 1969 | |
| 1970 | struct new_sa_defrag_extent { |
| 1971 | struct rb_root root; |
| 1972 | struct list_head head; |
| 1973 | struct btrfs_path *path; |
| 1974 | struct inode *inode; |
| 1975 | u64 file_pos; |
| 1976 | u64 len; |
| 1977 | u64 bytenr; |
| 1978 | u64 disk_len; |
| 1979 | u8 compress_type; |
| 1980 | }; |
| 1981 | |
| 1982 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 1983 | struct sa_defrag_extent_backref *b2) |
| 1984 | { |
| 1985 | if (b1->root_id < b2->root_id) |
| 1986 | return -1; |
| 1987 | else if (b1->root_id > b2->root_id) |
| 1988 | return 1; |
| 1989 | |
| 1990 | if (b1->inum < b2->inum) |
| 1991 | return -1; |
| 1992 | else if (b1->inum > b2->inum) |
| 1993 | return 1; |
| 1994 | |
| 1995 | if (b1->file_pos < b2->file_pos) |
| 1996 | return -1; |
| 1997 | else if (b1->file_pos > b2->file_pos) |
| 1998 | return 1; |
| 1999 | |
| 2000 | /* |
| 2001 | * [------------------------------] ===> (a range of space) |
| 2002 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2003 | * |<---------------------------->| ===> (fs/file tree B) |
| 2004 | * |
| 2005 | * A range of space can refer to two file extents in one tree while |
| 2006 | * refer to only one file extent in another tree. |
| 2007 | * |
| 2008 | * So we may process a disk offset more than one time(two extents in A) |
| 2009 | * and locate at the same extent(one extent in B), then insert two same |
| 2010 | * backrefs(both refer to the extent in B). |
| 2011 | */ |
| 2012 | return 0; |
| 2013 | } |
| 2014 | |
| 2015 | static void backref_insert(struct rb_root *root, |
| 2016 | struct sa_defrag_extent_backref *backref) |
| 2017 | { |
| 2018 | struct rb_node **p = &root->rb_node; |
| 2019 | struct rb_node *parent = NULL; |
| 2020 | struct sa_defrag_extent_backref *entry; |
| 2021 | int ret; |
| 2022 | |
| 2023 | while (*p) { |
| 2024 | parent = *p; |
| 2025 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2026 | |
| 2027 | ret = backref_comp(backref, entry); |
| 2028 | if (ret < 0) |
| 2029 | p = &(*p)->rb_left; |
| 2030 | else |
| 2031 | p = &(*p)->rb_right; |
| 2032 | } |
| 2033 | |
| 2034 | rb_link_node(&backref->node, parent, p); |
| 2035 | rb_insert_color(&backref->node, root); |
| 2036 | } |
| 2037 | |
| 2038 | /* |
| 2039 | * Note the backref might has changed, and in this case we just return 0. |
| 2040 | */ |
| 2041 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2042 | void *ctx) |
| 2043 | { |
| 2044 | struct btrfs_file_extent_item *extent; |
| 2045 | struct btrfs_fs_info *fs_info; |
| 2046 | struct old_sa_defrag_extent *old = ctx; |
| 2047 | struct new_sa_defrag_extent *new = old->new; |
| 2048 | struct btrfs_path *path = new->path; |
| 2049 | struct btrfs_key key; |
| 2050 | struct btrfs_root *root; |
| 2051 | struct sa_defrag_extent_backref *backref; |
| 2052 | struct extent_buffer *leaf; |
| 2053 | struct inode *inode = new->inode; |
| 2054 | int slot; |
| 2055 | int ret; |
| 2056 | u64 extent_offset; |
| 2057 | u64 num_bytes; |
| 2058 | |
| 2059 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2060 | inum == btrfs_ino(inode)) |
| 2061 | return 0; |
| 2062 | |
| 2063 | key.objectid = root_id; |
| 2064 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2065 | key.offset = (u64)-1; |
| 2066 | |
| 2067 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2068 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2069 | if (IS_ERR(root)) { |
| 2070 | if (PTR_ERR(root) == -ENOENT) |
| 2071 | return 0; |
| 2072 | WARN_ON(1); |
| 2073 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2074 | inum, offset, root_id); |
| 2075 | return PTR_ERR(root); |
| 2076 | } |
| 2077 | |
| 2078 | key.objectid = inum; |
| 2079 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2080 | if (offset > (u64)-1 << 32) |
| 2081 | key.offset = 0; |
| 2082 | else |
| 2083 | key.offset = offset; |
| 2084 | |
| 2085 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2086 | if (ret < 0) { |
| 2087 | WARN_ON(1); |
| 2088 | return ret; |
| 2089 | } |
| 2090 | |
| 2091 | while (1) { |
| 2092 | cond_resched(); |
| 2093 | |
| 2094 | leaf = path->nodes[0]; |
| 2095 | slot = path->slots[0]; |
| 2096 | |
| 2097 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2098 | ret = btrfs_next_leaf(root, path); |
| 2099 | if (ret < 0) { |
| 2100 | goto out; |
| 2101 | } else if (ret > 0) { |
| 2102 | ret = 0; |
| 2103 | goto out; |
| 2104 | } |
| 2105 | continue; |
| 2106 | } |
| 2107 | |
| 2108 | path->slots[0]++; |
| 2109 | |
| 2110 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2111 | |
| 2112 | if (key.objectid > inum) |
| 2113 | goto out; |
| 2114 | |
| 2115 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2116 | continue; |
| 2117 | |
| 2118 | extent = btrfs_item_ptr(leaf, slot, |
| 2119 | struct btrfs_file_extent_item); |
| 2120 | |
| 2121 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2122 | continue; |
| 2123 | |
| 2124 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
| 2125 | if (key.offset - extent_offset != offset) |
| 2126 | continue; |
| 2127 | |
| 2128 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
| 2129 | if (extent_offset >= old->extent_offset + old->offset + |
| 2130 | old->len || extent_offset + num_bytes <= |
| 2131 | old->extent_offset + old->offset) |
| 2132 | continue; |
| 2133 | |
| 2134 | break; |
| 2135 | } |
| 2136 | |
| 2137 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2138 | if (!backref) { |
| 2139 | ret = -ENOENT; |
| 2140 | goto out; |
| 2141 | } |
| 2142 | |
| 2143 | backref->root_id = root_id; |
| 2144 | backref->inum = inum; |
| 2145 | backref->file_pos = offset + extent_offset; |
| 2146 | backref->num_bytes = num_bytes; |
| 2147 | backref->extent_offset = extent_offset; |
| 2148 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2149 | backref->old = old; |
| 2150 | backref_insert(&new->root, backref); |
| 2151 | old->count++; |
| 2152 | out: |
| 2153 | btrfs_release_path(path); |
| 2154 | WARN_ON(ret); |
| 2155 | return ret; |
| 2156 | } |
| 2157 | |
| 2158 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2159 | struct new_sa_defrag_extent *new) |
| 2160 | { |
| 2161 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2162 | struct old_sa_defrag_extent *old, *tmp; |
| 2163 | int ret; |
| 2164 | |
| 2165 | new->path = path; |
| 2166 | |
| 2167 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2168 | ret = iterate_inodes_from_logical(old->bytenr, fs_info, |
| 2169 | path, record_one_backref, |
| 2170 | old); |
| 2171 | BUG_ON(ret < 0 && ret != -ENOENT); |
| 2172 | |
| 2173 | /* no backref to be processed for this extent */ |
| 2174 | if (!old->count) { |
| 2175 | list_del(&old->list); |
| 2176 | kfree(old); |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | if (list_empty(&new->head)) |
| 2181 | return false; |
| 2182 | |
| 2183 | return true; |
| 2184 | } |
| 2185 | |
| 2186 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2187 | struct btrfs_file_extent_item *fi, |
| 2188 | u64 disk_bytenr) |
| 2189 | { |
| 2190 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr) |
| 2191 | return 0; |
| 2192 | |
| 2193 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2194 | return 0; |
| 2195 | |
| 2196 | if (btrfs_file_extent_compression(leaf, fi) || |
| 2197 | btrfs_file_extent_encryption(leaf, fi) || |
| 2198 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2199 | return 0; |
| 2200 | |
| 2201 | return 1; |
| 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | * Note the backref might has changed, and in this case we just return 0. |
| 2206 | */ |
| 2207 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2208 | struct sa_defrag_extent_backref *prev, |
| 2209 | struct sa_defrag_extent_backref *backref) |
| 2210 | { |
| 2211 | struct btrfs_file_extent_item *extent; |
| 2212 | struct btrfs_file_extent_item *item; |
| 2213 | struct btrfs_ordered_extent *ordered; |
| 2214 | struct btrfs_trans_handle *trans; |
| 2215 | struct btrfs_fs_info *fs_info; |
| 2216 | struct btrfs_root *root; |
| 2217 | struct btrfs_key key; |
| 2218 | struct extent_buffer *leaf; |
| 2219 | struct old_sa_defrag_extent *old = backref->old; |
| 2220 | struct new_sa_defrag_extent *new = old->new; |
| 2221 | struct inode *src_inode = new->inode; |
| 2222 | struct inode *inode; |
| 2223 | struct extent_state *cached = NULL; |
| 2224 | int ret = 0; |
| 2225 | u64 start; |
| 2226 | u64 len; |
| 2227 | u64 lock_start; |
| 2228 | u64 lock_end; |
| 2229 | bool merge = false; |
| 2230 | int index; |
| 2231 | |
| 2232 | if (prev && prev->root_id == backref->root_id && |
| 2233 | prev->inum == backref->inum && |
| 2234 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2235 | merge = true; |
| 2236 | |
| 2237 | /* step 1: get root */ |
| 2238 | key.objectid = backref->root_id; |
| 2239 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2240 | key.offset = (u64)-1; |
| 2241 | |
| 2242 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2243 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2244 | |
| 2245 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2246 | if (IS_ERR(root)) { |
| 2247 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2248 | if (PTR_ERR(root) == -ENOENT) |
| 2249 | return 0; |
| 2250 | return PTR_ERR(root); |
| 2251 | } |
| 2252 | if (btrfs_root_refs(&root->root_item) == 0) { |
| 2253 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2254 | /* parse ENOENT to 0 */ |
| 2255 | return 0; |
| 2256 | } |
| 2257 | |
| 2258 | /* step 2: get inode */ |
| 2259 | key.objectid = backref->inum; |
| 2260 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2261 | key.offset = 0; |
| 2262 | |
| 2263 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2264 | if (IS_ERR(inode)) { |
| 2265 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2266 | return 0; |
| 2267 | } |
| 2268 | |
| 2269 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2270 | |
| 2271 | /* step 3: relink backref */ |
| 2272 | lock_start = backref->file_pos; |
| 2273 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2274 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2275 | 0, &cached); |
| 2276 | |
| 2277 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2278 | if (ordered) { |
| 2279 | btrfs_put_ordered_extent(ordered); |
| 2280 | goto out_unlock; |
| 2281 | } |
| 2282 | |
| 2283 | trans = btrfs_join_transaction(root); |
| 2284 | if (IS_ERR(trans)) { |
| 2285 | ret = PTR_ERR(trans); |
| 2286 | goto out_unlock; |
| 2287 | } |
| 2288 | |
| 2289 | key.objectid = backref->inum; |
| 2290 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2291 | key.offset = backref->file_pos; |
| 2292 | |
| 2293 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2294 | if (ret < 0) { |
| 2295 | goto out_free_path; |
| 2296 | } else if (ret > 0) { |
| 2297 | ret = 0; |
| 2298 | goto out_free_path; |
| 2299 | } |
| 2300 | |
| 2301 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2302 | struct btrfs_file_extent_item); |
| 2303 | |
| 2304 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2305 | backref->generation) |
| 2306 | goto out_free_path; |
| 2307 | |
| 2308 | btrfs_release_path(path); |
| 2309 | |
| 2310 | start = backref->file_pos; |
| 2311 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2312 | start += old->extent_offset + old->offset - |
| 2313 | backref->extent_offset; |
| 2314 | |
| 2315 | len = min(backref->extent_offset + backref->num_bytes, |
| 2316 | old->extent_offset + old->offset + old->len); |
| 2317 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2318 | |
| 2319 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2320 | start + len, 1); |
| 2321 | if (ret) |
| 2322 | goto out_free_path; |
| 2323 | again: |
| 2324 | key.objectid = btrfs_ino(inode); |
| 2325 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2326 | key.offset = start; |
| 2327 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2328 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2329 | if (merge) { |
| 2330 | struct btrfs_file_extent_item *fi; |
| 2331 | u64 extent_len; |
| 2332 | struct btrfs_key found_key; |
| 2333 | |
| 2334 | ret = btrfs_search_slot(trans, root, &key, path, 1, 1); |
| 2335 | if (ret < 0) |
| 2336 | goto out_free_path; |
| 2337 | |
| 2338 | path->slots[0]--; |
| 2339 | leaf = path->nodes[0]; |
| 2340 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2341 | |
| 2342 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2343 | struct btrfs_file_extent_item); |
| 2344 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2345 | |
| 2346 | if (relink_is_mergable(leaf, fi, new->bytenr) && |
| 2347 | extent_len + found_key.offset == start) { |
| 2348 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2349 | extent_len + len); |
| 2350 | btrfs_mark_buffer_dirty(leaf); |
| 2351 | inode_add_bytes(inode, len); |
| 2352 | |
| 2353 | ret = 1; |
| 2354 | goto out_free_path; |
| 2355 | } else { |
| 2356 | merge = false; |
| 2357 | btrfs_release_path(path); |
| 2358 | goto again; |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2363 | sizeof(*extent)); |
| 2364 | if (ret) { |
| 2365 | btrfs_abort_transaction(trans, root, ret); |
| 2366 | goto out_free_path; |
| 2367 | } |
| 2368 | |
| 2369 | leaf = path->nodes[0]; |
| 2370 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2371 | struct btrfs_file_extent_item); |
| 2372 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2373 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2374 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2375 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2376 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2377 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2378 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2379 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2380 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2381 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2382 | |
| 2383 | btrfs_mark_buffer_dirty(leaf); |
| 2384 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2385 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2386 | |
| 2387 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2388 | new->disk_len, 0, |
| 2389 | backref->root_id, backref->inum, |
| 2390 | new->file_pos, 0); /* start - extent_offset */ |
| 2391 | if (ret) { |
| 2392 | btrfs_abort_transaction(trans, root, ret); |
| 2393 | goto out_free_path; |
| 2394 | } |
| 2395 | |
| 2396 | ret = 1; |
| 2397 | out_free_path: |
| 2398 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2399 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2400 | btrfs_end_transaction(trans, root); |
| 2401 | out_unlock: |
| 2402 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2403 | &cached, GFP_NOFS); |
| 2404 | iput(inode); |
| 2405 | return ret; |
| 2406 | } |
| 2407 | |
| 2408 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2409 | { |
| 2410 | struct btrfs_path *path; |
| 2411 | struct old_sa_defrag_extent *old, *tmp; |
| 2412 | struct sa_defrag_extent_backref *backref; |
| 2413 | struct sa_defrag_extent_backref *prev = NULL; |
| 2414 | struct inode *inode; |
| 2415 | struct btrfs_root *root; |
| 2416 | struct rb_node *node; |
| 2417 | int ret; |
| 2418 | |
| 2419 | inode = new->inode; |
| 2420 | root = BTRFS_I(inode)->root; |
| 2421 | |
| 2422 | path = btrfs_alloc_path(); |
| 2423 | if (!path) |
| 2424 | return; |
| 2425 | |
| 2426 | if (!record_extent_backrefs(path, new)) { |
| 2427 | btrfs_free_path(path); |
| 2428 | goto out; |
| 2429 | } |
| 2430 | btrfs_release_path(path); |
| 2431 | |
| 2432 | while (1) { |
| 2433 | node = rb_first(&new->root); |
| 2434 | if (!node) |
| 2435 | break; |
| 2436 | rb_erase(node, &new->root); |
| 2437 | |
| 2438 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2439 | |
| 2440 | ret = relink_extent_backref(path, prev, backref); |
| 2441 | WARN_ON(ret < 0); |
| 2442 | |
| 2443 | kfree(prev); |
| 2444 | |
| 2445 | if (ret == 1) |
| 2446 | prev = backref; |
| 2447 | else |
| 2448 | prev = NULL; |
| 2449 | cond_resched(); |
| 2450 | } |
| 2451 | kfree(prev); |
| 2452 | |
| 2453 | btrfs_free_path(path); |
| 2454 | |
| 2455 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2456 | list_del(&old->list); |
| 2457 | kfree(old); |
| 2458 | } |
| 2459 | out: |
| 2460 | atomic_dec(&root->fs_info->defrag_running); |
| 2461 | wake_up(&root->fs_info->transaction_wait); |
| 2462 | |
| 2463 | kfree(new); |
| 2464 | } |
| 2465 | |
| 2466 | static struct new_sa_defrag_extent * |
| 2467 | record_old_file_extents(struct inode *inode, |
| 2468 | struct btrfs_ordered_extent *ordered) |
| 2469 | { |
| 2470 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2471 | struct btrfs_path *path; |
| 2472 | struct btrfs_key key; |
| 2473 | struct old_sa_defrag_extent *old, *tmp; |
| 2474 | struct new_sa_defrag_extent *new; |
| 2475 | int ret; |
| 2476 | |
| 2477 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2478 | if (!new) |
| 2479 | return NULL; |
| 2480 | |
| 2481 | new->inode = inode; |
| 2482 | new->file_pos = ordered->file_offset; |
| 2483 | new->len = ordered->len; |
| 2484 | new->bytenr = ordered->start; |
| 2485 | new->disk_len = ordered->disk_len; |
| 2486 | new->compress_type = ordered->compress_type; |
| 2487 | new->root = RB_ROOT; |
| 2488 | INIT_LIST_HEAD(&new->head); |
| 2489 | |
| 2490 | path = btrfs_alloc_path(); |
| 2491 | if (!path) |
| 2492 | goto out_kfree; |
| 2493 | |
| 2494 | key.objectid = btrfs_ino(inode); |
| 2495 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2496 | key.offset = new->file_pos; |
| 2497 | |
| 2498 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2499 | if (ret < 0) |
| 2500 | goto out_free_path; |
| 2501 | if (ret > 0 && path->slots[0] > 0) |
| 2502 | path->slots[0]--; |
| 2503 | |
| 2504 | /* find out all the old extents for the file range */ |
| 2505 | while (1) { |
| 2506 | struct btrfs_file_extent_item *extent; |
| 2507 | struct extent_buffer *l; |
| 2508 | int slot; |
| 2509 | u64 num_bytes; |
| 2510 | u64 offset; |
| 2511 | u64 end; |
| 2512 | u64 disk_bytenr; |
| 2513 | u64 extent_offset; |
| 2514 | |
| 2515 | l = path->nodes[0]; |
| 2516 | slot = path->slots[0]; |
| 2517 | |
| 2518 | if (slot >= btrfs_header_nritems(l)) { |
| 2519 | ret = btrfs_next_leaf(root, path); |
| 2520 | if (ret < 0) |
| 2521 | goto out_free_list; |
| 2522 | else if (ret > 0) |
| 2523 | break; |
| 2524 | continue; |
| 2525 | } |
| 2526 | |
| 2527 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2528 | |
| 2529 | if (key.objectid != btrfs_ino(inode)) |
| 2530 | break; |
| 2531 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2532 | break; |
| 2533 | if (key.offset >= new->file_pos + new->len) |
| 2534 | break; |
| 2535 | |
| 2536 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2537 | |
| 2538 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2539 | if (key.offset + num_bytes < new->file_pos) |
| 2540 | goto next; |
| 2541 | |
| 2542 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2543 | if (!disk_bytenr) |
| 2544 | goto next; |
| 2545 | |
| 2546 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2547 | |
| 2548 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2549 | if (!old) |
| 2550 | goto out_free_list; |
| 2551 | |
| 2552 | offset = max(new->file_pos, key.offset); |
| 2553 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2554 | |
| 2555 | old->bytenr = disk_bytenr; |
| 2556 | old->extent_offset = extent_offset; |
| 2557 | old->offset = offset - key.offset; |
| 2558 | old->len = end - offset; |
| 2559 | old->new = new; |
| 2560 | old->count = 0; |
| 2561 | list_add_tail(&old->list, &new->head); |
| 2562 | next: |
| 2563 | path->slots[0]++; |
| 2564 | cond_resched(); |
| 2565 | } |
| 2566 | |
| 2567 | btrfs_free_path(path); |
| 2568 | atomic_inc(&root->fs_info->defrag_running); |
| 2569 | |
| 2570 | return new; |
| 2571 | |
| 2572 | out_free_list: |
| 2573 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2574 | list_del(&old->list); |
| 2575 | kfree(old); |
| 2576 | } |
| 2577 | out_free_path: |
| 2578 | btrfs_free_path(path); |
| 2579 | out_kfree: |
| 2580 | kfree(new); |
| 2581 | return NULL; |
| 2582 | } |
| 2583 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 2584 | /* |
| 2585 | * helper function for btrfs_finish_ordered_io, this |
| 2586 | * just reads in some of the csum leaves to prime them into ram |
| 2587 | * before we start the transaction. It limits the amount of btree |
| 2588 | * reads required while inside the transaction. |
| 2589 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2590 | /* as ordered data IO finishes, this gets called so we can finish |
| 2591 | * an ordered extent if the range of bytes in the file it covers are |
| 2592 | * fully written. |
| 2593 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2594 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2595 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2596 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2597 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2598 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2599 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2600 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2601 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2602 | int compress_type = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2603 | int ret; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2604 | bool nolock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2605 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2606 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2607 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2608 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2609 | ret = -EIO; |
| 2610 | goto out; |
| 2611 | } |
| 2612 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2613 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2614 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2615 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2616 | if (nolock) |
| 2617 | trans = btrfs_join_transaction_nolock(root); |
| 2618 | else |
| 2619 | trans = btrfs_join_transaction(root); |
| 2620 | if (IS_ERR(trans)) { |
| 2621 | ret = PTR_ERR(trans); |
| 2622 | trans = NULL; |
| 2623 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2624 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2625 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2626 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2627 | if (ret) /* -ENOMEM or corruption */ |
| 2628 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2629 | goto out; |
| 2630 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2631 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2632 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2633 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2634 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2635 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2636 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2637 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2638 | EXTENT_DEFRAG, 1, cached_state); |
| 2639 | if (ret) { |
| 2640 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
| 2641 | if (last_snapshot >= BTRFS_I(inode)->generation) |
| 2642 | /* the inode is shared */ |
| 2643 | new = record_old_file_extents(inode, ordered_extent); |
| 2644 | |
| 2645 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2646 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2647 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2648 | } |
| 2649 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2650 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2651 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2652 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2653 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2654 | if (IS_ERR(trans)) { |
| 2655 | ret = PTR_ERR(trans); |
| 2656 | trans = NULL; |
| 2657 | goto out_unlock; |
| 2658 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2659 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2660 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2661 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2662 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2663 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2664 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2665 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2666 | ordered_extent->file_offset, |
| 2667 | ordered_extent->file_offset + |
| 2668 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2669 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2670 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2671 | ret = insert_reserved_file_extent(trans, inode, |
| 2672 | ordered_extent->file_offset, |
| 2673 | ordered_extent->start, |
| 2674 | ordered_extent->disk_len, |
| 2675 | ordered_extent->len, |
| 2676 | ordered_extent->len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2677 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2678 | BTRFS_FILE_EXTENT_REG); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2679 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2680 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2681 | ordered_extent->file_offset, ordered_extent->len, |
| 2682 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2683 | if (ret < 0) { |
| 2684 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2685 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2686 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2687 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2688 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2689 | &ordered_extent->list); |
| 2690 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2691 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2692 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2693 | if (ret) { /* -ENOMEM or corruption */ |
| 2694 | btrfs_abort_transaction(trans, root, ret); |
| 2695 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2696 | } |
| 2697 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2698 | out_unlock: |
| 2699 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2700 | ordered_extent->file_offset + |
| 2701 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2702 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2703 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2704 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2705 | if (trans) |
| 2706 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2707 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2708 | if (ret) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2709 | clear_extent_uptodate(io_tree, ordered_extent->file_offset, |
| 2710 | ordered_extent->file_offset + |
| 2711 | ordered_extent->len - 1, NULL, GFP_NOFS); |
| 2712 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2713 | /* |
| 2714 | * If the ordered extent had an IOERR or something else went |
| 2715 | * wrong we need to return the space for this ordered extent |
| 2716 | * back to the allocator. |
| 2717 | */ |
| 2718 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2719 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2720 | btrfs_free_reserved_extent(root, ordered_extent->start, |
| 2721 | ordered_extent->disk_len); |
| 2722 | } |
| 2723 | |
| 2724 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2725 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2726 | * This needs to be done to make sure anybody waiting knows we are done |
| 2727 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2728 | */ |
| 2729 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2730 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2731 | /* for snapshot-aware defrag */ |
| 2732 | if (new) |
| 2733 | relink_file_extents(new); |
| 2734 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2735 | /* once for us */ |
| 2736 | btrfs_put_ordered_extent(ordered_extent); |
| 2737 | /* once for the tree */ |
| 2738 | btrfs_put_ordered_extent(ordered_extent); |
| 2739 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2740 | return ret; |
| 2741 | } |
| 2742 | |
| 2743 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2744 | { |
| 2745 | struct btrfs_ordered_extent *ordered_extent; |
| 2746 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2747 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2748 | } |
| 2749 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2750 | 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] | 2751 | struct extent_state *state, int uptodate) |
| 2752 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2753 | struct inode *inode = page->mapping->host; |
| 2754 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2755 | struct btrfs_ordered_extent *ordered_extent = NULL; |
| 2756 | struct btrfs_workers *workers; |
| 2757 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2758 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2759 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2760 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2761 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2762 | end - start + 1, uptodate)) |
| 2763 | return 0; |
| 2764 | |
| 2765 | ordered_extent->work.func = finish_ordered_fn; |
| 2766 | ordered_extent->work.flags = 0; |
| 2767 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2768 | if (btrfs_is_free_space_inode(inode)) |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2769 | workers = &root->fs_info->endio_freespace_worker; |
| 2770 | else |
| 2771 | workers = &root->fs_info->endio_write_workers; |
| 2772 | btrfs_queue_worker(workers, &ordered_extent->work); |
| 2773 | |
| 2774 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2775 | } |
| 2776 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2777 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2778 | * 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] | 2779 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2780 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2781 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2782 | static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end, |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2783 | struct extent_state *state, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2784 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2785 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2786 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2787 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2788 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 2789 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2790 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2791 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2792 | u32 csum = ~(u32)0; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2793 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 2794 | DEFAULT_RATELIMIT_BURST); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2795 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2796 | if (PageChecked(page)) { |
| 2797 | ClearPageChecked(page); |
| 2798 | goto good; |
| 2799 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2800 | |
| 2801 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 2802 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2803 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2804 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2805 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2806 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2807 | GFP_NOFS); |
| 2808 | return 0; |
| 2809 | } |
| 2810 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 2811 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2812 | private = state->private; |
| 2813 | ret = 0; |
| 2814 | } else { |
| 2815 | ret = get_state_private(io_tree, start, &private); |
| 2816 | } |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2817 | kaddr = kmap_atomic(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2818 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2819 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2820 | |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 2821 | csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2822 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2823 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2824 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2825 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2826 | kunmap_atomic(kaddr); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2827 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2828 | return 0; |
| 2829 | |
| 2830 | zeroit: |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2831 | if (__ratelimit(&_rs)) |
| 2832 | btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu", |
| 2833 | (unsigned long long)btrfs_ino(page->mapping->host), |
| 2834 | (unsigned long long)start, csum, |
| 2835 | (unsigned long long)private); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2836 | memset(kaddr + offset, 1, end - start + 1); |
| 2837 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2838 | kunmap_atomic(kaddr); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2839 | if (private == 0) |
| 2840 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2841 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2842 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2843 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2844 | struct delayed_iput { |
| 2845 | struct list_head list; |
| 2846 | struct inode *inode; |
| 2847 | }; |
| 2848 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2849 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2850 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2851 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2852 | { |
| 2853 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2854 | struct delayed_iput *delayed; |
| 2855 | |
| 2856 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2857 | return; |
| 2858 | |
| 2859 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2860 | delayed->inode = inode; |
| 2861 | |
| 2862 | spin_lock(&fs_info->delayed_iput_lock); |
| 2863 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2864 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2865 | } |
| 2866 | |
| 2867 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2868 | { |
| 2869 | LIST_HEAD(list); |
| 2870 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2871 | struct delayed_iput *delayed; |
| 2872 | int empty; |
| 2873 | |
| 2874 | spin_lock(&fs_info->delayed_iput_lock); |
| 2875 | empty = list_empty(&fs_info->delayed_iputs); |
| 2876 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2877 | if (empty) |
| 2878 | return; |
| 2879 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2880 | spin_lock(&fs_info->delayed_iput_lock); |
| 2881 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2882 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2883 | |
| 2884 | while (!list_empty(&list)) { |
| 2885 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2886 | list_del(&delayed->list); |
| 2887 | iput(delayed->inode); |
| 2888 | kfree(delayed); |
| 2889 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2892 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 2893 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2894 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2895 | * structure. |
| 2896 | */ |
| 2897 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2898 | struct btrfs_root *root) |
| 2899 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2900 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2901 | int ret; |
| 2902 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2903 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2904 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2905 | return; |
| 2906 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2907 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2908 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2909 | spin_unlock(&root->orphan_lock); |
| 2910 | return; |
| 2911 | } |
| 2912 | |
| 2913 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 2914 | spin_unlock(&root->orphan_lock); |
| 2915 | return; |
| 2916 | } |
| 2917 | |
| 2918 | block_rsv = root->orphan_block_rsv; |
| 2919 | root->orphan_block_rsv = NULL; |
| 2920 | spin_unlock(&root->orphan_lock); |
| 2921 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2922 | if (root->orphan_item_inserted && |
| 2923 | btrfs_root_refs(&root->root_item) > 0) { |
| 2924 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2925 | root->root_key.objectid); |
| 2926 | BUG_ON(ret); |
| 2927 | root->orphan_item_inserted = 0; |
| 2928 | } |
| 2929 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2930 | if (block_rsv) { |
| 2931 | WARN_ON(block_rsv->size > 0); |
| 2932 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2937 | * This creates an orphan entry for the given inode in case something goes |
| 2938 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2939 | * |
| 2940 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2941 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2942 | */ |
| 2943 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2944 | { |
| 2945 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2946 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2947 | int reserve = 0; |
| 2948 | int insert = 0; |
| 2949 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2950 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2951 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 2952 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2953 | if (!block_rsv) |
| 2954 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2955 | } |
| 2956 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2957 | spin_lock(&root->orphan_lock); |
| 2958 | if (!root->orphan_block_rsv) { |
| 2959 | root->orphan_block_rsv = block_rsv; |
| 2960 | } else if (block_rsv) { |
| 2961 | btrfs_free_block_rsv(root, block_rsv); |
| 2962 | block_rsv = NULL; |
| 2963 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2964 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2965 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2966 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2967 | #if 0 |
| 2968 | /* |
| 2969 | * For proper ENOSPC handling, we should do orphan |
| 2970 | * cleanup when mounting. But this introduces backward |
| 2971 | * compatibility issue. |
| 2972 | */ |
| 2973 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2974 | insert = 2; |
| 2975 | else |
| 2976 | insert = 1; |
| 2977 | #endif |
| 2978 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 2979 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2980 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2981 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 2982 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 2983 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2984 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2985 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2986 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2987 | /* grab metadata reservation from transaction handle */ |
| 2988 | if (reserve) { |
| 2989 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2990 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2994 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2995 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2996 | if (ret && ret != -EEXIST) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2997 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2998 | &BTRFS_I(inode)->runtime_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2999 | btrfs_abort_transaction(trans, root, ret); |
| 3000 | return ret; |
| 3001 | } |
| 3002 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3006 | if (insert >= 2) { |
| 3007 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3008 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3009 | if (ret && ret != -EEXIST) { |
| 3010 | btrfs_abort_transaction(trans, root, ret); |
| 3011 | return ret; |
| 3012 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3013 | } |
| 3014 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | /* |
| 3018 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3019 | * item for this particular inode. |
| 3020 | */ |
| 3021 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3022 | { |
| 3023 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3024 | int delete_item = 0; |
| 3025 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3026 | int ret = 0; |
| 3027 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3028 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3029 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3030 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3031 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3032 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3033 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3034 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3035 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3036 | spin_unlock(&root->orphan_lock); |
| 3037 | |
| 3038 | if (trans && delete_item) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3039 | ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3040 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3041 | } |
| 3042 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3043 | if (release_rsv) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3044 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3045 | atomic_dec(&root->orphan_inodes); |
| 3046 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3047 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3048 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | /* |
| 3052 | * this cleans up any orphans that may be left on the list from the last use |
| 3053 | * of this root. |
| 3054 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3055 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3056 | { |
| 3057 | struct btrfs_path *path; |
| 3058 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3059 | struct btrfs_key key, found_key; |
| 3060 | struct btrfs_trans_handle *trans; |
| 3061 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3062 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3063 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3064 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3065 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3066 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3067 | |
| 3068 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3069 | if (!path) { |
| 3070 | ret = -ENOMEM; |
| 3071 | goto out; |
| 3072 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3073 | path->reada = -1; |
| 3074 | |
| 3075 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 3076 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 3077 | key.offset = (u64)-1; |
| 3078 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3079 | while (1) { |
| 3080 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3081 | if (ret < 0) |
| 3082 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3083 | |
| 3084 | /* |
| 3085 | * 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] | 3086 | * 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] | 3087 | * find the key and see if we have stuff that matches |
| 3088 | */ |
| 3089 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3090 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3091 | if (path->slots[0] == 0) |
| 3092 | break; |
| 3093 | path->slots[0]--; |
| 3094 | } |
| 3095 | |
| 3096 | /* pull out the item */ |
| 3097 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3098 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3099 | |
| 3100 | /* make sure the item matches what we want */ |
| 3101 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3102 | break; |
| 3103 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 3104 | break; |
| 3105 | |
| 3106 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3107 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3108 | |
| 3109 | /* |
| 3110 | * this is where we are basically btrfs_lookup, without the |
| 3111 | * crossing root thing. we store the inode number in the |
| 3112 | * offset of the orphan item. |
| 3113 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3114 | |
| 3115 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3116 | btrfs_err(root->fs_info, |
| 3117 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3118 | ret = -EINVAL; |
| 3119 | goto out; |
| 3120 | } |
| 3121 | |
| 3122 | last_objectid = found_key.offset; |
| 3123 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3124 | found_key.objectid = found_key.offset; |
| 3125 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3126 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3127 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3128 | ret = PTR_RET(inode); |
| 3129 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3130 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3131 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3132 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3133 | struct btrfs_root *dead_root; |
| 3134 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3135 | int is_dead_root = 0; |
| 3136 | |
| 3137 | /* |
| 3138 | * this is an orphan in the tree root. Currently these |
| 3139 | * could come from 2 sources: |
| 3140 | * a) a snapshot deletion in progress |
| 3141 | * b) a free space cache inode |
| 3142 | * We need to distinguish those two, as the snapshot |
| 3143 | * orphan must not get deleted. |
| 3144 | * find_dead_roots already ran before us, so if this |
| 3145 | * is a snapshot deletion, we should find the root |
| 3146 | * in the dead_roots list |
| 3147 | */ |
| 3148 | spin_lock(&fs_info->trans_lock); |
| 3149 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3150 | root_list) { |
| 3151 | if (dead_root->root_key.objectid == |
| 3152 | found_key.objectid) { |
| 3153 | is_dead_root = 1; |
| 3154 | break; |
| 3155 | } |
| 3156 | } |
| 3157 | spin_unlock(&fs_info->trans_lock); |
| 3158 | if (is_dead_root) { |
| 3159 | /* prevent this orphan from being found again */ |
| 3160 | key.offset = found_key.objectid - 1; |
| 3161 | continue; |
| 3162 | } |
| 3163 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3164 | /* |
| 3165 | * Inode is already gone but the orphan item is still there, |
| 3166 | * kill the orphan item. |
| 3167 | */ |
| 3168 | if (ret == -ESTALE) { |
| 3169 | trans = btrfs_start_transaction(root, 1); |
| 3170 | if (IS_ERR(trans)) { |
| 3171 | ret = PTR_ERR(trans); |
| 3172 | goto out; |
| 3173 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3174 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3175 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3176 | ret = btrfs_del_orphan_item(trans, root, |
| 3177 | found_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3178 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3179 | btrfs_end_transaction(trans, root); |
| 3180 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3181 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3182 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3183 | /* |
| 3184 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3185 | * the proper thing when we hit it |
| 3186 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3187 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3188 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3189 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3190 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3191 | /* if we have links, this was a truncate, lets do that */ |
| 3192 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3193 | if (!S_ISREG(inode->i_mode)) { |
| 3194 | WARN_ON(1); |
| 3195 | iput(inode); |
| 3196 | continue; |
| 3197 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3198 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3199 | |
| 3200 | /* 1 for the orphan item deletion. */ |
| 3201 | trans = btrfs_start_transaction(root, 1); |
| 3202 | if (IS_ERR(trans)) { |
| 3203 | ret = PTR_ERR(trans); |
| 3204 | goto out; |
| 3205 | } |
| 3206 | ret = btrfs_orphan_add(trans, inode); |
| 3207 | btrfs_end_transaction(trans, root); |
| 3208 | if (ret) |
| 3209 | goto out; |
| 3210 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3211 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3212 | if (ret) |
| 3213 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3214 | } else { |
| 3215 | nr_unlink++; |
| 3216 | } |
| 3217 | |
| 3218 | /* this will do delete_inode and everything for us */ |
| 3219 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3220 | if (ret) |
| 3221 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3222 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3223 | /* release the path since we're done with it */ |
| 3224 | btrfs_release_path(path); |
| 3225 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3226 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3227 | |
| 3228 | if (root->orphan_block_rsv) |
| 3229 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3230 | (u64)-1); |
| 3231 | |
| 3232 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3233 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3234 | if (!IS_ERR(trans)) |
| 3235 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3236 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3237 | |
| 3238 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3239 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3240 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3241 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3242 | |
| 3243 | out: |
| 3244 | if (ret) |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3245 | btrfs_crit(root->fs_info, |
| 3246 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3247 | btrfs_free_path(path); |
| 3248 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3249 | } |
| 3250 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3251 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3252 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3253 | * don't find any xattrs, we know there can't be any acls. |
| 3254 | * |
| 3255 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3256 | */ |
| 3257 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 3258 | int slot, u64 objectid) |
| 3259 | { |
| 3260 | u32 nritems = btrfs_header_nritems(leaf); |
| 3261 | struct btrfs_key found_key; |
| 3262 | int scanned = 0; |
| 3263 | |
| 3264 | slot++; |
| 3265 | while (slot < nritems) { |
| 3266 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3267 | |
| 3268 | /* we found a different objectid, there must not be acls */ |
| 3269 | if (found_key.objectid != objectid) |
| 3270 | return 0; |
| 3271 | |
| 3272 | /* we found an xattr, assume we've got an acl */ |
| 3273 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 3274 | return 1; |
| 3275 | |
| 3276 | /* |
| 3277 | * we found a key greater than an xattr key, there can't |
| 3278 | * be any acls later on |
| 3279 | */ |
| 3280 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3281 | return 0; |
| 3282 | |
| 3283 | slot++; |
| 3284 | scanned++; |
| 3285 | |
| 3286 | /* |
| 3287 | * it goes inode, inode backrefs, xattrs, extents, |
| 3288 | * so if there are a ton of hard links to an inode there can |
| 3289 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3290 | * this is just an optimization |
| 3291 | */ |
| 3292 | if (scanned >= 8) |
| 3293 | break; |
| 3294 | } |
| 3295 | /* we hit the end of the leaf before we found an xattr or |
| 3296 | * something larger than an xattr. We have to assume the inode |
| 3297 | * has acls |
| 3298 | */ |
| 3299 | return 1; |
| 3300 | } |
| 3301 | |
| 3302 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3303 | * read an inode from the btree into the in-memory inode |
| 3304 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3305 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3306 | { |
| 3307 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3308 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3309 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3310 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3311 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3312 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3313 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3314 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3315 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3316 | bool filled = false; |
| 3317 | |
| 3318 | ret = btrfs_fill_inode(inode, &rdev); |
| 3319 | if (!ret) |
| 3320 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3321 | |
| 3322 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3323 | if (!path) |
| 3324 | goto make_bad; |
| 3325 | |
Josef Bacik | d90c732 | 2011-05-17 09:50:54 -0400 | [diff] [blame] | 3326 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3327 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3328 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3329 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3330 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3331 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3332 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3333 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3334 | |
| 3335 | if (filled) |
| 3336 | goto cache_acl; |
| 3337 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3338 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3339 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3340 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3341 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3342 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3343 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3344 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3345 | |
| 3346 | tspec = btrfs_inode_atime(inode_item); |
| 3347 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3348 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3349 | |
| 3350 | tspec = btrfs_inode_mtime(inode_item); |
| 3351 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3352 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3353 | |
| 3354 | tspec = btrfs_inode_ctime(inode_item); |
| 3355 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3356 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3357 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3358 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3359 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3360 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3361 | |
| 3362 | /* |
| 3363 | * If we were modified in the current generation and evicted from memory |
| 3364 | * and then re-read we need to do a full sync since we don't have any |
| 3365 | * idea about which extents were modified before we were evicted from |
| 3366 | * cache. |
| 3367 | */ |
| 3368 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3369 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3370 | &BTRFS_I(inode)->runtime_flags); |
| 3371 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3372 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3373 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3374 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3375 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3376 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3377 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3378 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3379 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3380 | /* |
| 3381 | * try to precache a NULL acl entry for files that don't have |
| 3382 | * any xattrs or acls |
| 3383 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3384 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
| 3385 | btrfs_ino(inode)); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3386 | if (!maybe_acls) |
| 3387 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3388 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3389 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3390 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3391 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3392 | case S_IFREG: |
| 3393 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3394 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3395 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3396 | inode->i_fop = &btrfs_file_operations; |
| 3397 | inode->i_op = &btrfs_file_inode_operations; |
| 3398 | break; |
| 3399 | case S_IFDIR: |
| 3400 | inode->i_fop = &btrfs_dir_file_operations; |
| 3401 | if (root == root->fs_info->tree_root) |
| 3402 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3403 | else |
| 3404 | inode->i_op = &btrfs_dir_inode_operations; |
| 3405 | break; |
| 3406 | case S_IFLNK: |
| 3407 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3408 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3409 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3410 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3411 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3412 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3413 | init_special_inode(inode, inode->i_mode, rdev); |
| 3414 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3415 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3416 | |
| 3417 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3418 | return; |
| 3419 | |
| 3420 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3421 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3422 | make_bad_inode(inode); |
| 3423 | } |
| 3424 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3425 | /* |
| 3426 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3427 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3428 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3429 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3430 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3431 | struct inode *inode) |
| 3432 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3433 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3434 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3435 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3436 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3437 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3438 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3439 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3440 | &token); |
| 3441 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3442 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3443 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3444 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
| 3445 | inode->i_atime.tv_sec, &token); |
| 3446 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 3447 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3448 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3449 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 3450 | inode->i_mtime.tv_sec, &token); |
| 3451 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 3452 | inode->i_mtime.tv_nsec, &token); |
| 3453 | |
| 3454 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 3455 | inode->i_ctime.tv_sec, &token); |
| 3456 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 3457 | inode->i_ctime.tv_nsec, &token); |
| 3458 | |
| 3459 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3460 | &token); |
| 3461 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3462 | &token); |
| 3463 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3464 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3465 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3466 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3467 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3468 | } |
| 3469 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3470 | /* |
| 3471 | * copy everything in the in-memory inode into the btree. |
| 3472 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3473 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3474 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3475 | { |
| 3476 | struct btrfs_inode_item *inode_item; |
| 3477 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3478 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3479 | int ret; |
| 3480 | |
| 3481 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3482 | if (!path) |
| 3483 | return -ENOMEM; |
| 3484 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3485 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3486 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3487 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3488 | if (ret) { |
| 3489 | if (ret > 0) |
| 3490 | ret = -ENOENT; |
| 3491 | goto failed; |
| 3492 | } |
| 3493 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3494 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3495 | leaf = path->nodes[0]; |
| 3496 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3497 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3498 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3499 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3500 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3501 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3502 | ret = 0; |
| 3503 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3504 | btrfs_free_path(path); |
| 3505 | return ret; |
| 3506 | } |
| 3507 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3508 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3509 | * copy everything in the in-memory inode into the btree. |
| 3510 | */ |
| 3511 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3512 | struct btrfs_root *root, struct inode *inode) |
| 3513 | { |
| 3514 | int ret; |
| 3515 | |
| 3516 | /* |
| 3517 | * If the inode is a free space inode, we can deadlock during commit |
| 3518 | * if we put it into the delayed code. |
| 3519 | * |
| 3520 | * The data relocation inode should also be directly updated |
| 3521 | * without delay |
| 3522 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3523 | if (!btrfs_is_free_space_inode(inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3524 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3525 | btrfs_update_root_times(trans, root); |
| 3526 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3527 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3528 | if (!ret) |
| 3529 | btrfs_set_inode_last_trans(trans, inode); |
| 3530 | return ret; |
| 3531 | } |
| 3532 | |
| 3533 | return btrfs_update_inode_item(trans, root, inode); |
| 3534 | } |
| 3535 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3536 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3537 | struct btrfs_root *root, |
| 3538 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3539 | { |
| 3540 | int ret; |
| 3541 | |
| 3542 | ret = btrfs_update_inode(trans, root, inode); |
| 3543 | if (ret == -ENOSPC) |
| 3544 | return btrfs_update_inode_item(trans, root, inode); |
| 3545 | return ret; |
| 3546 | } |
| 3547 | |
| 3548 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3549 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3550 | * recovery code. It remove a link in a directory with a given name, and |
| 3551 | * also drops the back refs in the inode to the directory |
| 3552 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3553 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3554 | struct btrfs_root *root, |
| 3555 | struct inode *dir, struct inode *inode, |
| 3556 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3557 | { |
| 3558 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3559 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3560 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3561 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3562 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3563 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3564 | u64 ino = btrfs_ino(inode); |
| 3565 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3566 | |
| 3567 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3568 | if (!path) { |
| 3569 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3570 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3571 | } |
| 3572 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3573 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3574 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3575 | name, name_len, -1); |
| 3576 | if (IS_ERR(di)) { |
| 3577 | ret = PTR_ERR(di); |
| 3578 | goto err; |
| 3579 | } |
| 3580 | if (!di) { |
| 3581 | ret = -ENOENT; |
| 3582 | goto err; |
| 3583 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3584 | leaf = path->nodes[0]; |
| 3585 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3586 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3587 | if (ret) |
| 3588 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3589 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3590 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3591 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3592 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3593 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3594 | btrfs_info(root->fs_info, |
| 3595 | "failed to delete reference to %.*s, inode %llu parent %llu", |
| 3596 | name_len, name, |
| 3597 | (unsigned long long)ino, (unsigned long long)dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3598 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3599 | goto err; |
| 3600 | } |
| 3601 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3602 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3603 | if (ret) { |
| 3604 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3605 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3606 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3607 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3608 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3609 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3610 | if (ret != 0 && ret != -ENOENT) { |
| 3611 | btrfs_abort_transaction(trans, root, ret); |
| 3612 | goto err; |
| 3613 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3614 | |
| 3615 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3616 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3617 | if (ret == -ENOENT) |
| 3618 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3619 | else if (ret) |
| 3620 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3621 | err: |
| 3622 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3623 | if (ret) |
| 3624 | goto out; |
| 3625 | |
| 3626 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3627 | inode_inc_iversion(inode); |
| 3628 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3629 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3630 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3631 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3632 | return ret; |
| 3633 | } |
| 3634 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3635 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3636 | struct btrfs_root *root, |
| 3637 | struct inode *dir, struct inode *inode, |
| 3638 | const char *name, int name_len) |
| 3639 | { |
| 3640 | int ret; |
| 3641 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3642 | if (!ret) { |
| 3643 | btrfs_drop_nlink(inode); |
| 3644 | ret = btrfs_update_inode(trans, root, inode); |
| 3645 | } |
| 3646 | return ret; |
| 3647 | } |
| 3648 | |
| 3649 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3650 | /* helper to check if there is any shared block in the path */ |
| 3651 | static int check_path_shared(struct btrfs_root *root, |
| 3652 | struct btrfs_path *path) |
| 3653 | { |
| 3654 | struct extent_buffer *eb; |
| 3655 | int level; |
Dan Carpenter | 0e4dcbef | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 3656 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3657 | |
| 3658 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 3659 | int ret; |
| 3660 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3661 | if (!path->nodes[level]) |
| 3662 | break; |
| 3663 | eb = path->nodes[level]; |
| 3664 | if (!btrfs_block_can_be_shared(root, eb)) |
| 3665 | continue; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3666 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, level, 1, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3667 | &refs, NULL); |
| 3668 | if (refs > 1) |
| 3669 | return 1; |
| 3670 | } |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 3671 | return 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | /* |
| 3675 | * helper to start transaction for unlink and rmdir. |
| 3676 | * |
| 3677 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 3678 | * so in enospc case, we should make sure they will free space before |
| 3679 | * allowing them to use the global metadata reservation. |
| 3680 | */ |
| 3681 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 3682 | struct dentry *dentry) |
| 3683 | { |
| 3684 | struct btrfs_trans_handle *trans; |
| 3685 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 3686 | struct btrfs_path *path; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3687 | struct btrfs_dir_item *di; |
| 3688 | struct inode *inode = dentry->d_inode; |
| 3689 | u64 index; |
| 3690 | int check_link = 1; |
| 3691 | int err = -ENOSPC; |
| 3692 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3693 | u64 ino = btrfs_ino(inode); |
| 3694 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3695 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3696 | /* |
| 3697 | * 1 for the possible orphan item |
| 3698 | * 1 for the dir item |
| 3699 | * 1 for the dir index |
| 3700 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3701 | * 1 for the inode |
| 3702 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3703 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3704 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3705 | return trans; |
| 3706 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3707 | if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3708 | return ERR_PTR(-ENOSPC); |
| 3709 | |
| 3710 | /* check if there is someone else holds reference */ |
| 3711 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 3712 | return ERR_PTR(-ENOSPC); |
| 3713 | |
| 3714 | if (atomic_read(&inode->i_count) > 2) |
| 3715 | return ERR_PTR(-ENOSPC); |
| 3716 | |
| 3717 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 3718 | return ERR_PTR(-ENOSPC); |
| 3719 | |
| 3720 | path = btrfs_alloc_path(); |
| 3721 | if (!path) { |
| 3722 | root->fs_info->enospc_unlink = 0; |
| 3723 | return ERR_PTR(-ENOMEM); |
| 3724 | } |
| 3725 | |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 3726 | /* 1 for the orphan item */ |
| 3727 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3728 | if (IS_ERR(trans)) { |
| 3729 | btrfs_free_path(path); |
| 3730 | root->fs_info->enospc_unlink = 0; |
| 3731 | return trans; |
| 3732 | } |
| 3733 | |
| 3734 | path->skip_locking = 1; |
| 3735 | path->search_commit_root = 1; |
| 3736 | |
| 3737 | ret = btrfs_lookup_inode(trans, root, path, |
| 3738 | &BTRFS_I(dir)->location, 0); |
| 3739 | if (ret < 0) { |
| 3740 | err = ret; |
| 3741 | goto out; |
| 3742 | } |
| 3743 | if (ret == 0) { |
| 3744 | if (check_path_shared(root, path)) |
| 3745 | goto out; |
| 3746 | } else { |
| 3747 | check_link = 0; |
| 3748 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3749 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3750 | |
| 3751 | ret = btrfs_lookup_inode(trans, root, path, |
| 3752 | &BTRFS_I(inode)->location, 0); |
| 3753 | if (ret < 0) { |
| 3754 | err = ret; |
| 3755 | goto out; |
| 3756 | } |
| 3757 | if (ret == 0) { |
| 3758 | if (check_path_shared(root, path)) |
| 3759 | goto out; |
| 3760 | } else { |
| 3761 | check_link = 0; |
| 3762 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3763 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3764 | |
| 3765 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 3766 | ret = btrfs_lookup_file_extent(trans, root, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3767 | ino, (u64)-1, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3768 | if (ret < 0) { |
| 3769 | err = ret; |
| 3770 | goto out; |
| 3771 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3772 | BUG_ON(ret == 0); /* Corruption */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3773 | if (check_path_shared(root, path)) |
| 3774 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3775 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | if (!check_link) { |
| 3779 | err = 0; |
| 3780 | goto out; |
| 3781 | } |
| 3782 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3783 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3784 | dentry->d_name.name, dentry->d_name.len, 0); |
| 3785 | if (IS_ERR(di)) { |
| 3786 | err = PTR_ERR(di); |
| 3787 | goto out; |
| 3788 | } |
| 3789 | if (di) { |
| 3790 | if (check_path_shared(root, path)) |
| 3791 | goto out; |
| 3792 | } else { |
| 3793 | err = 0; |
| 3794 | goto out; |
| 3795 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3796 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3797 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 3798 | ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name, |
| 3799 | dentry->d_name.len, ino, dir_ino, 0, |
| 3800 | &index); |
| 3801 | if (ret) { |
| 3802 | err = ret; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3803 | goto out; |
| 3804 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 3805 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3806 | if (check_path_shared(root, path)) |
| 3807 | goto out; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 3808 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3809 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3810 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3811 | /* |
| 3812 | * This is a commit root search, if we can lookup inode item and other |
| 3813 | * relative items in the commit root, it means the transaction of |
| 3814 | * dir/file creation has been committed, and the dir index item that we |
| 3815 | * delay to insert has also been inserted into the commit root. So |
| 3816 | * we needn't worry about the delayed insertion of the dir index item |
| 3817 | * here. |
| 3818 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3819 | di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3820 | dentry->d_name.name, dentry->d_name.len, 0); |
| 3821 | if (IS_ERR(di)) { |
| 3822 | err = PTR_ERR(di); |
| 3823 | goto out; |
| 3824 | } |
| 3825 | BUG_ON(ret == -ENOENT); |
| 3826 | if (check_path_shared(root, path)) |
| 3827 | goto out; |
| 3828 | |
| 3829 | err = 0; |
| 3830 | out: |
| 3831 | btrfs_free_path(path); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 3832 | /* Migrate the orphan reservation over */ |
| 3833 | if (!err) |
| 3834 | err = btrfs_block_rsv_migrate(trans->block_rsv, |
| 3835 | &root->fs_info->global_block_rsv, |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3836 | trans->bytes_reserved); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 3837 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3838 | if (err) { |
| 3839 | btrfs_end_transaction(trans, root); |
| 3840 | root->fs_info->enospc_unlink = 0; |
| 3841 | return ERR_PTR(err); |
| 3842 | } |
| 3843 | |
| 3844 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 3845 | return trans; |
| 3846 | } |
| 3847 | |
| 3848 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 3849 | struct btrfs_root *root) |
| 3850 | { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3851 | if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) { |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3852 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 3853 | trans->bytes_reserved); |
| 3854 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3855 | BUG_ON(!root->fs_info->enospc_unlink); |
| 3856 | root->fs_info->enospc_unlink = 0; |
| 3857 | } |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 3858 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3859 | } |
| 3860 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3861 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3862 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3863 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3864 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3865 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3866 | int ret; |
| 3867 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3868 | trans = __unlink_start_trans(dir, dentry); |
| 3869 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3870 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3871 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3872 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3873 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3874 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3875 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3876 | if (ret) |
| 3877 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3878 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3879 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3880 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3881 | if (ret) |
| 3882 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3883 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3884 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3885 | out: |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3886 | __unlink_end_trans(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3887 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3888 | return ret; |
| 3889 | } |
| 3890 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3891 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3892 | struct btrfs_root *root, |
| 3893 | struct inode *dir, u64 objectid, |
| 3894 | const char *name, int name_len) |
| 3895 | { |
| 3896 | struct btrfs_path *path; |
| 3897 | struct extent_buffer *leaf; |
| 3898 | struct btrfs_dir_item *di; |
| 3899 | struct btrfs_key key; |
| 3900 | u64 index; |
| 3901 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3902 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3903 | |
| 3904 | path = btrfs_alloc_path(); |
| 3905 | if (!path) |
| 3906 | return -ENOMEM; |
| 3907 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3908 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3909 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3910 | if (IS_ERR_OR_NULL(di)) { |
| 3911 | if (!di) |
| 3912 | ret = -ENOENT; |
| 3913 | else |
| 3914 | ret = PTR_ERR(di); |
| 3915 | goto out; |
| 3916 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3917 | |
| 3918 | leaf = path->nodes[0]; |
| 3919 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3920 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3921 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3922 | if (ret) { |
| 3923 | btrfs_abort_transaction(trans, root, ret); |
| 3924 | goto out; |
| 3925 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3926 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3927 | |
| 3928 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3929 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3930 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3931 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3932 | if (ret != -ENOENT) { |
| 3933 | btrfs_abort_transaction(trans, root, ret); |
| 3934 | goto out; |
| 3935 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3936 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3937 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3938 | if (IS_ERR_OR_NULL(di)) { |
| 3939 | if (!di) |
| 3940 | ret = -ENOENT; |
| 3941 | else |
| 3942 | ret = PTR_ERR(di); |
| 3943 | btrfs_abort_transaction(trans, root, ret); |
| 3944 | goto out; |
| 3945 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3946 | |
| 3947 | leaf = path->nodes[0]; |
| 3948 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3949 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3950 | index = key.offset; |
| 3951 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3952 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3953 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3954 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3955 | if (ret) { |
| 3956 | btrfs_abort_transaction(trans, root, ret); |
| 3957 | goto out; |
| 3958 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3959 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3960 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3961 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3962 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3963 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3964 | if (ret) |
| 3965 | btrfs_abort_transaction(trans, root, ret); |
| 3966 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3967 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3968 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3969 | } |
| 3970 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3971 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3972 | { |
| 3973 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3974 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3975 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3976 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3977 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3978 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3979 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3980 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 3981 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3982 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3983 | trans = __unlink_start_trans(dir, dentry); |
| 3984 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3985 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3986 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3987 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3988 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3989 | BTRFS_I(inode)->location.objectid, |
| 3990 | dentry->d_name.name, |
| 3991 | dentry->d_name.len); |
| 3992 | goto out; |
| 3993 | } |
| 3994 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3995 | err = btrfs_orphan_add(trans, inode); |
| 3996 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3997 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3998 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3999 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4000 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 4001 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4002 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4003 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4004 | out: |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4005 | __unlink_end_trans(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4006 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4007 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4008 | return err; |
| 4009 | } |
| 4010 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4011 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4012 | * this can truncate away extent items, csum items and directory items. |
| 4013 | * 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] | 4014 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4015 | * |
| 4016 | * csum items that cross the new i_size are truncated to the new size |
| 4017 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4018 | * |
| 4019 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4020 | * 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] | 4021 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4022 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4023 | struct btrfs_root *root, |
| 4024 | struct inode *inode, |
| 4025 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4026 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4027 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4028 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4029 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4030 | struct btrfs_key key; |
| 4031 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4032 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4033 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4034 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4035 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4036 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4037 | int found_extent; |
| 4038 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4039 | int pending_del_nr = 0; |
| 4040 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4041 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4042 | int ret; |
| 4043 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4044 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4045 | |
| 4046 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4047 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4048 | path = btrfs_alloc_path(); |
| 4049 | if (!path) |
| 4050 | return -ENOMEM; |
| 4051 | path->reada = -1; |
| 4052 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4053 | /* |
| 4054 | * We want to drop from the next block forward in case this new size is |
| 4055 | * not block aligned since we will be keeping the last block of the |
| 4056 | * extent just the way it is. |
| 4057 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4058 | if (root->ref_cows || root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4059 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4060 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4061 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4062 | /* |
| 4063 | * This function is also used to drop the items in the log tree before |
| 4064 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4065 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4066 | * items. |
| 4067 | */ |
| 4068 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4069 | btrfs_kill_delayed_inode_items(inode); |
| 4070 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4071 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4072 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4073 | key.type = (u8)-1; |
| 4074 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4075 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4076 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4077 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4078 | if (ret < 0) { |
| 4079 | err = ret; |
| 4080 | goto out; |
| 4081 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4082 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4083 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4084 | /* there are no items in the tree for us to truncate, we're |
| 4085 | * done |
| 4086 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4087 | if (path->slots[0] == 0) |
| 4088 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4089 | path->slots[0]--; |
| 4090 | } |
| 4091 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4092 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4093 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4094 | leaf = path->nodes[0]; |
| 4095 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4096 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4097 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4098 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4099 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4100 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4101 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4102 | break; |
| 4103 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4104 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4105 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4106 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4107 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4108 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4109 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4110 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4111 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4112 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4113 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4114 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4115 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4116 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4117 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4118 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4119 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4120 | } else { |
| 4121 | if (item_end < new_size) |
| 4122 | break; |
| 4123 | if (found_key.offset >= new_size) |
| 4124 | del_item = 1; |
| 4125 | else |
| 4126 | del_item = 0; |
| 4127 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4128 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4129 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4130 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4131 | goto delete; |
| 4132 | |
| 4133 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4134 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4135 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4136 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4137 | u64 orig_num_bytes = |
| 4138 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4139 | extent_num_bytes = ALIGN(new_size - |
| 4140 | found_key.offset, |
| 4141 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4142 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4143 | extent_num_bytes); |
| 4144 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4145 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4146 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4147 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4148 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4149 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4150 | extent_num_bytes = |
| 4151 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4152 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4153 | extent_offset = found_key.offset - |
| 4154 | btrfs_file_extent_offset(leaf, fi); |
| 4155 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4156 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4157 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4158 | if (extent_start != 0) { |
| 4159 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4160 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4161 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4162 | } |
| 4163 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4164 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4165 | /* |
| 4166 | * we can't truncate inline items that have had |
| 4167 | * special encodings |
| 4168 | */ |
| 4169 | if (!del_item && |
| 4170 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4171 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4172 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4173 | u32 size = new_size - found_key.offset; |
| 4174 | |
| 4175 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4176 | inode_sub_bytes(inode, item_end + 1 - |
| 4177 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4178 | } |
| 4179 | size = |
| 4180 | btrfs_file_extent_calc_inline_size(size); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4181 | btrfs_truncate_item(trans, root, path, |
| 4182 | size, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4183 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4184 | inode_sub_bytes(inode, item_end + 1 - |
| 4185 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4186 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4187 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4188 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4189 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4190 | if (!pending_del_nr) { |
| 4191 | /* no pending yet, add ourselves */ |
| 4192 | pending_del_slot = path->slots[0]; |
| 4193 | pending_del_nr = 1; |
| 4194 | } else if (pending_del_nr && |
| 4195 | path->slots[0] + 1 == pending_del_slot) { |
| 4196 | /* hop on the pending chunk */ |
| 4197 | pending_del_nr++; |
| 4198 | pending_del_slot = path->slots[0]; |
| 4199 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4200 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4201 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4202 | } else { |
| 4203 | break; |
| 4204 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4205 | if (found_extent && (root->ref_cows || |
| 4206 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4207 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4208 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4209 | extent_num_bytes, 0, |
| 4210 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4211 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4212 | BUG_ON(ret); |
| 4213 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4214 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4215 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4216 | break; |
| 4217 | |
| 4218 | if (path->slots[0] == 0 || |
| 4219 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4220 | if (pending_del_nr) { |
| 4221 | ret = btrfs_del_items(trans, root, path, |
| 4222 | pending_del_slot, |
| 4223 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4224 | if (ret) { |
| 4225 | btrfs_abort_transaction(trans, |
| 4226 | root, ret); |
| 4227 | goto error; |
| 4228 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4229 | pending_del_nr = 0; |
| 4230 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4231 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4232 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4233 | } else { |
| 4234 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4235 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4236 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4237 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4238 | if (pending_del_nr) { |
| 4239 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4240 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4241 | if (ret) |
| 4242 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4243 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4244 | error: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4245 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4246 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4247 | } |
| 4248 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4249 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4250 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4251 | * @inode - inode that we're zeroing |
| 4252 | * @from - the offset to start zeroing |
| 4253 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4254 | * offset |
| 4255 | * @front - zero up to the offset instead of from the offset on |
| 4256 | * |
| 4257 | * This will find the page for the "from" offset and cow the page and zero the |
| 4258 | * 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] | 4259 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4260 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4261 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4262 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4263 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4264 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4265 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4266 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4267 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4268 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4269 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4270 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4271 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4272 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4273 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4274 | int ret = 0; |
| 4275 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4276 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4277 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4278 | if ((offset & (blocksize - 1)) == 0 && |
| 4279 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4280 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4281 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4282 | if (ret) |
| 4283 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4284 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4285 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4286 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4287 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4288 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4289 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4290 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4291 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4292 | |
| 4293 | page_start = page_offset(page); |
| 4294 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4295 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4296 | if (!PageUptodate(page)) { |
| 4297 | ret = btrfs_readpage(NULL, page); |
| 4298 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4299 | if (page->mapping != mapping) { |
| 4300 | unlock_page(page); |
| 4301 | page_cache_release(page); |
| 4302 | goto again; |
| 4303 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4304 | if (!PageUptodate(page)) { |
| 4305 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4306 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4307 | } |
| 4308 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4309 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4310 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4311 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4312 | set_page_extent_mapped(page); |
| 4313 | |
| 4314 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4315 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4316 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4317 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4318 | unlock_page(page); |
| 4319 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4320 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4321 | btrfs_put_ordered_extent(ordered); |
| 4322 | goto again; |
| 4323 | } |
| 4324 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4325 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4326 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4327 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4328 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4329 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4330 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4331 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4332 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4333 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4334 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4335 | goto out_unlock; |
| 4336 | } |
| 4337 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4338 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4339 | if (!len) |
| 4340 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4341 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4342 | if (front) |
| 4343 | memset(kaddr, 0, offset); |
| 4344 | else |
| 4345 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4346 | flush_dcache_page(page); |
| 4347 | kunmap(page); |
| 4348 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4349 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4350 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4351 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4352 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4353 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4354 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4355 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4356 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4357 | unlock_page(page); |
| 4358 | page_cache_release(page); |
| 4359 | out: |
| 4360 | return ret; |
| 4361 | } |
| 4362 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4363 | /* |
| 4364 | * This function puts in dummy file extents for the area we're creating a hole |
| 4365 | * for. So if we are truncating this file to a larger size we need to insert |
| 4366 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4367 | * the range between oldsize and size |
| 4368 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4369 | 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] | 4370 | { |
| 4371 | struct btrfs_trans_handle *trans; |
| 4372 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4373 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4374 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4375 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4376 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4377 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4378 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4379 | u64 last_byte; |
| 4380 | u64 cur_offset; |
| 4381 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4382 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4383 | |
| 4384 | if (size <= hole_start) |
| 4385 | return 0; |
| 4386 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4387 | while (1) { |
| 4388 | struct btrfs_ordered_extent *ordered; |
| 4389 | btrfs_wait_ordered_range(inode, hole_start, |
| 4390 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4391 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4392 | &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4393 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 4394 | if (!ordered) |
| 4395 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4396 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4397 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4398 | btrfs_put_ordered_extent(ordered); |
| 4399 | } |
| 4400 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4401 | cur_offset = hole_start; |
| 4402 | while (1) { |
| 4403 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4404 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4405 | if (IS_ERR(em)) { |
| 4406 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4407 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4408 | break; |
| 4409 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4410 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4411 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4412 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4413 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4414 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4415 | |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 4416 | trans = btrfs_start_transaction(root, 3); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4417 | if (IS_ERR(trans)) { |
| 4418 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4419 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4420 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4421 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4422 | err = btrfs_drop_extents(trans, root, inode, |
| 4423 | cur_offset, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 4424 | cur_offset + hole_size, 1); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4425 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4426 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4427 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 4428 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4429 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4430 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4431 | err = btrfs_insert_file_extent(trans, root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4432 | btrfs_ino(inode), cur_offset, 0, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4433 | 0, hole_size, 0, hole_size, |
| 4434 | 0, 0, 0); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4435 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4436 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4437 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 4438 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4439 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4440 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4441 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4442 | cur_offset + hole_size - 1, 0); |
| 4443 | hole_em = alloc_extent_map(); |
| 4444 | if (!hole_em) { |
| 4445 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4446 | &BTRFS_I(inode)->runtime_flags); |
| 4447 | goto next; |
| 4448 | } |
| 4449 | hole_em->start = cur_offset; |
| 4450 | hole_em->len = hole_size; |
| 4451 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4452 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4453 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4454 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4455 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4456 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4457 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4458 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
| 4459 | hole_em->generation = trans->transid; |
| 4460 | |
| 4461 | while (1) { |
| 4462 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4463 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4464 | write_unlock(&em_tree->lock); |
| 4465 | if (err != -EEXIST) |
| 4466 | break; |
| 4467 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4468 | cur_offset + |
| 4469 | hole_size - 1, 0); |
| 4470 | } |
| 4471 | free_extent_map(hole_em); |
| 4472 | next: |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 4473 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4474 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4475 | } |
| 4476 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4477 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4478 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4479 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4480 | break; |
| 4481 | } |
| 4482 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4483 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4484 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4485 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4486 | return err; |
| 4487 | } |
| 4488 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4489 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4490 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4491 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4492 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4493 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4494 | loff_t newsize = attr->ia_size; |
| 4495 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4496 | int ret; |
| 4497 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4498 | if (newsize == oldsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4499 | return 0; |
| 4500 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4501 | /* |
| 4502 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4503 | * special case where we need to update the times despite not having |
| 4504 | * these flags set. For all other operations the VFS set these flags |
| 4505 | * explicitly if it wants a timestamp update. |
| 4506 | */ |
| 4507 | if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) |
| 4508 | inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb); |
| 4509 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4510 | if (newsize > oldsize) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4511 | truncate_pagecache(inode, oldsize, newsize); |
| 4512 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4513 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4514 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4515 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4516 | trans = btrfs_start_transaction(root, 1); |
| 4517 | if (IS_ERR(trans)) |
| 4518 | return PTR_ERR(trans); |
| 4519 | |
| 4520 | i_size_write(inode, newsize); |
| 4521 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4522 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4523 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4524 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4525 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4526 | /* |
| 4527 | * We're truncating a file that used to have good data down to |
| 4528 | * zero. Make sure it gets into the ordered flush list so that |
| 4529 | * any new writes get down to disk quickly. |
| 4530 | */ |
| 4531 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4532 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4533 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4534 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4535 | /* |
| 4536 | * 1 for the orphan item we're going to add |
| 4537 | * 1 for the orphan item deletion. |
| 4538 | */ |
| 4539 | trans = btrfs_start_transaction(root, 2); |
| 4540 | if (IS_ERR(trans)) |
| 4541 | return PTR_ERR(trans); |
| 4542 | |
| 4543 | /* |
| 4544 | * We need to do this in case we fail at _any_ point during the |
| 4545 | * actual truncate. Once we do the truncate_setsize we could |
| 4546 | * invalidate pages which forces any outstanding ordered io to |
| 4547 | * be instantly completed which will give us extents that need |
| 4548 | * to be truncated. If we fail to get an orphan inode down we |
| 4549 | * could have left over extents that were never meant to live, |
| 4550 | * so we need to garuntee from this point on that everything |
| 4551 | * will be consistent. |
| 4552 | */ |
| 4553 | ret = btrfs_orphan_add(trans, inode); |
| 4554 | btrfs_end_transaction(trans, root); |
| 4555 | if (ret) |
| 4556 | return ret; |
| 4557 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4558 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4559 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4560 | |
| 4561 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4562 | btrfs_inode_block_unlocked_dio(inode); |
| 4563 | inode_dio_wait(inode); |
| 4564 | btrfs_inode_resume_unlocked_dio(inode); |
| 4565 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4566 | ret = btrfs_truncate(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4567 | if (ret && inode->i_nlink) |
| 4568 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4571 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4574 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4575 | { |
| 4576 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4577 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4578 | int err; |
| 4579 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4580 | if (btrfs_root_readonly(root)) |
| 4581 | return -EROFS; |
| 4582 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4583 | err = inode_change_ok(inode, attr); |
| 4584 | if (err) |
| 4585 | return err; |
| 4586 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4587 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4588 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4589 | if (err) |
| 4590 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4591 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4592 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4593 | if (attr->ia_valid) { |
| 4594 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4595 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4596 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4597 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4598 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4599 | err = btrfs_acl_chmod(inode); |
| 4600 | } |
| 4601 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4602 | return err; |
| 4603 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4604 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4605 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4606 | { |
| 4607 | struct btrfs_trans_handle *trans; |
| 4608 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4609 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4610 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4611 | int ret; |
| 4612 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4613 | trace_btrfs_inode_evict(inode); |
| 4614 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4615 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4616 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 4617 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4618 | goto no_delete; |
| 4619 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4620 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4621 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4622 | goto no_delete; |
| 4623 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4624 | /* 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] | 4625 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4626 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4627 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4628 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4629 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4630 | goto no_delete; |
| 4631 | } |
| 4632 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4633 | if (inode->i_nlink > 0) { |
| 4634 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4635 | goto no_delete; |
| 4636 | } |
| 4637 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4638 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4639 | if (ret) { |
| 4640 | btrfs_orphan_del(NULL, inode); |
| 4641 | goto no_delete; |
| 4642 | } |
| 4643 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4644 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4645 | if (!rsv) { |
| 4646 | btrfs_orphan_del(NULL, inode); |
| 4647 | goto no_delete; |
| 4648 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4649 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4650 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4651 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4652 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4653 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4654 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4655 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4656 | * This is a bit simpler than btrfs_truncate since we've already |
| 4657 | * reserved our space for our orphan item in the unlink, so we just |
| 4658 | * need to reserve some slack space in case we add bytes and update |
| 4659 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4660 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4661 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4662 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4663 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4664 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4665 | /* |
| 4666 | * Try and steal from the global reserve since we will |
| 4667 | * likely not use this space anyway, we want to try as |
| 4668 | * hard as possible to get this to work. |
| 4669 | */ |
| 4670 | if (ret) |
| 4671 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4672 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4673 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4674 | btrfs_warn(root->fs_info, |
| 4675 | "Could not get space for a delete, will truncate on mount %d", |
| 4676 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4677 | btrfs_orphan_del(NULL, inode); |
| 4678 | btrfs_free_block_rsv(root, rsv); |
| 4679 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4680 | } |
| 4681 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4682 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4683 | if (IS_ERR(trans)) { |
| 4684 | btrfs_orphan_del(NULL, inode); |
| 4685 | btrfs_free_block_rsv(root, rsv); |
| 4686 | goto no_delete; |
| 4687 | } |
| 4688 | |
| 4689 | trans->block_rsv = rsv; |
| 4690 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4691 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4692 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4693 | break; |
| 4694 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4695 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4696 | btrfs_end_transaction(trans, root); |
| 4697 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4698 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4699 | } |
| 4700 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4701 | btrfs_free_block_rsv(root, rsv); |
| 4702 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4703 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4704 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4705 | ret = btrfs_orphan_del(trans, inode); |
| 4706 | BUG_ON(ret); |
| 4707 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4708 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4709 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4710 | if (!(root == root->fs_info->tree_root || |
| 4711 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4712 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4713 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4714 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4715 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4716 | no_delete: |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 4717 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4718 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4719 | } |
| 4720 | |
| 4721 | /* |
| 4722 | * this returns the key found in the dir entry in the location pointer. |
| 4723 | * If no dir entries were found, location->objectid is 0. |
| 4724 | */ |
| 4725 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 4726 | struct btrfs_key *location) |
| 4727 | { |
| 4728 | const char *name = dentry->d_name.name; |
| 4729 | int namelen = dentry->d_name.len; |
| 4730 | struct btrfs_dir_item *di; |
| 4731 | struct btrfs_path *path; |
| 4732 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4733 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4734 | |
| 4735 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4736 | if (!path) |
| 4737 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4738 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4739 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4740 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4741 | if (IS_ERR(di)) |
| 4742 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4743 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4744 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4745 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4746 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4747 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4748 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4749 | btrfs_free_path(path); |
| 4750 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4751 | out_err: |
| 4752 | location->objectid = 0; |
| 4753 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | /* |
| 4757 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 4758 | * needs to be changed to reflect the root directory of the tree root. This |
| 4759 | * is kind of like crossing a mount point. |
| 4760 | */ |
| 4761 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4762 | struct inode *dir, |
| 4763 | struct dentry *dentry, |
| 4764 | struct btrfs_key *location, |
| 4765 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4766 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4767 | struct btrfs_path *path; |
| 4768 | struct btrfs_root *new_root; |
| 4769 | struct btrfs_root_ref *ref; |
| 4770 | struct extent_buffer *leaf; |
| 4771 | int ret; |
| 4772 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4773 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4774 | path = btrfs_alloc_path(); |
| 4775 | if (!path) { |
| 4776 | err = -ENOMEM; |
| 4777 | goto out; |
| 4778 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4779 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4780 | err = -ENOENT; |
| 4781 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 4782 | BTRFS_I(dir)->root->root_key.objectid, |
| 4783 | location->objectid); |
| 4784 | if (ret) { |
| 4785 | if (ret < 0) |
| 4786 | err = ret; |
| 4787 | goto out; |
| 4788 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4789 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4790 | leaf = path->nodes[0]; |
| 4791 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4792 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4793 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 4794 | goto out; |
| 4795 | |
| 4796 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 4797 | (unsigned long)(ref + 1), |
| 4798 | dentry->d_name.len); |
| 4799 | if (ret) |
| 4800 | goto out; |
| 4801 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4802 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4803 | |
| 4804 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 4805 | if (IS_ERR(new_root)) { |
| 4806 | err = PTR_ERR(new_root); |
| 4807 | goto out; |
| 4808 | } |
| 4809 | |
| 4810 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 4811 | err = -ENOENT; |
| 4812 | goto out; |
| 4813 | } |
| 4814 | |
| 4815 | *sub_root = new_root; |
| 4816 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 4817 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4818 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4819 | err = 0; |
| 4820 | out: |
| 4821 | btrfs_free_path(path); |
| 4822 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4823 | } |
| 4824 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4825 | static void inode_tree_add(struct inode *inode) |
| 4826 | { |
| 4827 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4828 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4829 | struct rb_node **p; |
| 4830 | struct rb_node *parent; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4831 | u64 ino = btrfs_ino(inode); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4832 | again: |
| 4833 | p = &root->inode_tree.rb_node; |
| 4834 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4835 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 4836 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4837 | return; |
| 4838 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4839 | spin_lock(&root->inode_lock); |
| 4840 | while (*p) { |
| 4841 | parent = *p; |
| 4842 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 4843 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4844 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4845 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4846 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4847 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4848 | else { |
| 4849 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 4850 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4851 | rb_erase(parent, &root->inode_tree); |
| 4852 | RB_CLEAR_NODE(parent); |
| 4853 | spin_unlock(&root->inode_lock); |
| 4854 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4855 | } |
| 4856 | } |
| 4857 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 4858 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 4859 | spin_unlock(&root->inode_lock); |
| 4860 | } |
| 4861 | |
| 4862 | static void inode_tree_del(struct inode *inode) |
| 4863 | { |
| 4864 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4865 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4866 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4867 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4868 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4869 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4870 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4871 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4872 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4873 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4874 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4875 | /* |
| 4876 | * Free space cache has inodes in the tree root, but the tree root has a |
| 4877 | * root_refs of 0, so this could end up dropping the tree root as a |
| 4878 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 4879 | * make sure we don't drop it. |
| 4880 | */ |
| 4881 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 4882 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4883 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 4884 | spin_lock(&root->inode_lock); |
| 4885 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 4886 | spin_unlock(&root->inode_lock); |
| 4887 | if (empty) |
| 4888 | btrfs_add_dead_root(root); |
| 4889 | } |
| 4890 | } |
| 4891 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4892 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4893 | { |
| 4894 | struct rb_node *node; |
| 4895 | struct rb_node *prev; |
| 4896 | struct btrfs_inode *entry; |
| 4897 | struct inode *inode; |
| 4898 | u64 objectid = 0; |
| 4899 | |
| 4900 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4901 | |
| 4902 | spin_lock(&root->inode_lock); |
| 4903 | again: |
| 4904 | node = root->inode_tree.rb_node; |
| 4905 | prev = NULL; |
| 4906 | while (node) { |
| 4907 | prev = node; |
| 4908 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4909 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4910 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4911 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4912 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4913 | node = node->rb_right; |
| 4914 | else |
| 4915 | break; |
| 4916 | } |
| 4917 | if (!node) { |
| 4918 | while (prev) { |
| 4919 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4920 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4921 | node = prev; |
| 4922 | break; |
| 4923 | } |
| 4924 | prev = rb_next(prev); |
| 4925 | } |
| 4926 | } |
| 4927 | while (node) { |
| 4928 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4929 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4930 | inode = igrab(&entry->vfs_inode); |
| 4931 | if (inode) { |
| 4932 | spin_unlock(&root->inode_lock); |
| 4933 | if (atomic_read(&inode->i_count) > 1) |
| 4934 | d_prune_aliases(inode); |
| 4935 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4936 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4937 | * the inode cache when its usage count |
| 4938 | * hits zero. |
| 4939 | */ |
| 4940 | iput(inode); |
| 4941 | cond_resched(); |
| 4942 | spin_lock(&root->inode_lock); |
| 4943 | goto again; |
| 4944 | } |
| 4945 | |
| 4946 | if (cond_resched_lock(&root->inode_lock)) |
| 4947 | goto again; |
| 4948 | |
| 4949 | node = rb_next(node); |
| 4950 | } |
| 4951 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4952 | } |
| 4953 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4954 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4955 | { |
| 4956 | struct btrfs_iget_args *args = p; |
| 4957 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4958 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4959 | return 0; |
| 4960 | } |
| 4961 | |
| 4962 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4963 | { |
| 4964 | struct btrfs_iget_args *args = opaque; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4965 | return args->ino == btrfs_ino(inode) && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4966 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4967 | } |
| 4968 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4969 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4970 | u64 objectid, |
| 4971 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4972 | { |
| 4973 | struct inode *inode; |
| 4974 | struct btrfs_iget_args args; |
| 4975 | args.ino = objectid; |
| 4976 | args.root = root; |
| 4977 | |
| 4978 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 4979 | btrfs_init_locked_inode, |
| 4980 | (void *)&args); |
| 4981 | return inode; |
| 4982 | } |
| 4983 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4984 | /* Get an inode object given its location and corresponding root. |
| 4985 | * Returns in *is_new if the inode was read from disk |
| 4986 | */ |
| 4987 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4988 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4989 | { |
| 4990 | struct inode *inode; |
| 4991 | |
| 4992 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4993 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4994 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4995 | |
| 4996 | if (inode->i_state & I_NEW) { |
| 4997 | BTRFS_I(inode)->root = root; |
| 4998 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4999 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5000 | if (!is_bad_inode(inode)) { |
| 5001 | inode_tree_add(inode); |
| 5002 | unlock_new_inode(inode); |
| 5003 | if (new) |
| 5004 | *new = 1; |
| 5005 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5006 | unlock_new_inode(inode); |
| 5007 | iput(inode); |
| 5008 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5009 | } |
| 5010 | } |
| 5011 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5012 | return inode; |
| 5013 | } |
| 5014 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5015 | static struct inode *new_simple_dir(struct super_block *s, |
| 5016 | struct btrfs_key *key, |
| 5017 | struct btrfs_root *root) |
| 5018 | { |
| 5019 | struct inode *inode = new_inode(s); |
| 5020 | |
| 5021 | if (!inode) |
| 5022 | return ERR_PTR(-ENOMEM); |
| 5023 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5024 | BTRFS_I(inode)->root = root; |
| 5025 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5026 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5027 | |
| 5028 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5029 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5030 | inode->i_fop = &simple_dir_operations; |
| 5031 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 5032 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 5033 | |
| 5034 | return inode; |
| 5035 | } |
| 5036 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5037 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5038 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5039 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5040 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5041 | struct btrfs_root *sub_root = root; |
| 5042 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5043 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5044 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5045 | |
| 5046 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5047 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5048 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5049 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5050 | if (ret < 0) |
| 5051 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5052 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5053 | if (location.objectid == 0) |
| 5054 | return NULL; |
| 5055 | |
| 5056 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5057 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5058 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5059 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5060 | |
| 5061 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5062 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5063 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5064 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5065 | &location, &sub_root); |
| 5066 | if (ret < 0) { |
| 5067 | if (ret != -ENOENT) |
| 5068 | inode = ERR_PTR(ret); |
| 5069 | else |
| 5070 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5071 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5072 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5073 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5074 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5075 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5076 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5077 | down_read(&root->fs_info->cleanup_work_sem); |
| 5078 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5079 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5080 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5081 | if (ret) |
| 5082 | inode = ERR_PTR(ret); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5085 | return inode; |
| 5086 | } |
| 5087 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5088 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5089 | { |
| 5090 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5091 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5092 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5093 | if (!inode && !IS_ROOT(dentry)) |
| 5094 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5095 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5096 | if (inode) { |
| 5097 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5098 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5099 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5100 | |
| 5101 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5102 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5103 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5104 | return 0; |
| 5105 | } |
| 5106 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5107 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5108 | { |
| 5109 | if (dentry->d_fsdata) |
| 5110 | kfree(dentry->d_fsdata); |
| 5111 | } |
| 5112 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5113 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5114 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5115 | { |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5116 | struct dentry *ret; |
| 5117 | |
| 5118 | ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5119 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5120 | } |
| 5121 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5122 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5123 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5124 | }; |
| 5125 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 5126 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 5127 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5128 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5129 | struct inode *inode = file_inode(filp); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5130 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5131 | struct btrfs_item *item; |
| 5132 | struct btrfs_dir_item *di; |
| 5133 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5134 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5135 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5136 | struct list_head ins_list; |
| 5137 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5138 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5139 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5140 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5141 | unsigned char d_type; |
| 5142 | int over = 0; |
| 5143 | u32 di_cur; |
| 5144 | u32 di_total; |
| 5145 | u32 di_len; |
| 5146 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5147 | char tmp_name[32]; |
| 5148 | char *name_ptr; |
| 5149 | int name_len; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5150 | int is_curr = 0; /* filp->f_pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5151 | |
| 5152 | /* FIXME, use a real flag for deciding about the key type */ |
| 5153 | if (root->fs_info->tree_root == root) |
| 5154 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5155 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5156 | /* special case for "." */ |
| 5157 | if (filp->f_pos == 0) { |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 5158 | over = filldir(dirent, ".", 1, |
| 5159 | filp->f_pos, btrfs_ino(inode), DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5160 | if (over) |
| 5161 | return 0; |
| 5162 | filp->f_pos = 1; |
| 5163 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5164 | /* special case for .., just use the back ref */ |
| 5165 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 5166 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5167 | over = filldir(dirent, "..", 2, |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 5168 | filp->f_pos, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5169 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5170 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5171 | filp->f_pos = 2; |
| 5172 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5173 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5174 | if (!path) |
| 5175 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5176 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5177 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5178 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5179 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5180 | INIT_LIST_HEAD(&ins_list); |
| 5181 | INIT_LIST_HEAD(&del_list); |
| 5182 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5183 | } |
| 5184 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5185 | btrfs_set_key_type(&key, key_type); |
| 5186 | key.offset = filp->f_pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5187 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5188 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5189 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5190 | if (ret < 0) |
| 5191 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5192 | |
| 5193 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5194 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5195 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5196 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5197 | ret = btrfs_next_leaf(root, path); |
| 5198 | if (ret < 0) |
| 5199 | goto err; |
| 5200 | else if (ret > 0) |
| 5201 | break; |
| 5202 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5203 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5204 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5205 | item = btrfs_item_nr(leaf, slot); |
| 5206 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5207 | |
| 5208 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5209 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5210 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5211 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5212 | if (found_key.offset < filp->f_pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5213 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5214 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5215 | btrfs_should_delete_dir_index(&del_list, |
| 5216 | found_key.offset)) |
| 5217 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5218 | |
| 5219 | filp->f_pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5220 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5221 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5222 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5223 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5224 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5225 | |
| 5226 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5227 | struct btrfs_key location; |
| 5228 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5229 | if (verify_dir_item(root, leaf, di)) |
| 5230 | break; |
| 5231 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5232 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5233 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5234 | name_ptr = tmp_name; |
| 5235 | } else { |
| 5236 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5237 | if (!name_ptr) { |
| 5238 | ret = -ENOMEM; |
| 5239 | goto err; |
| 5240 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5241 | } |
| 5242 | read_extent_buffer(leaf, name_ptr, |
| 5243 | (unsigned long)(di + 1), name_len); |
| 5244 | |
| 5245 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5246 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5247 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5248 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5249 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5250 | * skip it. |
| 5251 | * |
| 5252 | * In contrast to old kernels, we insert the snapshot's |
| 5253 | * dir item and dir index after it has been created, so |
| 5254 | * we won't find a reference to our own snapshot. We |
| 5255 | * still keep the following code for backward |
| 5256 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5257 | */ |
| 5258 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5259 | location.objectid == root->root_key.objectid) { |
| 5260 | over = 0; |
| 5261 | goto skip; |
| 5262 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5263 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5264 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5265 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5266 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5267 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5268 | if (name_ptr != tmp_name) |
| 5269 | kfree(name_ptr); |
| 5270 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5271 | if (over) |
| 5272 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5273 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5274 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5275 | di_cur += di_len; |
| 5276 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5277 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5278 | next: |
| 5279 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5280 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5281 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5282 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5283 | if (is_curr) |
| 5284 | filp->f_pos++; |
| 5285 | ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir, |
| 5286 | &ins_list); |
| 5287 | if (ret) |
| 5288 | goto nopos; |
| 5289 | } |
| 5290 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5291 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 5292 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 5293 | /* |
| 5294 | * 32-bit glibc will use getdents64, but then strtol - |
| 5295 | * so the last number we can serve is this. |
| 5296 | */ |
| 5297 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 5298 | else |
| 5299 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5300 | nopos: |
| 5301 | ret = 0; |
| 5302 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5303 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5304 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5305 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5306 | return ret; |
| 5307 | } |
| 5308 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5309 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5310 | { |
| 5311 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5312 | struct btrfs_trans_handle *trans; |
| 5313 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5314 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5315 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5316 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5317 | return 0; |
| 5318 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5319 | 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] | 5320 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5321 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5322 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5323 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5324 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5325 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5326 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5327 | if (IS_ERR(trans)) |
| 5328 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5329 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5330 | } |
| 5331 | return ret; |
| 5332 | } |
| 5333 | |
| 5334 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5335 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5336 | * inode changes. But, it is most likely to find the inode in cache. |
| 5337 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5338 | * to keep or drop this code. |
| 5339 | */ |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5340 | int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5341 | { |
| 5342 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5343 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5344 | int ret; |
| 5345 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5346 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5347 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5348 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5349 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5350 | if (IS_ERR(trans)) |
| 5351 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5352 | |
| 5353 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5354 | if (ret && ret == -ENOSPC) { |
| 5355 | /* whoops, lets try again with the full transaction */ |
| 5356 | btrfs_end_transaction(trans, root); |
| 5357 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5358 | if (IS_ERR(trans)) |
| 5359 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5360 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5361 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5362 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5363 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5364 | if (BTRFS_I(inode)->delayed_node) |
| 5365 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5366 | |
| 5367 | return ret; |
| 5368 | } |
| 5369 | |
| 5370 | /* |
| 5371 | * This is a copy of file_update_time. We need this so we can return error on |
| 5372 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5373 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5374 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5375 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5376 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5377 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5378 | |
| 5379 | if (btrfs_root_readonly(root)) |
| 5380 | return -EROFS; |
| 5381 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5382 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5383 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5384 | if (flags & S_CTIME) |
| 5385 | inode->i_ctime = *now; |
| 5386 | if (flags & S_MTIME) |
| 5387 | inode->i_mtime = *now; |
| 5388 | if (flags & S_ATIME) |
| 5389 | inode->i_atime = *now; |
| 5390 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5391 | } |
| 5392 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5393 | /* |
| 5394 | * find the highest existing sequence number in a directory |
| 5395 | * and then set the in-memory index_cnt variable to reflect |
| 5396 | * free sequence numbers |
| 5397 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5398 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5399 | { |
| 5400 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5401 | struct btrfs_key key, found_key; |
| 5402 | struct btrfs_path *path; |
| 5403 | struct extent_buffer *leaf; |
| 5404 | int ret; |
| 5405 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5406 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5407 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 5408 | key.offset = (u64)-1; |
| 5409 | |
| 5410 | path = btrfs_alloc_path(); |
| 5411 | if (!path) |
| 5412 | return -ENOMEM; |
| 5413 | |
| 5414 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5415 | if (ret < 0) |
| 5416 | goto out; |
| 5417 | /* FIXME: we should be able to handle this */ |
| 5418 | if (ret == 0) |
| 5419 | goto out; |
| 5420 | ret = 0; |
| 5421 | |
| 5422 | /* |
| 5423 | * MAGIC NUMBER EXPLANATION: |
| 5424 | * since we search a directory based on f_pos we have to start at 2 |
| 5425 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5426 | * else has to start at 2 |
| 5427 | */ |
| 5428 | if (path->slots[0] == 0) { |
| 5429 | BTRFS_I(inode)->index_cnt = 2; |
| 5430 | goto out; |
| 5431 | } |
| 5432 | |
| 5433 | path->slots[0]--; |
| 5434 | |
| 5435 | leaf = path->nodes[0]; |
| 5436 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5437 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5438 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5439 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 5440 | BTRFS_I(inode)->index_cnt = 2; |
| 5441 | goto out; |
| 5442 | } |
| 5443 | |
| 5444 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5445 | out: |
| 5446 | btrfs_free_path(path); |
| 5447 | return ret; |
| 5448 | } |
| 5449 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5450 | /* |
| 5451 | * helper to find a free sequence number in a given directory. This current |
| 5452 | * code is very simple, later versions will do smarter things in the btree |
| 5453 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5454 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5455 | { |
| 5456 | int ret = 0; |
| 5457 | |
| 5458 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5459 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5460 | if (ret) { |
| 5461 | ret = btrfs_set_inode_index_count(dir); |
| 5462 | if (ret) |
| 5463 | return ret; |
| 5464 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5465 | } |
| 5466 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5467 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5468 | BTRFS_I(dir)->index_cnt++; |
| 5469 | |
| 5470 | return ret; |
| 5471 | } |
| 5472 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5473 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5474 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5475 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5476 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5477 | u64 ref_objectid, u64 objectid, |
| 5478 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5479 | { |
| 5480 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5481 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5482 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5483 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5484 | struct btrfs_inode_ref *ref; |
| 5485 | struct btrfs_key key[2]; |
| 5486 | u32 sizes[2]; |
| 5487 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5488 | int ret; |
| 5489 | int owner; |
| 5490 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5491 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5492 | if (!path) |
| 5493 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5494 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5495 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5496 | if (!inode) { |
| 5497 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5498 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5499 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5500 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5501 | /* |
| 5502 | * we have to initialize this early, so we can reclaim the inode |
| 5503 | * number if we fail afterwards in this function. |
| 5504 | */ |
| 5505 | inode->i_ino = objectid; |
| 5506 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5507 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5508 | trace_btrfs_inode_request(dir); |
| 5509 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5510 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5511 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5512 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5513 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5514 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5515 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5516 | } |
| 5517 | /* |
| 5518 | * index_cnt is ignored for everything but a dir, |
| 5519 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5520 | * number |
| 5521 | */ |
| 5522 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5523 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5524 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5525 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5526 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5527 | /* |
| 5528 | * We could have gotten an inode number from somebody who was fsynced |
| 5529 | * and then removed in this same transaction, so let's just set full |
| 5530 | * sync since it will be a full sync anyway and this will blow away the |
| 5531 | * old info in the log. |
| 5532 | */ |
| 5533 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5534 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5535 | if (S_ISDIR(mode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5536 | owner = 0; |
| 5537 | else |
| 5538 | owner = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5539 | |
| 5540 | key[0].objectid = objectid; |
| 5541 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 5542 | key[0].offset = 0; |
| 5543 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5544 | /* |
| 5545 | * Start new inodes with an inode_ref. This is slightly more |
| 5546 | * efficient for small numbers of hard links since they will |
| 5547 | * be packed into one item. Extended refs will kick in if we |
| 5548 | * add more hard links than can fit in the ref item. |
| 5549 | */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5550 | key[1].objectid = objectid; |
| 5551 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 5552 | key[1].offset = ref_objectid; |
| 5553 | |
| 5554 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 5555 | sizes[1] = name_len + sizeof(*ref); |
| 5556 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5557 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5558 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 5559 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5560 | goto fail; |
| 5561 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5562 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5563 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5564 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5565 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5566 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5567 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5568 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5569 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5570 | |
| 5571 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5572 | struct btrfs_inode_ref); |
| 5573 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5574 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5575 | ptr = (unsigned long)(ref + 1); |
| 5576 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5577 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5578 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5579 | btrfs_free_path(path); |
| 5580 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5581 | location = &BTRFS_I(inode)->location; |
| 5582 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5583 | location->offset = 0; |
| 5584 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 5585 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5586 | btrfs_inherit_iflags(inode, dir); |
| 5587 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5588 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5589 | if (btrfs_test_opt(root, NODATASUM)) |
| 5590 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5591 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5592 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5593 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5594 | } |
| 5595 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5596 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5597 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5598 | |
| 5599 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5600 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5601 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5602 | btrfs_update_root_times(trans, root); |
| 5603 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5604 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5605 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5606 | if (dir) |
| 5607 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5608 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5609 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5610 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5611 | } |
| 5612 | |
| 5613 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5614 | { |
| 5615 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5616 | } |
| 5617 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5618 | /* |
| 5619 | * utility function to add 'inode' into 'parent_inode' with |
| 5620 | * a give name and a given sequence number. |
| 5621 | * if 'add_backref' is true, also insert a backref from the |
| 5622 | * inode to the parent directory. |
| 5623 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5624 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5625 | struct inode *parent_inode, struct inode *inode, |
| 5626 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5627 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5628 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5629 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5630 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5631 | u64 ino = btrfs_ino(inode); |
| 5632 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5633 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5634 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5635 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 5636 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5637 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5638 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 5639 | key.offset = 0; |
| 5640 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5641 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5642 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5643 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 5644 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5645 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5646 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5647 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 5648 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5649 | } |
| 5650 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5651 | /* Nothing to clean up yet */ |
| 5652 | if (ret) |
| 5653 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5654 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5655 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 5656 | parent_inode, &key, |
| 5657 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 5658 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5659 | goto fail_dir_item; |
| 5660 | else if (ret) { |
| 5661 | btrfs_abort_transaction(trans, root, ret); |
| 5662 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5663 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5664 | |
| 5665 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 5666 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5667 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5668 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 5669 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 5670 | if (ret) |
| 5671 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5672 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 5673 | |
| 5674 | fail_dir_item: |
| 5675 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5676 | u64 local_index; |
| 5677 | int err; |
| 5678 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 5679 | key.objectid, root->root_key.objectid, |
| 5680 | parent_ino, &local_index, name, name_len); |
| 5681 | |
| 5682 | } else if (add_backref) { |
| 5683 | u64 local_index; |
| 5684 | int err; |
| 5685 | |
| 5686 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 5687 | ino, parent_ino, &local_index); |
| 5688 | } |
| 5689 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5693 | struct inode *dir, struct dentry *dentry, |
| 5694 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5695 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5696 | int err = btrfs_add_link(trans, dir, inode, |
| 5697 | dentry->d_name.name, dentry->d_name.len, |
| 5698 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5699 | if (err > 0) |
| 5700 | err = -EEXIST; |
| 5701 | return err; |
| 5702 | } |
| 5703 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5704 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 5705 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5706 | { |
| 5707 | struct btrfs_trans_handle *trans; |
| 5708 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5709 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5710 | int err; |
| 5711 | int drop_inode = 0; |
| 5712 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5713 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5714 | |
| 5715 | if (!new_valid_dev(rdev)) |
| 5716 | return -EINVAL; |
| 5717 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5718 | /* |
| 5719 | * 2 for inode item and ref |
| 5720 | * 2 for dir items |
| 5721 | * 1 for xattr if selinux is on |
| 5722 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5723 | trans = btrfs_start_transaction(root, 5); |
| 5724 | if (IS_ERR(trans)) |
| 5725 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5726 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5727 | err = btrfs_find_free_ino(root, &objectid); |
| 5728 | if (err) |
| 5729 | goto out_unlock; |
| 5730 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5731 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5732 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5733 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5734 | if (IS_ERR(inode)) { |
| 5735 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5736 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5737 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5738 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5739 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5740 | if (err) { |
| 5741 | drop_inode = 1; |
| 5742 | goto out_unlock; |
| 5743 | } |
| 5744 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5745 | /* |
| 5746 | * If the active LSM wants to access the inode during |
| 5747 | * d_instantiate it needs these. Smack checks to see |
| 5748 | * if the filesystem supports xattrs by looking at the |
| 5749 | * ops vector. |
| 5750 | */ |
| 5751 | |
| 5752 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5753 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5754 | if (err) |
| 5755 | drop_inode = 1; |
| 5756 | else { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5757 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 5758 | btrfs_update_inode(trans, root, inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5759 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5760 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5761 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5762 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5763 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5764 | if (drop_inode) { |
| 5765 | inode_dec_link_count(inode); |
| 5766 | iput(inode); |
| 5767 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5768 | return err; |
| 5769 | } |
| 5770 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5771 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 5772 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5773 | { |
| 5774 | struct btrfs_trans_handle *trans; |
| 5775 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5776 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5777 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5778 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5779 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5780 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5781 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5782 | /* |
| 5783 | * 2 for inode item and ref |
| 5784 | * 2 for dir items |
| 5785 | * 1 for xattr if selinux is on |
| 5786 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5787 | trans = btrfs_start_transaction(root, 5); |
| 5788 | if (IS_ERR(trans)) |
| 5789 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5790 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5791 | err = btrfs_find_free_ino(root, &objectid); |
| 5792 | if (err) |
| 5793 | goto out_unlock; |
| 5794 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5795 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5796 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5797 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5798 | if (IS_ERR(inode)) { |
| 5799 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5800 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5801 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5802 | drop_inode_on_err = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5803 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5804 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5805 | if (err) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5806 | goto out_unlock; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5807 | |
Filipe Brandenburger | 9185aa5 | 2012-11-30 03:40:08 +0000 | [diff] [blame] | 5808 | err = btrfs_update_inode(trans, root, inode); |
| 5809 | if (err) |
| 5810 | goto out_unlock; |
| 5811 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5812 | /* |
| 5813 | * If the active LSM wants to access the inode during |
| 5814 | * d_instantiate it needs these. Smack checks to see |
| 5815 | * if the filesystem supports xattrs by looking at the |
| 5816 | * ops vector. |
| 5817 | */ |
| 5818 | inode->i_fop = &btrfs_file_operations; |
| 5819 | inode->i_op = &btrfs_file_inode_operations; |
| 5820 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5821 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5822 | if (err) |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5823 | goto out_unlock; |
| 5824 | |
| 5825 | inode->i_mapping->a_ops = &btrfs_aops; |
| 5826 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 5827 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 5828 | d_instantiate(dentry, inode); |
| 5829 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5830 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5831 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5832 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5833 | inode_dec_link_count(inode); |
| 5834 | iput(inode); |
| 5835 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5836 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5837 | return err; |
| 5838 | } |
| 5839 | |
| 5840 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 5841 | struct dentry *dentry) |
| 5842 | { |
| 5843 | struct btrfs_trans_handle *trans; |
| 5844 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5845 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5846 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5847 | int err; |
| 5848 | int drop_inode = 0; |
| 5849 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5850 | /* do not allow sys_link's with other subvols of the same device */ |
| 5851 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 5852 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5853 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5854 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 5855 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5856 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5857 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5858 | if (err) |
| 5859 | goto fail; |
| 5860 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5861 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5862 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5863 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5864 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5865 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5866 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5867 | if (IS_ERR(trans)) { |
| 5868 | err = PTR_ERR(trans); |
| 5869 | goto fail; |
| 5870 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5871 | |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5872 | btrfs_inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5873 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5874 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 5875 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 5876 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5877 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5878 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5879 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5880 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5881 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5882 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 5883 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5884 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5885 | if (err) |
| 5886 | goto fail; |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5887 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5888 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5889 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5890 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5891 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5892 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5893 | if (drop_inode) { |
| 5894 | inode_dec_link_count(inode); |
| 5895 | iput(inode); |
| 5896 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5897 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5898 | return err; |
| 5899 | } |
| 5900 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 5901 | 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] | 5902 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5903 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5904 | struct btrfs_trans_handle *trans; |
| 5905 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5906 | int err = 0; |
| 5907 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5908 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5909 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5910 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5911 | /* |
| 5912 | * 2 items for inode and ref |
| 5913 | * 2 items for dir items |
| 5914 | * 1 for xattr if selinux is on |
| 5915 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5916 | trans = btrfs_start_transaction(root, 5); |
| 5917 | if (IS_ERR(trans)) |
| 5918 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5919 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5920 | err = btrfs_find_free_ino(root, &objectid); |
| 5921 | if (err) |
| 5922 | goto out_fail; |
| 5923 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5924 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5925 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5926 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5927 | if (IS_ERR(inode)) { |
| 5928 | err = PTR_ERR(inode); |
| 5929 | goto out_fail; |
| 5930 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5931 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5932 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5933 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5934 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5935 | if (err) |
| 5936 | goto out_fail; |
| 5937 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5938 | inode->i_op = &btrfs_dir_inode_operations; |
| 5939 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5940 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5941 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5942 | err = btrfs_update_inode(trans, root, inode); |
| 5943 | if (err) |
| 5944 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5945 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5946 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 5947 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5948 | if (err) |
| 5949 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5950 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5951 | d_instantiate(dentry, inode); |
| 5952 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5953 | |
| 5954 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5955 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5956 | if (drop_on_err) |
| 5957 | iput(inode); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5958 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5959 | return err; |
| 5960 | } |
| 5961 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5962 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 5963 | * and an extent that you want to insert, deal with overlap and insert |
| 5964 | * the new extent into the tree. |
| 5965 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5966 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 5967 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5968 | struct extent_map *em, |
| 5969 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5970 | { |
| 5971 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5972 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5973 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 5974 | start_diff = map_start - em->start; |
| 5975 | em->start = map_start; |
| 5976 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5977 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 5978 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5979 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5980 | em->block_len -= start_diff; |
| 5981 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5982 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5983 | } |
| 5984 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5985 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 5986 | struct inode *inode, struct page *page, |
| 5987 | size_t pg_offset, u64 extent_offset, |
| 5988 | struct btrfs_file_extent_item *item) |
| 5989 | { |
| 5990 | int ret; |
| 5991 | struct extent_buffer *leaf = path->nodes[0]; |
| 5992 | char *tmp; |
| 5993 | size_t max_size; |
| 5994 | unsigned long inline_size; |
| 5995 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5996 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5997 | |
| 5998 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5999 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6000 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6001 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 6002 | btrfs_item_nr(leaf, path->slots[0])); |
| 6003 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6004 | if (!tmp) |
| 6005 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6006 | ptr = btrfs_file_extent_inline_start(item); |
| 6007 | |
| 6008 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6009 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 6010 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6011 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6012 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6013 | if (ret) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6014 | char *kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6015 | unsigned long copy_size = min_t(u64, |
| 6016 | PAGE_CACHE_SIZE - pg_offset, |
| 6017 | max_size - extent_offset); |
| 6018 | memset(kaddr + pg_offset, 0, copy_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6019 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6020 | } |
| 6021 | kfree(tmp); |
| 6022 | return 0; |
| 6023 | } |
| 6024 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6025 | /* |
| 6026 | * 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] | 6027 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6028 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6029 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6030 | * |
| 6031 | * This also copies inline extents directly into the page. |
| 6032 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6033 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6034 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6035 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6036 | int create) |
| 6037 | { |
| 6038 | int ret; |
| 6039 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6040 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6041 | u64 extent_start = 0; |
| 6042 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6043 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6044 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6045 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6046 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6047 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6048 | struct extent_buffer *leaf; |
| 6049 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6050 | struct extent_map *em = NULL; |
| 6051 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6052 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6053 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6054 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6055 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6056 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6057 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6058 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6059 | if (em) |
| 6060 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6061 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6062 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6063 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6064 | if (em->start > start || em->start + em->len <= start) |
| 6065 | free_extent_map(em); |
| 6066 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6067 | free_extent_map(em); |
| 6068 | else |
| 6069 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6070 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6071 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6072 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6073 | err = -ENOMEM; |
| 6074 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6075 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6076 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6077 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6078 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6079 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6080 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6081 | |
| 6082 | if (!path) { |
| 6083 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6084 | if (!path) { |
| 6085 | err = -ENOMEM; |
| 6086 | goto out; |
| 6087 | } |
| 6088 | /* |
| 6089 | * Chances are we'll be called again, so go ahead and do |
| 6090 | * readahead |
| 6091 | */ |
| 6092 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6093 | } |
| 6094 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6095 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6096 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6097 | if (ret < 0) { |
| 6098 | err = ret; |
| 6099 | goto out; |
| 6100 | } |
| 6101 | |
| 6102 | if (ret != 0) { |
| 6103 | if (path->slots[0] == 0) |
| 6104 | goto not_found; |
| 6105 | path->slots[0]--; |
| 6106 | } |
| 6107 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6108 | leaf = path->nodes[0]; |
| 6109 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6110 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6111 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6112 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6113 | found_type = btrfs_key_type(&found_key); |
| 6114 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6115 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 6116 | goto not_found; |
| 6117 | } |
| 6118 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6119 | found_type = btrfs_file_extent_type(leaf, item); |
| 6120 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6121 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6122 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6123 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6124 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6125 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6126 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6127 | size_t size; |
| 6128 | size = btrfs_file_extent_inline_len(leaf, item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6129 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6130 | } |
| 6131 | |
| 6132 | if (start >= extent_end) { |
| 6133 | path->slots[0]++; |
| 6134 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6135 | ret = btrfs_next_leaf(root, path); |
| 6136 | if (ret < 0) { |
| 6137 | err = ret; |
| 6138 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6139 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6140 | if (ret > 0) |
| 6141 | goto not_found; |
| 6142 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6143 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6144 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6145 | if (found_key.objectid != objectid || |
| 6146 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6147 | goto not_found; |
| 6148 | if (start + len <= found_key.offset) |
| 6149 | goto not_found; |
| 6150 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6151 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6152 | em->len = found_key.offset - start; |
| 6153 | goto not_found_em; |
| 6154 | } |
| 6155 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6156 | em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6157 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6158 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6159 | em->start = extent_start; |
| 6160 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 6161 | em->orig_start = extent_start - |
| 6162 | btrfs_file_extent_offset(leaf, item); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6163 | em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 6164 | item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6165 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 6166 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6167 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6168 | goto insert; |
| 6169 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6170 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6171 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6172 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6173 | em->block_start = bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6174 | em->block_len = em->orig_block_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6175 | } else { |
| 6176 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 6177 | em->block_start = bytenr; |
| 6178 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6179 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 6180 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6181 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6182 | goto insert; |
| 6183 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6184 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6185 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6186 | size_t size; |
| 6187 | size_t extent_offset; |
| 6188 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6189 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6190 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6191 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6192 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6193 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6194 | goto out; |
| 6195 | } |
| 6196 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6197 | size = btrfs_file_extent_inline_len(leaf, item); |
| 6198 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6199 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6200 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6201 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6202 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6203 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6204 | em->orig_start = em->start; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6205 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6206 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6207 | em->compress_type = compress_type; |
| 6208 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6209 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6210 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6211 | if (btrfs_file_extent_compression(leaf, item) != |
| 6212 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6213 | ret = uncompress_inline(path, inode, page, |
| 6214 | pg_offset, |
| 6215 | extent_offset, item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6216 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6217 | } else { |
| 6218 | map = kmap(page); |
| 6219 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6220 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6221 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6222 | memset(map + pg_offset + copy_size, 0, |
| 6223 | PAGE_CACHE_SIZE - pg_offset - |
| 6224 | copy_size); |
| 6225 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6226 | kunmap(page); |
| 6227 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6228 | flush_dcache_page(page); |
| 6229 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6230 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6231 | if (!trans) { |
| 6232 | kunmap(page); |
| 6233 | free_extent_map(em); |
| 6234 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6235 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6236 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6237 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6238 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6239 | if (IS_ERR(trans)) |
| 6240 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6241 | goto again; |
| 6242 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6243 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6244 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6245 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6246 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6247 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6248 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6249 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6250 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6251 | goto insert; |
| 6252 | } else { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 6253 | WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6254 | } |
| 6255 | not_found: |
| 6256 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6257 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6258 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6259 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6260 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6261 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6262 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6263 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6264 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6265 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 6266 | (unsigned long long)em->start, |
| 6267 | (unsigned long long)em->len, |
| 6268 | (unsigned long long)start, |
| 6269 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6270 | err = -EIO; |
| 6271 | goto out; |
| 6272 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6273 | |
| 6274 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6275 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6276 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6277 | /* it is possible that someone inserted the extent into the tree |
| 6278 | * while we had the lock dropped. It is also possible that |
| 6279 | * an overlapping map exists in the tree |
| 6280 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6281 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6282 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6283 | |
| 6284 | ret = 0; |
| 6285 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6286 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6287 | if (existing && (existing->start > start || |
| 6288 | existing->start + existing->len <= start)) { |
| 6289 | free_extent_map(existing); |
| 6290 | existing = NULL; |
| 6291 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6292 | if (!existing) { |
| 6293 | existing = lookup_extent_mapping(em_tree, em->start, |
| 6294 | em->len); |
| 6295 | if (existing) { |
| 6296 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6297 | em, start, |
| 6298 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6299 | free_extent_map(existing); |
| 6300 | if (err) { |
| 6301 | free_extent_map(em); |
| 6302 | em = NULL; |
| 6303 | } |
| 6304 | } else { |
| 6305 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6306 | free_extent_map(em); |
| 6307 | em = NULL; |
| 6308 | } |
| 6309 | } else { |
| 6310 | free_extent_map(em); |
| 6311 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6312 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6313 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6314 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6315 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6316 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6317 | |
Tsutomu Itoh | f0bd95e | 2012-10-01 03:08:37 -0600 | [diff] [blame] | 6318 | if (em) |
| 6319 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6320 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6321 | if (path) |
| 6322 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6323 | if (trans) { |
| 6324 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6325 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6326 | err = ret; |
| 6327 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6328 | if (err) { |
| 6329 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6330 | return ERR_PTR(err); |
| 6331 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6332 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6333 | return em; |
| 6334 | } |
| 6335 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6336 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6337 | size_t pg_offset, u64 start, u64 len, |
| 6338 | int create) |
| 6339 | { |
| 6340 | struct extent_map *em; |
| 6341 | struct extent_map *hole_em = NULL; |
| 6342 | u64 range_start = start; |
| 6343 | u64 end; |
| 6344 | u64 found; |
| 6345 | u64 found_end; |
| 6346 | int err = 0; |
| 6347 | |
| 6348 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6349 | if (IS_ERR(em)) |
| 6350 | return em; |
| 6351 | if (em) { |
| 6352 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6353 | * if our em maps to |
| 6354 | * - a hole or |
| 6355 | * - a pre-alloc extent, |
| 6356 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6357 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6358 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6359 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6360 | return em; |
| 6361 | else |
| 6362 | hole_em = em; |
| 6363 | } |
| 6364 | |
| 6365 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6366 | end = start + len; |
| 6367 | if (end < start) |
| 6368 | end = (u64)-1; |
| 6369 | else |
| 6370 | end -= 1; |
| 6371 | |
| 6372 | em = NULL; |
| 6373 | |
| 6374 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6375 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6376 | end, len, EXTENT_DELALLOC, 1); |
| 6377 | found_end = range_start + found; |
| 6378 | if (found_end < range_start) |
| 6379 | found_end = (u64)-1; |
| 6380 | |
| 6381 | /* |
| 6382 | * we didn't find anything useful, return |
| 6383 | * the original results from get_extent() |
| 6384 | */ |
| 6385 | if (range_start > end || found_end <= start) { |
| 6386 | em = hole_em; |
| 6387 | hole_em = NULL; |
| 6388 | goto out; |
| 6389 | } |
| 6390 | |
| 6391 | /* adjust the range_start to make sure it doesn't |
| 6392 | * go backwards from the start they passed in |
| 6393 | */ |
| 6394 | range_start = max(start,range_start); |
| 6395 | found = found_end - range_start; |
| 6396 | |
| 6397 | if (found > 0) { |
| 6398 | u64 hole_start = start; |
| 6399 | u64 hole_len = len; |
| 6400 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6401 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6402 | if (!em) { |
| 6403 | err = -ENOMEM; |
| 6404 | goto out; |
| 6405 | } |
| 6406 | /* |
| 6407 | * when btrfs_get_extent can't find anything it |
| 6408 | * returns one huge hole |
| 6409 | * |
| 6410 | * make sure what it found really fits our range, and |
| 6411 | * adjust to make sure it is based on the start from |
| 6412 | * the caller |
| 6413 | */ |
| 6414 | if (hole_em) { |
| 6415 | u64 calc_end = extent_map_end(hole_em); |
| 6416 | |
| 6417 | if (calc_end <= start || (hole_em->start > end)) { |
| 6418 | free_extent_map(hole_em); |
| 6419 | hole_em = NULL; |
| 6420 | } else { |
| 6421 | hole_start = max(hole_em->start, start); |
| 6422 | hole_len = calc_end - hole_start; |
| 6423 | } |
| 6424 | } |
| 6425 | em->bdev = NULL; |
| 6426 | if (hole_em && range_start > hole_start) { |
| 6427 | /* our hole starts before our delalloc, so we |
| 6428 | * have to return just the parts of the hole |
| 6429 | * that go until the delalloc starts |
| 6430 | */ |
| 6431 | em->len = min(hole_len, |
| 6432 | range_start - hole_start); |
| 6433 | em->start = hole_start; |
| 6434 | em->orig_start = hole_start; |
| 6435 | /* |
| 6436 | * don't adjust block start at all, |
| 6437 | * it is fixed at EXTENT_MAP_HOLE |
| 6438 | */ |
| 6439 | em->block_start = hole_em->block_start; |
| 6440 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6441 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6442 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6443 | } else { |
| 6444 | em->start = range_start; |
| 6445 | em->len = found; |
| 6446 | em->orig_start = range_start; |
| 6447 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6448 | em->block_len = found; |
| 6449 | } |
| 6450 | } else if (hole_em) { |
| 6451 | return hole_em; |
| 6452 | } |
| 6453 | out: |
| 6454 | |
| 6455 | free_extent_map(hole_em); |
| 6456 | if (err) { |
| 6457 | free_extent_map(em); |
| 6458 | return ERR_PTR(err); |
| 6459 | } |
| 6460 | return em; |
| 6461 | } |
| 6462 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6463 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6464 | u64 start, u64 len) |
| 6465 | { |
| 6466 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6467 | struct btrfs_trans_handle *trans; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6468 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6469 | struct btrfs_key ins; |
| 6470 | u64 alloc_hint; |
| 6471 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6472 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6473 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6474 | if (IS_ERR(trans)) |
| 6475 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6476 | |
| 6477 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 6478 | |
| 6479 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 6480 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 6481 | alloc_hint, &ins, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6482 | if (ret) { |
| 6483 | em = ERR_PTR(ret); |
| 6484 | goto out; |
| 6485 | } |
| 6486 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6487 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6488 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6489 | if (IS_ERR(em)) |
| 6490 | goto out; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6491 | |
| 6492 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6493 | ins.offset, ins.offset, 0); |
| 6494 | if (ret) { |
| 6495 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 6496 | em = ERR_PTR(ret); |
| 6497 | } |
| 6498 | out: |
| 6499 | btrfs_end_transaction(trans, root); |
| 6500 | return em; |
| 6501 | } |
| 6502 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6503 | /* |
| 6504 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6505 | * block must be cow'd |
| 6506 | */ |
| 6507 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 6508 | struct inode *inode, u64 offset, u64 len) |
| 6509 | { |
| 6510 | struct btrfs_path *path; |
| 6511 | int ret; |
| 6512 | struct extent_buffer *leaf; |
| 6513 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6514 | struct btrfs_file_extent_item *fi; |
| 6515 | struct btrfs_key key; |
| 6516 | u64 disk_bytenr; |
| 6517 | u64 backref_offset; |
| 6518 | u64 extent_end; |
| 6519 | u64 num_bytes; |
| 6520 | int slot; |
| 6521 | int found_type; |
| 6522 | |
| 6523 | path = btrfs_alloc_path(); |
| 6524 | if (!path) |
| 6525 | return -ENOMEM; |
| 6526 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6527 | ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6528 | offset, 0); |
| 6529 | if (ret < 0) |
| 6530 | goto out; |
| 6531 | |
| 6532 | slot = path->slots[0]; |
| 6533 | if (ret == 1) { |
| 6534 | if (slot == 0) { |
| 6535 | /* can't find the item, must cow */ |
| 6536 | ret = 0; |
| 6537 | goto out; |
| 6538 | } |
| 6539 | slot--; |
| 6540 | } |
| 6541 | ret = 0; |
| 6542 | leaf = path->nodes[0]; |
| 6543 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6544 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6545 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6546 | /* not our file or wrong item type, must cow */ |
| 6547 | goto out; |
| 6548 | } |
| 6549 | |
| 6550 | if (key.offset > offset) { |
| 6551 | /* Wrong offset, must cow */ |
| 6552 | goto out; |
| 6553 | } |
| 6554 | |
| 6555 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6556 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6557 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6558 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6559 | /* not a regular extent, must cow */ |
| 6560 | goto out; |
| 6561 | } |
| 6562 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6563 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6564 | |
| 6565 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 6566 | if (extent_end < offset + len) { |
| 6567 | /* extent doesn't include our full range, must cow */ |
| 6568 | goto out; |
| 6569 | } |
| 6570 | |
| 6571 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6572 | goto out; |
| 6573 | |
| 6574 | /* |
| 6575 | * look for other files referencing this extent, if we |
| 6576 | * find any we must cow |
| 6577 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6578 | if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6579 | key.offset - backref_offset, disk_bytenr)) |
| 6580 | goto out; |
| 6581 | |
| 6582 | /* |
| 6583 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 6584 | * in this extent we are about to write. If there |
| 6585 | * are any csums in that range we have to cow in order |
| 6586 | * to keep the csums correct |
| 6587 | */ |
| 6588 | disk_bytenr += backref_offset; |
| 6589 | disk_bytenr += offset - key.offset; |
| 6590 | num_bytes = min(offset + len, extent_end) - offset; |
| 6591 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 6592 | goto out; |
| 6593 | /* |
| 6594 | * all of the above have passed, it is safe to overwrite this extent |
| 6595 | * without cow |
| 6596 | */ |
| 6597 | ret = 1; |
| 6598 | out: |
| 6599 | btrfs_free_path(path); |
| 6600 | return ret; |
| 6601 | } |
| 6602 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6603 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 6604 | struct extent_state **cached_state, int writing) |
| 6605 | { |
| 6606 | struct btrfs_ordered_extent *ordered; |
| 6607 | int ret = 0; |
| 6608 | |
| 6609 | while (1) { |
| 6610 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6611 | 0, cached_state); |
| 6612 | /* |
| 6613 | * We're concerned with the entire range that we're going to be |
| 6614 | * doing DIO to, so we need to make sure theres no ordered |
| 6615 | * extents in this range. |
| 6616 | */ |
| 6617 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6618 | lockend - lockstart + 1); |
| 6619 | |
| 6620 | /* |
| 6621 | * We need to make sure there are no buffered pages in this |
| 6622 | * range either, we could have raced between the invalidate in |
| 6623 | * generic_file_direct_write and locking the extent. The |
| 6624 | * invalidate needs to happen so that reads after a write do not |
| 6625 | * get stale data. |
| 6626 | */ |
| 6627 | if (!ordered && (!writing || |
| 6628 | !test_range_bit(&BTRFS_I(inode)->io_tree, |
| 6629 | lockstart, lockend, EXTENT_UPTODATE, 0, |
| 6630 | *cached_state))) |
| 6631 | break; |
| 6632 | |
| 6633 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6634 | cached_state, GFP_NOFS); |
| 6635 | |
| 6636 | if (ordered) { |
| 6637 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6638 | btrfs_put_ordered_extent(ordered); |
| 6639 | } else { |
| 6640 | /* Screw you mmap */ |
| 6641 | ret = filemap_write_and_wait_range(inode->i_mapping, |
| 6642 | lockstart, |
| 6643 | lockend); |
| 6644 | if (ret) |
| 6645 | break; |
| 6646 | |
| 6647 | /* |
| 6648 | * If we found a page that couldn't be invalidated just |
| 6649 | * fall back to buffered. |
| 6650 | */ |
| 6651 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6652 | lockstart >> PAGE_CACHE_SHIFT, |
| 6653 | lockend >> PAGE_CACHE_SHIFT); |
| 6654 | if (ret) |
| 6655 | break; |
| 6656 | } |
| 6657 | |
| 6658 | cond_resched(); |
| 6659 | } |
| 6660 | |
| 6661 | return ret; |
| 6662 | } |
| 6663 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6664 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 6665 | u64 len, u64 orig_start, |
| 6666 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6667 | u64 orig_block_len, u64 ram_bytes, |
| 6668 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6669 | { |
| 6670 | struct extent_map_tree *em_tree; |
| 6671 | struct extent_map *em; |
| 6672 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6673 | int ret; |
| 6674 | |
| 6675 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 6676 | em = alloc_extent_map(); |
| 6677 | if (!em) |
| 6678 | return ERR_PTR(-ENOMEM); |
| 6679 | |
| 6680 | em->start = start; |
| 6681 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 6682 | em->mod_start = start; |
| 6683 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6684 | em->len = len; |
| 6685 | em->block_len = block_len; |
| 6686 | em->block_start = block_start; |
| 6687 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6688 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6689 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6690 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6691 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6692 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 6693 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6694 | |
| 6695 | do { |
| 6696 | btrfs_drop_extent_cache(inode, em->start, |
| 6697 | em->start + em->len - 1, 0); |
| 6698 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6699 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6700 | write_unlock(&em_tree->lock); |
| 6701 | } while (ret == -EEXIST); |
| 6702 | |
| 6703 | if (ret) { |
| 6704 | free_extent_map(em); |
| 6705 | return ERR_PTR(ret); |
| 6706 | } |
| 6707 | |
| 6708 | return em; |
| 6709 | } |
| 6710 | |
| 6711 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6712 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 6713 | struct buffer_head *bh_result, int create) |
| 6714 | { |
| 6715 | struct extent_map *em; |
| 6716 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6717 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6718 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6719 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6720 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6721 | struct btrfs_trans_handle *trans; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6722 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6723 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6724 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6725 | if (create) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6726 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6727 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6728 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6729 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6730 | lockstart = start; |
| 6731 | lockend = start + len - 1; |
| 6732 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6733 | /* |
| 6734 | * If this errors out it's because we couldn't invalidate pagecache for |
| 6735 | * this range and we need to fallback to buffered. |
| 6736 | */ |
| 6737 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 6738 | return -ENOTBLK; |
| 6739 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6740 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6741 | if (IS_ERR(em)) { |
| 6742 | ret = PTR_ERR(em); |
| 6743 | goto unlock_err; |
| 6744 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6745 | |
| 6746 | /* |
| 6747 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 6748 | * io. INLINE is special, and we could probably kludge it in here, but |
| 6749 | * it's still buffered so for safety lets just fall back to the generic |
| 6750 | * buffered path. |
| 6751 | * |
| 6752 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 6753 | * decompress it, so there will be buffering required no matter what we |
| 6754 | * do, so go ahead and fallback to buffered. |
| 6755 | * |
| 6756 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 6757 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 6758 | * the generic code. |
| 6759 | */ |
| 6760 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 6761 | em->block_start == EXTENT_MAP_INLINE) { |
| 6762 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6763 | ret = -ENOTBLK; |
| 6764 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6765 | } |
| 6766 | |
| 6767 | /* Just a good old fashioned hole, return */ |
| 6768 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 6769 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 6770 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6771 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6772 | } |
| 6773 | |
| 6774 | /* |
| 6775 | * We don't allocate a new extent in the following cases |
| 6776 | * |
| 6777 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 6778 | * existing extent. |
| 6779 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 6780 | * just use the extent. |
| 6781 | * |
| 6782 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6783 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6784 | len = min(len, em->len - (start - em->start)); |
| 6785 | lockstart = start + len; |
| 6786 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6787 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6788 | |
| 6789 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 6790 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 6791 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6792 | int type; |
| 6793 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6794 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6795 | |
| 6796 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6797 | type = BTRFS_ORDERED_PREALLOC; |
| 6798 | else |
| 6799 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6800 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6801 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6802 | |
| 6803 | /* |
| 6804 | * we're not going to log anything, but we do need |
| 6805 | * to make sure the current transaction stays open |
| 6806 | * while we look for nocow cross refs |
| 6807 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6808 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6809 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6810 | goto must_cow; |
| 6811 | |
| 6812 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6813 | u64 orig_start = em->orig_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6814 | u64 orig_block_len = em->orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6815 | u64 ram_bytes = em->ram_bytes; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6816 | |
| 6817 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 6818 | free_extent_map(em); |
| 6819 | em = create_pinned_em(inode, start, len, |
| 6820 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6821 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6822 | orig_block_len, |
| 6823 | ram_bytes, type); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6824 | if (IS_ERR(em)) { |
| 6825 | btrfs_end_transaction(trans, root); |
| 6826 | goto unlock_err; |
| 6827 | } |
| 6828 | } |
| 6829 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6830 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 6831 | block_start, len, len, type); |
| 6832 | btrfs_end_transaction(trans, root); |
| 6833 | if (ret) { |
| 6834 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6835 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6836 | } |
| 6837 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6838 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6839 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6840 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6841 | must_cow: |
| 6842 | /* |
| 6843 | * this will cow the extent, reset the len in case we changed |
| 6844 | * it above |
| 6845 | */ |
| 6846 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6847 | free_extent_map(em); |
| 6848 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6849 | if (IS_ERR(em)) { |
| 6850 | ret = PTR_ERR(em); |
| 6851 | goto unlock_err; |
| 6852 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6853 | len = min(len, em->len - (start - em->start)); |
| 6854 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6855 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 6856 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6857 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6858 | bh_result->b_bdev = em->bdev; |
| 6859 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6860 | if (create) { |
| 6861 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6862 | set_buffer_new(bh_result); |
| 6863 | |
| 6864 | /* |
| 6865 | * Need to update the i_size under the extent lock so buffered |
| 6866 | * readers will get the updated i_size when we unlock. |
| 6867 | */ |
| 6868 | if (start + len > i_size_read(inode)) |
| 6869 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6870 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6871 | spin_lock(&BTRFS_I(inode)->lock); |
| 6872 | BTRFS_I(inode)->outstanding_extents++; |
| 6873 | spin_unlock(&BTRFS_I(inode)->lock); |
| 6874 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6875 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6876 | lockstart + len - 1, EXTENT_DELALLOC, NULL, |
| 6877 | &cached_state, GFP_NOFS); |
| 6878 | BUG_ON(ret); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6879 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6880 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6881 | /* |
| 6882 | * In the case of write we need to clear and unlock the entire range, |
| 6883 | * in the case of read we need to unlock only the end area that we |
| 6884 | * aren't using if there is any left over space. |
| 6885 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6886 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6887 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6888 | lockend, unlock_bits, 1, 0, |
| 6889 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6890 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6891 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6892 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6893 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6894 | free_extent_map(em); |
| 6895 | |
| 6896 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6897 | |
| 6898 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6899 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6900 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 6901 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | struct btrfs_dio_private { |
| 6905 | struct inode *inode; |
| 6906 | u64 logical_offset; |
| 6907 | u64 disk_bytenr; |
| 6908 | u64 bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6909 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6910 | |
| 6911 | /* number of bios pending for this dio */ |
| 6912 | atomic_t pending_bios; |
| 6913 | |
| 6914 | /* IO errors */ |
| 6915 | int errors; |
| 6916 | |
| 6917 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6918 | }; |
| 6919 | |
| 6920 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 6921 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6922 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6923 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 6924 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6925 | struct inode *inode = dip->inode; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6926 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6927 | u64 start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6928 | |
| 6929 | start = dip->logical_offset; |
| 6930 | do { |
| 6931 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 6932 | struct page *page = bvec->bv_page; |
| 6933 | char *kaddr; |
| 6934 | u32 csum = ~(u32)0; |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6935 | u64 private = ~(u32)0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6936 | unsigned long flags; |
| 6937 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6938 | if (get_state_private(&BTRFS_I(inode)->io_tree, |
| 6939 | start, &private)) |
| 6940 | goto failed; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6941 | local_irq_save(flags); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6942 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 6943 | csum = btrfs_csum_data(kaddr + bvec->bv_offset, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6944 | csum, bvec->bv_len); |
| 6945 | btrfs_csum_final(csum, (char *)&csum); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6946 | kunmap_atomic(kaddr); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6947 | local_irq_restore(flags); |
| 6948 | |
| 6949 | flush_dcache_page(bvec->bv_page); |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6950 | if (csum != private) { |
| 6951 | failed: |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6952 | btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u", |
| 6953 | (unsigned long long)btrfs_ino(inode), |
| 6954 | (unsigned long long)start, |
| 6955 | csum, (unsigned)private); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6956 | err = -EIO; |
| 6957 | } |
| 6958 | } |
| 6959 | |
| 6960 | start += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6961 | bvec++; |
| 6962 | } while (bvec <= bvec_end); |
| 6963 | |
| 6964 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6965 | dip->logical_offset + dip->bytes - 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6966 | bio->bi_private = dip->private; |
| 6967 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6968 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6969 | |
| 6970 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 6971 | if (err) |
| 6972 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6973 | dio_end_io(bio, err); |
| 6974 | } |
| 6975 | |
| 6976 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 6977 | { |
| 6978 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6979 | struct inode *inode = dip->inode; |
| 6980 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6981 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6982 | u64 ordered_offset = dip->logical_offset; |
| 6983 | u64 ordered_bytes = dip->bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6984 | int ret; |
| 6985 | |
| 6986 | if (err) |
| 6987 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6988 | again: |
| 6989 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 6990 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6991 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6992 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6993 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6994 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6995 | ordered->work.func = finish_ordered_fn; |
| 6996 | ordered->work.flags = 0; |
| 6997 | btrfs_queue_worker(&root->fs_info->endio_write_workers, |
| 6998 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6999 | out_test: |
| 7000 | /* |
| 7001 | * our bio might span multiple ordered extents. If we haven't |
| 7002 | * completed the accounting for the whole dio, go back and try again |
| 7003 | */ |
| 7004 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 7005 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 7006 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7007 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7008 | goto again; |
| 7009 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7010 | out_done: |
| 7011 | bio->bi_private = dip->private; |
| 7012 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7013 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7014 | |
| 7015 | /* If we had an error make sure to clear the uptodate flag */ |
| 7016 | if (err) |
| 7017 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7018 | dio_end_io(bio, err); |
| 7019 | } |
| 7020 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7021 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 7022 | struct bio *bio, int mirror_num, |
| 7023 | unsigned long bio_flags, u64 offset) |
| 7024 | { |
| 7025 | int ret; |
| 7026 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7027 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7028 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7029 | return 0; |
| 7030 | } |
| 7031 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7032 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 7033 | { |
| 7034 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7035 | |
| 7036 | if (err) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7037 | printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 7038 | "sector %#Lx len %u err no %d\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7039 | (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw, |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 7040 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7041 | dip->errors = 1; |
| 7042 | |
| 7043 | /* |
| 7044 | * before atomic variable goto zero, we must make sure |
| 7045 | * dip->errors is perceived to be set. |
| 7046 | */ |
| 7047 | smp_mb__before_atomic_dec(); |
| 7048 | } |
| 7049 | |
| 7050 | /* if there are more bios still pending for this dio, just exit */ |
| 7051 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 7052 | goto out; |
| 7053 | |
| 7054 | if (dip->errors) |
| 7055 | bio_io_error(dip->orig_bio); |
| 7056 | else { |
| 7057 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 7058 | bio_endio(dip->orig_bio, 0); |
| 7059 | } |
| 7060 | out: |
| 7061 | bio_put(bio); |
| 7062 | } |
| 7063 | |
| 7064 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 7065 | u64 first_sector, gfp_t gfp_flags) |
| 7066 | { |
| 7067 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 7068 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 7069 | } |
| 7070 | |
| 7071 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 7072 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7073 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7074 | { |
| 7075 | int write = rw & REQ_WRITE; |
| 7076 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7077 | int ret; |
| 7078 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7079 | if (async_submit) |
| 7080 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7081 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7082 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7083 | |
| 7084 | if (!write) { |
| 7085 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 7086 | if (ret) |
| 7087 | goto err; |
| 7088 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7089 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7090 | if (skip_sum) |
| 7091 | goto map; |
| 7092 | |
| 7093 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7094 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 7095 | inode, rw, bio, 0, 0, |
| 7096 | file_offset, |
| 7097 | __btrfs_submit_bio_start_direct_io, |
| 7098 | __btrfs_submit_bio_done); |
| 7099 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7100 | } else if (write) { |
| 7101 | /* |
| 7102 | * If we aren't doing async submit, calculate the csum of the |
| 7103 | * bio now. |
| 7104 | */ |
| 7105 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 7106 | if (ret) |
| 7107 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7108 | } else if (!skip_sum) { |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7109 | ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7110 | if (ret) |
| 7111 | goto err; |
| 7112 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7113 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7114 | map: |
| 7115 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7116 | err: |
| 7117 | bio_put(bio); |
| 7118 | return ret; |
| 7119 | } |
| 7120 | |
| 7121 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 7122 | int skip_sum) |
| 7123 | { |
| 7124 | struct inode *inode = dip->inode; |
| 7125 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7126 | struct bio *bio; |
| 7127 | struct bio *orig_bio = dip->orig_bio; |
| 7128 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 7129 | u64 start_sector = orig_bio->bi_sector; |
| 7130 | u64 file_offset = dip->logical_offset; |
| 7131 | u64 submit_len = 0; |
| 7132 | u64 map_length; |
| 7133 | int nr_pages = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7134 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7135 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7136 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7137 | map_length = orig_bio->bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7138 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7139 | &map_length, NULL, 0); |
| 7140 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 7141 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7142 | return -EIO; |
| 7143 | } |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7144 | if (map_length >= orig_bio->bi_size) { |
| 7145 | bio = orig_bio; |
| 7146 | goto submit; |
| 7147 | } |
| 7148 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7149 | /* async crcs make it difficult to collect full stripe writes. */ |
| 7150 | if (btrfs_get_alloc_profile(root, 1) & |
| 7151 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) |
| 7152 | async_submit = 0; |
| 7153 | else |
| 7154 | async_submit = 1; |
| 7155 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7156 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7157 | if (!bio) |
| 7158 | return -ENOMEM; |
| 7159 | bio->bi_private = dip; |
| 7160 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7161 | atomic_inc(&dip->pending_bios); |
| 7162 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7163 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 7164 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 7165 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 7166 | bvec->bv_offset) < bvec->bv_len)) { |
| 7167 | /* |
| 7168 | * inc the count before we submit the bio so |
| 7169 | * we know the end IO handler won't happen before |
| 7170 | * we inc the count. Otherwise, the dip might get freed |
| 7171 | * before we're done setting it up |
| 7172 | */ |
| 7173 | atomic_inc(&dip->pending_bios); |
| 7174 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7175 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7176 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7177 | if (ret) { |
| 7178 | bio_put(bio); |
| 7179 | atomic_dec(&dip->pending_bios); |
| 7180 | goto out_err; |
| 7181 | } |
| 7182 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7183 | start_sector += submit_len >> 9; |
| 7184 | file_offset += submit_len; |
| 7185 | |
| 7186 | submit_len = 0; |
| 7187 | nr_pages = 0; |
| 7188 | |
| 7189 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7190 | start_sector, GFP_NOFS); |
| 7191 | if (!bio) |
| 7192 | goto out_err; |
| 7193 | bio->bi_private = dip; |
| 7194 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7195 | |
| 7196 | map_length = orig_bio->bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7197 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7198 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7199 | &map_length, NULL, 0); |
| 7200 | if (ret) { |
| 7201 | bio_put(bio); |
| 7202 | goto out_err; |
| 7203 | } |
| 7204 | } else { |
| 7205 | submit_len += bvec->bv_len; |
| 7206 | nr_pages ++; |
| 7207 | bvec++; |
| 7208 | } |
| 7209 | } |
| 7210 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7211 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7212 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7213 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7214 | if (!ret) |
| 7215 | return 0; |
| 7216 | |
| 7217 | bio_put(bio); |
| 7218 | out_err: |
| 7219 | dip->errors = 1; |
| 7220 | /* |
| 7221 | * before atomic variable goto zero, we must |
| 7222 | * make sure dip->errors is perceived to be set. |
| 7223 | */ |
| 7224 | smp_mb__before_atomic_dec(); |
| 7225 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 7226 | bio_io_error(dip->orig_bio); |
| 7227 | |
| 7228 | /* bio_end_io() will handle error, so we needn't return it */ |
| 7229 | return 0; |
| 7230 | } |
| 7231 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7232 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 7233 | loff_t file_offset) |
| 7234 | { |
| 7235 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7236 | struct btrfs_dio_private *dip; |
| 7237 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7238 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 7239 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7240 | int ret = 0; |
| 7241 | |
| 7242 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7243 | |
| 7244 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 7245 | if (!dip) { |
| 7246 | ret = -ENOMEM; |
| 7247 | goto free_ordered; |
| 7248 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7249 | |
| 7250 | dip->private = bio->bi_private; |
| 7251 | dip->inode = inode; |
| 7252 | dip->logical_offset = file_offset; |
| 7253 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7254 | dip->bytes = 0; |
| 7255 | do { |
| 7256 | dip->bytes += bvec->bv_len; |
| 7257 | bvec++; |
| 7258 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 7259 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7260 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7261 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7262 | dip->errors = 0; |
| 7263 | dip->orig_bio = bio; |
| 7264 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7265 | |
| 7266 | if (write) |
| 7267 | bio->bi_end_io = btrfs_endio_direct_write; |
| 7268 | else |
| 7269 | bio->bi_end_io = btrfs_endio_direct_read; |
| 7270 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7271 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 7272 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7273 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7274 | free_ordered: |
| 7275 | /* |
| 7276 | * If this is a write, we need to clean up the reserved space and kill |
| 7277 | * the ordered extent. |
| 7278 | */ |
| 7279 | if (write) { |
| 7280 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 7281 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7282 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 7283 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 7284 | btrfs_free_reserved_extent(root, ordered->start, |
| 7285 | ordered->disk_len); |
| 7286 | btrfs_put_ordered_extent(ordered); |
| 7287 | btrfs_put_ordered_extent(ordered); |
| 7288 | } |
| 7289 | bio_endio(bio, ret); |
| 7290 | } |
| 7291 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7292 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 7293 | const struct iovec *iov, loff_t offset, |
| 7294 | unsigned long nr_segs) |
| 7295 | { |
| 7296 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7297 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7298 | size_t size; |
| 7299 | unsigned long addr; |
| 7300 | unsigned blocksize_mask = root->sectorsize - 1; |
| 7301 | ssize_t retval = -EINVAL; |
| 7302 | loff_t end = offset; |
| 7303 | |
| 7304 | if (offset & blocksize_mask) |
| 7305 | goto out; |
| 7306 | |
| 7307 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 7308 | for (seg = 0; seg < nr_segs; seg++) { |
| 7309 | addr = (unsigned long)iov[seg].iov_base; |
| 7310 | size = iov[seg].iov_len; |
| 7311 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7312 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7313 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7314 | |
| 7315 | /* If this is a write we don't need to check anymore */ |
| 7316 | if (rw & WRITE) |
| 7317 | continue; |
| 7318 | |
| 7319 | /* |
| 7320 | * Check to make sure we don't have duplicate iov_base's in this |
| 7321 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 7322 | * when reading back. |
| 7323 | */ |
| 7324 | for (i = seg + 1; i < nr_segs; i++) { |
| 7325 | if (iov[seg].iov_base == iov[i].iov_base) |
| 7326 | goto out; |
| 7327 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7328 | } |
| 7329 | retval = 0; |
| 7330 | out: |
| 7331 | return retval; |
| 7332 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7333 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7334 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 7335 | const struct iovec *iov, loff_t offset, |
| 7336 | unsigned long nr_segs) |
| 7337 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7338 | struct file *file = iocb->ki_filp; |
| 7339 | struct inode *inode = file->f_mapping->host; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7340 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7341 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7342 | bool wakeup = true; |
| 7343 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7344 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7345 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7346 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7347 | offset, nr_segs)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7348 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7349 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7350 | atomic_inc(&inode->i_dio_count); |
| 7351 | smp_mb__after_atomic_inc(); |
| 7352 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7353 | if (rw & WRITE) { |
| 7354 | count = iov_length(iov, nr_segs); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7355 | /* |
| 7356 | * If the write DIO is beyond the EOF, we need update |
| 7357 | * the isize, but it is protected by i_mutex. So we can |
| 7358 | * not unlock the i_mutex at this case. |
| 7359 | */ |
| 7360 | if (offset + count <= inode->i_size) { |
| 7361 | mutex_unlock(&inode->i_mutex); |
| 7362 | relock = true; |
| 7363 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7364 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 7365 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7366 | goto out; |
| 7367 | } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 7368 | &BTRFS_I(inode)->runtime_flags))) { |
| 7369 | inode_dio_done(inode); |
| 7370 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 7371 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7372 | } |
| 7373 | |
| 7374 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 7375 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 7376 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7377 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7378 | if (rw & WRITE) { |
| 7379 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 7380 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7381 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7382 | btrfs_delalloc_release_space(inode, |
| 7383 | count - (size_t)ret); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7384 | else |
| 7385 | btrfs_delalloc_release_metadata(inode, 0); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7386 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7387 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7388 | if (wakeup) |
| 7389 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7390 | if (relock) |
| 7391 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7392 | |
| 7393 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7394 | } |
| 7395 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7396 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 7397 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7398 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 7399 | __u64 start, __u64 len) |
| 7400 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7401 | int ret; |
| 7402 | |
| 7403 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 7404 | if (ret) |
| 7405 | return ret; |
| 7406 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7407 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7408 | } |
| 7409 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7410 | int btrfs_readpage(struct file *file, struct page *page) |
| 7411 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7412 | struct extent_io_tree *tree; |
| 7413 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 7414 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7415 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7416 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7417 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 7418 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7419 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7420 | |
| 7421 | |
| 7422 | if (current->flags & PF_MEMALLOC) { |
| 7423 | redirty_page_for_writepage(wbc, page); |
| 7424 | unlock_page(page); |
| 7425 | return 0; |
| 7426 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7427 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7428 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 7429 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7430 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 7431 | int btrfs_writepages(struct address_space *mapping, |
| 7432 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7433 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7434 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7435 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7436 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7437 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 7438 | } |
| 7439 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7440 | static int |
| 7441 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 7442 | struct list_head *pages, unsigned nr_pages) |
| 7443 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7444 | struct extent_io_tree *tree; |
| 7445 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7446 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 7447 | btrfs_get_extent); |
| 7448 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7449 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7450 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7451 | struct extent_io_tree *tree; |
| 7452 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7453 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7454 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7455 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 7456 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7457 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7458 | if (ret == 1) { |
| 7459 | ClearPagePrivate(page); |
| 7460 | set_page_private(page, 0); |
| 7461 | page_cache_release(page); |
| 7462 | } |
| 7463 | return ret; |
| 7464 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7465 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7466 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 7467 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 7468 | if (PageWriteback(page) || PageDirty(page)) |
| 7469 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 7470 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7471 | } |
| 7472 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7473 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 7474 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7475 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7476 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7477 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7478 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7479 | u64 page_start = page_offset(page); |
| 7480 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7481 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7482 | /* |
| 7483 | * we have the page locked, so new writeback can't start, |
| 7484 | * and the dirty bit won't be cleared while we are here. |
| 7485 | * |
| 7486 | * Wait for IO on this page so that we can safely clear |
| 7487 | * the PagePrivate2 bit and do ordered accounting |
| 7488 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7489 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7490 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7491 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7492 | if (offset) { |
| 7493 | btrfs_releasepage(page, GFP_NOFS); |
| 7494 | return; |
| 7495 | } |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7496 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 7497 | ordered = btrfs_lookup_ordered_extent(inode, page_offset(page)); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7498 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7499 | /* |
| 7500 | * IO on this page will never be started, so we need |
| 7501 | * to account for any ordered extents now |
| 7502 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7503 | clear_extent_bit(tree, page_start, page_end, |
| 7504 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7505 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 7506 | EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7507 | /* |
| 7508 | * whoever cleared the private bit is responsible |
| 7509 | * for the finish_ordered_io |
| 7510 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7511 | if (TestClearPagePrivate2(page) && |
| 7512 | btrfs_dec_test_ordered_pending(inode, &ordered, page_start, |
| 7513 | PAGE_CACHE_SIZE, 1)) { |
| 7514 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7515 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7516 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7517 | cached_state = NULL; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7518 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7519 | } |
| 7520 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 7521 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7522 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 7523 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7524 | __btrfs_releasepage(page, GFP_NOFS); |
| 7525 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7526 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7527 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7528 | ClearPagePrivate(page); |
| 7529 | set_page_private(page, 0); |
| 7530 | page_cache_release(page); |
| 7531 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7532 | } |
| 7533 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7534 | /* |
| 7535 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 7536 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 7537 | * be careful to check for EOF conditions here. We set the page up correctly |
| 7538 | * for a written page which means we get ENOSPC checking when writing into |
| 7539 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 7540 | * support these features. |
| 7541 | * |
| 7542 | * We are not allowed to take the i_mutex here so we have to play games to |
| 7543 | * protect against truncate races as the page could now be beyond EOF. Because |
| 7544 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 7545 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 7546 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 7547 | * unlock the page. |
| 7548 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7549 | 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] | 7550 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7551 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 7552 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7553 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7554 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7555 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7556 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7557 | char *kaddr; |
| 7558 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7559 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7560 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7561 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7562 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7563 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7564 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7565 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7566 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7567 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 7568 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7569 | reserved = 1; |
| 7570 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7571 | if (ret) { |
| 7572 | if (ret == -ENOMEM) |
| 7573 | ret = VM_FAULT_OOM; |
| 7574 | else /* -ENOSPC, -EIO, etc */ |
| 7575 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7576 | if (reserved) |
| 7577 | goto out; |
| 7578 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7579 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7580 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7581 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7582 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7583 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7584 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7585 | page_start = page_offset(page); |
| 7586 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7587 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7588 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7589 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7590 | /* page got truncated out from underneath us */ |
| 7591 | goto out_unlock; |
| 7592 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7593 | wait_on_page_writeback(page); |
| 7594 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7595 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7596 | set_page_extent_mapped(page); |
| 7597 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7598 | /* |
| 7599 | * we can't set the delalloc bits if there are pending ordered |
| 7600 | * extents. Drop our locks and wait for them to finish |
| 7601 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7602 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 7603 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7604 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7605 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7606 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7607 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7608 | btrfs_put_ordered_extent(ordered); |
| 7609 | goto again; |
| 7610 | } |
| 7611 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7612 | /* |
| 7613 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 7614 | * if it was already dirty, so for space accounting reasons we need to |
| 7615 | * clear any delalloc bits for the range we are fixing to save. There |
| 7616 | * is probably a better way to do this, but for now keep consistent with |
| 7617 | * prepare_pages in the normal write path. |
| 7618 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7619 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7620 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7621 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7622 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7623 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7624 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 7625 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7626 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7627 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7628 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7629 | ret = VM_FAULT_SIGBUS; |
| 7630 | goto out_unlock; |
| 7631 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7632 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7633 | |
| 7634 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7635 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7636 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7637 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7638 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7639 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7640 | if (zero_start != PAGE_CACHE_SIZE) { |
| 7641 | kaddr = kmap(page); |
| 7642 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 7643 | flush_dcache_page(page); |
| 7644 | kunmap(page); |
| 7645 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7646 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7647 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7648 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7649 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7650 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 7651 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7652 | 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] | 7653 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7654 | 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] | 7655 | |
| 7656 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7657 | if (!ret) { |
| 7658 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7659 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7660 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7661 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7662 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7663 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7664 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7665 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7666 | return ret; |
| 7667 | } |
| 7668 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7669 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7670 | { |
| 7671 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7672 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7673 | int ret; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7674 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7675 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7676 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7677 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7678 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 7679 | ret = btrfs_truncate_page(inode, inode->i_size, 0, 0); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 7680 | if (ret) |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7681 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7682 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7683 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7684 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7685 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7686 | /* |
| 7687 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 7688 | * 3 things going on here |
| 7689 | * |
| 7690 | * 1) We need to reserve space for our orphan item and the space to |
| 7691 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 7692 | * orphan item because we didn't reserve space to remove it. |
| 7693 | * |
| 7694 | * 2) We need to reserve space to update our inode. |
| 7695 | * |
| 7696 | * 3) We need to have something to cache all the space that is going to |
| 7697 | * be free'd up by the truncate operation, but also have some slack |
| 7698 | * space reserved in case it uses space during the truncate (thank you |
| 7699 | * very much snapshotting). |
| 7700 | * |
| 7701 | * And we need these to all be seperate. The fact is we can use alot of |
| 7702 | * space doing the truncate, and we have no earthly idea how much space |
| 7703 | * we will use, so we need the truncate reservation to be seperate so it |
| 7704 | * doesn't end up using space reserved for updating the inode or |
| 7705 | * removing the orphan item. We also need to be able to stop the |
| 7706 | * transaction and start a new one, which means we need to be able to |
| 7707 | * update the inode several times, and we have no idea of knowing how |
| 7708 | * many times that will be, so we can't just reserve 1 item for the |
| 7709 | * entirety of the opration, so that has to be done seperately as well. |
| 7710 | * Then there is the orphan item, which does indeed need to be held on |
| 7711 | * to for the whole operation, and we need nobody to touch this reserved |
| 7712 | * space except the orphan code. |
| 7713 | * |
| 7714 | * So that leaves us with |
| 7715 | * |
| 7716 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 7717 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 7718 | * transaction reservation. |
| 7719 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 7720 | * updating the inode. |
| 7721 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 7722 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7723 | if (!rsv) |
| 7724 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 7725 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7726 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7727 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7728 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7729 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7730 | * 1 for updating the inode. |
| 7731 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 7732 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7733 | if (IS_ERR(trans)) { |
| 7734 | err = PTR_ERR(trans); |
| 7735 | goto out; |
| 7736 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7737 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7738 | /* Migrate the slack space for the truncate to our reserve */ |
| 7739 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 7740 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7741 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7742 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7743 | /* |
| 7744 | * setattr is responsible for setting the ordered_data_close flag, |
| 7745 | * but that is only tested during the last file release. That |
| 7746 | * could happen well after the next commit, leaving a great big |
| 7747 | * window where new writes may get lost if someone chooses to write |
| 7748 | * to this file after truncating to zero |
| 7749 | * |
| 7750 | * The inode doesn't have any dirty data here, and so if we commit |
| 7751 | * this is a noop. If someone immediately starts writing to the inode |
| 7752 | * it is very likely we'll catch some of their writes in this |
| 7753 | * transaction, and the commit will find this file on the ordered |
| 7754 | * data list with good things to send down. |
| 7755 | * |
| 7756 | * This is a best effort solution, there is still a window where |
| 7757 | * using truncate to replace the contents of the file will |
| 7758 | * end up with a zero length file after a crash. |
| 7759 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7760 | if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 7761 | &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7762 | btrfs_add_ordered_operation(trans, root, inode); |
| 7763 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7764 | /* |
| 7765 | * So if we truncate and then write and fsync we normally would just |
| 7766 | * write the extents that changed, which is a problem if we need to |
| 7767 | * first truncate that entire inode. So set this flag so we write out |
| 7768 | * all of the extents in the inode to the sync log so we're completely |
| 7769 | * safe. |
| 7770 | */ |
| 7771 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7772 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7773 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7774 | while (1) { |
| 7775 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 7776 | inode->i_size, |
| 7777 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7778 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7779 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7780 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7781 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7782 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7783 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7784 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7785 | if (ret) { |
| 7786 | err = ret; |
| 7787 | break; |
| 7788 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7789 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7790 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7791 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7792 | |
| 7793 | trans = btrfs_start_transaction(root, 2); |
| 7794 | if (IS_ERR(trans)) { |
| 7795 | ret = err = PTR_ERR(trans); |
| 7796 | trans = NULL; |
| 7797 | break; |
| 7798 | } |
| 7799 | |
| 7800 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 7801 | rsv, min_size); |
| 7802 | BUG_ON(ret); /* shouldn't happen */ |
| 7803 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7804 | } |
| 7805 | |
| 7806 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7807 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7808 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7809 | if (ret) |
| 7810 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7811 | } |
| 7812 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7813 | if (trans) { |
| 7814 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 7815 | ret = btrfs_update_inode(trans, root, inode); |
| 7816 | if (ret && !err) |
| 7817 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7818 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7819 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7820 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7821 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7822 | |
| 7823 | out: |
| 7824 | btrfs_free_block_rsv(root, rsv); |
| 7825 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7826 | if (ret && !err) |
| 7827 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7828 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7829 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7830 | } |
| 7831 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7832 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7833 | * create a new subvolume directory/inode (helper for the ioctl). |
| 7834 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7835 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7836 | struct btrfs_root *new_root, u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7837 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7838 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7839 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7840 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7841 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 7842 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 7843 | new_dirid, new_dirid, |
| 7844 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 7845 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7846 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 7847 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7848 | inode->i_op = &btrfs_dir_inode_operations; |
| 7849 | inode->i_fop = &btrfs_dir_file_operations; |
| 7850 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 7851 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7852 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7853 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7854 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 7855 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7856 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 7857 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7858 | } |
| 7859 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7860 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 7861 | { |
| 7862 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7863 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7864 | |
| 7865 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 7866 | if (!ei) |
| 7867 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7868 | |
| 7869 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7870 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 7871 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7872 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7873 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7874 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7875 | ei->disk_i_size = 0; |
| 7876 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7877 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7878 | ei->index_cnt = (u64)-1; |
| 7879 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7880 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7881 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7882 | spin_lock_init(&ei->lock); |
| 7883 | ei->outstanding_extents = 0; |
| 7884 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7885 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7886 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7887 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7888 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7889 | ei->delayed_node = NULL; |
| 7890 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7891 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 7892 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 7893 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 7894 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 7895 | ei->io_tree.track_uptodate = 1; |
| 7896 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7897 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7898 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 7899 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7900 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7901 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7902 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7903 | RB_CLEAR_NODE(&ei->rb_node); |
| 7904 | |
| 7905 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7906 | } |
| 7907 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7908 | static void btrfs_i_callback(struct rcu_head *head) |
| 7909 | { |
| 7910 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7911 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 7912 | } |
| 7913 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7914 | void btrfs_destroy_inode(struct inode *inode) |
| 7915 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7916 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7917 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7918 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 7919 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7920 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7921 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 7922 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7923 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 7924 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7925 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7926 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7927 | * This can happen where we create an inode, but somebody else also |
| 7928 | * created the same inode and we need to destroy the one we already |
| 7929 | * created. |
| 7930 | */ |
| 7931 | if (!root) |
| 7932 | goto free; |
| 7933 | |
| 7934 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7935 | * Make sure we're properly removed from the ordered operation |
| 7936 | * lists. |
| 7937 | */ |
| 7938 | smp_mb(); |
| 7939 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 7940 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 7941 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 7942 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 7943 | } |
| 7944 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7945 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 7946 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7947 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
| 7948 | (unsigned long long)btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7949 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7950 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7951 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7952 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7953 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 7954 | if (!ordered) |
| 7955 | break; |
| 7956 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7957 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
| 7958 | (unsigned long long)ordered->file_offset, |
| 7959 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7960 | btrfs_remove_ordered_extent(inode, ordered); |
| 7961 | btrfs_put_ordered_extent(ordered); |
| 7962 | btrfs_put_ordered_extent(ordered); |
| 7963 | } |
| 7964 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7965 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7966 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7967 | free: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7968 | btrfs_remove_delayed_node(inode); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7969 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7970 | } |
| 7971 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7972 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7973 | { |
| 7974 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7975 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 7976 | /* the snap/subvol tree is on deleting */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7977 | if (btrfs_root_refs(&root->root_item) == 0 && |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 7978 | root != root->fs_info->tree_root) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7979 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7980 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7981 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7982 | } |
| 7983 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 7984 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7985 | { |
| 7986 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 7987 | |
| 7988 | inode_init_once(&ei->vfs_inode); |
| 7989 | } |
| 7990 | |
| 7991 | void btrfs_destroy_cachep(void) |
| 7992 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 7993 | /* |
| 7994 | * Make sure all delayed rcu free inodes are flushed before we |
| 7995 | * destroy cache. |
| 7996 | */ |
| 7997 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7998 | if (btrfs_inode_cachep) |
| 7999 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8000 | if (btrfs_trans_handle_cachep) |
| 8001 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 8002 | if (btrfs_transaction_cachep) |
| 8003 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8004 | if (btrfs_path_cachep) |
| 8005 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8006 | if (btrfs_free_space_cachep) |
| 8007 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8008 | if (btrfs_delalloc_work_cachep) |
| 8009 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8010 | } |
| 8011 | |
| 8012 | int btrfs_init_cachep(void) |
| 8013 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8014 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8015 | sizeof(struct btrfs_inode), 0, |
| 8016 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8017 | if (!btrfs_inode_cachep) |
| 8018 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8019 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8020 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8021 | sizeof(struct btrfs_trans_handle), 0, |
| 8022 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8023 | if (!btrfs_trans_handle_cachep) |
| 8024 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8025 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8026 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8027 | sizeof(struct btrfs_transaction), 0, |
| 8028 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8029 | if (!btrfs_transaction_cachep) |
| 8030 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8031 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8032 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8033 | sizeof(struct btrfs_path), 0, |
| 8034 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8035 | if (!btrfs_path_cachep) |
| 8036 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8037 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8038 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8039 | sizeof(struct btrfs_free_space), 0, |
| 8040 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 8041 | if (!btrfs_free_space_cachep) |
| 8042 | goto fail; |
| 8043 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8044 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 8045 | sizeof(struct btrfs_delalloc_work), 0, |
| 8046 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 8047 | NULL); |
| 8048 | if (!btrfs_delalloc_work_cachep) |
| 8049 | goto fail; |
| 8050 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8051 | return 0; |
| 8052 | fail: |
| 8053 | btrfs_destroy_cachep(); |
| 8054 | return -ENOMEM; |
| 8055 | } |
| 8056 | |
| 8057 | static int btrfs_getattr(struct vfsmount *mnt, |
| 8058 | struct dentry *dentry, struct kstat *stat) |
| 8059 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8060 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8061 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8062 | u32 blocksize = inode->i_sb->s_blocksize; |
| 8063 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8064 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8065 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 8066 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8067 | |
| 8068 | spin_lock(&BTRFS_I(inode)->lock); |
| 8069 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 8070 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8071 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8072 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8073 | return 0; |
| 8074 | } |
| 8075 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8076 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 8077 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8078 | { |
| 8079 | struct btrfs_trans_handle *trans; |
| 8080 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8081 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8082 | struct inode *new_inode = new_dentry->d_inode; |
| 8083 | struct inode *old_inode = old_dentry->d_inode; |
| 8084 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8085 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8086 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8087 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8088 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8089 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8090 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8091 | return -EPERM; |
| 8092 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8093 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8094 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8095 | return -EXDEV; |
| 8096 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8097 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 8098 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8099 | return -ENOTEMPTY; |
| 8100 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8101 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8102 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8103 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8104 | |
| 8105 | |
| 8106 | /* check for collisions, even if the name isn't there */ |
| 8107 | ret = btrfs_check_dir_item_collision(root, new_dir->i_ino, |
| 8108 | new_dentry->d_name.name, |
| 8109 | new_dentry->d_name.len); |
| 8110 | |
| 8111 | if (ret) { |
| 8112 | if (ret == -EEXIST) { |
| 8113 | /* we shouldn't get |
| 8114 | * eexist without a new_inode */ |
| 8115 | if (!new_inode) { |
| 8116 | WARN_ON(1); |
| 8117 | return ret; |
| 8118 | } |
| 8119 | } else { |
| 8120 | /* maybe -EOVERFLOW */ |
| 8121 | return ret; |
| 8122 | } |
| 8123 | } |
| 8124 | ret = 0; |
| 8125 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8126 | /* |
| 8127 | * we're using rename to replace one file with another. |
| 8128 | * and the replacement file is large. Start IO on it now so |
| 8129 | * we don't add too much work to the end of the transaction |
| 8130 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 8131 | 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] | 8132 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 8133 | filemap_flush(old_inode->i_mapping); |
| 8134 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8135 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8136 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8137 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8138 | /* |
| 8139 | * We want to reserve the absolute worst case amount of items. So if |
| 8140 | * both inodes are subvols and we need to unlink them then that would |
| 8141 | * require 4 item modifications, but if they are both normal inodes it |
| 8142 | * would require 5 item modifications, so we'll assume their normal |
| 8143 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8144 | * should cover the worst case number of items we'll modify. |
| 8145 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 8146 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8147 | if (IS_ERR(trans)) { |
| 8148 | ret = PTR_ERR(trans); |
| 8149 | goto out_notrans; |
| 8150 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8151 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8152 | if (dest != root) |
| 8153 | btrfs_record_root_in_trans(trans, dest); |
| 8154 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8155 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8156 | if (ret) |
| 8157 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8158 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8159 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8160 | /* force full log commit if subvolume involved. */ |
| 8161 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 8162 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8163 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8164 | new_dentry->d_name.name, |
| 8165 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8166 | old_ino, |
| 8167 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8168 | if (ret) |
| 8169 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8170 | /* |
| 8171 | * this is an ugly little race, but the rename is required |
| 8172 | * to make sure that if we crash, the inode is either at the |
| 8173 | * old name or the new one. pinning the log transaction lets |
| 8174 | * us make sure we don't allow a log commit to come in after |
| 8175 | * we unlink the name but before we add the new name back in. |
| 8176 | */ |
| 8177 | btrfs_pin_log_trans(root); |
| 8178 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8179 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8180 | * make sure the inode gets flushed if it is replacing |
| 8181 | * something. |
| 8182 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8183 | 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] | 8184 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8185 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8186 | inode_inc_iversion(old_dir); |
| 8187 | inode_inc_iversion(new_dir); |
| 8188 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8189 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8190 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8191 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8192 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8193 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 8194 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 8195 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8196 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8197 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 8198 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 8199 | old_dentry->d_name.name, |
| 8200 | old_dentry->d_name.len); |
| 8201 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 8202 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 8203 | old_dentry->d_inode, |
| 8204 | old_dentry->d_name.name, |
| 8205 | old_dentry->d_name.len); |
| 8206 | if (!ret) |
| 8207 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8208 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8209 | if (ret) { |
| 8210 | btrfs_abort_transaction(trans, root, ret); |
| 8211 | goto out_fail; |
| 8212 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8213 | |
| 8214 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8215 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8216 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8217 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8218 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 8219 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 8220 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 8221 | root_objectid, |
| 8222 | new_dentry->d_name.name, |
| 8223 | new_dentry->d_name.len); |
| 8224 | BUG_ON(new_inode->i_nlink == 0); |
| 8225 | } else { |
| 8226 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 8227 | new_dentry->d_inode, |
| 8228 | new_dentry->d_name.name, |
| 8229 | new_dentry->d_name.len); |
| 8230 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8231 | if (!ret && new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8232 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8233 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8234 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8235 | if (ret) { |
| 8236 | btrfs_abort_transaction(trans, root, ret); |
| 8237 | goto out_fail; |
| 8238 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8239 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8240 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8241 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 8242 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8243 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8244 | if (ret) { |
| 8245 | btrfs_abort_transaction(trans, root, ret); |
| 8246 | goto out_fail; |
| 8247 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8248 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8249 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 8250 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 8251 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8252 | btrfs_end_log_trans(root); |
| 8253 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8254 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8255 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8256 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8257 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8258 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8259 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8260 | return ret; |
| 8261 | } |
| 8262 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8263 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 8264 | { |
| 8265 | struct btrfs_delalloc_work *delalloc_work; |
| 8266 | |
| 8267 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 8268 | work); |
| 8269 | if (delalloc_work->wait) |
| 8270 | btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1); |
| 8271 | else |
| 8272 | filemap_flush(delalloc_work->inode->i_mapping); |
| 8273 | |
| 8274 | if (delalloc_work->delay_iput) |
| 8275 | btrfs_add_delayed_iput(delalloc_work->inode); |
| 8276 | else |
| 8277 | iput(delalloc_work->inode); |
| 8278 | complete(&delalloc_work->completion); |
| 8279 | } |
| 8280 | |
| 8281 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 8282 | int wait, int delay_iput) |
| 8283 | { |
| 8284 | struct btrfs_delalloc_work *work; |
| 8285 | |
| 8286 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 8287 | if (!work) |
| 8288 | return NULL; |
| 8289 | |
| 8290 | init_completion(&work->completion); |
| 8291 | INIT_LIST_HEAD(&work->list); |
| 8292 | work->inode = inode; |
| 8293 | work->wait = wait; |
| 8294 | work->delay_iput = delay_iput; |
| 8295 | work->work.func = btrfs_run_delalloc_work; |
| 8296 | |
| 8297 | return work; |
| 8298 | } |
| 8299 | |
| 8300 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 8301 | { |
| 8302 | wait_for_completion(&work->completion); |
| 8303 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 8304 | } |
| 8305 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8306 | /* |
| 8307 | * some fairly slow code that needs optimization. This walks the list |
| 8308 | * of all the inodes with pending delalloc and forces them to disk. |
| 8309 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 8310 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8311 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8312 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8313 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8314 | struct btrfs_delalloc_work *work, *next; |
| 8315 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8316 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8317 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8318 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8319 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 8320 | return -EROFS; |
| 8321 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8322 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8323 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 8324 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 8325 | spin_lock(&root->fs_info->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8326 | list_splice_init(&root->fs_info->delalloc_inodes, &splice); |
| 8327 | while (!list_empty(&splice)) { |
| 8328 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8329 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8330 | |
| 8331 | list_del_init(&binode->delalloc_inodes); |
| 8332 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8333 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8334 | if (!inode) { |
| 8335 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 8336 | &binode->runtime_flags); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8337 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8338 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8339 | |
| 8340 | list_add_tail(&binode->delalloc_inodes, |
| 8341 | &root->fs_info->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 8342 | spin_unlock(&root->fs_info->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8343 | |
| 8344 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
| 8345 | if (unlikely(!work)) { |
| 8346 | ret = -ENOMEM; |
| 8347 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8348 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8349 | list_add_tail(&work->list, &works); |
| 8350 | btrfs_queue_worker(&root->fs_info->flush_workers, |
| 8351 | &work->work); |
| 8352 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8353 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 8354 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8355 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 8356 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8357 | |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8358 | list_for_each_entry_safe(work, next, &works, list) { |
| 8359 | list_del_init(&work->list); |
| 8360 | btrfs_wait_and_free_delalloc_work(work); |
| 8361 | } |
| 8362 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8363 | /* the filemap_flush will queue IO into the worker threads, but |
| 8364 | * we have to make sure the IO is actually started and that |
| 8365 | * ordered extents get created before we return |
| 8366 | */ |
| 8367 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8368 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8369 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8370 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8371 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 8372 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8373 | } |
| 8374 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8375 | return 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8376 | out: |
| 8377 | list_for_each_entry_safe(work, next, &works, list) { |
| 8378 | list_del_init(&work->list); |
| 8379 | btrfs_wait_and_free_delalloc_work(work); |
| 8380 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8381 | |
| 8382 | if (!list_empty_careful(&splice)) { |
| 8383 | spin_lock(&root->fs_info->delalloc_lock); |
| 8384 | list_splice_tail(&splice, &root->fs_info->delalloc_inodes); |
| 8385 | spin_unlock(&root->fs_info->delalloc_lock); |
| 8386 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8387 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8388 | } |
| 8389 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8390 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 8391 | const char *symname) |
| 8392 | { |
| 8393 | struct btrfs_trans_handle *trans; |
| 8394 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 8395 | struct btrfs_path *path; |
| 8396 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8397 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8398 | int err; |
| 8399 | int drop_inode = 0; |
| 8400 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8401 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8402 | int name_len; |
| 8403 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8404 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8405 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8406 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8407 | |
| 8408 | name_len = strlen(symname) + 1; |
| 8409 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 8410 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8411 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8412 | /* |
| 8413 | * 2 items for inode item and ref |
| 8414 | * 2 items for dir items |
| 8415 | * 1 item for xattr if selinux is on |
| 8416 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8417 | trans = btrfs_start_transaction(root, 5); |
| 8418 | if (IS_ERR(trans)) |
| 8419 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8420 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 8421 | err = btrfs_find_free_ino(root, &objectid); |
| 8422 | if (err) |
| 8423 | goto out_unlock; |
| 8424 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8425 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8426 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 8427 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8428 | if (IS_ERR(inode)) { |
| 8429 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8430 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8431 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8432 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 8433 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8434 | if (err) { |
| 8435 | drop_inode = 1; |
| 8436 | goto out_unlock; |
| 8437 | } |
| 8438 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 8439 | /* |
| 8440 | * If the active LSM wants to access the inode during |
| 8441 | * d_instantiate it needs these. Smack checks to see |
| 8442 | * if the filesystem supports xattrs by looking at the |
| 8443 | * ops vector. |
| 8444 | */ |
| 8445 | inode->i_fop = &btrfs_file_operations; |
| 8446 | inode->i_op = &btrfs_file_inode_operations; |
| 8447 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 8448 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8449 | if (err) |
| 8450 | drop_inode = 1; |
| 8451 | else { |
| 8452 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8453 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8454 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8455 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8456 | if (drop_inode) |
| 8457 | goto out_unlock; |
| 8458 | |
| 8459 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8460 | if (!path) { |
| 8461 | err = -ENOMEM; |
| 8462 | drop_inode = 1; |
| 8463 | goto out_unlock; |
| 8464 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8465 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8466 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8467 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 8468 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 8469 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 8470 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8471 | if (err) { |
| 8472 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 8473 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8474 | goto out_unlock; |
| 8475 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8476 | leaf = path->nodes[0]; |
| 8477 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 8478 | struct btrfs_file_extent_item); |
| 8479 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 8480 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8481 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 8482 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 8483 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 8484 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 8485 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 8486 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8487 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8488 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 8489 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8490 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8491 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8492 | inode->i_op = &btrfs_symlink_inode_operations; |
| 8493 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8494 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8495 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8496 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8497 | err = btrfs_update_inode(trans, root, inode); |
| 8498 | if (err) |
| 8499 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8500 | |
| 8501 | out_unlock: |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 8502 | if (!err) |
| 8503 | d_instantiate(dentry, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8504 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8505 | if (drop_inode) { |
| 8506 | inode_dec_link_count(inode); |
| 8507 | iput(inode); |
| 8508 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8509 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8510 | return err; |
| 8511 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8512 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8513 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8514 | u64 start, u64 num_bytes, u64 min_size, |
| 8515 | loff_t actual_len, u64 *alloc_hint, |
| 8516 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8517 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8518 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 8519 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8520 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8521 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8522 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8523 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8524 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8525 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8526 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8527 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8528 | if (trans) |
| 8529 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8530 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8531 | if (own_trans) { |
| 8532 | trans = btrfs_start_transaction(root, 3); |
| 8533 | if (IS_ERR(trans)) { |
| 8534 | ret = PTR_ERR(trans); |
| 8535 | break; |
| 8536 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8537 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8538 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8539 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 8540 | cur_bytes = max(cur_bytes, min_size); |
| 8541 | ret = btrfs_reserve_extent(trans, root, cur_bytes, |
Zach Brown | 24542bf | 2012-11-16 00:04:43 +0000 | [diff] [blame] | 8542 | min_size, 0, *alloc_hint, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8543 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8544 | if (own_trans) |
| 8545 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8546 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8547 | } |
| 8548 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8549 | ret = insert_reserved_file_extent(trans, inode, |
| 8550 | cur_offset, ins.objectid, |
| 8551 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 8552 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8553 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8554 | if (ret) { |
| 8555 | btrfs_abort_transaction(trans, root, ret); |
| 8556 | if (own_trans) |
| 8557 | btrfs_end_transaction(trans, root); |
| 8558 | break; |
| 8559 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 8560 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8561 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8562 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8563 | em = alloc_extent_map(); |
| 8564 | if (!em) { |
| 8565 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 8566 | &BTRFS_I(inode)->runtime_flags); |
| 8567 | goto next; |
| 8568 | } |
| 8569 | |
| 8570 | em->start = cur_offset; |
| 8571 | em->orig_start = cur_offset; |
| 8572 | em->len = ins.offset; |
| 8573 | em->block_start = ins.objectid; |
| 8574 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 8575 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 8576 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8577 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 8578 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 8579 | em->generation = trans->transid; |
| 8580 | |
| 8581 | while (1) { |
| 8582 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 8583 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8584 | write_unlock(&em_tree->lock); |
| 8585 | if (ret != -EEXIST) |
| 8586 | break; |
| 8587 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8588 | cur_offset + ins.offset - 1, |
| 8589 | 0); |
| 8590 | } |
| 8591 | free_extent_map(em); |
| 8592 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8593 | num_bytes -= ins.offset; |
| 8594 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8595 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8596 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8597 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8598 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 8599 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8600 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8601 | (actual_len > inode->i_size) && |
| 8602 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8603 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8604 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8605 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8606 | i_size = cur_offset; |
| 8607 | i_size_write(inode, i_size); |
| 8608 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8609 | } |
| 8610 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8611 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8612 | |
| 8613 | if (ret) { |
| 8614 | btrfs_abort_transaction(trans, root, ret); |
| 8615 | if (own_trans) |
| 8616 | btrfs_end_transaction(trans, root); |
| 8617 | break; |
| 8618 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8619 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8620 | if (own_trans) |
| 8621 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8622 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8623 | return ret; |
| 8624 | } |
| 8625 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8626 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8627 | u64 start, u64 num_bytes, u64 min_size, |
| 8628 | loff_t actual_len, u64 *alloc_hint) |
| 8629 | { |
| 8630 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8631 | min_size, actual_len, alloc_hint, |
| 8632 | NULL); |
| 8633 | } |
| 8634 | |
| 8635 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 8636 | struct btrfs_trans_handle *trans, int mode, |
| 8637 | u64 start, u64 num_bytes, u64 min_size, |
| 8638 | loff_t actual_len, u64 *alloc_hint) |
| 8639 | { |
| 8640 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8641 | min_size, actual_len, alloc_hint, trans); |
| 8642 | } |
| 8643 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8644 | static int btrfs_set_page_dirty(struct page *page) |
| 8645 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8646 | return __set_page_dirty_nobuffers(page); |
| 8647 | } |
| 8648 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 8649 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8650 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8651 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8652 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8653 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8654 | if (mask & MAY_WRITE && |
| 8655 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 8656 | if (btrfs_root_readonly(root)) |
| 8657 | return -EROFS; |
| 8658 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 8659 | return -EACCES; |
| 8660 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 8661 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8662 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8663 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8664 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8665 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8666 | .lookup = btrfs_lookup, |
| 8667 | .create = btrfs_create, |
| 8668 | .unlink = btrfs_unlink, |
| 8669 | .link = btrfs_link, |
| 8670 | .mkdir = btrfs_mkdir, |
| 8671 | .rmdir = btrfs_rmdir, |
| 8672 | .rename = btrfs_rename, |
| 8673 | .symlink = btrfs_symlink, |
| 8674 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8675 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8676 | .setxattr = btrfs_setxattr, |
| 8677 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 8678 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8679 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8680 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8681 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8682 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8683 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8684 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8685 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8686 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8687 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8688 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 8689 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8690 | .llseek = generic_file_llseek, |
| 8691 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 8692 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8693 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8694 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8695 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8696 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 8697 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8698 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8699 | }; |
| 8700 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8701 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8702 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 8703 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 8704 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8705 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8706 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8707 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 8708 | .set_bit_hook = btrfs_set_bit_hook, |
| 8709 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8710 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 8711 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8712 | }; |
| 8713 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 8714 | /* |
| 8715 | * btrfs doesn't support the bmap operation because swapfiles |
| 8716 | * use bmap to make a mapping of extents in the file. They assume |
| 8717 | * these extents won't change over the life of the file and they |
| 8718 | * use the bmap result to do IO directly to the drive. |
| 8719 | * |
| 8720 | * the btrfs bmap call would return logical addresses that aren't |
| 8721 | * suitable for IO and they also will change frequently as COW |
| 8722 | * operations happen. So, swapfile + btrfs == corruption. |
| 8723 | * |
| 8724 | * For now we're avoiding this by dropping bmap. |
| 8725 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8726 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8727 | .readpage = btrfs_readpage, |
| 8728 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8729 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8730 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8731 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8732 | .invalidatepage = btrfs_invalidatepage, |
| 8733 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8734 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 8735 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8736 | }; |
| 8737 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8738 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8739 | .readpage = btrfs_readpage, |
| 8740 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 8741 | .invalidatepage = btrfs_invalidatepage, |
| 8742 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8743 | }; |
| 8744 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8745 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8746 | .getattr = btrfs_getattr, |
| 8747 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8748 | .setxattr = btrfs_setxattr, |
| 8749 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 8750 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8751 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8752 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8753 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8754 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8755 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8756 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8757 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8758 | .getattr = btrfs_getattr, |
| 8759 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8760 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8761 | .setxattr = btrfs_setxattr, |
| 8762 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8763 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8764 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8765 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8766 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8767 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8768 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8769 | .readlink = generic_readlink, |
| 8770 | .follow_link = page_follow_link_light, |
| 8771 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 8772 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 8773 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8774 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 8775 | .setxattr = btrfs_setxattr, |
| 8776 | .getxattr = btrfs_getxattr, |
| 8777 | .listxattr = btrfs_listxattr, |
| 8778 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8779 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8780 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8781 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8782 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 8783 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8784 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 8785 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8786 | }; |