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, |
| 103 | u64 orig_block_len, int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 104 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 105 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 106 | struct inode *inode, struct inode *dir, |
| 107 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 108 | { |
| 109 | int err; |
| 110 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 111 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 112 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 113 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 114 | return err; |
| 115 | } |
| 116 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 117 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 118 | * this does all the hard work for inserting an inline extent into |
| 119 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 120 | * no overlapping inline items exist in the btree |
| 121 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 122 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 123 | struct btrfs_root *root, struct inode *inode, |
| 124 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 125 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 126 | struct page **compressed_pages) |
| 127 | { |
| 128 | struct btrfs_key key; |
| 129 | struct btrfs_path *path; |
| 130 | struct extent_buffer *leaf; |
| 131 | struct page *page = NULL; |
| 132 | char *kaddr; |
| 133 | unsigned long ptr; |
| 134 | struct btrfs_file_extent_item *ei; |
| 135 | int err = 0; |
| 136 | int ret; |
| 137 | size_t cur_size = size; |
| 138 | size_t datasize; |
| 139 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 140 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 141 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 142 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 143 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 144 | path = btrfs_alloc_path(); |
| 145 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 146 | return -ENOMEM; |
| 147 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 148 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 149 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 150 | key.objectid = btrfs_ino(inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 151 | key.offset = start; |
| 152 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 153 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 154 | |
| 155 | inode_add_bytes(inode, size); |
| 156 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 157 | datasize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 158 | if (ret) { |
| 159 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 160 | goto fail; |
| 161 | } |
| 162 | leaf = path->nodes[0]; |
| 163 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 164 | struct btrfs_file_extent_item); |
| 165 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 166 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 167 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 168 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 169 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 170 | ptr = btrfs_file_extent_inline_start(ei); |
| 171 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 172 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 173 | struct page *cpage; |
| 174 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 175 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 176 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 177 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 178 | PAGE_CACHE_SIZE); |
| 179 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 180 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 181 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 182 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 183 | |
| 184 | i++; |
| 185 | ptr += cur_size; |
| 186 | compressed_size -= cur_size; |
| 187 | } |
| 188 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 189 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 190 | } else { |
| 191 | page = find_get_page(inode->i_mapping, |
| 192 | start >> PAGE_CACHE_SHIFT); |
| 193 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 194 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 195 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 196 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 197 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 198 | page_cache_release(page); |
| 199 | } |
| 200 | btrfs_mark_buffer_dirty(leaf); |
| 201 | btrfs_free_path(path); |
| 202 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 203 | /* |
| 204 | * we're an inline extent, so nobody can |
| 205 | * extend the file past i_size without locking |
| 206 | * a page we already have locked. |
| 207 | * |
| 208 | * We must do any isize and inode updates |
| 209 | * before we unlock the pages. Otherwise we |
| 210 | * could end up racing with unlink. |
| 211 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 212 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 213 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 214 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 215 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 216 | fail: |
| 217 | btrfs_free_path(path); |
| 218 | return err; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /* |
| 223 | * conditionally insert an inline extent into the file. This |
| 224 | * does the checks required to make sure the data is small enough |
| 225 | * to fit as an inline extent. |
| 226 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 227 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 228 | struct btrfs_root *root, |
| 229 | struct inode *inode, u64 start, u64 end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 230 | size_t compressed_size, int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 231 | struct page **compressed_pages) |
| 232 | { |
| 233 | u64 isize = i_size_read(inode); |
| 234 | u64 actual_end = min(end + 1, isize); |
| 235 | u64 inline_len = actual_end - start; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 236 | u64 aligned_end = ALIGN(end, root->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | u64 data_len = inline_len; |
| 238 | int ret; |
| 239 | |
| 240 | if (compressed_size) |
| 241 | data_len = compressed_size; |
| 242 | |
| 243 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 244 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 245 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 246 | (!compressed_size && |
| 247 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 248 | end + 1 < isize || |
| 249 | data_len > root->fs_info->max_inline) { |
| 250 | return 1; |
| 251 | } |
| 252 | |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 253 | ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 254 | if (ret) |
| 255 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 256 | |
| 257 | if (isize > actual_end) |
| 258 | inline_len = min_t(u64, isize, actual_end); |
| 259 | ret = insert_inline_extent(trans, root, inode, start, |
| 260 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 261 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 262 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 263 | btrfs_abort_transaction(trans, root, ret); |
| 264 | return ret; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 265 | } else if (ret == -ENOSPC) { |
| 266 | return 1; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 267 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 268 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 269 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 270 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 271 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 275 | struct async_extent { |
| 276 | u64 start; |
| 277 | u64 ram_size; |
| 278 | u64 compressed_size; |
| 279 | struct page **pages; |
| 280 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 281 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 282 | struct list_head list; |
| 283 | }; |
| 284 | |
| 285 | struct async_cow { |
| 286 | struct inode *inode; |
| 287 | struct btrfs_root *root; |
| 288 | struct page *locked_page; |
| 289 | u64 start; |
| 290 | u64 end; |
| 291 | struct list_head extents; |
| 292 | struct btrfs_work work; |
| 293 | }; |
| 294 | |
| 295 | static noinline int add_async_extent(struct async_cow *cow, |
| 296 | u64 start, u64 ram_size, |
| 297 | u64 compressed_size, |
| 298 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 299 | unsigned long nr_pages, |
| 300 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 301 | { |
| 302 | struct async_extent *async_extent; |
| 303 | |
| 304 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 305 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 306 | async_extent->start = start; |
| 307 | async_extent->ram_size = ram_size; |
| 308 | async_extent->compressed_size = compressed_size; |
| 309 | async_extent->pages = pages; |
| 310 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 311 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 312 | list_add_tail(&async_extent->list, &cow->extents); |
| 313 | return 0; |
| 314 | } |
| 315 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 316 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 317 | * we create compressed extents in two phases. The first |
| 318 | * phase compresses a range of pages that have already been |
| 319 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 320 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 321 | * This is done inside an ordered work queue, and the compression |
| 322 | * is spread across many cpus. The actual IO submission is step |
| 323 | * two, and the ordered work queue takes care of making sure that |
| 324 | * happens in the same order things were put onto the queue by |
| 325 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 326 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 327 | * If this code finds it can't get good compression, it puts an |
| 328 | * entry onto the work queue to write the uncompressed bytes. This |
| 329 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 330 | * are written in the same order that the flusher thread sent them |
| 331 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 332 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 333 | static noinline int compress_file_range(struct inode *inode, |
| 334 | struct page *locked_page, |
| 335 | u64 start, u64 end, |
| 336 | struct async_cow *async_cow, |
| 337 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 338 | { |
| 339 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 340 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 341 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 342 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 343 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 344 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 345 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 346 | struct page **pages = NULL; |
| 347 | unsigned long nr_pages; |
| 348 | unsigned long nr_pages_ret = 0; |
| 349 | unsigned long total_compressed = 0; |
| 350 | unsigned long total_in = 0; |
| 351 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 352 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 353 | int i; |
| 354 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 355 | int compress_type = root->fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame^] | 356 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 357 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 358 | /* if this is a small write inside eof, kick off a defrag */ |
| 359 | if ((end - start + 1) < 16 * 1024 && |
| 360 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 361 | btrfs_add_inode_defrag(NULL, inode); |
| 362 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 363 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 364 | again: |
| 365 | will_compress = 0; |
| 366 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 367 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 368 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 369 | /* |
| 370 | * we don't want to send crud past the end of i_size through |
| 371 | * compression, that's just a waste of CPU time. So, if the |
| 372 | * end of the file is before the start of our current |
| 373 | * requested range of bytes, we bail out to the uncompressed |
| 374 | * cleanup code that can deal with all of this. |
| 375 | * |
| 376 | * It isn't really the fastest way to fix things, but this is a |
| 377 | * very uncommon corner. |
| 378 | */ |
| 379 | if (actual_end <= start) |
| 380 | goto cleanup_and_bail_uncompressed; |
| 381 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 382 | total_compressed = actual_end - start; |
| 383 | |
| 384 | /* we want to make sure that amount of ram required to uncompress |
| 385 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 386 | * of a compressed extent to 128k. This is a crucial number |
| 387 | * because it also controls how easily we can spread reads across |
| 388 | * cpus for decompression. |
| 389 | * |
| 390 | * We also want to make sure the amount of IO required to do |
| 391 | * a random read is reasonably small, so we limit the size of |
| 392 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 393 | */ |
| 394 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 395 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 396 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 397 | total_in = 0; |
| 398 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 399 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 400 | /* |
| 401 | * we do compression for mount -o compress and when the |
| 402 | * inode has not been flagged as nocompress. This flag can |
| 403 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 404 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 405 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 406 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 407 | (BTRFS_I(inode)->force_compress) || |
| 408 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 409 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 410 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 411 | if (!pages) { |
| 412 | /* just bail out to the uncompressed code */ |
| 413 | goto cont; |
| 414 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 415 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 416 | if (BTRFS_I(inode)->force_compress) |
| 417 | compress_type = BTRFS_I(inode)->force_compress; |
| 418 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame^] | 419 | /* |
| 420 | * we need to call clear_page_dirty_for_io on each |
| 421 | * page in the range. Otherwise applications with the file |
| 422 | * mmap'd can wander in and change the page contents while |
| 423 | * we are compressing them. |
| 424 | * |
| 425 | * If the compression fails for any reason, we set the pages |
| 426 | * dirty again later on. |
| 427 | */ |
| 428 | extent_range_clear_dirty_for_io(inode, start, end); |
| 429 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 430 | ret = btrfs_compress_pages(compress_type, |
| 431 | inode->i_mapping, start, |
| 432 | total_compressed, pages, |
| 433 | nr_pages, &nr_pages_ret, |
| 434 | &total_in, |
| 435 | &total_compressed, |
| 436 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 437 | |
| 438 | if (!ret) { |
| 439 | unsigned long offset = total_compressed & |
| 440 | (PAGE_CACHE_SIZE - 1); |
| 441 | struct page *page = pages[nr_pages_ret - 1]; |
| 442 | char *kaddr; |
| 443 | |
| 444 | /* zero the tail end of the last page, we might be |
| 445 | * sending it down to disk |
| 446 | */ |
| 447 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 448 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 449 | memset(kaddr + offset, 0, |
| 450 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 451 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 452 | } |
| 453 | will_compress = 1; |
| 454 | } |
| 455 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 456 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 457 | if (start == 0) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 458 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 459 | if (IS_ERR(trans)) { |
| 460 | ret = PTR_ERR(trans); |
| 461 | trans = NULL; |
| 462 | goto cleanup_and_out; |
| 463 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 464 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 465 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 466 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 467 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 468 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 469 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 470 | */ |
| 471 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 472 | start, end, 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 473 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 474 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 475 | ret = cow_file_range_inline(trans, root, inode, |
| 476 | start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 477 | total_compressed, |
| 478 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 479 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 480 | if (ret <= 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 481 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 482 | * inline extent creation worked or returned error, |
| 483 | * we don't need to create any more async work items. |
| 484 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 485 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 486 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 487 | &BTRFS_I(inode)->io_tree, |
| 488 | start, end, NULL, |
| 489 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 490 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 491 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 492 | |
| 493 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 494 | goto free_pages_out; |
| 495 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 496 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | if (will_compress) { |
| 500 | /* |
| 501 | * we aren't doing an inline extent round the compressed size |
| 502 | * up to a block size boundary so the allocator does sane |
| 503 | * things |
| 504 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 505 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 506 | |
| 507 | /* |
| 508 | * one last check to make sure the compression is really a |
| 509 | * win, compare the page count read with the blocks on disk |
| 510 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 511 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 512 | if (total_compressed >= total_in) { |
| 513 | will_compress = 0; |
| 514 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 515 | num_bytes = total_in; |
| 516 | } |
| 517 | } |
| 518 | if (!will_compress && pages) { |
| 519 | /* |
| 520 | * the compression code ran but failed to make things smaller, |
| 521 | * free any pages it allocated and our page pointer array |
| 522 | */ |
| 523 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 524 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 525 | page_cache_release(pages[i]); |
| 526 | } |
| 527 | kfree(pages); |
| 528 | pages = NULL; |
| 529 | total_compressed = 0; |
| 530 | nr_pages_ret = 0; |
| 531 | |
| 532 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 533 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 534 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 535 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 536 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 537 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 538 | if (will_compress) { |
| 539 | *num_added += 1; |
| 540 | |
| 541 | /* the async work queues will take care of doing actual |
| 542 | * allocation on disk for these compressed pages, |
| 543 | * and will submit them to the elevator. |
| 544 | */ |
| 545 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 546 | total_compressed, pages, nr_pages_ret, |
| 547 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 548 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 549 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 550 | start += num_bytes; |
| 551 | pages = NULL; |
| 552 | cond_resched(); |
| 553 | goto again; |
| 554 | } |
| 555 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 556 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 557 | /* |
| 558 | * No compression, but we still need to write the pages in |
| 559 | * the file we've been given so far. redirty the locked |
| 560 | * page if it corresponds to our extent and set things up |
| 561 | * for the async work queue to run cow_file_range to do |
| 562 | * the normal delalloc dance |
| 563 | */ |
| 564 | if (page_offset(locked_page) >= start && |
| 565 | page_offset(locked_page) <= end) { |
| 566 | __set_page_dirty_nobuffers(locked_page); |
| 567 | /* unlocked later on in the async handlers */ |
| 568 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame^] | 569 | if (redirty) |
| 570 | extent_range_redirty_for_io(inode, start, end); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 571 | add_async_extent(async_cow, start, end - start + 1, |
| 572 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 573 | *num_added += 1; |
| 574 | } |
| 575 | |
| 576 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 577 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 578 | |
| 579 | free_pages_out: |
| 580 | for (i = 0; i < nr_pages_ret; i++) { |
| 581 | WARN_ON(pages[i]->mapping); |
| 582 | page_cache_release(pages[i]); |
| 583 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 584 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 585 | |
| 586 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 587 | |
| 588 | cleanup_and_out: |
| 589 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 590 | start, end, NULL, |
| 591 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 592 | EXTENT_CLEAR_DIRTY | |
| 593 | EXTENT_CLEAR_DELALLOC | |
| 594 | EXTENT_SET_WRITEBACK | |
| 595 | EXTENT_END_WRITEBACK); |
| 596 | if (!trans || IS_ERR(trans)) |
| 597 | btrfs_error(root->fs_info, ret, "Failed to join transaction"); |
| 598 | else |
| 599 | btrfs_abort_transaction(trans, root, ret); |
| 600 | goto free_pages_out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* |
| 604 | * phase two of compressed writeback. This is the ordered portion |
| 605 | * of the code, which only gets called in the order the work was |
| 606 | * queued. We walk all the async extents created by compress_file_range |
| 607 | * and send them down to the disk. |
| 608 | */ |
| 609 | static noinline int submit_compressed_extents(struct inode *inode, |
| 610 | struct async_cow *async_cow) |
| 611 | { |
| 612 | struct async_extent *async_extent; |
| 613 | u64 alloc_hint = 0; |
| 614 | struct btrfs_trans_handle *trans; |
| 615 | struct btrfs_key ins; |
| 616 | struct extent_map *em; |
| 617 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 618 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 619 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 620 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 621 | |
| 622 | if (list_empty(&async_cow->extents)) |
| 623 | return 0; |
| 624 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 625 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 626 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 627 | async_extent = list_entry(async_cow->extents.next, |
| 628 | struct async_extent, list); |
| 629 | list_del(&async_extent->list); |
| 630 | |
| 631 | io_tree = &BTRFS_I(inode)->io_tree; |
| 632 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 633 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 634 | /* did the compression code fall back to uncompressed IO? */ |
| 635 | if (!async_extent->pages) { |
| 636 | int page_started = 0; |
| 637 | unsigned long nr_written = 0; |
| 638 | |
| 639 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 640 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 641 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 642 | |
| 643 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 644 | ret = cow_file_range(inode, async_cow->locked_page, |
| 645 | async_extent->start, |
| 646 | async_extent->start + |
| 647 | async_extent->ram_size - 1, |
| 648 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 649 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 650 | /* JDM XXX */ |
| 651 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 652 | /* |
| 653 | * if page_started, cow_file_range inserted an |
| 654 | * inline extent and took care of all the unlocking |
| 655 | * and IO for us. Otherwise, we need to submit |
| 656 | * all those pages down to the drive. |
| 657 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 658 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 659 | extent_write_locked_range(io_tree, |
| 660 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 661 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 662 | async_extent->ram_size - 1, |
| 663 | btrfs_get_extent, |
| 664 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 665 | else if (ret) |
| 666 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 667 | kfree(async_extent); |
| 668 | cond_resched(); |
| 669 | continue; |
| 670 | } |
| 671 | |
| 672 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 673 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 674 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 675 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 676 | if (IS_ERR(trans)) { |
| 677 | ret = PTR_ERR(trans); |
| 678 | } else { |
| 679 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 680 | ret = btrfs_reserve_extent(trans, root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 681 | async_extent->compressed_size, |
| 682 | async_extent->compressed_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 683 | 0, alloc_hint, &ins, 1); |
Miao Xie | 962197b | 2012-09-11 23:27:35 -0600 | [diff] [blame] | 684 | if (ret && ret != -ENOSPC) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 685 | btrfs_abort_transaction(trans, root, ret); |
| 686 | btrfs_end_transaction(trans, root); |
| 687 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 688 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 689 | if (ret) { |
| 690 | int i; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 691 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 692 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 693 | WARN_ON(async_extent->pages[i]->mapping); |
| 694 | page_cache_release(async_extent->pages[i]); |
| 695 | } |
| 696 | kfree(async_extent->pages); |
| 697 | async_extent->nr_pages = 0; |
| 698 | async_extent->pages = NULL; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 699 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 700 | if (ret == -ENOSPC) |
| 701 | goto retry; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 702 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 703 | } |
| 704 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 705 | /* |
| 706 | * here we're doing allocation and writeback of the |
| 707 | * compressed pages |
| 708 | */ |
| 709 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 710 | async_extent->start + |
| 711 | async_extent->ram_size - 1, 0); |
| 712 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 713 | em = alloc_extent_map(); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 714 | if (!em) |
| 715 | goto out_free_reserve; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 716 | em->start = async_extent->start; |
| 717 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 718 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 719 | em->mod_start = em->start; |
| 720 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 721 | |
| 722 | em->block_start = ins.objectid; |
| 723 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 724 | em->orig_block_len = ins.offset; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 725 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 726 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 727 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 728 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 729 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 730 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 731 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 732 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 733 | ret = add_extent_mapping(em_tree, em); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 734 | if (!ret) |
| 735 | list_move(&em->list, |
| 736 | &em_tree->modified_extents); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 737 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 738 | if (ret != -EEXIST) { |
| 739 | free_extent_map(em); |
| 740 | break; |
| 741 | } |
| 742 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 743 | async_extent->start + |
| 744 | async_extent->ram_size - 1, 0); |
| 745 | } |
| 746 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 747 | if (ret) |
| 748 | goto out_free_reserve; |
| 749 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 750 | ret = btrfs_add_ordered_extent_compress(inode, |
| 751 | async_extent->start, |
| 752 | ins.objectid, |
| 753 | async_extent->ram_size, |
| 754 | ins.offset, |
| 755 | BTRFS_ORDERED_COMPRESSED, |
| 756 | async_extent->compress_type); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 757 | if (ret) |
| 758 | goto out_free_reserve; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 759 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 760 | /* |
| 761 | * clear dirty, set writeback and unlock the pages. |
| 762 | */ |
| 763 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 764 | &BTRFS_I(inode)->io_tree, |
| 765 | async_extent->start, |
| 766 | async_extent->start + |
| 767 | async_extent->ram_size - 1, |
| 768 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 769 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 770 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 771 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 772 | |
| 773 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 774 | async_extent->start, |
| 775 | async_extent->ram_size, |
| 776 | ins.objectid, |
| 777 | ins.offset, async_extent->pages, |
| 778 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 779 | alloc_hint = ins.objectid + ins.offset; |
| 780 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 781 | if (ret) |
| 782 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 783 | cond_resched(); |
| 784 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 785 | ret = 0; |
| 786 | out: |
| 787 | return ret; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 788 | out_free_reserve: |
| 789 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 790 | out_free: |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 791 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 792 | async_extent->start, |
| 793 | async_extent->start + |
| 794 | async_extent->ram_size - 1, |
| 795 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 796 | EXTENT_CLEAR_UNLOCK | |
| 797 | EXTENT_CLEAR_DELALLOC | |
| 798 | EXTENT_CLEAR_DIRTY | |
| 799 | EXTENT_SET_WRITEBACK | |
| 800 | EXTENT_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 801 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 802 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 803 | } |
| 804 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 805 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 806 | u64 num_bytes) |
| 807 | { |
| 808 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 809 | struct extent_map *em; |
| 810 | u64 alloc_hint = 0; |
| 811 | |
| 812 | read_lock(&em_tree->lock); |
| 813 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 814 | if (em) { |
| 815 | /* |
| 816 | * if block start isn't an actual block number then find the |
| 817 | * first block in this inode and use that as a hint. If that |
| 818 | * block is also bogus then just don't worry about it. |
| 819 | */ |
| 820 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 821 | free_extent_map(em); |
| 822 | em = search_extent_mapping(em_tree, 0, 0); |
| 823 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 824 | alloc_hint = em->block_start; |
| 825 | if (em) |
| 826 | free_extent_map(em); |
| 827 | } else { |
| 828 | alloc_hint = em->block_start; |
| 829 | free_extent_map(em); |
| 830 | } |
| 831 | } |
| 832 | read_unlock(&em_tree->lock); |
| 833 | |
| 834 | return alloc_hint; |
| 835 | } |
| 836 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 837 | /* |
| 838 | * when extent_io.c finds a delayed allocation range in the file, |
| 839 | * the call backs end up in this code. The basic idea is to |
| 840 | * allocate extents on disk for the range, and create ordered data structs |
| 841 | * in ram to track those extents. |
| 842 | * |
| 843 | * locked_page is the page that writepage had locked already. We use |
| 844 | * it to make sure we don't do extra locks or unlocks. |
| 845 | * |
| 846 | * *page_started is set to one if we unlock locked_page and do everything |
| 847 | * required to start IO on it. It may be clean and already done with |
| 848 | * IO when we return. |
| 849 | */ |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 850 | static noinline int __cow_file_range(struct btrfs_trans_handle *trans, |
| 851 | struct inode *inode, |
| 852 | struct btrfs_root *root, |
| 853 | struct page *locked_page, |
| 854 | u64 start, u64 end, int *page_started, |
| 855 | unsigned long *nr_written, |
| 856 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 857 | { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 858 | u64 alloc_hint = 0; |
| 859 | u64 num_bytes; |
| 860 | unsigned long ram_size; |
| 861 | u64 disk_num_bytes; |
| 862 | u64 cur_alloc_size; |
| 863 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 864 | struct btrfs_key ins; |
| 865 | struct extent_map *em; |
| 866 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 867 | int ret = 0; |
| 868 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 869 | BUG_ON(btrfs_is_free_space_inode(inode)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 870 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 871 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 872 | num_bytes = max(blocksize, num_bytes); |
| 873 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 874 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 875 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 876 | if (num_bytes < 64 * 1024 && |
| 877 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 878 | btrfs_add_inode_defrag(trans, inode); |
| 879 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 880 | if (start == 0) { |
| 881 | /* lets try to make an inline extent */ |
| 882 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 883 | start, end, 0, 0, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 884 | if (ret == 0) { |
| 885 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 886 | &BTRFS_I(inode)->io_tree, |
| 887 | start, end, NULL, |
| 888 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 889 | EXTENT_CLEAR_UNLOCK | |
| 890 | EXTENT_CLEAR_DELALLOC | |
| 891 | EXTENT_CLEAR_DIRTY | |
| 892 | EXTENT_SET_WRITEBACK | |
| 893 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 894 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 895 | *nr_written = *nr_written + |
| 896 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 897 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 898 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 899 | } else if (ret < 0) { |
| 900 | btrfs_abort_transaction(trans, root, ret); |
| 901 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 902 | } |
| 903 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 904 | |
| 905 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 906 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 907 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 908 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 909 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 910 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 911 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 912 | unsigned long op; |
| 913 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 914 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 915 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 916 | root->sectorsize, 0, alloc_hint, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 917 | &ins, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 918 | if (ret < 0) { |
| 919 | btrfs_abort_transaction(trans, root, ret); |
| 920 | goto out_unlock; |
| 921 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 922 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 923 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 924 | BUG_ON(!em); /* -ENOMEM */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 925 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 926 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 927 | ram_size = ins.offset; |
| 928 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 929 | em->mod_start = em->start; |
| 930 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 931 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 932 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 933 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 934 | em->orig_block_len = ins.offset; |
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); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 941 | ret = add_extent_mapping(em_tree, em); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 942 | if (!ret) |
| 943 | list_move(&em->list, |
| 944 | &em_tree->modified_extents); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 945 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 946 | if (ret != -EEXIST) { |
| 947 | free_extent_map(em); |
| 948 | break; |
| 949 | } |
| 950 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 951 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 952 | } |
| 953 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 954 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 955 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 956 | ram_size, cur_alloc_size, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 957 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 958 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 959 | if (root->root_key.objectid == |
| 960 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 961 | ret = btrfs_reloc_clone_csums(inode, start, |
| 962 | cur_alloc_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 963 | if (ret) { |
| 964 | btrfs_abort_transaction(trans, root, ret); |
| 965 | goto out_unlock; |
| 966 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 967 | } |
| 968 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 969 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 970 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 971 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 972 | /* we're not doing compressed IO, don't unlock the first |
| 973 | * page (which the caller expects to stay locked), don't |
| 974 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 975 | * |
| 976 | * Do set the Private2 bit so we know this page was properly |
| 977 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 978 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 979 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 980 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 981 | EXTENT_SET_PRIVATE2; |
| 982 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 983 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 984 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 985 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 986 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 987 | num_bytes -= cur_alloc_size; |
| 988 | alloc_hint = ins.objectid + ins.offset; |
| 989 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 990 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 991 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 992 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 993 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 994 | out_unlock: |
| 995 | extent_clear_unlock_delalloc(inode, |
| 996 | &BTRFS_I(inode)->io_tree, |
Josef Bacik | beb42dd | 2012-05-30 15:35:17 -0400 | [diff] [blame] | 997 | start, end, locked_page, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 998 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 999 | EXTENT_CLEAR_UNLOCK | |
| 1000 | EXTENT_CLEAR_DELALLOC | |
| 1001 | EXTENT_CLEAR_DIRTY | |
| 1002 | EXTENT_SET_WRITEBACK | |
| 1003 | EXTENT_END_WRITEBACK); |
| 1004 | |
| 1005 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1006 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1007 | |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1008 | static noinline int cow_file_range(struct inode *inode, |
| 1009 | struct page *locked_page, |
| 1010 | u64 start, u64 end, int *page_started, |
| 1011 | unsigned long *nr_written, |
| 1012 | int unlock) |
| 1013 | { |
| 1014 | struct btrfs_trans_handle *trans; |
| 1015 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1016 | int ret; |
| 1017 | |
| 1018 | trans = btrfs_join_transaction(root); |
| 1019 | if (IS_ERR(trans)) { |
| 1020 | extent_clear_unlock_delalloc(inode, |
| 1021 | &BTRFS_I(inode)->io_tree, |
| 1022 | start, end, locked_page, |
| 1023 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1024 | EXTENT_CLEAR_UNLOCK | |
| 1025 | EXTENT_CLEAR_DELALLOC | |
| 1026 | EXTENT_CLEAR_DIRTY | |
| 1027 | EXTENT_SET_WRITEBACK | |
| 1028 | EXTENT_END_WRITEBACK); |
| 1029 | return PTR_ERR(trans); |
| 1030 | } |
| 1031 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 1032 | |
| 1033 | ret = __cow_file_range(trans, inode, root, locked_page, start, end, |
| 1034 | page_started, nr_written, unlock); |
| 1035 | |
| 1036 | btrfs_end_transaction(trans, root); |
| 1037 | |
| 1038 | return ret; |
| 1039 | } |
| 1040 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1041 | /* |
| 1042 | * work queue call back to started compression on a file and pages |
| 1043 | */ |
| 1044 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1045 | { |
| 1046 | struct async_cow *async_cow; |
| 1047 | int num_added = 0; |
| 1048 | async_cow = container_of(work, struct async_cow, work); |
| 1049 | |
| 1050 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1051 | async_cow->start, async_cow->end, async_cow, |
| 1052 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1053 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1054 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1055 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1056 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * work queue call back to submit previously compressed pages |
| 1061 | */ |
| 1062 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1063 | { |
| 1064 | struct async_cow *async_cow; |
| 1065 | struct btrfs_root *root; |
| 1066 | unsigned long nr_pages; |
| 1067 | |
| 1068 | async_cow = container_of(work, struct async_cow, work); |
| 1069 | |
| 1070 | root = async_cow->root; |
| 1071 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1072 | PAGE_CACHE_SHIFT; |
| 1073 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1074 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1075 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1076 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1077 | wake_up(&root->fs_info->async_submit_wait); |
| 1078 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1079 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1080 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1081 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1082 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1083 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1084 | { |
| 1085 | struct async_cow *async_cow; |
| 1086 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1087 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1088 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1089 | kfree(async_cow); |
| 1090 | } |
| 1091 | |
| 1092 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1093 | u64 start, u64 end, int *page_started, |
| 1094 | unsigned long *nr_written) |
| 1095 | { |
| 1096 | struct async_cow *async_cow; |
| 1097 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1098 | unsigned long nr_pages; |
| 1099 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1100 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1101 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1102 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1103 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1104 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1105 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1106 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1107 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1108 | async_cow->root = root; |
| 1109 | async_cow->locked_page = locked_page; |
| 1110 | async_cow->start = start; |
| 1111 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1112 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1113 | cur_end = end; |
| 1114 | else |
| 1115 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1116 | |
| 1117 | async_cow->end = cur_end; |
| 1118 | INIT_LIST_HEAD(&async_cow->extents); |
| 1119 | |
| 1120 | async_cow->work.func = async_cow_start; |
| 1121 | async_cow->work.ordered_func = async_cow_submit; |
| 1122 | async_cow->work.ordered_free = async_cow_free; |
| 1123 | async_cow->work.flags = 0; |
| 1124 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1125 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1126 | PAGE_CACHE_SHIFT; |
| 1127 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1128 | |
| 1129 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 1130 | &async_cow->work); |
| 1131 | |
| 1132 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1133 | wait_event(root->fs_info->async_submit_wait, |
| 1134 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1135 | limit)); |
| 1136 | } |
| 1137 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1138 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1139 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1140 | wait_event(root->fs_info->async_submit_wait, |
| 1141 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1142 | 0)); |
| 1143 | } |
| 1144 | |
| 1145 | *nr_written += nr_pages; |
| 1146 | start = cur_end + 1; |
| 1147 | } |
| 1148 | *page_started = 1; |
| 1149 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1152 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1153 | u64 bytenr, u64 num_bytes) |
| 1154 | { |
| 1155 | int ret; |
| 1156 | struct btrfs_ordered_sum *sums; |
| 1157 | LIST_HEAD(list); |
| 1158 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1159 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1160 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1161 | if (ret == 0 && list_empty(&list)) |
| 1162 | return 0; |
| 1163 | |
| 1164 | while (!list_empty(&list)) { |
| 1165 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1166 | list_del(&sums->list); |
| 1167 | kfree(sums); |
| 1168 | } |
| 1169 | return 1; |
| 1170 | } |
| 1171 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1172 | /* |
| 1173 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1174 | * of the extents that exist in the file, and COWs the file as required. |
| 1175 | * |
| 1176 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1177 | * blocks on disk |
| 1178 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1179 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1180 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1181 | u64 start, u64 end, int *page_started, int force, |
| 1182 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1183 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1184 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1185 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1186 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1187 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1188 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1189 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1190 | u64 cow_start; |
| 1191 | u64 cur_offset; |
| 1192 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1193 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1194 | u64 disk_bytenr; |
| 1195 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1196 | u64 disk_num_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1197 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1198 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1199 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1200 | int nocow; |
| 1201 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1202 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1203 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1204 | |
| 1205 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1206 | if (!path) { |
| 1207 | extent_clear_unlock_delalloc(inode, |
| 1208 | &BTRFS_I(inode)->io_tree, |
| 1209 | start, end, locked_page, |
| 1210 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1211 | EXTENT_CLEAR_UNLOCK | |
| 1212 | EXTENT_CLEAR_DELALLOC | |
| 1213 | EXTENT_CLEAR_DIRTY | |
| 1214 | EXTENT_SET_WRITEBACK | |
| 1215 | EXTENT_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1216 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1217 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1218 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1219 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1220 | |
| 1221 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1222 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1223 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1224 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1225 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1226 | if (IS_ERR(trans)) { |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1227 | extent_clear_unlock_delalloc(inode, |
| 1228 | &BTRFS_I(inode)->io_tree, |
| 1229 | start, end, locked_page, |
| 1230 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1231 | EXTENT_CLEAR_UNLOCK | |
| 1232 | EXTENT_CLEAR_DELALLOC | |
| 1233 | EXTENT_CLEAR_DIRTY | |
| 1234 | EXTENT_SET_WRITEBACK | |
| 1235 | EXTENT_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1236 | btrfs_free_path(path); |
| 1237 | return PTR_ERR(trans); |
| 1238 | } |
| 1239 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1240 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1241 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1242 | cow_start = (u64)-1; |
| 1243 | cur_offset = start; |
| 1244 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1245 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1246 | cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1247 | if (ret < 0) { |
| 1248 | btrfs_abort_transaction(trans, root, ret); |
| 1249 | goto error; |
| 1250 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1251 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1252 | leaf = path->nodes[0]; |
| 1253 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1254 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1255 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1256 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1257 | path->slots[0]--; |
| 1258 | } |
| 1259 | check_prev = 0; |
| 1260 | next_slot: |
| 1261 | leaf = path->nodes[0]; |
| 1262 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1263 | ret = btrfs_next_leaf(root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1264 | if (ret < 0) { |
| 1265 | btrfs_abort_transaction(trans, root, ret); |
| 1266 | goto error; |
| 1267 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1268 | if (ret > 0) |
| 1269 | break; |
| 1270 | leaf = path->nodes[0]; |
| 1271 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1272 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1273 | nocow = 0; |
| 1274 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1275 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1276 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1277 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1278 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1279 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1280 | found_key.offset > end) |
| 1281 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1282 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1283 | if (found_key.offset > cur_offset) { |
| 1284 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1285 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1286 | goto out_check; |
| 1287 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1288 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1289 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1290 | struct btrfs_file_extent_item); |
| 1291 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1292 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1293 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1294 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1295 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1296 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1297 | extent_end = found_key.offset + |
| 1298 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1299 | disk_num_bytes = |
| 1300 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1301 | if (extent_end <= start) { |
| 1302 | path->slots[0]++; |
| 1303 | goto next_slot; |
| 1304 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1305 | if (disk_bytenr == 0) |
| 1306 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1307 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1308 | btrfs_file_extent_encryption(leaf, fi) || |
| 1309 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1310 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1311 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1312 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1313 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1314 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1315 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1316 | found_key.offset - |
| 1317 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1318 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1319 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1320 | disk_bytenr += cur_offset - found_key.offset; |
| 1321 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1322 | /* |
| 1323 | * force cow if csum exists in the range. |
| 1324 | * this ensure that csum for a given extent are |
| 1325 | * either valid or do not exist. |
| 1326 | */ |
| 1327 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1328 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1329 | nocow = 1; |
| 1330 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1331 | extent_end = found_key.offset + |
| 1332 | btrfs_file_extent_inline_len(leaf, fi); |
| 1333 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1334 | } else { |
| 1335 | BUG_ON(1); |
| 1336 | } |
| 1337 | out_check: |
| 1338 | if (extent_end <= start) { |
| 1339 | path->slots[0]++; |
| 1340 | goto next_slot; |
| 1341 | } |
| 1342 | if (!nocow) { |
| 1343 | if (cow_start == (u64)-1) |
| 1344 | cow_start = cur_offset; |
| 1345 | cur_offset = extent_end; |
| 1346 | if (cur_offset > end) |
| 1347 | break; |
| 1348 | path->slots[0]++; |
| 1349 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1350 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1351 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1352 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1353 | if (cow_start != (u64)-1) { |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1354 | ret = __cow_file_range(trans, inode, root, locked_page, |
| 1355 | cow_start, found_key.offset - 1, |
| 1356 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1357 | if (ret) { |
| 1358 | btrfs_abort_transaction(trans, root, ret); |
| 1359 | goto error; |
| 1360 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1361 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1362 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1363 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1364 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1365 | struct extent_map *em; |
| 1366 | struct extent_map_tree *em_tree; |
| 1367 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1368 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1369 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1370 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1371 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1372 | em->len = num_bytes; |
| 1373 | em->block_len = num_bytes; |
| 1374 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1375 | em->orig_block_len = disk_num_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); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1384 | ret = add_extent_mapping(em_tree, em); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1385 | if (!ret) |
| 1386 | list_move(&em->list, |
| 1387 | &em_tree->modified_extents); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1388 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1389 | if (ret != -EEXIST) { |
| 1390 | free_extent_map(em); |
| 1391 | break; |
| 1392 | } |
| 1393 | btrfs_drop_extent_cache(inode, em->start, |
| 1394 | em->start + em->len - 1, 0); |
| 1395 | } |
| 1396 | type = BTRFS_ORDERED_PREALLOC; |
| 1397 | } else { |
| 1398 | type = BTRFS_ORDERED_NOCOW; |
| 1399 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1400 | |
| 1401 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1402 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1403 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1404 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1405 | if (root->root_key.objectid == |
| 1406 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1407 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1408 | num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1409 | if (ret) { |
| 1410 | btrfs_abort_transaction(trans, root, ret); |
| 1411 | goto error; |
| 1412 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1413 | } |
| 1414 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1415 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1416 | cur_offset, cur_offset + num_bytes - 1, |
| 1417 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1418 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1419 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1420 | cur_offset = extent_end; |
| 1421 | if (cur_offset > end) |
| 1422 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1423 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1424 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1425 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1426 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1427 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1428 | cur_offset = end; |
| 1429 | } |
| 1430 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1431 | if (cow_start != (u64)-1) { |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1432 | ret = __cow_file_range(trans, inode, root, locked_page, |
| 1433 | cow_start, end, |
| 1434 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1435 | if (ret) { |
| 1436 | btrfs_abort_transaction(trans, root, ret); |
| 1437 | goto error; |
| 1438 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1439 | } |
| 1440 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1441 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1442 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1443 | if (!ret) |
| 1444 | ret = err; |
| 1445 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1446 | if (ret && cur_offset < end) |
| 1447 | extent_clear_unlock_delalloc(inode, |
| 1448 | &BTRFS_I(inode)->io_tree, |
| 1449 | cur_offset, end, locked_page, |
| 1450 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1451 | EXTENT_CLEAR_UNLOCK | |
| 1452 | EXTENT_CLEAR_DELALLOC | |
| 1453 | EXTENT_CLEAR_DIRTY | |
| 1454 | EXTENT_SET_WRITEBACK | |
| 1455 | EXTENT_END_WRITEBACK); |
| 1456 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1457 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1458 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1459 | } |
| 1460 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1461 | /* |
| 1462 | * extent_io.c call back to do delayed allocation processing |
| 1463 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1464 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1465 | u64 start, u64 end, int *page_started, |
| 1466 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1467 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1468 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1469 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1470 | |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1471 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -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, 1, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1474 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1475 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1476 | page_started, 0, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1477 | } else if (!btrfs_test_opt(root, COMPRESS) && |
| 1478 | !(BTRFS_I(inode)->force_compress) && |
| 1479 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1480 | ret = cow_file_range(inode, locked_page, start, end, |
| 1481 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1482 | } else { |
| 1483 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1484 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1485 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1486 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1487 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1488 | return ret; |
| 1489 | } |
| 1490 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1491 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1492 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1493 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1494 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1495 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1496 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1497 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1498 | spin_lock(&BTRFS_I(inode)->lock); |
| 1499 | BTRFS_I(inode)->outstanding_extents++; |
| 1500 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1505 | * extents so we can keep track of new extents that are just merged onto old |
| 1506 | * extents, such as when we are doing sequential writes, so we can properly |
| 1507 | * account for the metadata space we'll need. |
| 1508 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1509 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1510 | struct extent_state *new, |
| 1511 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1512 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1513 | /* not delalloc, ignore it */ |
| 1514 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1515 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1516 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1517 | spin_lock(&BTRFS_I(inode)->lock); |
| 1518 | BTRFS_I(inode)->outstanding_extents--; |
| 1519 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1520 | } |
| 1521 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1522 | /* |
| 1523 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1524 | * bytes in this file, and to maintain the list of inodes that |
| 1525 | * have pending delalloc work to be done. |
| 1526 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1527 | static void btrfs_set_bit_hook(struct inode *inode, |
| 1528 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1529 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1530 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1531 | /* |
| 1532 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1533 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1534 | * bit, which is only set or cleared with irqs on |
| 1535 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1536 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1537 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1538 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1539 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1540 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1541 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1542 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1543 | } else { |
| 1544 | spin_lock(&BTRFS_I(inode)->lock); |
| 1545 | BTRFS_I(inode)->outstanding_extents++; |
| 1546 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1547 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1548 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1549 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1550 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1551 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1552 | BTRFS_I(inode)->delalloc_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1553 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1554 | &BTRFS_I(inode)->runtime_flags)) { |
| 1555 | spin_lock(&root->fs_info->delalloc_lock); |
| 1556 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1557 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1558 | &root->fs_info->delalloc_inodes); |
| 1559 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1560 | &BTRFS_I(inode)->runtime_flags); |
| 1561 | } |
| 1562 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1563 | } |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1564 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1565 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1566 | } |
| 1567 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1568 | /* |
| 1569 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1570 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1571 | static void btrfs_clear_bit_hook(struct inode *inode, |
| 1572 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1573 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1574 | /* |
| 1575 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1576 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1577 | * bit, which is only set or cleared with irqs on |
| 1578 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1579 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1580 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1581 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1582 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1583 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1584 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1585 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1586 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1587 | spin_lock(&BTRFS_I(inode)->lock); |
| 1588 | BTRFS_I(inode)->outstanding_extents--; |
| 1589 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1590 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1591 | |
| 1592 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1593 | btrfs_delalloc_release_metadata(inode, len); |
| 1594 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1595 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 1596 | && do_list) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1597 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1598 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1599 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1600 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1601 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1602 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1603 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1604 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1605 | &BTRFS_I(inode)->runtime_flags)) { |
| 1606 | spin_lock(&root->fs_info->delalloc_lock); |
| 1607 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1608 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1609 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1610 | &BTRFS_I(inode)->runtime_flags); |
| 1611 | } |
| 1612 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1613 | } |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1614 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1615 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1616 | } |
| 1617 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1618 | /* |
| 1619 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1620 | * we don't create bios that span stripes or chunks |
| 1621 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1622 | 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] | 1623 | size_t size, struct bio *bio, |
| 1624 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1625 | { |
| 1626 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1627 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1628 | u64 length = 0; |
| 1629 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1630 | int ret; |
| 1631 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1632 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1633 | return 0; |
| 1634 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1635 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1636 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1637 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1638 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1639 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1640 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1641 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1642 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1643 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1644 | } |
| 1645 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1646 | /* |
| 1647 | * in order to insert checksums into the metadata in large chunks, |
| 1648 | * we wait until bio submission time. All the pages in the bio are |
| 1649 | * checksummed and sums are attached onto the ordered extent record. |
| 1650 | * |
| 1651 | * At IO completion time the cums attached on the ordered extent record |
| 1652 | * are inserted into the btree |
| 1653 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1654 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1655 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1656 | unsigned long bio_flags, |
| 1657 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1658 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1659 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1660 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1661 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1662 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1663 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1664 | return 0; |
| 1665 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1666 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1667 | /* |
| 1668 | * in order to insert checksums into the metadata in large chunks, |
| 1669 | * we wait until bio submission time. All the pages in the bio are |
| 1670 | * checksummed and sums are attached onto the ordered extent record. |
| 1671 | * |
| 1672 | * At IO completion time the cums attached on the ordered extent record |
| 1673 | * are inserted into the btree |
| 1674 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1675 | 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] | 1676 | int mirror_num, unsigned long bio_flags, |
| 1677 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1678 | { |
| 1679 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1680 | int ret; |
| 1681 | |
| 1682 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1683 | if (ret) |
| 1684 | bio_endio(bio, ret); |
| 1685 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1686 | } |
| 1687 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1688 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1689 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1690 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1691 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1692 | 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] | 1693 | int mirror_num, unsigned long bio_flags, |
| 1694 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1695 | { |
| 1696 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1697 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1698 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1699 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1700 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1701 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1702 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1703 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1704 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1705 | metadata = 2; |
| 1706 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1707 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1708 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1709 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1710 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1711 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1712 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1713 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1714 | mirror_num, |
| 1715 | bio_flags); |
| 1716 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1717 | } else if (!skip_sum) { |
| 1718 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1719 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1720 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1721 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1722 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1723 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1724 | /* csum items have already been cloned */ |
| 1725 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1726 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1727 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1728 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1729 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1730 | bio_flags, bio_offset, |
| 1731 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1732 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1733 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1734 | } else if (!skip_sum) { |
| 1735 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1736 | if (ret) |
| 1737 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1738 | } |
| 1739 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1740 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1741 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1742 | |
| 1743 | out: |
| 1744 | if (ret < 0) |
| 1745 | bio_endio(bio, ret); |
| 1746 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1747 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1748 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1749 | /* |
| 1750 | * given a list of ordered sums record them in the inode. This happens |
| 1751 | * at IO completion time based on sums calculated at bio submission time. |
| 1752 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1753 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1754 | struct inode *inode, u64 file_offset, |
| 1755 | struct list_head *list) |
| 1756 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1757 | struct btrfs_ordered_sum *sum; |
| 1758 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1759 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1760 | btrfs_csum_file_blocks(trans, |
| 1761 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1762 | } |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1766 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1767 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1768 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1769 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1770 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1771 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1772 | } |
| 1773 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1774 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1775 | struct btrfs_writepage_fixup { |
| 1776 | struct page *page; |
| 1777 | struct btrfs_work work; |
| 1778 | }; |
| 1779 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1780 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1781 | { |
| 1782 | struct btrfs_writepage_fixup *fixup; |
| 1783 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1784 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1785 | struct page *page; |
| 1786 | struct inode *inode; |
| 1787 | u64 page_start; |
| 1788 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1789 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1790 | |
| 1791 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1792 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1793 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1794 | lock_page(page); |
| 1795 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1796 | ClearPageChecked(page); |
| 1797 | goto out_page; |
| 1798 | } |
| 1799 | |
| 1800 | inode = page->mapping->host; |
| 1801 | page_start = page_offset(page); |
| 1802 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1803 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1804 | 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] | 1805 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1806 | |
| 1807 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1808 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1809 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1810 | |
| 1811 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1812 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1813 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1814 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1815 | unlock_page(page); |
| 1816 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1817 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1818 | goto again; |
| 1819 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1820 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1821 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1822 | if (ret) { |
| 1823 | mapping_set_error(page->mapping, ret); |
| 1824 | end_extent_writepage(page, ret, page_start, page_end); |
| 1825 | ClearPageChecked(page); |
| 1826 | goto out; |
| 1827 | } |
| 1828 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1829 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1830 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1831 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1832 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1833 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1834 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1835 | out_page: |
| 1836 | unlock_page(page); |
| 1837 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1838 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | /* |
| 1842 | * There are a few paths in the higher layers of the kernel that directly |
| 1843 | * set the page dirty bit without asking the filesystem if it is a |
| 1844 | * good idea. This causes problems because we want to make sure COW |
| 1845 | * properly happens and the data=ordered rules are followed. |
| 1846 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1847 | * 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] | 1848 | * hasn't been properly setup for IO. We kick off an async process |
| 1849 | * to fix it up. The async helper will wait for ordered extents, set |
| 1850 | * the delalloc bit and make it safe to write the page. |
| 1851 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1852 | 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] | 1853 | { |
| 1854 | struct inode *inode = page->mapping->host; |
| 1855 | struct btrfs_writepage_fixup *fixup; |
| 1856 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1857 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1858 | /* this page is properly in the ordered list */ |
| 1859 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1860 | return 0; |
| 1861 | |
| 1862 | if (PageChecked(page)) |
| 1863 | return -EAGAIN; |
| 1864 | |
| 1865 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1866 | if (!fixup) |
| 1867 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1868 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1869 | SetPageChecked(page); |
| 1870 | page_cache_get(page); |
| 1871 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1872 | fixup->page = page; |
| 1873 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1874 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1875 | } |
| 1876 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1877 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1878 | struct inode *inode, u64 file_pos, |
| 1879 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1880 | u64 num_bytes, u64 ram_bytes, |
| 1881 | u8 compression, u8 encryption, |
| 1882 | u16 other_encoding, int extent_type) |
| 1883 | { |
| 1884 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1885 | struct btrfs_file_extent_item *fi; |
| 1886 | struct btrfs_path *path; |
| 1887 | struct extent_buffer *leaf; |
| 1888 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1889 | int ret; |
| 1890 | |
| 1891 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1892 | if (!path) |
| 1893 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1894 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1895 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1896 | |
| 1897 | /* |
| 1898 | * we may be replacing one extent in the tree with another. |
| 1899 | * The new extent is pinned in the extent map, and we don't want |
| 1900 | * to drop it from the cache until it is completely in the btree. |
| 1901 | * |
| 1902 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1903 | * the caller is expected to unpin it and allow it to be merged |
| 1904 | * with the others. |
| 1905 | */ |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1906 | ret = btrfs_drop_extents(trans, root, inode, file_pos, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 1907 | file_pos + num_bytes, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1908 | if (ret) |
| 1909 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1910 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1911 | ins.objectid = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1912 | ins.offset = file_pos; |
| 1913 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1914 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1915 | if (ret) |
| 1916 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1917 | leaf = path->nodes[0]; |
| 1918 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1919 | struct btrfs_file_extent_item); |
| 1920 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1921 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1922 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1923 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1924 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1925 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1926 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1927 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1928 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1929 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1930 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1931 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 1932 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1933 | |
| 1934 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1935 | |
| 1936 | ins.objectid = disk_bytenr; |
| 1937 | ins.offset = disk_num_bytes; |
| 1938 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1939 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1940 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1941 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1942 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1943 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1944 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1945 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1946 | } |
| 1947 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 1948 | /* snapshot-aware defrag */ |
| 1949 | struct sa_defrag_extent_backref { |
| 1950 | struct rb_node node; |
| 1951 | struct old_sa_defrag_extent *old; |
| 1952 | u64 root_id; |
| 1953 | u64 inum; |
| 1954 | u64 file_pos; |
| 1955 | u64 extent_offset; |
| 1956 | u64 num_bytes; |
| 1957 | u64 generation; |
| 1958 | }; |
| 1959 | |
| 1960 | struct old_sa_defrag_extent { |
| 1961 | struct list_head list; |
| 1962 | struct new_sa_defrag_extent *new; |
| 1963 | |
| 1964 | u64 extent_offset; |
| 1965 | u64 bytenr; |
| 1966 | u64 offset; |
| 1967 | u64 len; |
| 1968 | int count; |
| 1969 | }; |
| 1970 | |
| 1971 | struct new_sa_defrag_extent { |
| 1972 | struct rb_root root; |
| 1973 | struct list_head head; |
| 1974 | struct btrfs_path *path; |
| 1975 | struct inode *inode; |
| 1976 | u64 file_pos; |
| 1977 | u64 len; |
| 1978 | u64 bytenr; |
| 1979 | u64 disk_len; |
| 1980 | u8 compress_type; |
| 1981 | }; |
| 1982 | |
| 1983 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 1984 | struct sa_defrag_extent_backref *b2) |
| 1985 | { |
| 1986 | if (b1->root_id < b2->root_id) |
| 1987 | return -1; |
| 1988 | else if (b1->root_id > b2->root_id) |
| 1989 | return 1; |
| 1990 | |
| 1991 | if (b1->inum < b2->inum) |
| 1992 | return -1; |
| 1993 | else if (b1->inum > b2->inum) |
| 1994 | return 1; |
| 1995 | |
| 1996 | if (b1->file_pos < b2->file_pos) |
| 1997 | return -1; |
| 1998 | else if (b1->file_pos > b2->file_pos) |
| 1999 | return 1; |
| 2000 | |
| 2001 | /* |
| 2002 | * [------------------------------] ===> (a range of space) |
| 2003 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2004 | * |<---------------------------->| ===> (fs/file tree B) |
| 2005 | * |
| 2006 | * A range of space can refer to two file extents in one tree while |
| 2007 | * refer to only one file extent in another tree. |
| 2008 | * |
| 2009 | * So we may process a disk offset more than one time(two extents in A) |
| 2010 | * and locate at the same extent(one extent in B), then insert two same |
| 2011 | * backrefs(both refer to the extent in B). |
| 2012 | */ |
| 2013 | return 0; |
| 2014 | } |
| 2015 | |
| 2016 | static void backref_insert(struct rb_root *root, |
| 2017 | struct sa_defrag_extent_backref *backref) |
| 2018 | { |
| 2019 | struct rb_node **p = &root->rb_node; |
| 2020 | struct rb_node *parent = NULL; |
| 2021 | struct sa_defrag_extent_backref *entry; |
| 2022 | int ret; |
| 2023 | |
| 2024 | while (*p) { |
| 2025 | parent = *p; |
| 2026 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2027 | |
| 2028 | ret = backref_comp(backref, entry); |
| 2029 | if (ret < 0) |
| 2030 | p = &(*p)->rb_left; |
| 2031 | else |
| 2032 | p = &(*p)->rb_right; |
| 2033 | } |
| 2034 | |
| 2035 | rb_link_node(&backref->node, parent, p); |
| 2036 | rb_insert_color(&backref->node, root); |
| 2037 | } |
| 2038 | |
| 2039 | /* |
| 2040 | * Note the backref might has changed, and in this case we just return 0. |
| 2041 | */ |
| 2042 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2043 | void *ctx) |
| 2044 | { |
| 2045 | struct btrfs_file_extent_item *extent; |
| 2046 | struct btrfs_fs_info *fs_info; |
| 2047 | struct old_sa_defrag_extent *old = ctx; |
| 2048 | struct new_sa_defrag_extent *new = old->new; |
| 2049 | struct btrfs_path *path = new->path; |
| 2050 | struct btrfs_key key; |
| 2051 | struct btrfs_root *root; |
| 2052 | struct sa_defrag_extent_backref *backref; |
| 2053 | struct extent_buffer *leaf; |
| 2054 | struct inode *inode = new->inode; |
| 2055 | int slot; |
| 2056 | int ret; |
| 2057 | u64 extent_offset; |
| 2058 | u64 num_bytes; |
| 2059 | |
| 2060 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2061 | inum == btrfs_ino(inode)) |
| 2062 | return 0; |
| 2063 | |
| 2064 | key.objectid = root_id; |
| 2065 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2066 | key.offset = (u64)-1; |
| 2067 | |
| 2068 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2069 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2070 | if (IS_ERR(root)) { |
| 2071 | if (PTR_ERR(root) == -ENOENT) |
| 2072 | return 0; |
| 2073 | WARN_ON(1); |
| 2074 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2075 | inum, offset, root_id); |
| 2076 | return PTR_ERR(root); |
| 2077 | } |
| 2078 | |
| 2079 | key.objectid = inum; |
| 2080 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2081 | if (offset > (u64)-1 << 32) |
| 2082 | key.offset = 0; |
| 2083 | else |
| 2084 | key.offset = offset; |
| 2085 | |
| 2086 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2087 | if (ret < 0) { |
| 2088 | WARN_ON(1); |
| 2089 | return ret; |
| 2090 | } |
| 2091 | |
| 2092 | while (1) { |
| 2093 | cond_resched(); |
| 2094 | |
| 2095 | leaf = path->nodes[0]; |
| 2096 | slot = path->slots[0]; |
| 2097 | |
| 2098 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2099 | ret = btrfs_next_leaf(root, path); |
| 2100 | if (ret < 0) { |
| 2101 | goto out; |
| 2102 | } else if (ret > 0) { |
| 2103 | ret = 0; |
| 2104 | goto out; |
| 2105 | } |
| 2106 | continue; |
| 2107 | } |
| 2108 | |
| 2109 | path->slots[0]++; |
| 2110 | |
| 2111 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2112 | |
| 2113 | if (key.objectid > inum) |
| 2114 | goto out; |
| 2115 | |
| 2116 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2117 | continue; |
| 2118 | |
| 2119 | extent = btrfs_item_ptr(leaf, slot, |
| 2120 | struct btrfs_file_extent_item); |
| 2121 | |
| 2122 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2123 | continue; |
| 2124 | |
| 2125 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
| 2126 | if (key.offset - extent_offset != offset) |
| 2127 | continue; |
| 2128 | |
| 2129 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
| 2130 | if (extent_offset >= old->extent_offset + old->offset + |
| 2131 | old->len || extent_offset + num_bytes <= |
| 2132 | old->extent_offset + old->offset) |
| 2133 | continue; |
| 2134 | |
| 2135 | break; |
| 2136 | } |
| 2137 | |
| 2138 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2139 | if (!backref) { |
| 2140 | ret = -ENOENT; |
| 2141 | goto out; |
| 2142 | } |
| 2143 | |
| 2144 | backref->root_id = root_id; |
| 2145 | backref->inum = inum; |
| 2146 | backref->file_pos = offset + extent_offset; |
| 2147 | backref->num_bytes = num_bytes; |
| 2148 | backref->extent_offset = extent_offset; |
| 2149 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2150 | backref->old = old; |
| 2151 | backref_insert(&new->root, backref); |
| 2152 | old->count++; |
| 2153 | out: |
| 2154 | btrfs_release_path(path); |
| 2155 | WARN_ON(ret); |
| 2156 | return ret; |
| 2157 | } |
| 2158 | |
| 2159 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2160 | struct new_sa_defrag_extent *new) |
| 2161 | { |
| 2162 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2163 | struct old_sa_defrag_extent *old, *tmp; |
| 2164 | int ret; |
| 2165 | |
| 2166 | new->path = path; |
| 2167 | |
| 2168 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2169 | ret = iterate_inodes_from_logical(old->bytenr, fs_info, |
| 2170 | path, record_one_backref, |
| 2171 | old); |
| 2172 | BUG_ON(ret < 0 && ret != -ENOENT); |
| 2173 | |
| 2174 | /* no backref to be processed for this extent */ |
| 2175 | if (!old->count) { |
| 2176 | list_del(&old->list); |
| 2177 | kfree(old); |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | if (list_empty(&new->head)) |
| 2182 | return false; |
| 2183 | |
| 2184 | return true; |
| 2185 | } |
| 2186 | |
| 2187 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2188 | struct btrfs_file_extent_item *fi, |
| 2189 | u64 disk_bytenr) |
| 2190 | { |
| 2191 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr) |
| 2192 | return 0; |
| 2193 | |
| 2194 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2195 | return 0; |
| 2196 | |
| 2197 | if (btrfs_file_extent_compression(leaf, fi) || |
| 2198 | btrfs_file_extent_encryption(leaf, fi) || |
| 2199 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2200 | return 0; |
| 2201 | |
| 2202 | return 1; |
| 2203 | } |
| 2204 | |
| 2205 | /* |
| 2206 | * Note the backref might has changed, and in this case we just return 0. |
| 2207 | */ |
| 2208 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2209 | struct sa_defrag_extent_backref *prev, |
| 2210 | struct sa_defrag_extent_backref *backref) |
| 2211 | { |
| 2212 | struct btrfs_file_extent_item *extent; |
| 2213 | struct btrfs_file_extent_item *item; |
| 2214 | struct btrfs_ordered_extent *ordered; |
| 2215 | struct btrfs_trans_handle *trans; |
| 2216 | struct btrfs_fs_info *fs_info; |
| 2217 | struct btrfs_root *root; |
| 2218 | struct btrfs_key key; |
| 2219 | struct extent_buffer *leaf; |
| 2220 | struct old_sa_defrag_extent *old = backref->old; |
| 2221 | struct new_sa_defrag_extent *new = old->new; |
| 2222 | struct inode *src_inode = new->inode; |
| 2223 | struct inode *inode; |
| 2224 | struct extent_state *cached = NULL; |
| 2225 | int ret = 0; |
| 2226 | u64 start; |
| 2227 | u64 len; |
| 2228 | u64 lock_start; |
| 2229 | u64 lock_end; |
| 2230 | bool merge = false; |
| 2231 | int index; |
| 2232 | |
| 2233 | if (prev && prev->root_id == backref->root_id && |
| 2234 | prev->inum == backref->inum && |
| 2235 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2236 | merge = true; |
| 2237 | |
| 2238 | /* step 1: get root */ |
| 2239 | key.objectid = backref->root_id; |
| 2240 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2241 | key.offset = (u64)-1; |
| 2242 | |
| 2243 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2244 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2245 | |
| 2246 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2247 | if (IS_ERR(root)) { |
| 2248 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2249 | if (PTR_ERR(root) == -ENOENT) |
| 2250 | return 0; |
| 2251 | return PTR_ERR(root); |
| 2252 | } |
| 2253 | if (btrfs_root_refs(&root->root_item) == 0) { |
| 2254 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2255 | /* parse ENOENT to 0 */ |
| 2256 | return 0; |
| 2257 | } |
| 2258 | |
| 2259 | /* step 2: get inode */ |
| 2260 | key.objectid = backref->inum; |
| 2261 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2262 | key.offset = 0; |
| 2263 | |
| 2264 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2265 | if (IS_ERR(inode)) { |
| 2266 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2267 | return 0; |
| 2268 | } |
| 2269 | |
| 2270 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2271 | |
| 2272 | /* step 3: relink backref */ |
| 2273 | lock_start = backref->file_pos; |
| 2274 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2275 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2276 | 0, &cached); |
| 2277 | |
| 2278 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2279 | if (ordered) { |
| 2280 | btrfs_put_ordered_extent(ordered); |
| 2281 | goto out_unlock; |
| 2282 | } |
| 2283 | |
| 2284 | trans = btrfs_join_transaction(root); |
| 2285 | if (IS_ERR(trans)) { |
| 2286 | ret = PTR_ERR(trans); |
| 2287 | goto out_unlock; |
| 2288 | } |
| 2289 | |
| 2290 | key.objectid = backref->inum; |
| 2291 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2292 | key.offset = backref->file_pos; |
| 2293 | |
| 2294 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2295 | if (ret < 0) { |
| 2296 | goto out_free_path; |
| 2297 | } else if (ret > 0) { |
| 2298 | ret = 0; |
| 2299 | goto out_free_path; |
| 2300 | } |
| 2301 | |
| 2302 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2303 | struct btrfs_file_extent_item); |
| 2304 | |
| 2305 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2306 | backref->generation) |
| 2307 | goto out_free_path; |
| 2308 | |
| 2309 | btrfs_release_path(path); |
| 2310 | |
| 2311 | start = backref->file_pos; |
| 2312 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2313 | start += old->extent_offset + old->offset - |
| 2314 | backref->extent_offset; |
| 2315 | |
| 2316 | len = min(backref->extent_offset + backref->num_bytes, |
| 2317 | old->extent_offset + old->offset + old->len); |
| 2318 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2319 | |
| 2320 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2321 | start + len, 1); |
| 2322 | if (ret) |
| 2323 | goto out_free_path; |
| 2324 | again: |
| 2325 | key.objectid = btrfs_ino(inode); |
| 2326 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2327 | key.offset = start; |
| 2328 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2329 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2330 | if (merge) { |
| 2331 | struct btrfs_file_extent_item *fi; |
| 2332 | u64 extent_len; |
| 2333 | struct btrfs_key found_key; |
| 2334 | |
| 2335 | ret = btrfs_search_slot(trans, root, &key, path, 1, 1); |
| 2336 | if (ret < 0) |
| 2337 | goto out_free_path; |
| 2338 | |
| 2339 | path->slots[0]--; |
| 2340 | leaf = path->nodes[0]; |
| 2341 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2342 | |
| 2343 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2344 | struct btrfs_file_extent_item); |
| 2345 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2346 | |
| 2347 | if (relink_is_mergable(leaf, fi, new->bytenr) && |
| 2348 | extent_len + found_key.offset == start) { |
| 2349 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2350 | extent_len + len); |
| 2351 | btrfs_mark_buffer_dirty(leaf); |
| 2352 | inode_add_bytes(inode, len); |
| 2353 | |
| 2354 | ret = 1; |
| 2355 | goto out_free_path; |
| 2356 | } else { |
| 2357 | merge = false; |
| 2358 | btrfs_release_path(path); |
| 2359 | goto again; |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2364 | sizeof(*extent)); |
| 2365 | if (ret) { |
| 2366 | btrfs_abort_transaction(trans, root, ret); |
| 2367 | goto out_free_path; |
| 2368 | } |
| 2369 | |
| 2370 | leaf = path->nodes[0]; |
| 2371 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2372 | struct btrfs_file_extent_item); |
| 2373 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2374 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2375 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2376 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2377 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2378 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2379 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2380 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2381 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2382 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2383 | |
| 2384 | btrfs_mark_buffer_dirty(leaf); |
| 2385 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2386 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2387 | |
| 2388 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2389 | new->disk_len, 0, |
| 2390 | backref->root_id, backref->inum, |
| 2391 | new->file_pos, 0); /* start - extent_offset */ |
| 2392 | if (ret) { |
| 2393 | btrfs_abort_transaction(trans, root, ret); |
| 2394 | goto out_free_path; |
| 2395 | } |
| 2396 | |
| 2397 | ret = 1; |
| 2398 | out_free_path: |
| 2399 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2400 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2401 | btrfs_end_transaction(trans, root); |
| 2402 | out_unlock: |
| 2403 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2404 | &cached, GFP_NOFS); |
| 2405 | iput(inode); |
| 2406 | return ret; |
| 2407 | } |
| 2408 | |
| 2409 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2410 | { |
| 2411 | struct btrfs_path *path; |
| 2412 | struct old_sa_defrag_extent *old, *tmp; |
| 2413 | struct sa_defrag_extent_backref *backref; |
| 2414 | struct sa_defrag_extent_backref *prev = NULL; |
| 2415 | struct inode *inode; |
| 2416 | struct btrfs_root *root; |
| 2417 | struct rb_node *node; |
| 2418 | int ret; |
| 2419 | |
| 2420 | inode = new->inode; |
| 2421 | root = BTRFS_I(inode)->root; |
| 2422 | |
| 2423 | path = btrfs_alloc_path(); |
| 2424 | if (!path) |
| 2425 | return; |
| 2426 | |
| 2427 | if (!record_extent_backrefs(path, new)) { |
| 2428 | btrfs_free_path(path); |
| 2429 | goto out; |
| 2430 | } |
| 2431 | btrfs_release_path(path); |
| 2432 | |
| 2433 | while (1) { |
| 2434 | node = rb_first(&new->root); |
| 2435 | if (!node) |
| 2436 | break; |
| 2437 | rb_erase(node, &new->root); |
| 2438 | |
| 2439 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2440 | |
| 2441 | ret = relink_extent_backref(path, prev, backref); |
| 2442 | WARN_ON(ret < 0); |
| 2443 | |
| 2444 | kfree(prev); |
| 2445 | |
| 2446 | if (ret == 1) |
| 2447 | prev = backref; |
| 2448 | else |
| 2449 | prev = NULL; |
| 2450 | cond_resched(); |
| 2451 | } |
| 2452 | kfree(prev); |
| 2453 | |
| 2454 | btrfs_free_path(path); |
| 2455 | |
| 2456 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2457 | list_del(&old->list); |
| 2458 | kfree(old); |
| 2459 | } |
| 2460 | out: |
| 2461 | atomic_dec(&root->fs_info->defrag_running); |
| 2462 | wake_up(&root->fs_info->transaction_wait); |
| 2463 | |
| 2464 | kfree(new); |
| 2465 | } |
| 2466 | |
| 2467 | static struct new_sa_defrag_extent * |
| 2468 | record_old_file_extents(struct inode *inode, |
| 2469 | struct btrfs_ordered_extent *ordered) |
| 2470 | { |
| 2471 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2472 | struct btrfs_path *path; |
| 2473 | struct btrfs_key key; |
| 2474 | struct old_sa_defrag_extent *old, *tmp; |
| 2475 | struct new_sa_defrag_extent *new; |
| 2476 | int ret; |
| 2477 | |
| 2478 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2479 | if (!new) |
| 2480 | return NULL; |
| 2481 | |
| 2482 | new->inode = inode; |
| 2483 | new->file_pos = ordered->file_offset; |
| 2484 | new->len = ordered->len; |
| 2485 | new->bytenr = ordered->start; |
| 2486 | new->disk_len = ordered->disk_len; |
| 2487 | new->compress_type = ordered->compress_type; |
| 2488 | new->root = RB_ROOT; |
| 2489 | INIT_LIST_HEAD(&new->head); |
| 2490 | |
| 2491 | path = btrfs_alloc_path(); |
| 2492 | if (!path) |
| 2493 | goto out_kfree; |
| 2494 | |
| 2495 | key.objectid = btrfs_ino(inode); |
| 2496 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2497 | key.offset = new->file_pos; |
| 2498 | |
| 2499 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2500 | if (ret < 0) |
| 2501 | goto out_free_path; |
| 2502 | if (ret > 0 && path->slots[0] > 0) |
| 2503 | path->slots[0]--; |
| 2504 | |
| 2505 | /* find out all the old extents for the file range */ |
| 2506 | while (1) { |
| 2507 | struct btrfs_file_extent_item *extent; |
| 2508 | struct extent_buffer *l; |
| 2509 | int slot; |
| 2510 | u64 num_bytes; |
| 2511 | u64 offset; |
| 2512 | u64 end; |
| 2513 | u64 disk_bytenr; |
| 2514 | u64 extent_offset; |
| 2515 | |
| 2516 | l = path->nodes[0]; |
| 2517 | slot = path->slots[0]; |
| 2518 | |
| 2519 | if (slot >= btrfs_header_nritems(l)) { |
| 2520 | ret = btrfs_next_leaf(root, path); |
| 2521 | if (ret < 0) |
| 2522 | goto out_free_list; |
| 2523 | else if (ret > 0) |
| 2524 | break; |
| 2525 | continue; |
| 2526 | } |
| 2527 | |
| 2528 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2529 | |
| 2530 | if (key.objectid != btrfs_ino(inode)) |
| 2531 | break; |
| 2532 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2533 | break; |
| 2534 | if (key.offset >= new->file_pos + new->len) |
| 2535 | break; |
| 2536 | |
| 2537 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2538 | |
| 2539 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2540 | if (key.offset + num_bytes < new->file_pos) |
| 2541 | goto next; |
| 2542 | |
| 2543 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2544 | if (!disk_bytenr) |
| 2545 | goto next; |
| 2546 | |
| 2547 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2548 | |
| 2549 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2550 | if (!old) |
| 2551 | goto out_free_list; |
| 2552 | |
| 2553 | offset = max(new->file_pos, key.offset); |
| 2554 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2555 | |
| 2556 | old->bytenr = disk_bytenr; |
| 2557 | old->extent_offset = extent_offset; |
| 2558 | old->offset = offset - key.offset; |
| 2559 | old->len = end - offset; |
| 2560 | old->new = new; |
| 2561 | old->count = 0; |
| 2562 | list_add_tail(&old->list, &new->head); |
| 2563 | next: |
| 2564 | path->slots[0]++; |
| 2565 | cond_resched(); |
| 2566 | } |
| 2567 | |
| 2568 | btrfs_free_path(path); |
| 2569 | atomic_inc(&root->fs_info->defrag_running); |
| 2570 | |
| 2571 | return new; |
| 2572 | |
| 2573 | out_free_list: |
| 2574 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2575 | list_del(&old->list); |
| 2576 | kfree(old); |
| 2577 | } |
| 2578 | out_free_path: |
| 2579 | btrfs_free_path(path); |
| 2580 | out_kfree: |
| 2581 | kfree(new); |
| 2582 | return NULL; |
| 2583 | } |
| 2584 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 2585 | /* |
| 2586 | * helper function for btrfs_finish_ordered_io, this |
| 2587 | * just reads in some of the csum leaves to prime them into ram |
| 2588 | * before we start the transaction. It limits the amount of btree |
| 2589 | * reads required while inside the transaction. |
| 2590 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2591 | /* as ordered data IO finishes, this gets called so we can finish |
| 2592 | * an ordered extent if the range of bytes in the file it covers are |
| 2593 | * fully written. |
| 2594 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2595 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2596 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2597 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2598 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2599 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2600 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2601 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2602 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2603 | int compress_type = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2604 | int ret; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2605 | bool nolock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2606 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2607 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2608 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2609 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2610 | ret = -EIO; |
| 2611 | goto out; |
| 2612 | } |
| 2613 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2614 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2615 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2616 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2617 | if (nolock) |
| 2618 | trans = btrfs_join_transaction_nolock(root); |
| 2619 | else |
| 2620 | trans = btrfs_join_transaction(root); |
| 2621 | if (IS_ERR(trans)) { |
| 2622 | ret = PTR_ERR(trans); |
| 2623 | trans = NULL; |
| 2624 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2625 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2626 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2627 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2628 | if (ret) /* -ENOMEM or corruption */ |
| 2629 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2630 | goto out; |
| 2631 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2632 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2633 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2634 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2635 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2636 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2637 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2638 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2639 | EXTENT_DEFRAG, 1, cached_state); |
| 2640 | if (ret) { |
| 2641 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
| 2642 | if (last_snapshot >= BTRFS_I(inode)->generation) |
| 2643 | /* the inode is shared */ |
| 2644 | new = record_old_file_extents(inode, ordered_extent); |
| 2645 | |
| 2646 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2647 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2648 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2649 | } |
| 2650 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2651 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2652 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2653 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2654 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2655 | if (IS_ERR(trans)) { |
| 2656 | ret = PTR_ERR(trans); |
| 2657 | trans = NULL; |
| 2658 | goto out_unlock; |
| 2659 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2660 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2661 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2662 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2663 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2664 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2665 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2666 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2667 | ordered_extent->file_offset, |
| 2668 | ordered_extent->file_offset + |
| 2669 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2670 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2671 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2672 | ret = insert_reserved_file_extent(trans, inode, |
| 2673 | ordered_extent->file_offset, |
| 2674 | ordered_extent->start, |
| 2675 | ordered_extent->disk_len, |
| 2676 | ordered_extent->len, |
| 2677 | ordered_extent->len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2678 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2679 | BTRFS_FILE_EXTENT_REG); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2680 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2681 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2682 | ordered_extent->file_offset, ordered_extent->len, |
| 2683 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2684 | if (ret < 0) { |
| 2685 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2686 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2687 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2688 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2689 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2690 | &ordered_extent->list); |
| 2691 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2692 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2693 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2694 | if (ret) { /* -ENOMEM or corruption */ |
| 2695 | btrfs_abort_transaction(trans, root, ret); |
| 2696 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2697 | } |
| 2698 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2699 | out_unlock: |
| 2700 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2701 | ordered_extent->file_offset + |
| 2702 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2703 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2704 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2705 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2706 | if (trans) |
| 2707 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2708 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2709 | if (ret) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2710 | clear_extent_uptodate(io_tree, ordered_extent->file_offset, |
| 2711 | ordered_extent->file_offset + |
| 2712 | ordered_extent->len - 1, NULL, GFP_NOFS); |
| 2713 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2714 | /* |
| 2715 | * If the ordered extent had an IOERR or something else went |
| 2716 | * wrong we need to return the space for this ordered extent |
| 2717 | * back to the allocator. |
| 2718 | */ |
| 2719 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2720 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2721 | btrfs_free_reserved_extent(root, ordered_extent->start, |
| 2722 | ordered_extent->disk_len); |
| 2723 | } |
| 2724 | |
| 2725 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2726 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2727 | * This needs to be done to make sure anybody waiting knows we are done |
| 2728 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2729 | */ |
| 2730 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2731 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2732 | /* for snapshot-aware defrag */ |
| 2733 | if (new) |
| 2734 | relink_file_extents(new); |
| 2735 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2736 | /* once for us */ |
| 2737 | btrfs_put_ordered_extent(ordered_extent); |
| 2738 | /* once for the tree */ |
| 2739 | btrfs_put_ordered_extent(ordered_extent); |
| 2740 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2741 | return ret; |
| 2742 | } |
| 2743 | |
| 2744 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2745 | { |
| 2746 | struct btrfs_ordered_extent *ordered_extent; |
| 2747 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2748 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2749 | } |
| 2750 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2751 | 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] | 2752 | struct extent_state *state, int uptodate) |
| 2753 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2754 | struct inode *inode = page->mapping->host; |
| 2755 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2756 | struct btrfs_ordered_extent *ordered_extent = NULL; |
| 2757 | struct btrfs_workers *workers; |
| 2758 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2759 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2760 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2761 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2762 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2763 | end - start + 1, uptodate)) |
| 2764 | return 0; |
| 2765 | |
| 2766 | ordered_extent->work.func = finish_ordered_fn; |
| 2767 | ordered_extent->work.flags = 0; |
| 2768 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2769 | if (btrfs_is_free_space_inode(inode)) |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2770 | workers = &root->fs_info->endio_freespace_worker; |
| 2771 | else |
| 2772 | workers = &root->fs_info->endio_write_workers; |
| 2773 | btrfs_queue_worker(workers, &ordered_extent->work); |
| 2774 | |
| 2775 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2776 | } |
| 2777 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2778 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2779 | * 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] | 2780 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2781 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2782 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2783 | 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] | 2784 | struct extent_state *state, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2785 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2786 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2787 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2788 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2789 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 2790 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2791 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2792 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2793 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2794 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2795 | if (PageChecked(page)) { |
| 2796 | ClearPageChecked(page); |
| 2797 | goto good; |
| 2798 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2799 | |
| 2800 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 2801 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2802 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2803 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2804 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2805 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2806 | GFP_NOFS); |
| 2807 | return 0; |
| 2808 | } |
| 2809 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 2810 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2811 | private = state->private; |
| 2812 | ret = 0; |
| 2813 | } else { |
| 2814 | ret = get_state_private(io_tree, start, &private); |
| 2815 | } |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2816 | kaddr = kmap_atomic(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2817 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2818 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2819 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2820 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 2821 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2822 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2823 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2824 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2825 | kunmap_atomic(kaddr); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2826 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2827 | return 0; |
| 2828 | |
| 2829 | zeroit: |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 2830 | printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2831 | "private %llu\n", |
| 2832 | (unsigned long long)btrfs_ino(page->mapping->host), |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 2833 | (unsigned long long)start, csum, |
| 2834 | (unsigned long long)private); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2835 | memset(kaddr + offset, 1, end - start + 1); |
| 2836 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2837 | kunmap_atomic(kaddr); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2838 | if (private == 0) |
| 2839 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2840 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2841 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2842 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2843 | struct delayed_iput { |
| 2844 | struct list_head list; |
| 2845 | struct inode *inode; |
| 2846 | }; |
| 2847 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2848 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2849 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2850 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2851 | { |
| 2852 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2853 | struct delayed_iput *delayed; |
| 2854 | |
| 2855 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2856 | return; |
| 2857 | |
| 2858 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2859 | delayed->inode = inode; |
| 2860 | |
| 2861 | spin_lock(&fs_info->delayed_iput_lock); |
| 2862 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2863 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2864 | } |
| 2865 | |
| 2866 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2867 | { |
| 2868 | LIST_HEAD(list); |
| 2869 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2870 | struct delayed_iput *delayed; |
| 2871 | int empty; |
| 2872 | |
| 2873 | spin_lock(&fs_info->delayed_iput_lock); |
| 2874 | empty = list_empty(&fs_info->delayed_iputs); |
| 2875 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2876 | if (empty) |
| 2877 | return; |
| 2878 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2879 | spin_lock(&fs_info->delayed_iput_lock); |
| 2880 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2881 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2882 | |
| 2883 | while (!list_empty(&list)) { |
| 2884 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2885 | list_del(&delayed->list); |
| 2886 | iput(delayed->inode); |
| 2887 | kfree(delayed); |
| 2888 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2891 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 2892 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2893 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2894 | * structure. |
| 2895 | */ |
| 2896 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2897 | struct btrfs_root *root) |
| 2898 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2899 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2900 | int ret; |
| 2901 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2902 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2903 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2904 | return; |
| 2905 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2906 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2907 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2908 | spin_unlock(&root->orphan_lock); |
| 2909 | return; |
| 2910 | } |
| 2911 | |
| 2912 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 2913 | spin_unlock(&root->orphan_lock); |
| 2914 | return; |
| 2915 | } |
| 2916 | |
| 2917 | block_rsv = root->orphan_block_rsv; |
| 2918 | root->orphan_block_rsv = NULL; |
| 2919 | spin_unlock(&root->orphan_lock); |
| 2920 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2921 | if (root->orphan_item_inserted && |
| 2922 | btrfs_root_refs(&root->root_item) > 0) { |
| 2923 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2924 | root->root_key.objectid); |
| 2925 | BUG_ON(ret); |
| 2926 | root->orphan_item_inserted = 0; |
| 2927 | } |
| 2928 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2929 | if (block_rsv) { |
| 2930 | WARN_ON(block_rsv->size > 0); |
| 2931 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2932 | } |
| 2933 | } |
| 2934 | |
| 2935 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2936 | * This creates an orphan entry for the given inode in case something goes |
| 2937 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2938 | * |
| 2939 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2940 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2941 | */ |
| 2942 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2943 | { |
| 2944 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2945 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2946 | int reserve = 0; |
| 2947 | int insert = 0; |
| 2948 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2949 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2950 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 2951 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2952 | if (!block_rsv) |
| 2953 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2954 | } |
| 2955 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2956 | spin_lock(&root->orphan_lock); |
| 2957 | if (!root->orphan_block_rsv) { |
| 2958 | root->orphan_block_rsv = block_rsv; |
| 2959 | } else if (block_rsv) { |
| 2960 | btrfs_free_block_rsv(root, block_rsv); |
| 2961 | block_rsv = NULL; |
| 2962 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2963 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2964 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2965 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2966 | #if 0 |
| 2967 | /* |
| 2968 | * For proper ENOSPC handling, we should do orphan |
| 2969 | * cleanup when mounting. But this introduces backward |
| 2970 | * compatibility issue. |
| 2971 | */ |
| 2972 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2973 | insert = 2; |
| 2974 | else |
| 2975 | insert = 1; |
| 2976 | #endif |
| 2977 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 2978 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2979 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2980 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 2981 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 2982 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2983 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2984 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2985 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2986 | /* grab metadata reservation from transaction handle */ |
| 2987 | if (reserve) { |
| 2988 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2989 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2993 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2994 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2995 | if (ret && ret != -EEXIST) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2996 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2997 | &BTRFS_I(inode)->runtime_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2998 | btrfs_abort_transaction(trans, root, ret); |
| 2999 | return ret; |
| 3000 | } |
| 3001 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3005 | if (insert >= 2) { |
| 3006 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3007 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3008 | if (ret && ret != -EEXIST) { |
| 3009 | btrfs_abort_transaction(trans, root, ret); |
| 3010 | return ret; |
| 3011 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3012 | } |
| 3013 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | /* |
| 3017 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3018 | * item for this particular inode. |
| 3019 | */ |
| 3020 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3021 | { |
| 3022 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3023 | int delete_item = 0; |
| 3024 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3025 | int ret = 0; |
| 3026 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3027 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3028 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3029 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3030 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3031 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3032 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3033 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3034 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3035 | spin_unlock(&root->orphan_lock); |
| 3036 | |
| 3037 | if (trans && delete_item) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3038 | ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3039 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3040 | } |
| 3041 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3042 | if (release_rsv) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3043 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3044 | atomic_dec(&root->orphan_inodes); |
| 3045 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3046 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3047 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | /* |
| 3051 | * this cleans up any orphans that may be left on the list from the last use |
| 3052 | * of this root. |
| 3053 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3054 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3055 | { |
| 3056 | struct btrfs_path *path; |
| 3057 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3058 | struct btrfs_key key, found_key; |
| 3059 | struct btrfs_trans_handle *trans; |
| 3060 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3061 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3062 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3063 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3064 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3065 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3066 | |
| 3067 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3068 | if (!path) { |
| 3069 | ret = -ENOMEM; |
| 3070 | goto out; |
| 3071 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3072 | path->reada = -1; |
| 3073 | |
| 3074 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 3075 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 3076 | key.offset = (u64)-1; |
| 3077 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3078 | while (1) { |
| 3079 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3080 | if (ret < 0) |
| 3081 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3082 | |
| 3083 | /* |
| 3084 | * 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] | 3085 | * 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] | 3086 | * find the key and see if we have stuff that matches |
| 3087 | */ |
| 3088 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3089 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3090 | if (path->slots[0] == 0) |
| 3091 | break; |
| 3092 | path->slots[0]--; |
| 3093 | } |
| 3094 | |
| 3095 | /* pull out the item */ |
| 3096 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3097 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3098 | |
| 3099 | /* make sure the item matches what we want */ |
| 3100 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3101 | break; |
| 3102 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 3103 | break; |
| 3104 | |
| 3105 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3106 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3107 | |
| 3108 | /* |
| 3109 | * this is where we are basically btrfs_lookup, without the |
| 3110 | * crossing root thing. we store the inode number in the |
| 3111 | * offset of the orphan item. |
| 3112 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3113 | |
| 3114 | if (found_key.offset == last_objectid) { |
| 3115 | printk(KERN_ERR "btrfs: Error removing orphan entry, " |
| 3116 | "stopping orphan cleanup\n"); |
| 3117 | ret = -EINVAL; |
| 3118 | goto out; |
| 3119 | } |
| 3120 | |
| 3121 | last_objectid = found_key.offset; |
| 3122 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3123 | found_key.objectid = found_key.offset; |
| 3124 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3125 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3126 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3127 | ret = PTR_RET(inode); |
| 3128 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3129 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3130 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3131 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3132 | struct btrfs_root *dead_root; |
| 3133 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3134 | int is_dead_root = 0; |
| 3135 | |
| 3136 | /* |
| 3137 | * this is an orphan in the tree root. Currently these |
| 3138 | * could come from 2 sources: |
| 3139 | * a) a snapshot deletion in progress |
| 3140 | * b) a free space cache inode |
| 3141 | * We need to distinguish those two, as the snapshot |
| 3142 | * orphan must not get deleted. |
| 3143 | * find_dead_roots already ran before us, so if this |
| 3144 | * is a snapshot deletion, we should find the root |
| 3145 | * in the dead_roots list |
| 3146 | */ |
| 3147 | spin_lock(&fs_info->trans_lock); |
| 3148 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3149 | root_list) { |
| 3150 | if (dead_root->root_key.objectid == |
| 3151 | found_key.objectid) { |
| 3152 | is_dead_root = 1; |
| 3153 | break; |
| 3154 | } |
| 3155 | } |
| 3156 | spin_unlock(&fs_info->trans_lock); |
| 3157 | if (is_dead_root) { |
| 3158 | /* prevent this orphan from being found again */ |
| 3159 | key.offset = found_key.objectid - 1; |
| 3160 | continue; |
| 3161 | } |
| 3162 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3163 | /* |
| 3164 | * Inode is already gone but the orphan item is still there, |
| 3165 | * kill the orphan item. |
| 3166 | */ |
| 3167 | if (ret == -ESTALE) { |
| 3168 | trans = btrfs_start_transaction(root, 1); |
| 3169 | if (IS_ERR(trans)) { |
| 3170 | ret = PTR_ERR(trans); |
| 3171 | goto out; |
| 3172 | } |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3173 | printk(KERN_ERR "auto deleting %Lu\n", |
| 3174 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3175 | ret = btrfs_del_orphan_item(trans, root, |
| 3176 | found_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3177 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3178 | btrfs_end_transaction(trans, root); |
| 3179 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3180 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3181 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3182 | /* |
| 3183 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3184 | * the proper thing when we hit it |
| 3185 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3186 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3187 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3188 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3189 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3190 | /* if we have links, this was a truncate, lets do that */ |
| 3191 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3192 | if (!S_ISREG(inode->i_mode)) { |
| 3193 | WARN_ON(1); |
| 3194 | iput(inode); |
| 3195 | continue; |
| 3196 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3197 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3198 | |
| 3199 | /* 1 for the orphan item deletion. */ |
| 3200 | trans = btrfs_start_transaction(root, 1); |
| 3201 | if (IS_ERR(trans)) { |
| 3202 | ret = PTR_ERR(trans); |
| 3203 | goto out; |
| 3204 | } |
| 3205 | ret = btrfs_orphan_add(trans, inode); |
| 3206 | btrfs_end_transaction(trans, root); |
| 3207 | if (ret) |
| 3208 | goto out; |
| 3209 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3210 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3211 | if (ret) |
| 3212 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3213 | } else { |
| 3214 | nr_unlink++; |
| 3215 | } |
| 3216 | |
| 3217 | /* this will do delete_inode and everything for us */ |
| 3218 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3219 | if (ret) |
| 3220 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3221 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3222 | /* release the path since we're done with it */ |
| 3223 | btrfs_release_path(path); |
| 3224 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3225 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3226 | |
| 3227 | if (root->orphan_block_rsv) |
| 3228 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3229 | (u64)-1); |
| 3230 | |
| 3231 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3232 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3233 | if (!IS_ERR(trans)) |
| 3234 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3235 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3236 | |
| 3237 | if (nr_unlink) |
| 3238 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 3239 | if (nr_truncate) |
| 3240 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3241 | |
| 3242 | out: |
| 3243 | if (ret) |
| 3244 | printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret); |
| 3245 | btrfs_free_path(path); |
| 3246 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3247 | } |
| 3248 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3249 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3250 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3251 | * don't find any xattrs, we know there can't be any acls. |
| 3252 | * |
| 3253 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3254 | */ |
| 3255 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 3256 | int slot, u64 objectid) |
| 3257 | { |
| 3258 | u32 nritems = btrfs_header_nritems(leaf); |
| 3259 | struct btrfs_key found_key; |
| 3260 | int scanned = 0; |
| 3261 | |
| 3262 | slot++; |
| 3263 | while (slot < nritems) { |
| 3264 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3265 | |
| 3266 | /* we found a different objectid, there must not be acls */ |
| 3267 | if (found_key.objectid != objectid) |
| 3268 | return 0; |
| 3269 | |
| 3270 | /* we found an xattr, assume we've got an acl */ |
| 3271 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 3272 | return 1; |
| 3273 | |
| 3274 | /* |
| 3275 | * we found a key greater than an xattr key, there can't |
| 3276 | * be any acls later on |
| 3277 | */ |
| 3278 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3279 | return 0; |
| 3280 | |
| 3281 | slot++; |
| 3282 | scanned++; |
| 3283 | |
| 3284 | /* |
| 3285 | * it goes inode, inode backrefs, xattrs, extents, |
| 3286 | * so if there are a ton of hard links to an inode there can |
| 3287 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3288 | * this is just an optimization |
| 3289 | */ |
| 3290 | if (scanned >= 8) |
| 3291 | break; |
| 3292 | } |
| 3293 | /* we hit the end of the leaf before we found an xattr or |
| 3294 | * something larger than an xattr. We have to assume the inode |
| 3295 | * has acls |
| 3296 | */ |
| 3297 | return 1; |
| 3298 | } |
| 3299 | |
| 3300 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3301 | * read an inode from the btree into the in-memory inode |
| 3302 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3303 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3304 | { |
| 3305 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3306 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3307 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3308 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3309 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3310 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3311 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3312 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3313 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3314 | bool filled = false; |
| 3315 | |
| 3316 | ret = btrfs_fill_inode(inode, &rdev); |
| 3317 | if (!ret) |
| 3318 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3319 | |
| 3320 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3321 | if (!path) |
| 3322 | goto make_bad; |
| 3323 | |
Josef Bacik | d90c732 | 2011-05-17 09:50:54 -0400 | [diff] [blame] | 3324 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3325 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3326 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3327 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3328 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3329 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3330 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3331 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3332 | |
| 3333 | if (filled) |
| 3334 | goto cache_acl; |
| 3335 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3336 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3337 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3338 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3339 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3340 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3341 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3342 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3343 | |
| 3344 | tspec = btrfs_inode_atime(inode_item); |
| 3345 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3346 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3347 | |
| 3348 | tspec = btrfs_inode_mtime(inode_item); |
| 3349 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3350 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3351 | |
| 3352 | tspec = btrfs_inode_ctime(inode_item); |
| 3353 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3354 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3355 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3356 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3357 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3358 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3359 | |
| 3360 | /* |
| 3361 | * If we were modified in the current generation and evicted from memory |
| 3362 | * and then re-read we need to do a full sync since we don't have any |
| 3363 | * idea about which extents were modified before we were evicted from |
| 3364 | * cache. |
| 3365 | */ |
| 3366 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3367 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3368 | &BTRFS_I(inode)->runtime_flags); |
| 3369 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3370 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3371 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3372 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3373 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3374 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3375 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3376 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3377 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3378 | /* |
| 3379 | * try to precache a NULL acl entry for files that don't have |
| 3380 | * any xattrs or acls |
| 3381 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3382 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
| 3383 | btrfs_ino(inode)); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3384 | if (!maybe_acls) |
| 3385 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3386 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3387 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3388 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3389 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3390 | case S_IFREG: |
| 3391 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3392 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3393 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3394 | inode->i_fop = &btrfs_file_operations; |
| 3395 | inode->i_op = &btrfs_file_inode_operations; |
| 3396 | break; |
| 3397 | case S_IFDIR: |
| 3398 | inode->i_fop = &btrfs_dir_file_operations; |
| 3399 | if (root == root->fs_info->tree_root) |
| 3400 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3401 | else |
| 3402 | inode->i_op = &btrfs_dir_inode_operations; |
| 3403 | break; |
| 3404 | case S_IFLNK: |
| 3405 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3406 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3407 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3408 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3409 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3410 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3411 | init_special_inode(inode, inode->i_mode, rdev); |
| 3412 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3413 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3414 | |
| 3415 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3416 | return; |
| 3417 | |
| 3418 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3419 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3420 | make_bad_inode(inode); |
| 3421 | } |
| 3422 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3423 | /* |
| 3424 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3425 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3426 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3427 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3428 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3429 | struct inode *inode) |
| 3430 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3431 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3432 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3433 | btrfs_init_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_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3436 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3437 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3438 | &token); |
| 3439 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3440 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3441 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3442 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
| 3443 | inode->i_atime.tv_sec, &token); |
| 3444 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 3445 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3446 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3447 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 3448 | inode->i_mtime.tv_sec, &token); |
| 3449 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 3450 | inode->i_mtime.tv_nsec, &token); |
| 3451 | |
| 3452 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 3453 | inode->i_ctime.tv_sec, &token); |
| 3454 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 3455 | inode->i_ctime.tv_nsec, &token); |
| 3456 | |
| 3457 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3458 | &token); |
| 3459 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3460 | &token); |
| 3461 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3462 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3463 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3464 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3465 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3466 | } |
| 3467 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3468 | /* |
| 3469 | * copy everything in the in-memory inode into the btree. |
| 3470 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3471 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3472 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3473 | { |
| 3474 | struct btrfs_inode_item *inode_item; |
| 3475 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3476 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3477 | int ret; |
| 3478 | |
| 3479 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3480 | if (!path) |
| 3481 | return -ENOMEM; |
| 3482 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3483 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3484 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3485 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3486 | if (ret) { |
| 3487 | if (ret > 0) |
| 3488 | ret = -ENOENT; |
| 3489 | goto failed; |
| 3490 | } |
| 3491 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3492 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3493 | leaf = path->nodes[0]; |
| 3494 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3495 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3496 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3497 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3498 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3499 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3500 | ret = 0; |
| 3501 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3502 | btrfs_free_path(path); |
| 3503 | return ret; |
| 3504 | } |
| 3505 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3506 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3507 | * copy everything in the in-memory inode into the btree. |
| 3508 | */ |
| 3509 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3510 | struct btrfs_root *root, struct inode *inode) |
| 3511 | { |
| 3512 | int ret; |
| 3513 | |
| 3514 | /* |
| 3515 | * If the inode is a free space inode, we can deadlock during commit |
| 3516 | * if we put it into the delayed code. |
| 3517 | * |
| 3518 | * The data relocation inode should also be directly updated |
| 3519 | * without delay |
| 3520 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3521 | if (!btrfs_is_free_space_inode(inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3522 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3523 | btrfs_update_root_times(trans, root); |
| 3524 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3525 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3526 | if (!ret) |
| 3527 | btrfs_set_inode_last_trans(trans, inode); |
| 3528 | return ret; |
| 3529 | } |
| 3530 | |
| 3531 | return btrfs_update_inode_item(trans, root, inode); |
| 3532 | } |
| 3533 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3534 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3535 | struct btrfs_root *root, |
| 3536 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3537 | { |
| 3538 | int ret; |
| 3539 | |
| 3540 | ret = btrfs_update_inode(trans, root, inode); |
| 3541 | if (ret == -ENOSPC) |
| 3542 | return btrfs_update_inode_item(trans, root, inode); |
| 3543 | return ret; |
| 3544 | } |
| 3545 | |
| 3546 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3547 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3548 | * recovery code. It remove a link in a directory with a given name, and |
| 3549 | * also drops the back refs in the inode to the directory |
| 3550 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3551 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3552 | struct btrfs_root *root, |
| 3553 | struct inode *dir, struct inode *inode, |
| 3554 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3555 | { |
| 3556 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3557 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3558 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3559 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3560 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3561 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3562 | u64 ino = btrfs_ino(inode); |
| 3563 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3564 | |
| 3565 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3566 | if (!path) { |
| 3567 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3568 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3569 | } |
| 3570 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3571 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3572 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3573 | name, name_len, -1); |
| 3574 | if (IS_ERR(di)) { |
| 3575 | ret = PTR_ERR(di); |
| 3576 | goto err; |
| 3577 | } |
| 3578 | if (!di) { |
| 3579 | ret = -ENOENT; |
| 3580 | goto err; |
| 3581 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3582 | leaf = path->nodes[0]; |
| 3583 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3584 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3585 | if (ret) |
| 3586 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3587 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3588 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3589 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3590 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3591 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3592 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3593 | "inode %llu parent %llu\n", name_len, name, |
| 3594 | (unsigned long long)ino, (unsigned long long)dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3595 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3596 | goto err; |
| 3597 | } |
| 3598 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3599 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3600 | if (ret) { |
| 3601 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3602 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3603 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3604 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3605 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3606 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3607 | if (ret != 0 && ret != -ENOENT) { |
| 3608 | btrfs_abort_transaction(trans, root, ret); |
| 3609 | goto err; |
| 3610 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3611 | |
| 3612 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3613 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3614 | if (ret == -ENOENT) |
| 3615 | ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3616 | err: |
| 3617 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3618 | if (ret) |
| 3619 | goto out; |
| 3620 | |
| 3621 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3622 | inode_inc_iversion(inode); |
| 3623 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3624 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3625 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3626 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3627 | return ret; |
| 3628 | } |
| 3629 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3630 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3631 | struct btrfs_root *root, |
| 3632 | struct inode *dir, struct inode *inode, |
| 3633 | const char *name, int name_len) |
| 3634 | { |
| 3635 | int ret; |
| 3636 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3637 | if (!ret) { |
| 3638 | btrfs_drop_nlink(inode); |
| 3639 | ret = btrfs_update_inode(trans, root, inode); |
| 3640 | } |
| 3641 | return ret; |
| 3642 | } |
| 3643 | |
| 3644 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3645 | /* helper to check if there is any shared block in the path */ |
| 3646 | static int check_path_shared(struct btrfs_root *root, |
| 3647 | struct btrfs_path *path) |
| 3648 | { |
| 3649 | struct extent_buffer *eb; |
| 3650 | int level; |
Dan Carpenter | 0e4dcbef | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 3651 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3652 | |
| 3653 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 3654 | int ret; |
| 3655 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3656 | if (!path->nodes[level]) |
| 3657 | break; |
| 3658 | eb = path->nodes[level]; |
| 3659 | if (!btrfs_block_can_be_shared(root, eb)) |
| 3660 | continue; |
| 3661 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 3662 | &refs, NULL); |
| 3663 | if (refs > 1) |
| 3664 | return 1; |
| 3665 | } |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 3666 | return 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | /* |
| 3670 | * helper to start transaction for unlink and rmdir. |
| 3671 | * |
| 3672 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 3673 | * so in enospc case, we should make sure they will free space before |
| 3674 | * allowing them to use the global metadata reservation. |
| 3675 | */ |
| 3676 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 3677 | struct dentry *dentry) |
| 3678 | { |
| 3679 | struct btrfs_trans_handle *trans; |
| 3680 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 3681 | struct btrfs_path *path; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3682 | struct btrfs_dir_item *di; |
| 3683 | struct inode *inode = dentry->d_inode; |
| 3684 | u64 index; |
| 3685 | int check_link = 1; |
| 3686 | int err = -ENOSPC; |
| 3687 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3688 | u64 ino = btrfs_ino(inode); |
| 3689 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3690 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3691 | /* |
| 3692 | * 1 for the possible orphan item |
| 3693 | * 1 for the dir item |
| 3694 | * 1 for the dir index |
| 3695 | * 1 for the inode ref |
| 3696 | * 1 for the inode ref in the tree log |
| 3697 | * 2 for the dir entries in the log |
| 3698 | * 1 for the inode |
| 3699 | */ |
| 3700 | trans = btrfs_start_transaction(root, 8); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3701 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3702 | return trans; |
| 3703 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3704 | if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3705 | return ERR_PTR(-ENOSPC); |
| 3706 | |
| 3707 | /* check if there is someone else holds reference */ |
| 3708 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 3709 | return ERR_PTR(-ENOSPC); |
| 3710 | |
| 3711 | if (atomic_read(&inode->i_count) > 2) |
| 3712 | return ERR_PTR(-ENOSPC); |
| 3713 | |
| 3714 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 3715 | return ERR_PTR(-ENOSPC); |
| 3716 | |
| 3717 | path = btrfs_alloc_path(); |
| 3718 | if (!path) { |
| 3719 | root->fs_info->enospc_unlink = 0; |
| 3720 | return ERR_PTR(-ENOMEM); |
| 3721 | } |
| 3722 | |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 3723 | /* 1 for the orphan item */ |
| 3724 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3725 | if (IS_ERR(trans)) { |
| 3726 | btrfs_free_path(path); |
| 3727 | root->fs_info->enospc_unlink = 0; |
| 3728 | return trans; |
| 3729 | } |
| 3730 | |
| 3731 | path->skip_locking = 1; |
| 3732 | path->search_commit_root = 1; |
| 3733 | |
| 3734 | ret = btrfs_lookup_inode(trans, root, path, |
| 3735 | &BTRFS_I(dir)->location, 0); |
| 3736 | if (ret < 0) { |
| 3737 | err = ret; |
| 3738 | goto out; |
| 3739 | } |
| 3740 | if (ret == 0) { |
| 3741 | if (check_path_shared(root, path)) |
| 3742 | goto out; |
| 3743 | } else { |
| 3744 | check_link = 0; |
| 3745 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3746 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3747 | |
| 3748 | ret = btrfs_lookup_inode(trans, root, path, |
| 3749 | &BTRFS_I(inode)->location, 0); |
| 3750 | if (ret < 0) { |
| 3751 | err = ret; |
| 3752 | goto out; |
| 3753 | } |
| 3754 | if (ret == 0) { |
| 3755 | if (check_path_shared(root, path)) |
| 3756 | goto out; |
| 3757 | } else { |
| 3758 | check_link = 0; |
| 3759 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3760 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3761 | |
| 3762 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 3763 | ret = btrfs_lookup_file_extent(trans, root, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3764 | ino, (u64)-1, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3765 | if (ret < 0) { |
| 3766 | err = ret; |
| 3767 | goto out; |
| 3768 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3769 | BUG_ON(ret == 0); /* Corruption */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3770 | if (check_path_shared(root, path)) |
| 3771 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3772 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | if (!check_link) { |
| 3776 | err = 0; |
| 3777 | goto out; |
| 3778 | } |
| 3779 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3780 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3781 | dentry->d_name.name, dentry->d_name.len, 0); |
| 3782 | if (IS_ERR(di)) { |
| 3783 | err = PTR_ERR(di); |
| 3784 | goto out; |
| 3785 | } |
| 3786 | if (di) { |
| 3787 | if (check_path_shared(root, path)) |
| 3788 | goto out; |
| 3789 | } else { |
| 3790 | err = 0; |
| 3791 | goto out; |
| 3792 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3793 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3794 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 3795 | ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name, |
| 3796 | dentry->d_name.len, ino, dir_ino, 0, |
| 3797 | &index); |
| 3798 | if (ret) { |
| 3799 | err = ret; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3800 | goto out; |
| 3801 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 3802 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3803 | if (check_path_shared(root, path)) |
| 3804 | goto out; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 3805 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3806 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3807 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3808 | /* |
| 3809 | * This is a commit root search, if we can lookup inode item and other |
| 3810 | * relative items in the commit root, it means the transaction of |
| 3811 | * dir/file creation has been committed, and the dir index item that we |
| 3812 | * delay to insert has also been inserted into the commit root. So |
| 3813 | * we needn't worry about the delayed insertion of the dir index item |
| 3814 | * here. |
| 3815 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3816 | di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3817 | dentry->d_name.name, dentry->d_name.len, 0); |
| 3818 | if (IS_ERR(di)) { |
| 3819 | err = PTR_ERR(di); |
| 3820 | goto out; |
| 3821 | } |
| 3822 | BUG_ON(ret == -ENOENT); |
| 3823 | if (check_path_shared(root, path)) |
| 3824 | goto out; |
| 3825 | |
| 3826 | err = 0; |
| 3827 | out: |
| 3828 | btrfs_free_path(path); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 3829 | /* Migrate the orphan reservation over */ |
| 3830 | if (!err) |
| 3831 | err = btrfs_block_rsv_migrate(trans->block_rsv, |
| 3832 | &root->fs_info->global_block_rsv, |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3833 | trans->bytes_reserved); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 3834 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3835 | if (err) { |
| 3836 | btrfs_end_transaction(trans, root); |
| 3837 | root->fs_info->enospc_unlink = 0; |
| 3838 | return ERR_PTR(err); |
| 3839 | } |
| 3840 | |
| 3841 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 3842 | return trans; |
| 3843 | } |
| 3844 | |
| 3845 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 3846 | struct btrfs_root *root) |
| 3847 | { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3848 | if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) { |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3849 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 3850 | trans->bytes_reserved); |
| 3851 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3852 | BUG_ON(!root->fs_info->enospc_unlink); |
| 3853 | root->fs_info->enospc_unlink = 0; |
| 3854 | } |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 3855 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3856 | } |
| 3857 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3858 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3859 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3860 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3861 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3862 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3863 | int ret; |
| 3864 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3865 | trans = __unlink_start_trans(dir, dentry); |
| 3866 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3867 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3868 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3869 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3870 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3871 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3872 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3873 | if (ret) |
| 3874 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3875 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3876 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3877 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3878 | if (ret) |
| 3879 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3880 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3881 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3882 | out: |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3883 | __unlink_end_trans(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3884 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3885 | return ret; |
| 3886 | } |
| 3887 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3888 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3889 | struct btrfs_root *root, |
| 3890 | struct inode *dir, u64 objectid, |
| 3891 | const char *name, int name_len) |
| 3892 | { |
| 3893 | struct btrfs_path *path; |
| 3894 | struct extent_buffer *leaf; |
| 3895 | struct btrfs_dir_item *di; |
| 3896 | struct btrfs_key key; |
| 3897 | u64 index; |
| 3898 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3899 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3900 | |
| 3901 | path = btrfs_alloc_path(); |
| 3902 | if (!path) |
| 3903 | return -ENOMEM; |
| 3904 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3905 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3906 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3907 | if (IS_ERR_OR_NULL(di)) { |
| 3908 | if (!di) |
| 3909 | ret = -ENOENT; |
| 3910 | else |
| 3911 | ret = PTR_ERR(di); |
| 3912 | goto out; |
| 3913 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3914 | |
| 3915 | leaf = path->nodes[0]; |
| 3916 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3917 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3918 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3919 | if (ret) { |
| 3920 | btrfs_abort_transaction(trans, root, ret); |
| 3921 | goto out; |
| 3922 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3923 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3924 | |
| 3925 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3926 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3927 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3928 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3929 | if (ret != -ENOENT) { |
| 3930 | btrfs_abort_transaction(trans, root, ret); |
| 3931 | goto out; |
| 3932 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3933 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3934 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3935 | if (IS_ERR_OR_NULL(di)) { |
| 3936 | if (!di) |
| 3937 | ret = -ENOENT; |
| 3938 | else |
| 3939 | ret = PTR_ERR(di); |
| 3940 | btrfs_abort_transaction(trans, root, ret); |
| 3941 | goto out; |
| 3942 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3943 | |
| 3944 | leaf = path->nodes[0]; |
| 3945 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3946 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3947 | index = key.offset; |
| 3948 | } |
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 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3951 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3952 | if (ret) { |
| 3953 | btrfs_abort_transaction(trans, root, ret); |
| 3954 | goto out; |
| 3955 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3956 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3957 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3958 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3959 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3960 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3961 | if (ret) |
| 3962 | btrfs_abort_transaction(trans, root, ret); |
| 3963 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3964 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3965 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3966 | } |
| 3967 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3968 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3969 | { |
| 3970 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3971 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3972 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3973 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3974 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3975 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3976 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3977 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 3978 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3979 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3980 | trans = __unlink_start_trans(dir, dentry); |
| 3981 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3982 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3983 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3984 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3985 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3986 | BTRFS_I(inode)->location.objectid, |
| 3987 | dentry->d_name.name, |
| 3988 | dentry->d_name.len); |
| 3989 | goto out; |
| 3990 | } |
| 3991 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3992 | err = btrfs_orphan_add(trans, inode); |
| 3993 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3994 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3995 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3996 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3997 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3998 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3999 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4000 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4001 | out: |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4002 | __unlink_end_trans(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4003 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4004 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4005 | return err; |
| 4006 | } |
| 4007 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4008 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4009 | * this can truncate away extent items, csum items and directory items. |
| 4010 | * 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] | 4011 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4012 | * |
| 4013 | * csum items that cross the new i_size are truncated to the new size |
| 4014 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4015 | * |
| 4016 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4017 | * 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] | 4018 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4019 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4020 | struct btrfs_root *root, |
| 4021 | struct inode *inode, |
| 4022 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4023 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4024 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4025 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4026 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4027 | struct btrfs_key key; |
| 4028 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4029 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4030 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4031 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4032 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4033 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4034 | int found_extent; |
| 4035 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4036 | int pending_del_nr = 0; |
| 4037 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4038 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4039 | int ret; |
| 4040 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4041 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4042 | |
| 4043 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4044 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4045 | path = btrfs_alloc_path(); |
| 4046 | if (!path) |
| 4047 | return -ENOMEM; |
| 4048 | path->reada = -1; |
| 4049 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4050 | /* |
| 4051 | * We want to drop from the next block forward in case this new size is |
| 4052 | * not block aligned since we will be keeping the last block of the |
| 4053 | * extent just the way it is. |
| 4054 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4055 | if (root->ref_cows || root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4056 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4057 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4058 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4059 | /* |
| 4060 | * This function is also used to drop the items in the log tree before |
| 4061 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4062 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4063 | * items. |
| 4064 | */ |
| 4065 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4066 | btrfs_kill_delayed_inode_items(inode); |
| 4067 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4068 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4069 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4070 | key.type = (u8)-1; |
| 4071 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4072 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4073 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4074 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4075 | if (ret < 0) { |
| 4076 | err = ret; |
| 4077 | goto out; |
| 4078 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4079 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4080 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4081 | /* there are no items in the tree for us to truncate, we're |
| 4082 | * done |
| 4083 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4084 | if (path->slots[0] == 0) |
| 4085 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4086 | path->slots[0]--; |
| 4087 | } |
| 4088 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4089 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4090 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4091 | leaf = path->nodes[0]; |
| 4092 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4093 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4094 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4095 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4096 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4097 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4098 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4099 | break; |
| 4100 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4101 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4102 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4103 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4104 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4105 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4106 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4107 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4108 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4109 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4110 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4111 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4112 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4113 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4114 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4115 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4116 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4117 | } else { |
| 4118 | if (item_end < new_size) |
| 4119 | break; |
| 4120 | if (found_key.offset >= new_size) |
| 4121 | del_item = 1; |
| 4122 | else |
| 4123 | del_item = 0; |
| 4124 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4125 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4126 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4127 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4128 | goto delete; |
| 4129 | |
| 4130 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4131 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4132 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4133 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4134 | u64 orig_num_bytes = |
| 4135 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4136 | extent_num_bytes = ALIGN(new_size - |
| 4137 | found_key.offset, |
| 4138 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4139 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4140 | extent_num_bytes); |
| 4141 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4142 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4143 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4144 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4145 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4146 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4147 | extent_num_bytes = |
| 4148 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4149 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4150 | extent_offset = found_key.offset - |
| 4151 | btrfs_file_extent_offset(leaf, fi); |
| 4152 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4153 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4154 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4155 | if (extent_start != 0) { |
| 4156 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4157 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4158 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4159 | } |
| 4160 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4161 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4162 | /* |
| 4163 | * we can't truncate inline items that have had |
| 4164 | * special encodings |
| 4165 | */ |
| 4166 | if (!del_item && |
| 4167 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4168 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4169 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4170 | u32 size = new_size - found_key.offset; |
| 4171 | |
| 4172 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4173 | inode_sub_bytes(inode, item_end + 1 - |
| 4174 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4175 | } |
| 4176 | size = |
| 4177 | btrfs_file_extent_calc_inline_size(size); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4178 | btrfs_truncate_item(trans, root, path, |
| 4179 | size, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4180 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4181 | inode_sub_bytes(inode, item_end + 1 - |
| 4182 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4183 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4184 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4185 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4186 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4187 | if (!pending_del_nr) { |
| 4188 | /* no pending yet, add ourselves */ |
| 4189 | pending_del_slot = path->slots[0]; |
| 4190 | pending_del_nr = 1; |
| 4191 | } else if (pending_del_nr && |
| 4192 | path->slots[0] + 1 == pending_del_slot) { |
| 4193 | /* hop on the pending chunk */ |
| 4194 | pending_del_nr++; |
| 4195 | pending_del_slot = path->slots[0]; |
| 4196 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4197 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4198 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4199 | } else { |
| 4200 | break; |
| 4201 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4202 | if (found_extent && (root->ref_cows || |
| 4203 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4204 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4205 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4206 | extent_num_bytes, 0, |
| 4207 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4208 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4209 | BUG_ON(ret); |
| 4210 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4211 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4212 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4213 | break; |
| 4214 | |
| 4215 | if (path->slots[0] == 0 || |
| 4216 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4217 | if (pending_del_nr) { |
| 4218 | ret = btrfs_del_items(trans, root, path, |
| 4219 | pending_del_slot, |
| 4220 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4221 | if (ret) { |
| 4222 | btrfs_abort_transaction(trans, |
| 4223 | root, ret); |
| 4224 | goto error; |
| 4225 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4226 | pending_del_nr = 0; |
| 4227 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4228 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4229 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4230 | } else { |
| 4231 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4232 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4233 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4234 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4235 | if (pending_del_nr) { |
| 4236 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4237 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4238 | if (ret) |
| 4239 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4240 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4241 | error: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4242 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4243 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4244 | } |
| 4245 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4246 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4247 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4248 | * @inode - inode that we're zeroing |
| 4249 | * @from - the offset to start zeroing |
| 4250 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4251 | * offset |
| 4252 | * @front - zero up to the offset instead of from the offset on |
| 4253 | * |
| 4254 | * This will find the page for the "from" offset and cow the page and zero the |
| 4255 | * 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] | 4256 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4257 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4258 | int front) |
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 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4261 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4262 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4263 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4264 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4265 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4266 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4267 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4268 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4269 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4270 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4271 | int ret = 0; |
| 4272 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4273 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4274 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4275 | if ((offset & (blocksize - 1)) == 0 && |
| 4276 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4277 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4278 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4279 | if (ret) |
| 4280 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4281 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4282 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4283 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4284 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4285 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4286 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4287 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4288 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4289 | |
| 4290 | page_start = page_offset(page); |
| 4291 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4292 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4293 | if (!PageUptodate(page)) { |
| 4294 | ret = btrfs_readpage(NULL, page); |
| 4295 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4296 | if (page->mapping != mapping) { |
| 4297 | unlock_page(page); |
| 4298 | page_cache_release(page); |
| 4299 | goto again; |
| 4300 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4301 | if (!PageUptodate(page)) { |
| 4302 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4303 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4304 | } |
| 4305 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4306 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4307 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4308 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4309 | set_page_extent_mapped(page); |
| 4310 | |
| 4311 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4312 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4313 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4314 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4315 | unlock_page(page); |
| 4316 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4317 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4318 | btrfs_put_ordered_extent(ordered); |
| 4319 | goto again; |
| 4320 | } |
| 4321 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4322 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4323 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4324 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4325 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4326 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4327 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4328 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4329 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4330 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4331 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4332 | goto out_unlock; |
| 4333 | } |
| 4334 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4335 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4336 | if (!len) |
| 4337 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4338 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4339 | if (front) |
| 4340 | memset(kaddr, 0, offset); |
| 4341 | else |
| 4342 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4343 | flush_dcache_page(page); |
| 4344 | kunmap(page); |
| 4345 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4346 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4347 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4348 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4349 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4350 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4351 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4352 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4353 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4354 | unlock_page(page); |
| 4355 | page_cache_release(page); |
| 4356 | out: |
| 4357 | return ret; |
| 4358 | } |
| 4359 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4360 | /* |
| 4361 | * This function puts in dummy file extents for the area we're creating a hole |
| 4362 | * for. So if we are truncating this file to a larger size we need to insert |
| 4363 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4364 | * the range between oldsize and size |
| 4365 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4366 | 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] | 4367 | { |
| 4368 | struct btrfs_trans_handle *trans; |
| 4369 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4370 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4371 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4372 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4373 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4374 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4375 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4376 | u64 last_byte; |
| 4377 | u64 cur_offset; |
| 4378 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4379 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4380 | |
| 4381 | if (size <= hole_start) |
| 4382 | return 0; |
| 4383 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4384 | while (1) { |
| 4385 | struct btrfs_ordered_extent *ordered; |
| 4386 | btrfs_wait_ordered_range(inode, hole_start, |
| 4387 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4388 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4389 | &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4390 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 4391 | if (!ordered) |
| 4392 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4393 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4394 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4395 | btrfs_put_ordered_extent(ordered); |
| 4396 | } |
| 4397 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4398 | cur_offset = hole_start; |
| 4399 | while (1) { |
| 4400 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4401 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4402 | if (IS_ERR(em)) { |
| 4403 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4404 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4405 | break; |
| 4406 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4407 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4408 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4409 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4410 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4411 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4412 | |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 4413 | trans = btrfs_start_transaction(root, 3); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4414 | if (IS_ERR(trans)) { |
| 4415 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4416 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4417 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4418 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4419 | err = btrfs_drop_extents(trans, root, inode, |
| 4420 | cur_offset, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 4421 | cur_offset + hole_size, 1); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4422 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4423 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4424 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 4425 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4426 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4427 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4428 | err = btrfs_insert_file_extent(trans, root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4429 | btrfs_ino(inode), cur_offset, 0, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4430 | 0, hole_size, 0, hole_size, |
| 4431 | 0, 0, 0); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4432 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4433 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4434 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 4435 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4436 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4437 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4438 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4439 | cur_offset + hole_size - 1, 0); |
| 4440 | hole_em = alloc_extent_map(); |
| 4441 | if (!hole_em) { |
| 4442 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4443 | &BTRFS_I(inode)->runtime_flags); |
| 4444 | goto next; |
| 4445 | } |
| 4446 | hole_em->start = cur_offset; |
| 4447 | hole_em->len = hole_size; |
| 4448 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4449 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4450 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4451 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4452 | hole_em->orig_block_len = 0; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4453 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4454 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
| 4455 | hole_em->generation = trans->transid; |
| 4456 | |
| 4457 | while (1) { |
| 4458 | write_lock(&em_tree->lock); |
| 4459 | err = add_extent_mapping(em_tree, hole_em); |
| 4460 | if (!err) |
| 4461 | list_move(&hole_em->list, |
| 4462 | &em_tree->modified_extents); |
| 4463 | write_unlock(&em_tree->lock); |
| 4464 | if (err != -EEXIST) |
| 4465 | break; |
| 4466 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4467 | cur_offset + |
| 4468 | hole_size - 1, 0); |
| 4469 | } |
| 4470 | free_extent_map(hole_em); |
| 4471 | next: |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 4472 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4473 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4474 | } |
| 4475 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4476 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4477 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4478 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4479 | break; |
| 4480 | } |
| 4481 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4482 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4483 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4484 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4485 | return err; |
| 4486 | } |
| 4487 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4488 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4489 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4490 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4491 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4492 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4493 | loff_t newsize = attr->ia_size; |
| 4494 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4495 | int ret; |
| 4496 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4497 | if (newsize == oldsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4498 | return 0; |
| 4499 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4500 | /* |
| 4501 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4502 | * special case where we need to update the times despite not having |
| 4503 | * these flags set. For all other operations the VFS set these flags |
| 4504 | * explicitly if it wants a timestamp update. |
| 4505 | */ |
| 4506 | if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) |
| 4507 | inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb); |
| 4508 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4509 | if (newsize > oldsize) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4510 | truncate_pagecache(inode, oldsize, newsize); |
| 4511 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4512 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4513 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4514 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4515 | trans = btrfs_start_transaction(root, 1); |
| 4516 | if (IS_ERR(trans)) |
| 4517 | return PTR_ERR(trans); |
| 4518 | |
| 4519 | i_size_write(inode, newsize); |
| 4520 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4521 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4522 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4523 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4524 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4525 | /* |
| 4526 | * We're truncating a file that used to have good data down to |
| 4527 | * zero. Make sure it gets into the ordered flush list so that |
| 4528 | * any new writes get down to disk quickly. |
| 4529 | */ |
| 4530 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4531 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4532 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4533 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4534 | /* |
| 4535 | * 1 for the orphan item we're going to add |
| 4536 | * 1 for the orphan item deletion. |
| 4537 | */ |
| 4538 | trans = btrfs_start_transaction(root, 2); |
| 4539 | if (IS_ERR(trans)) |
| 4540 | return PTR_ERR(trans); |
| 4541 | |
| 4542 | /* |
| 4543 | * We need to do this in case we fail at _any_ point during the |
| 4544 | * actual truncate. Once we do the truncate_setsize we could |
| 4545 | * invalidate pages which forces any outstanding ordered io to |
| 4546 | * be instantly completed which will give us extents that need |
| 4547 | * to be truncated. If we fail to get an orphan inode down we |
| 4548 | * could have left over extents that were never meant to live, |
| 4549 | * so we need to garuntee from this point on that everything |
| 4550 | * will be consistent. |
| 4551 | */ |
| 4552 | ret = btrfs_orphan_add(trans, inode); |
| 4553 | btrfs_end_transaction(trans, root); |
| 4554 | if (ret) |
| 4555 | return ret; |
| 4556 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4557 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4558 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4559 | |
| 4560 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4561 | btrfs_inode_block_unlocked_dio(inode); |
| 4562 | inode_dio_wait(inode); |
| 4563 | btrfs_inode_resume_unlocked_dio(inode); |
| 4564 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4565 | ret = btrfs_truncate(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4566 | if (ret && inode->i_nlink) |
| 4567 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4570 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4573 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4574 | { |
| 4575 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4576 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4577 | int err; |
| 4578 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4579 | if (btrfs_root_readonly(root)) |
| 4580 | return -EROFS; |
| 4581 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4582 | err = inode_change_ok(inode, attr); |
| 4583 | if (err) |
| 4584 | return err; |
| 4585 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4586 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4587 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4588 | if (err) |
| 4589 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4590 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4591 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4592 | if (attr->ia_valid) { |
| 4593 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4594 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4595 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4596 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4597 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4598 | err = btrfs_acl_chmod(inode); |
| 4599 | } |
| 4600 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4601 | return err; |
| 4602 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4603 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4604 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4605 | { |
| 4606 | struct btrfs_trans_handle *trans; |
| 4607 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4608 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4609 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4610 | int ret; |
| 4611 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4612 | trace_btrfs_inode_evict(inode); |
| 4613 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4614 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4615 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 4616 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4617 | goto no_delete; |
| 4618 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4619 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4620 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4621 | goto no_delete; |
| 4622 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4623 | /* 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] | 4624 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4625 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4626 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4627 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4628 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4629 | goto no_delete; |
| 4630 | } |
| 4631 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4632 | if (inode->i_nlink > 0) { |
| 4633 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4634 | goto no_delete; |
| 4635 | } |
| 4636 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4637 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4638 | if (ret) { |
| 4639 | btrfs_orphan_del(NULL, inode); |
| 4640 | goto no_delete; |
| 4641 | } |
| 4642 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4643 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4644 | if (!rsv) { |
| 4645 | btrfs_orphan_del(NULL, inode); |
| 4646 | goto no_delete; |
| 4647 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4648 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4649 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4650 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4651 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4652 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4653 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4654 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4655 | * This is a bit simpler than btrfs_truncate since we've already |
| 4656 | * reserved our space for our orphan item in the unlink, so we just |
| 4657 | * need to reserve some slack space in case we add bytes and update |
| 4658 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4659 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4660 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4661 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4662 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4663 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4664 | /* |
| 4665 | * Try and steal from the global reserve since we will |
| 4666 | * likely not use this space anyway, we want to try as |
| 4667 | * hard as possible to get this to work. |
| 4668 | */ |
| 4669 | if (ret) |
| 4670 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4671 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4672 | if (ret) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4673 | printk(KERN_WARNING "Could not get space for a " |
Josef Bacik | 482e6dc | 2011-08-19 10:31:56 -0400 | [diff] [blame] | 4674 | "delete, will truncate on mount %d\n", ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4675 | btrfs_orphan_del(NULL, inode); |
| 4676 | btrfs_free_block_rsv(root, rsv); |
| 4677 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4678 | } |
| 4679 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4680 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4681 | if (IS_ERR(trans)) { |
| 4682 | btrfs_orphan_del(NULL, inode); |
| 4683 | btrfs_free_block_rsv(root, rsv); |
| 4684 | goto no_delete; |
| 4685 | } |
| 4686 | |
| 4687 | trans->block_rsv = rsv; |
| 4688 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4689 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4690 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4691 | break; |
| 4692 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4693 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4694 | btrfs_end_transaction(trans, root); |
| 4695 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4696 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4697 | } |
| 4698 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4699 | btrfs_free_block_rsv(root, rsv); |
| 4700 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4701 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4702 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4703 | ret = btrfs_orphan_del(trans, inode); |
| 4704 | BUG_ON(ret); |
| 4705 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4706 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4707 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4708 | if (!(root == root->fs_info->tree_root || |
| 4709 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4710 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4711 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4712 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4713 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4714 | no_delete: |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 4715 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4716 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4717 | } |
| 4718 | |
| 4719 | /* |
| 4720 | * this returns the key found in the dir entry in the location pointer. |
| 4721 | * If no dir entries were found, location->objectid is 0. |
| 4722 | */ |
| 4723 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 4724 | struct btrfs_key *location) |
| 4725 | { |
| 4726 | const char *name = dentry->d_name.name; |
| 4727 | int namelen = dentry->d_name.len; |
| 4728 | struct btrfs_dir_item *di; |
| 4729 | struct btrfs_path *path; |
| 4730 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4731 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4732 | |
| 4733 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4734 | if (!path) |
| 4735 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4736 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4737 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4738 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4739 | if (IS_ERR(di)) |
| 4740 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4741 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4742 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4743 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4744 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4745 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4746 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4747 | btrfs_free_path(path); |
| 4748 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4749 | out_err: |
| 4750 | location->objectid = 0; |
| 4751 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | /* |
| 4755 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 4756 | * needs to be changed to reflect the root directory of the tree root. This |
| 4757 | * is kind of like crossing a mount point. |
| 4758 | */ |
| 4759 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4760 | struct inode *dir, |
| 4761 | struct dentry *dentry, |
| 4762 | struct btrfs_key *location, |
| 4763 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4764 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4765 | struct btrfs_path *path; |
| 4766 | struct btrfs_root *new_root; |
| 4767 | struct btrfs_root_ref *ref; |
| 4768 | struct extent_buffer *leaf; |
| 4769 | int ret; |
| 4770 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4771 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4772 | path = btrfs_alloc_path(); |
| 4773 | if (!path) { |
| 4774 | err = -ENOMEM; |
| 4775 | goto out; |
| 4776 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4777 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4778 | err = -ENOENT; |
| 4779 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 4780 | BTRFS_I(dir)->root->root_key.objectid, |
| 4781 | location->objectid); |
| 4782 | if (ret) { |
| 4783 | if (ret < 0) |
| 4784 | err = ret; |
| 4785 | goto out; |
| 4786 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4787 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4788 | leaf = path->nodes[0]; |
| 4789 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4790 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4791 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 4792 | goto out; |
| 4793 | |
| 4794 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 4795 | (unsigned long)(ref + 1), |
| 4796 | dentry->d_name.len); |
| 4797 | if (ret) |
| 4798 | goto out; |
| 4799 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4800 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4801 | |
| 4802 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 4803 | if (IS_ERR(new_root)) { |
| 4804 | err = PTR_ERR(new_root); |
| 4805 | goto out; |
| 4806 | } |
| 4807 | |
| 4808 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 4809 | err = -ENOENT; |
| 4810 | goto out; |
| 4811 | } |
| 4812 | |
| 4813 | *sub_root = new_root; |
| 4814 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 4815 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4816 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4817 | err = 0; |
| 4818 | out: |
| 4819 | btrfs_free_path(path); |
| 4820 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4821 | } |
| 4822 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4823 | static void inode_tree_add(struct inode *inode) |
| 4824 | { |
| 4825 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4826 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4827 | struct rb_node **p; |
| 4828 | struct rb_node *parent; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4829 | u64 ino = btrfs_ino(inode); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4830 | again: |
| 4831 | p = &root->inode_tree.rb_node; |
| 4832 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4833 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 4834 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4835 | return; |
| 4836 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4837 | spin_lock(&root->inode_lock); |
| 4838 | while (*p) { |
| 4839 | parent = *p; |
| 4840 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 4841 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4842 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4843 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4844 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4845 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4846 | else { |
| 4847 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 4848 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4849 | rb_erase(parent, &root->inode_tree); |
| 4850 | RB_CLEAR_NODE(parent); |
| 4851 | spin_unlock(&root->inode_lock); |
| 4852 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4853 | } |
| 4854 | } |
| 4855 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 4856 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 4857 | spin_unlock(&root->inode_lock); |
| 4858 | } |
| 4859 | |
| 4860 | static void inode_tree_del(struct inode *inode) |
| 4861 | { |
| 4862 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4863 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4864 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4865 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4866 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4867 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4868 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4869 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4870 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4871 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4872 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4873 | /* |
| 4874 | * Free space cache has inodes in the tree root, but the tree root has a |
| 4875 | * root_refs of 0, so this could end up dropping the tree root as a |
| 4876 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 4877 | * make sure we don't drop it. |
| 4878 | */ |
| 4879 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 4880 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4881 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 4882 | spin_lock(&root->inode_lock); |
| 4883 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 4884 | spin_unlock(&root->inode_lock); |
| 4885 | if (empty) |
| 4886 | btrfs_add_dead_root(root); |
| 4887 | } |
| 4888 | } |
| 4889 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4890 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4891 | { |
| 4892 | struct rb_node *node; |
| 4893 | struct rb_node *prev; |
| 4894 | struct btrfs_inode *entry; |
| 4895 | struct inode *inode; |
| 4896 | u64 objectid = 0; |
| 4897 | |
| 4898 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4899 | |
| 4900 | spin_lock(&root->inode_lock); |
| 4901 | again: |
| 4902 | node = root->inode_tree.rb_node; |
| 4903 | prev = NULL; |
| 4904 | while (node) { |
| 4905 | prev = node; |
| 4906 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4907 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4908 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4909 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4910 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4911 | node = node->rb_right; |
| 4912 | else |
| 4913 | break; |
| 4914 | } |
| 4915 | if (!node) { |
| 4916 | while (prev) { |
| 4917 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4918 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4919 | node = prev; |
| 4920 | break; |
| 4921 | } |
| 4922 | prev = rb_next(prev); |
| 4923 | } |
| 4924 | } |
| 4925 | while (node) { |
| 4926 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4927 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4928 | inode = igrab(&entry->vfs_inode); |
| 4929 | if (inode) { |
| 4930 | spin_unlock(&root->inode_lock); |
| 4931 | if (atomic_read(&inode->i_count) > 1) |
| 4932 | d_prune_aliases(inode); |
| 4933 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4934 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4935 | * the inode cache when its usage count |
| 4936 | * hits zero. |
| 4937 | */ |
| 4938 | iput(inode); |
| 4939 | cond_resched(); |
| 4940 | spin_lock(&root->inode_lock); |
| 4941 | goto again; |
| 4942 | } |
| 4943 | |
| 4944 | if (cond_resched_lock(&root->inode_lock)) |
| 4945 | goto again; |
| 4946 | |
| 4947 | node = rb_next(node); |
| 4948 | } |
| 4949 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4950 | } |
| 4951 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4952 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4953 | { |
| 4954 | struct btrfs_iget_args *args = p; |
| 4955 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4956 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4957 | return 0; |
| 4958 | } |
| 4959 | |
| 4960 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4961 | { |
| 4962 | struct btrfs_iget_args *args = opaque; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4963 | return args->ino == btrfs_ino(inode) && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4964 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4965 | } |
| 4966 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4967 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4968 | u64 objectid, |
| 4969 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4970 | { |
| 4971 | struct inode *inode; |
| 4972 | struct btrfs_iget_args args; |
| 4973 | args.ino = objectid; |
| 4974 | args.root = root; |
| 4975 | |
| 4976 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 4977 | btrfs_init_locked_inode, |
| 4978 | (void *)&args); |
| 4979 | return inode; |
| 4980 | } |
| 4981 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4982 | /* Get an inode object given its location and corresponding root. |
| 4983 | * Returns in *is_new if the inode was read from disk |
| 4984 | */ |
| 4985 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4986 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4987 | { |
| 4988 | struct inode *inode; |
| 4989 | |
| 4990 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4991 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4992 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4993 | |
| 4994 | if (inode->i_state & I_NEW) { |
| 4995 | BTRFS_I(inode)->root = root; |
| 4996 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4997 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4998 | if (!is_bad_inode(inode)) { |
| 4999 | inode_tree_add(inode); |
| 5000 | unlock_new_inode(inode); |
| 5001 | if (new) |
| 5002 | *new = 1; |
| 5003 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5004 | unlock_new_inode(inode); |
| 5005 | iput(inode); |
| 5006 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5007 | } |
| 5008 | } |
| 5009 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5010 | return inode; |
| 5011 | } |
| 5012 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5013 | static struct inode *new_simple_dir(struct super_block *s, |
| 5014 | struct btrfs_key *key, |
| 5015 | struct btrfs_root *root) |
| 5016 | { |
| 5017 | struct inode *inode = new_inode(s); |
| 5018 | |
| 5019 | if (!inode) |
| 5020 | return ERR_PTR(-ENOMEM); |
| 5021 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5022 | BTRFS_I(inode)->root = root; |
| 5023 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5024 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5025 | |
| 5026 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5027 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5028 | inode->i_fop = &simple_dir_operations; |
| 5029 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 5030 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 5031 | |
| 5032 | return inode; |
| 5033 | } |
| 5034 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5035 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5036 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5037 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5038 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5039 | struct btrfs_root *sub_root = root; |
| 5040 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5041 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5042 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5043 | |
| 5044 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5045 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5046 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5047 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5048 | if (ret < 0) |
| 5049 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5050 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5051 | if (location.objectid == 0) |
| 5052 | return NULL; |
| 5053 | |
| 5054 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5055 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5056 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5057 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5058 | |
| 5059 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5060 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5061 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5062 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5063 | &location, &sub_root); |
| 5064 | if (ret < 0) { |
| 5065 | if (ret != -ENOENT) |
| 5066 | inode = ERR_PTR(ret); |
| 5067 | else |
| 5068 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5069 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5070 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5071 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5072 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5073 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5074 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5075 | down_read(&root->fs_info->cleanup_work_sem); |
| 5076 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5077 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5078 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5079 | if (ret) |
| 5080 | inode = ERR_PTR(ret); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5083 | return inode; |
| 5084 | } |
| 5085 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5086 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5087 | { |
| 5088 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5089 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5090 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5091 | if (!inode && !IS_ROOT(dentry)) |
| 5092 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5093 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5094 | if (inode) { |
| 5095 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5096 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5097 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5098 | |
| 5099 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5100 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5101 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5102 | return 0; |
| 5103 | } |
| 5104 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5105 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5106 | { |
| 5107 | if (dentry->d_fsdata) |
| 5108 | kfree(dentry->d_fsdata); |
| 5109 | } |
| 5110 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5111 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5112 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5113 | { |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5114 | struct dentry *ret; |
| 5115 | |
| 5116 | ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5117 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5118 | } |
| 5119 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5120 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5121 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5122 | }; |
| 5123 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 5124 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 5125 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5126 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 5127 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5128 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5129 | struct btrfs_item *item; |
| 5130 | struct btrfs_dir_item *di; |
| 5131 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5132 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5133 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5134 | struct list_head ins_list; |
| 5135 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5136 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5137 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5138 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5139 | unsigned char d_type; |
| 5140 | int over = 0; |
| 5141 | u32 di_cur; |
| 5142 | u32 di_total; |
| 5143 | u32 di_len; |
| 5144 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5145 | char tmp_name[32]; |
| 5146 | char *name_ptr; |
| 5147 | int name_len; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5148 | int is_curr = 0; /* filp->f_pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5149 | |
| 5150 | /* FIXME, use a real flag for deciding about the key type */ |
| 5151 | if (root->fs_info->tree_root == root) |
| 5152 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5153 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5154 | /* special case for "." */ |
| 5155 | if (filp->f_pos == 0) { |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 5156 | over = filldir(dirent, ".", 1, |
| 5157 | filp->f_pos, btrfs_ino(inode), DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5158 | if (over) |
| 5159 | return 0; |
| 5160 | filp->f_pos = 1; |
| 5161 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5162 | /* special case for .., just use the back ref */ |
| 5163 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 5164 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5165 | over = filldir(dirent, "..", 2, |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 5166 | filp->f_pos, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5167 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5168 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5169 | filp->f_pos = 2; |
| 5170 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5171 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5172 | if (!path) |
| 5173 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5174 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5175 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5176 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5177 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5178 | INIT_LIST_HEAD(&ins_list); |
| 5179 | INIT_LIST_HEAD(&del_list); |
| 5180 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5181 | } |
| 5182 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5183 | btrfs_set_key_type(&key, key_type); |
| 5184 | key.offset = filp->f_pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5185 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5186 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5187 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5188 | if (ret < 0) |
| 5189 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5190 | |
| 5191 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5192 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5193 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5194 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5195 | ret = btrfs_next_leaf(root, path); |
| 5196 | if (ret < 0) |
| 5197 | goto err; |
| 5198 | else if (ret > 0) |
| 5199 | break; |
| 5200 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5201 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5202 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5203 | item = btrfs_item_nr(leaf, slot); |
| 5204 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5205 | |
| 5206 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5207 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5208 | if (btrfs_key_type(&found_key) != key_type) |
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 (found_key.offset < filp->f_pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5211 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5212 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5213 | btrfs_should_delete_dir_index(&del_list, |
| 5214 | found_key.offset)) |
| 5215 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5216 | |
| 5217 | filp->f_pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5218 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5219 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5220 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5221 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5222 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5223 | |
| 5224 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5225 | struct btrfs_key location; |
| 5226 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5227 | if (verify_dir_item(root, leaf, di)) |
| 5228 | break; |
| 5229 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5230 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5231 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5232 | name_ptr = tmp_name; |
| 5233 | } else { |
| 5234 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5235 | if (!name_ptr) { |
| 5236 | ret = -ENOMEM; |
| 5237 | goto err; |
| 5238 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5239 | } |
| 5240 | read_extent_buffer(leaf, name_ptr, |
| 5241 | (unsigned long)(di + 1), name_len); |
| 5242 | |
| 5243 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5244 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5245 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5246 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5247 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5248 | * skip it. |
| 5249 | * |
| 5250 | * In contrast to old kernels, we insert the snapshot's |
| 5251 | * dir item and dir index after it has been created, so |
| 5252 | * we won't find a reference to our own snapshot. We |
| 5253 | * still keep the following code for backward |
| 5254 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5255 | */ |
| 5256 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5257 | location.objectid == root->root_key.objectid) { |
| 5258 | over = 0; |
| 5259 | goto skip; |
| 5260 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5261 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5262 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5263 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5264 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5265 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5266 | if (name_ptr != tmp_name) |
| 5267 | kfree(name_ptr); |
| 5268 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5269 | if (over) |
| 5270 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5271 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5272 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5273 | di_cur += di_len; |
| 5274 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5275 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5276 | next: |
| 5277 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5278 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5279 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5280 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5281 | if (is_curr) |
| 5282 | filp->f_pos++; |
| 5283 | ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir, |
| 5284 | &ins_list); |
| 5285 | if (ret) |
| 5286 | goto nopos; |
| 5287 | } |
| 5288 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5289 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 5290 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 5291 | /* |
| 5292 | * 32-bit glibc will use getdents64, but then strtol - |
| 5293 | * so the last number we can serve is this. |
| 5294 | */ |
| 5295 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 5296 | else |
| 5297 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5298 | nopos: |
| 5299 | ret = 0; |
| 5300 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5301 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5302 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5303 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5304 | return ret; |
| 5305 | } |
| 5306 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5307 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5308 | { |
| 5309 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5310 | struct btrfs_trans_handle *trans; |
| 5311 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5312 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5313 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5314 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5315 | return 0; |
| 5316 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5317 | 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] | 5318 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5319 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5320 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5321 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5322 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5323 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5324 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5325 | if (IS_ERR(trans)) |
| 5326 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5327 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5328 | } |
| 5329 | return ret; |
| 5330 | } |
| 5331 | |
| 5332 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5333 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5334 | * inode changes. But, it is most likely to find the inode in cache. |
| 5335 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5336 | * to keep or drop this code. |
| 5337 | */ |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5338 | int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5339 | { |
| 5340 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5341 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5342 | int ret; |
| 5343 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5344 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5345 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5346 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5347 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5348 | if (IS_ERR(trans)) |
| 5349 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5350 | |
| 5351 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5352 | if (ret && ret == -ENOSPC) { |
| 5353 | /* whoops, lets try again with the full transaction */ |
| 5354 | btrfs_end_transaction(trans, root); |
| 5355 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5356 | if (IS_ERR(trans)) |
| 5357 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5358 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5359 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5360 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5361 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5362 | if (BTRFS_I(inode)->delayed_node) |
| 5363 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5364 | |
| 5365 | return ret; |
| 5366 | } |
| 5367 | |
| 5368 | /* |
| 5369 | * This is a copy of file_update_time. We need this so we can return error on |
| 5370 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5371 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5372 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5373 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5374 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5375 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5376 | |
| 5377 | if (btrfs_root_readonly(root)) |
| 5378 | return -EROFS; |
| 5379 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5380 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5381 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5382 | if (flags & S_CTIME) |
| 5383 | inode->i_ctime = *now; |
| 5384 | if (flags & S_MTIME) |
| 5385 | inode->i_mtime = *now; |
| 5386 | if (flags & S_ATIME) |
| 5387 | inode->i_atime = *now; |
| 5388 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5389 | } |
| 5390 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5391 | /* |
| 5392 | * find the highest existing sequence number in a directory |
| 5393 | * and then set the in-memory index_cnt variable to reflect |
| 5394 | * free sequence numbers |
| 5395 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5396 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5397 | { |
| 5398 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5399 | struct btrfs_key key, found_key; |
| 5400 | struct btrfs_path *path; |
| 5401 | struct extent_buffer *leaf; |
| 5402 | int ret; |
| 5403 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5404 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5405 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 5406 | key.offset = (u64)-1; |
| 5407 | |
| 5408 | path = btrfs_alloc_path(); |
| 5409 | if (!path) |
| 5410 | return -ENOMEM; |
| 5411 | |
| 5412 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5413 | if (ret < 0) |
| 5414 | goto out; |
| 5415 | /* FIXME: we should be able to handle this */ |
| 5416 | if (ret == 0) |
| 5417 | goto out; |
| 5418 | ret = 0; |
| 5419 | |
| 5420 | /* |
| 5421 | * MAGIC NUMBER EXPLANATION: |
| 5422 | * since we search a directory based on f_pos we have to start at 2 |
| 5423 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5424 | * else has to start at 2 |
| 5425 | */ |
| 5426 | if (path->slots[0] == 0) { |
| 5427 | BTRFS_I(inode)->index_cnt = 2; |
| 5428 | goto out; |
| 5429 | } |
| 5430 | |
| 5431 | path->slots[0]--; |
| 5432 | |
| 5433 | leaf = path->nodes[0]; |
| 5434 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5435 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5436 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5437 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 5438 | BTRFS_I(inode)->index_cnt = 2; |
| 5439 | goto out; |
| 5440 | } |
| 5441 | |
| 5442 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5443 | out: |
| 5444 | btrfs_free_path(path); |
| 5445 | return ret; |
| 5446 | } |
| 5447 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5448 | /* |
| 5449 | * helper to find a free sequence number in a given directory. This current |
| 5450 | * code is very simple, later versions will do smarter things in the btree |
| 5451 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5452 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5453 | { |
| 5454 | int ret = 0; |
| 5455 | |
| 5456 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5457 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5458 | if (ret) { |
| 5459 | ret = btrfs_set_inode_index_count(dir); |
| 5460 | if (ret) |
| 5461 | return ret; |
| 5462 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5463 | } |
| 5464 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5465 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5466 | BTRFS_I(dir)->index_cnt++; |
| 5467 | |
| 5468 | return ret; |
| 5469 | } |
| 5470 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5471 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5472 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5473 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5474 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5475 | u64 ref_objectid, u64 objectid, |
| 5476 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5477 | { |
| 5478 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5479 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5480 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5481 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5482 | struct btrfs_inode_ref *ref; |
| 5483 | struct btrfs_key key[2]; |
| 5484 | u32 sizes[2]; |
| 5485 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5486 | int ret; |
| 5487 | int owner; |
| 5488 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5489 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5490 | if (!path) |
| 5491 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5492 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5493 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5494 | if (!inode) { |
| 5495 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5496 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5497 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5498 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5499 | /* |
| 5500 | * we have to initialize this early, so we can reclaim the inode |
| 5501 | * number if we fail afterwards in this function. |
| 5502 | */ |
| 5503 | inode->i_ino = objectid; |
| 5504 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5505 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5506 | trace_btrfs_inode_request(dir); |
| 5507 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5508 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5509 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5510 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5511 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5512 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5513 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5514 | } |
| 5515 | /* |
| 5516 | * index_cnt is ignored for everything but a dir, |
| 5517 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5518 | * number |
| 5519 | */ |
| 5520 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5521 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5522 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5523 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5524 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5525 | /* |
| 5526 | * We could have gotten an inode number from somebody who was fsynced |
| 5527 | * and then removed in this same transaction, so let's just set full |
| 5528 | * sync since it will be a full sync anyway and this will blow away the |
| 5529 | * old info in the log. |
| 5530 | */ |
| 5531 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5532 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5533 | if (S_ISDIR(mode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5534 | owner = 0; |
| 5535 | else |
| 5536 | owner = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5537 | |
| 5538 | key[0].objectid = objectid; |
| 5539 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 5540 | key[0].offset = 0; |
| 5541 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5542 | /* |
| 5543 | * Start new inodes with an inode_ref. This is slightly more |
| 5544 | * efficient for small numbers of hard links since they will |
| 5545 | * be packed into one item. Extended refs will kick in if we |
| 5546 | * add more hard links than can fit in the ref item. |
| 5547 | */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5548 | key[1].objectid = objectid; |
| 5549 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 5550 | key[1].offset = ref_objectid; |
| 5551 | |
| 5552 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 5553 | sizes[1] = name_len + sizeof(*ref); |
| 5554 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5555 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5556 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 5557 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5558 | goto fail; |
| 5559 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5560 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5561 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5562 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5563 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5564 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5565 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5566 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5567 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5568 | |
| 5569 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5570 | struct btrfs_inode_ref); |
| 5571 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5572 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5573 | ptr = (unsigned long)(ref + 1); |
| 5574 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5575 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5576 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5577 | btrfs_free_path(path); |
| 5578 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5579 | location = &BTRFS_I(inode)->location; |
| 5580 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5581 | location->offset = 0; |
| 5582 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 5583 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5584 | btrfs_inherit_iflags(inode, dir); |
| 5585 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5586 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5587 | if (btrfs_test_opt(root, NODATASUM)) |
| 5588 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5589 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5590 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5591 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5592 | } |
| 5593 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5594 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5595 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5596 | |
| 5597 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5598 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5599 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5600 | btrfs_update_root_times(trans, root); |
| 5601 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5602 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5603 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5604 | if (dir) |
| 5605 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5606 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5607 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5608 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5612 | { |
| 5613 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5614 | } |
| 5615 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5616 | /* |
| 5617 | * utility function to add 'inode' into 'parent_inode' with |
| 5618 | * a give name and a given sequence number. |
| 5619 | * if 'add_backref' is true, also insert a backref from the |
| 5620 | * inode to the parent directory. |
| 5621 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5622 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5623 | struct inode *parent_inode, struct inode *inode, |
| 5624 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5625 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5626 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5627 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5628 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5629 | u64 ino = btrfs_ino(inode); |
| 5630 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5631 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5632 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5633 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 5634 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5635 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5636 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 5637 | key.offset = 0; |
| 5638 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5639 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5640 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5641 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 5642 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5643 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5644 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5645 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 5646 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5647 | } |
| 5648 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5649 | /* Nothing to clean up yet */ |
| 5650 | if (ret) |
| 5651 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5652 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5653 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 5654 | parent_inode, &key, |
| 5655 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 5656 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5657 | goto fail_dir_item; |
| 5658 | else if (ret) { |
| 5659 | btrfs_abort_transaction(trans, root, ret); |
| 5660 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5661 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5662 | |
| 5663 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 5664 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5665 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5666 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 5667 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 5668 | if (ret) |
| 5669 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5670 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 5671 | |
| 5672 | fail_dir_item: |
| 5673 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5674 | u64 local_index; |
| 5675 | int err; |
| 5676 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 5677 | key.objectid, root->root_key.objectid, |
| 5678 | parent_ino, &local_index, name, name_len); |
| 5679 | |
| 5680 | } else if (add_backref) { |
| 5681 | u64 local_index; |
| 5682 | int err; |
| 5683 | |
| 5684 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 5685 | ino, parent_ino, &local_index); |
| 5686 | } |
| 5687 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5691 | struct inode *dir, struct dentry *dentry, |
| 5692 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5693 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5694 | int err = btrfs_add_link(trans, dir, inode, |
| 5695 | dentry->d_name.name, dentry->d_name.len, |
| 5696 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5697 | if (err > 0) |
| 5698 | err = -EEXIST; |
| 5699 | return err; |
| 5700 | } |
| 5701 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5702 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 5703 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5704 | { |
| 5705 | struct btrfs_trans_handle *trans; |
| 5706 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5707 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5708 | int err; |
| 5709 | int drop_inode = 0; |
| 5710 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5711 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5712 | |
| 5713 | if (!new_valid_dev(rdev)) |
| 5714 | return -EINVAL; |
| 5715 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5716 | /* |
| 5717 | * 2 for inode item and ref |
| 5718 | * 2 for dir items |
| 5719 | * 1 for xattr if selinux is on |
| 5720 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5721 | trans = btrfs_start_transaction(root, 5); |
| 5722 | if (IS_ERR(trans)) |
| 5723 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5724 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5725 | err = btrfs_find_free_ino(root, &objectid); |
| 5726 | if (err) |
| 5727 | goto out_unlock; |
| 5728 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5729 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5730 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5731 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5732 | if (IS_ERR(inode)) { |
| 5733 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5734 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5735 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5736 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5737 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5738 | if (err) { |
| 5739 | drop_inode = 1; |
| 5740 | goto out_unlock; |
| 5741 | } |
| 5742 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5743 | /* |
| 5744 | * If the active LSM wants to access the inode during |
| 5745 | * d_instantiate it needs these. Smack checks to see |
| 5746 | * if the filesystem supports xattrs by looking at the |
| 5747 | * ops vector. |
| 5748 | */ |
| 5749 | |
| 5750 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5751 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5752 | if (err) |
| 5753 | drop_inode = 1; |
| 5754 | else { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5755 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 5756 | btrfs_update_inode(trans, root, inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5757 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5758 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5759 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5760 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5761 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5762 | if (drop_inode) { |
| 5763 | inode_dec_link_count(inode); |
| 5764 | iput(inode); |
| 5765 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5766 | return err; |
| 5767 | } |
| 5768 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5769 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 5770 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5771 | { |
| 5772 | struct btrfs_trans_handle *trans; |
| 5773 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5774 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5775 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5776 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5777 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5778 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5779 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5780 | /* |
| 5781 | * 2 for inode item and ref |
| 5782 | * 2 for dir items |
| 5783 | * 1 for xattr if selinux is on |
| 5784 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5785 | trans = btrfs_start_transaction(root, 5); |
| 5786 | if (IS_ERR(trans)) |
| 5787 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5788 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5789 | err = btrfs_find_free_ino(root, &objectid); |
| 5790 | if (err) |
| 5791 | goto out_unlock; |
| 5792 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5793 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5794 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5795 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5796 | if (IS_ERR(inode)) { |
| 5797 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5798 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5799 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5800 | drop_inode_on_err = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5801 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5802 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5803 | if (err) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5804 | goto out_unlock; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5805 | |
Filipe Brandenburger | 9185aa5 | 2012-11-30 03:40:08 +0000 | [diff] [blame] | 5806 | err = btrfs_update_inode(trans, root, inode); |
| 5807 | if (err) |
| 5808 | goto out_unlock; |
| 5809 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5810 | /* |
| 5811 | * If the active LSM wants to access the inode during |
| 5812 | * d_instantiate it needs these. Smack checks to see |
| 5813 | * if the filesystem supports xattrs by looking at the |
| 5814 | * ops vector. |
| 5815 | */ |
| 5816 | inode->i_fop = &btrfs_file_operations; |
| 5817 | inode->i_op = &btrfs_file_inode_operations; |
| 5818 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5819 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5820 | if (err) |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5821 | goto out_unlock; |
| 5822 | |
| 5823 | inode->i_mapping->a_ops = &btrfs_aops; |
| 5824 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 5825 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 5826 | d_instantiate(dentry, inode); |
| 5827 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5828 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5829 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5830 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5831 | inode_dec_link_count(inode); |
| 5832 | iput(inode); |
| 5833 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5834 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5835 | return err; |
| 5836 | } |
| 5837 | |
| 5838 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 5839 | struct dentry *dentry) |
| 5840 | { |
| 5841 | struct btrfs_trans_handle *trans; |
| 5842 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5843 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5844 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5845 | int err; |
| 5846 | int drop_inode = 0; |
| 5847 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5848 | /* do not allow sys_link's with other subvols of the same device */ |
| 5849 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 5850 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5851 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5852 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 5853 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5854 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5855 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5856 | if (err) |
| 5857 | goto fail; |
| 5858 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5859 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5860 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5861 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5862 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5863 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5864 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5865 | if (IS_ERR(trans)) { |
| 5866 | err = PTR_ERR(trans); |
| 5867 | goto fail; |
| 5868 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5869 | |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5870 | btrfs_inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5871 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5872 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 5873 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 5874 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5875 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5876 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5877 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5878 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5879 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5880 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 5881 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5882 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5883 | if (err) |
| 5884 | goto fail; |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5885 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5886 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5887 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5888 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5889 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5890 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5891 | if (drop_inode) { |
| 5892 | inode_dec_link_count(inode); |
| 5893 | iput(inode); |
| 5894 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5895 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5896 | return err; |
| 5897 | } |
| 5898 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 5899 | 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] | 5900 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5901 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5902 | struct btrfs_trans_handle *trans; |
| 5903 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5904 | int err = 0; |
| 5905 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5906 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5907 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5908 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5909 | /* |
| 5910 | * 2 items for inode and ref |
| 5911 | * 2 items for dir items |
| 5912 | * 1 for xattr if selinux is on |
| 5913 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5914 | trans = btrfs_start_transaction(root, 5); |
| 5915 | if (IS_ERR(trans)) |
| 5916 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5917 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5918 | err = btrfs_find_free_ino(root, &objectid); |
| 5919 | if (err) |
| 5920 | goto out_fail; |
| 5921 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5922 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5923 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5924 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5925 | if (IS_ERR(inode)) { |
| 5926 | err = PTR_ERR(inode); |
| 5927 | goto out_fail; |
| 5928 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5929 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5930 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5931 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5932 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5933 | if (err) |
| 5934 | goto out_fail; |
| 5935 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5936 | inode->i_op = &btrfs_dir_inode_operations; |
| 5937 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5938 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5939 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5940 | err = btrfs_update_inode(trans, root, inode); |
| 5941 | if (err) |
| 5942 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5943 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5944 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 5945 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5946 | if (err) |
| 5947 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5948 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5949 | d_instantiate(dentry, inode); |
| 5950 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5951 | |
| 5952 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5953 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5954 | if (drop_on_err) |
| 5955 | iput(inode); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5956 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5957 | return err; |
| 5958 | } |
| 5959 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5960 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 5961 | * and an extent that you want to insert, deal with overlap and insert |
| 5962 | * the new extent into the tree. |
| 5963 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5964 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 5965 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5966 | struct extent_map *em, |
| 5967 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5968 | { |
| 5969 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5970 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5971 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 5972 | start_diff = map_start - em->start; |
| 5973 | em->start = map_start; |
| 5974 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5975 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 5976 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5977 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5978 | em->block_len -= start_diff; |
| 5979 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5980 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5981 | } |
| 5982 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5983 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 5984 | struct inode *inode, struct page *page, |
| 5985 | size_t pg_offset, u64 extent_offset, |
| 5986 | struct btrfs_file_extent_item *item) |
| 5987 | { |
| 5988 | int ret; |
| 5989 | struct extent_buffer *leaf = path->nodes[0]; |
| 5990 | char *tmp; |
| 5991 | size_t max_size; |
| 5992 | unsigned long inline_size; |
| 5993 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5994 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5995 | |
| 5996 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5997 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5998 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 5999 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 6000 | btrfs_item_nr(leaf, path->slots[0])); |
| 6001 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6002 | if (!tmp) |
| 6003 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6004 | ptr = btrfs_file_extent_inline_start(item); |
| 6005 | |
| 6006 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6007 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 6008 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6009 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6010 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6011 | if (ret) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6012 | char *kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6013 | unsigned long copy_size = min_t(u64, |
| 6014 | PAGE_CACHE_SIZE - pg_offset, |
| 6015 | max_size - extent_offset); |
| 6016 | memset(kaddr + pg_offset, 0, copy_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6017 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6018 | } |
| 6019 | kfree(tmp); |
| 6020 | return 0; |
| 6021 | } |
| 6022 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6023 | /* |
| 6024 | * 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] | 6025 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6026 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6027 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6028 | * |
| 6029 | * This also copies inline extents directly into the page. |
| 6030 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6031 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6032 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6033 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6034 | int create) |
| 6035 | { |
| 6036 | int ret; |
| 6037 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6038 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6039 | u64 extent_start = 0; |
| 6040 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6041 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6042 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6043 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6044 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6045 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6046 | struct extent_buffer *leaf; |
| 6047 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6048 | struct extent_map *em = NULL; |
| 6049 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6050 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6051 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6052 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6053 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6054 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6055 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6056 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6057 | if (em) |
| 6058 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6059 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6060 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6061 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6062 | if (em->start > start || em->start + em->len <= start) |
| 6063 | free_extent_map(em); |
| 6064 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6065 | free_extent_map(em); |
| 6066 | else |
| 6067 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6068 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6069 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6070 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6071 | err = -ENOMEM; |
| 6072 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6073 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6074 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6075 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6076 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6077 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6078 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6079 | |
| 6080 | if (!path) { |
| 6081 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6082 | if (!path) { |
| 6083 | err = -ENOMEM; |
| 6084 | goto out; |
| 6085 | } |
| 6086 | /* |
| 6087 | * Chances are we'll be called again, so go ahead and do |
| 6088 | * readahead |
| 6089 | */ |
| 6090 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6091 | } |
| 6092 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6093 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6094 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6095 | if (ret < 0) { |
| 6096 | err = ret; |
| 6097 | goto out; |
| 6098 | } |
| 6099 | |
| 6100 | if (ret != 0) { |
| 6101 | if (path->slots[0] == 0) |
| 6102 | goto not_found; |
| 6103 | path->slots[0]--; |
| 6104 | } |
| 6105 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6106 | leaf = path->nodes[0]; |
| 6107 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6108 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6109 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6110 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6111 | found_type = btrfs_key_type(&found_key); |
| 6112 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6113 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 6114 | goto not_found; |
| 6115 | } |
| 6116 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6117 | found_type = btrfs_file_extent_type(leaf, item); |
| 6118 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6119 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6120 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6121 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6122 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6123 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6124 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6125 | size_t size; |
| 6126 | size = btrfs_file_extent_inline_len(leaf, item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6127 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6128 | } |
| 6129 | |
| 6130 | if (start >= extent_end) { |
| 6131 | path->slots[0]++; |
| 6132 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6133 | ret = btrfs_next_leaf(root, path); |
| 6134 | if (ret < 0) { |
| 6135 | err = ret; |
| 6136 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6137 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6138 | if (ret > 0) |
| 6139 | goto not_found; |
| 6140 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6141 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6142 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6143 | if (found_key.objectid != objectid || |
| 6144 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6145 | goto not_found; |
| 6146 | if (start + len <= found_key.offset) |
| 6147 | goto not_found; |
| 6148 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6149 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6150 | em->len = found_key.offset - start; |
| 6151 | goto not_found_em; |
| 6152 | } |
| 6153 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6154 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6155 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6156 | em->start = extent_start; |
| 6157 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 6158 | em->orig_start = extent_start - |
| 6159 | btrfs_file_extent_offset(leaf, item); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6160 | em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 6161 | item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6162 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 6163 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6164 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6165 | goto insert; |
| 6166 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6167 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6168 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6169 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6170 | em->block_start = bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6171 | em->block_len = em->orig_block_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6172 | } else { |
| 6173 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 6174 | em->block_start = bytenr; |
| 6175 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6176 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 6177 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6178 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6179 | goto insert; |
| 6180 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6181 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6182 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6183 | size_t size; |
| 6184 | size_t extent_offset; |
| 6185 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6186 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6187 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6188 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6189 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6190 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6191 | goto out; |
| 6192 | } |
| 6193 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6194 | size = btrfs_file_extent_inline_len(leaf, item); |
| 6195 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6196 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6197 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6198 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6199 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6200 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6201 | em->orig_start = em->start; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6202 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6203 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6204 | em->compress_type = compress_type; |
| 6205 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6206 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6207 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6208 | if (btrfs_file_extent_compression(leaf, item) != |
| 6209 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6210 | ret = uncompress_inline(path, inode, page, |
| 6211 | pg_offset, |
| 6212 | extent_offset, item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6213 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6214 | } else { |
| 6215 | map = kmap(page); |
| 6216 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6217 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6218 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6219 | memset(map + pg_offset + copy_size, 0, |
| 6220 | PAGE_CACHE_SIZE - pg_offset - |
| 6221 | copy_size); |
| 6222 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6223 | kunmap(page); |
| 6224 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6225 | flush_dcache_page(page); |
| 6226 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6227 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6228 | if (!trans) { |
| 6229 | kunmap(page); |
| 6230 | free_extent_map(em); |
| 6231 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6232 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6233 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6234 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6235 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6236 | if (IS_ERR(trans)) |
| 6237 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6238 | goto again; |
| 6239 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6240 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6241 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6242 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6243 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6244 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6245 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6246 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6247 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6248 | goto insert; |
| 6249 | } else { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 6250 | WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6251 | } |
| 6252 | not_found: |
| 6253 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6254 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6255 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6256 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6257 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6258 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6259 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6260 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6261 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6262 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 6263 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 6264 | (unsigned long long)em->len, |
| 6265 | (unsigned long long)start, |
| 6266 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6267 | err = -EIO; |
| 6268 | goto out; |
| 6269 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6270 | |
| 6271 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6272 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6273 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6274 | /* it is possible that someone inserted the extent into the tree |
| 6275 | * while we had the lock dropped. It is also possible that |
| 6276 | * an overlapping map exists in the tree |
| 6277 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6278 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6279 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6280 | |
| 6281 | ret = 0; |
| 6282 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6283 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6284 | if (existing && (existing->start > start || |
| 6285 | existing->start + existing->len <= start)) { |
| 6286 | free_extent_map(existing); |
| 6287 | existing = NULL; |
| 6288 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6289 | if (!existing) { |
| 6290 | existing = lookup_extent_mapping(em_tree, em->start, |
| 6291 | em->len); |
| 6292 | if (existing) { |
| 6293 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6294 | em, start, |
| 6295 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6296 | free_extent_map(existing); |
| 6297 | if (err) { |
| 6298 | free_extent_map(em); |
| 6299 | em = NULL; |
| 6300 | } |
| 6301 | } else { |
| 6302 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6303 | free_extent_map(em); |
| 6304 | em = NULL; |
| 6305 | } |
| 6306 | } else { |
| 6307 | free_extent_map(em); |
| 6308 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6309 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6310 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6311 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6312 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6313 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6314 | |
Tsutomu Itoh | f0bd95e | 2012-10-01 03:08:37 -0600 | [diff] [blame] | 6315 | if (em) |
| 6316 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6317 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6318 | if (path) |
| 6319 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6320 | if (trans) { |
| 6321 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6322 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6323 | err = ret; |
| 6324 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6325 | if (err) { |
| 6326 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6327 | return ERR_PTR(err); |
| 6328 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6329 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6330 | return em; |
| 6331 | } |
| 6332 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6333 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6334 | size_t pg_offset, u64 start, u64 len, |
| 6335 | int create) |
| 6336 | { |
| 6337 | struct extent_map *em; |
| 6338 | struct extent_map *hole_em = NULL; |
| 6339 | u64 range_start = start; |
| 6340 | u64 end; |
| 6341 | u64 found; |
| 6342 | u64 found_end; |
| 6343 | int err = 0; |
| 6344 | |
| 6345 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6346 | if (IS_ERR(em)) |
| 6347 | return em; |
| 6348 | if (em) { |
| 6349 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6350 | * if our em maps to |
| 6351 | * - a hole or |
| 6352 | * - a pre-alloc extent, |
| 6353 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6354 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6355 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6356 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6357 | return em; |
| 6358 | else |
| 6359 | hole_em = em; |
| 6360 | } |
| 6361 | |
| 6362 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6363 | end = start + len; |
| 6364 | if (end < start) |
| 6365 | end = (u64)-1; |
| 6366 | else |
| 6367 | end -= 1; |
| 6368 | |
| 6369 | em = NULL; |
| 6370 | |
| 6371 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6372 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6373 | end, len, EXTENT_DELALLOC, 1); |
| 6374 | found_end = range_start + found; |
| 6375 | if (found_end < range_start) |
| 6376 | found_end = (u64)-1; |
| 6377 | |
| 6378 | /* |
| 6379 | * we didn't find anything useful, return |
| 6380 | * the original results from get_extent() |
| 6381 | */ |
| 6382 | if (range_start > end || found_end <= start) { |
| 6383 | em = hole_em; |
| 6384 | hole_em = NULL; |
| 6385 | goto out; |
| 6386 | } |
| 6387 | |
| 6388 | /* adjust the range_start to make sure it doesn't |
| 6389 | * go backwards from the start they passed in |
| 6390 | */ |
| 6391 | range_start = max(start,range_start); |
| 6392 | found = found_end - range_start; |
| 6393 | |
| 6394 | if (found > 0) { |
| 6395 | u64 hole_start = start; |
| 6396 | u64 hole_len = len; |
| 6397 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6398 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6399 | if (!em) { |
| 6400 | err = -ENOMEM; |
| 6401 | goto out; |
| 6402 | } |
| 6403 | /* |
| 6404 | * when btrfs_get_extent can't find anything it |
| 6405 | * returns one huge hole |
| 6406 | * |
| 6407 | * make sure what it found really fits our range, and |
| 6408 | * adjust to make sure it is based on the start from |
| 6409 | * the caller |
| 6410 | */ |
| 6411 | if (hole_em) { |
| 6412 | u64 calc_end = extent_map_end(hole_em); |
| 6413 | |
| 6414 | if (calc_end <= start || (hole_em->start > end)) { |
| 6415 | free_extent_map(hole_em); |
| 6416 | hole_em = NULL; |
| 6417 | } else { |
| 6418 | hole_start = max(hole_em->start, start); |
| 6419 | hole_len = calc_end - hole_start; |
| 6420 | } |
| 6421 | } |
| 6422 | em->bdev = NULL; |
| 6423 | if (hole_em && range_start > hole_start) { |
| 6424 | /* our hole starts before our delalloc, so we |
| 6425 | * have to return just the parts of the hole |
| 6426 | * that go until the delalloc starts |
| 6427 | */ |
| 6428 | em->len = min(hole_len, |
| 6429 | range_start - hole_start); |
| 6430 | em->start = hole_start; |
| 6431 | em->orig_start = hole_start; |
| 6432 | /* |
| 6433 | * don't adjust block start at all, |
| 6434 | * it is fixed at EXTENT_MAP_HOLE |
| 6435 | */ |
| 6436 | em->block_start = hole_em->block_start; |
| 6437 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6438 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6439 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6440 | } else { |
| 6441 | em->start = range_start; |
| 6442 | em->len = found; |
| 6443 | em->orig_start = range_start; |
| 6444 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6445 | em->block_len = found; |
| 6446 | } |
| 6447 | } else if (hole_em) { |
| 6448 | return hole_em; |
| 6449 | } |
| 6450 | out: |
| 6451 | |
| 6452 | free_extent_map(hole_em); |
| 6453 | if (err) { |
| 6454 | free_extent_map(em); |
| 6455 | return ERR_PTR(err); |
| 6456 | } |
| 6457 | return em; |
| 6458 | } |
| 6459 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6460 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6461 | u64 start, u64 len) |
| 6462 | { |
| 6463 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6464 | struct btrfs_trans_handle *trans; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6465 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6466 | struct btrfs_key ins; |
| 6467 | u64 alloc_hint; |
| 6468 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6469 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6470 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6471 | if (IS_ERR(trans)) |
| 6472 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6473 | |
| 6474 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 6475 | |
| 6476 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 6477 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 6478 | alloc_hint, &ins, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6479 | if (ret) { |
| 6480 | em = ERR_PTR(ret); |
| 6481 | goto out; |
| 6482 | } |
| 6483 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6484 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
| 6485 | ins.offset, ins.offset, 0); |
| 6486 | if (IS_ERR(em)) |
| 6487 | goto out; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6488 | |
| 6489 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6490 | ins.offset, ins.offset, 0); |
| 6491 | if (ret) { |
| 6492 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 6493 | em = ERR_PTR(ret); |
| 6494 | } |
| 6495 | out: |
| 6496 | btrfs_end_transaction(trans, root); |
| 6497 | return em; |
| 6498 | } |
| 6499 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6500 | /* |
| 6501 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6502 | * block must be cow'd |
| 6503 | */ |
| 6504 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 6505 | struct inode *inode, u64 offset, u64 len) |
| 6506 | { |
| 6507 | struct btrfs_path *path; |
| 6508 | int ret; |
| 6509 | struct extent_buffer *leaf; |
| 6510 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6511 | struct btrfs_file_extent_item *fi; |
| 6512 | struct btrfs_key key; |
| 6513 | u64 disk_bytenr; |
| 6514 | u64 backref_offset; |
| 6515 | u64 extent_end; |
| 6516 | u64 num_bytes; |
| 6517 | int slot; |
| 6518 | int found_type; |
| 6519 | |
| 6520 | path = btrfs_alloc_path(); |
| 6521 | if (!path) |
| 6522 | return -ENOMEM; |
| 6523 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6524 | ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6525 | offset, 0); |
| 6526 | if (ret < 0) |
| 6527 | goto out; |
| 6528 | |
| 6529 | slot = path->slots[0]; |
| 6530 | if (ret == 1) { |
| 6531 | if (slot == 0) { |
| 6532 | /* can't find the item, must cow */ |
| 6533 | ret = 0; |
| 6534 | goto out; |
| 6535 | } |
| 6536 | slot--; |
| 6537 | } |
| 6538 | ret = 0; |
| 6539 | leaf = path->nodes[0]; |
| 6540 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6541 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6542 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6543 | /* not our file or wrong item type, must cow */ |
| 6544 | goto out; |
| 6545 | } |
| 6546 | |
| 6547 | if (key.offset > offset) { |
| 6548 | /* Wrong offset, must cow */ |
| 6549 | goto out; |
| 6550 | } |
| 6551 | |
| 6552 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6553 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6554 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6555 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6556 | /* not a regular extent, must cow */ |
| 6557 | goto out; |
| 6558 | } |
| 6559 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6560 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6561 | |
| 6562 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 6563 | if (extent_end < offset + len) { |
| 6564 | /* extent doesn't include our full range, must cow */ |
| 6565 | goto out; |
| 6566 | } |
| 6567 | |
| 6568 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6569 | goto out; |
| 6570 | |
| 6571 | /* |
| 6572 | * look for other files referencing this extent, if we |
| 6573 | * find any we must cow |
| 6574 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6575 | if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6576 | key.offset - backref_offset, disk_bytenr)) |
| 6577 | goto out; |
| 6578 | |
| 6579 | /* |
| 6580 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 6581 | * in this extent we are about to write. If there |
| 6582 | * are any csums in that range we have to cow in order |
| 6583 | * to keep the csums correct |
| 6584 | */ |
| 6585 | disk_bytenr += backref_offset; |
| 6586 | disk_bytenr += offset - key.offset; |
| 6587 | num_bytes = min(offset + len, extent_end) - offset; |
| 6588 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 6589 | goto out; |
| 6590 | /* |
| 6591 | * all of the above have passed, it is safe to overwrite this extent |
| 6592 | * without cow |
| 6593 | */ |
| 6594 | ret = 1; |
| 6595 | out: |
| 6596 | btrfs_free_path(path); |
| 6597 | return ret; |
| 6598 | } |
| 6599 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6600 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 6601 | struct extent_state **cached_state, int writing) |
| 6602 | { |
| 6603 | struct btrfs_ordered_extent *ordered; |
| 6604 | int ret = 0; |
| 6605 | |
| 6606 | while (1) { |
| 6607 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6608 | 0, cached_state); |
| 6609 | /* |
| 6610 | * We're concerned with the entire range that we're going to be |
| 6611 | * doing DIO to, so we need to make sure theres no ordered |
| 6612 | * extents in this range. |
| 6613 | */ |
| 6614 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6615 | lockend - lockstart + 1); |
| 6616 | |
| 6617 | /* |
| 6618 | * We need to make sure there are no buffered pages in this |
| 6619 | * range either, we could have raced between the invalidate in |
| 6620 | * generic_file_direct_write and locking the extent. The |
| 6621 | * invalidate needs to happen so that reads after a write do not |
| 6622 | * get stale data. |
| 6623 | */ |
| 6624 | if (!ordered && (!writing || |
| 6625 | !test_range_bit(&BTRFS_I(inode)->io_tree, |
| 6626 | lockstart, lockend, EXTENT_UPTODATE, 0, |
| 6627 | *cached_state))) |
| 6628 | break; |
| 6629 | |
| 6630 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6631 | cached_state, GFP_NOFS); |
| 6632 | |
| 6633 | if (ordered) { |
| 6634 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6635 | btrfs_put_ordered_extent(ordered); |
| 6636 | } else { |
| 6637 | /* Screw you mmap */ |
| 6638 | ret = filemap_write_and_wait_range(inode->i_mapping, |
| 6639 | lockstart, |
| 6640 | lockend); |
| 6641 | if (ret) |
| 6642 | break; |
| 6643 | |
| 6644 | /* |
| 6645 | * If we found a page that couldn't be invalidated just |
| 6646 | * fall back to buffered. |
| 6647 | */ |
| 6648 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6649 | lockstart >> PAGE_CACHE_SHIFT, |
| 6650 | lockend >> PAGE_CACHE_SHIFT); |
| 6651 | if (ret) |
| 6652 | break; |
| 6653 | } |
| 6654 | |
| 6655 | cond_resched(); |
| 6656 | } |
| 6657 | |
| 6658 | return ret; |
| 6659 | } |
| 6660 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6661 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 6662 | u64 len, u64 orig_start, |
| 6663 | u64 block_start, u64 block_len, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6664 | u64 orig_block_len, int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6665 | { |
| 6666 | struct extent_map_tree *em_tree; |
| 6667 | struct extent_map *em; |
| 6668 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6669 | int ret; |
| 6670 | |
| 6671 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 6672 | em = alloc_extent_map(); |
| 6673 | if (!em) |
| 6674 | return ERR_PTR(-ENOMEM); |
| 6675 | |
| 6676 | em->start = start; |
| 6677 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 6678 | em->mod_start = start; |
| 6679 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6680 | em->len = len; |
| 6681 | em->block_len = block_len; |
| 6682 | em->block_start = block_start; |
| 6683 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6684 | em->orig_block_len = orig_block_len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6685 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6686 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6687 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 6688 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6689 | |
| 6690 | do { |
| 6691 | btrfs_drop_extent_cache(inode, em->start, |
| 6692 | em->start + em->len - 1, 0); |
| 6693 | write_lock(&em_tree->lock); |
| 6694 | ret = add_extent_mapping(em_tree, em); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6695 | if (!ret) |
| 6696 | list_move(&em->list, |
| 6697 | &em_tree->modified_extents); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6698 | write_unlock(&em_tree->lock); |
| 6699 | } while (ret == -EEXIST); |
| 6700 | |
| 6701 | if (ret) { |
| 6702 | free_extent_map(em); |
| 6703 | return ERR_PTR(ret); |
| 6704 | } |
| 6705 | |
| 6706 | return em; |
| 6707 | } |
| 6708 | |
| 6709 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6710 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 6711 | struct buffer_head *bh_result, int create) |
| 6712 | { |
| 6713 | struct extent_map *em; |
| 6714 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6715 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6716 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6717 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6718 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6719 | struct btrfs_trans_handle *trans; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6720 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6721 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6722 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6723 | if (create) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6724 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6725 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6726 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6727 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6728 | lockstart = start; |
| 6729 | lockend = start + len - 1; |
| 6730 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6731 | /* |
| 6732 | * If this errors out it's because we couldn't invalidate pagecache for |
| 6733 | * this range and we need to fallback to buffered. |
| 6734 | */ |
| 6735 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 6736 | return -ENOTBLK; |
| 6737 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6738 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6739 | if (IS_ERR(em)) { |
| 6740 | ret = PTR_ERR(em); |
| 6741 | goto unlock_err; |
| 6742 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6743 | |
| 6744 | /* |
| 6745 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 6746 | * io. INLINE is special, and we could probably kludge it in here, but |
| 6747 | * it's still buffered so for safety lets just fall back to the generic |
| 6748 | * buffered path. |
| 6749 | * |
| 6750 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 6751 | * decompress it, so there will be buffering required no matter what we |
| 6752 | * do, so go ahead and fallback to buffered. |
| 6753 | * |
| 6754 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 6755 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 6756 | * the generic code. |
| 6757 | */ |
| 6758 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 6759 | em->block_start == EXTENT_MAP_INLINE) { |
| 6760 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6761 | ret = -ENOTBLK; |
| 6762 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6763 | } |
| 6764 | |
| 6765 | /* Just a good old fashioned hole, return */ |
| 6766 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 6767 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 6768 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6769 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6770 | } |
| 6771 | |
| 6772 | /* |
| 6773 | * We don't allocate a new extent in the following cases |
| 6774 | * |
| 6775 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 6776 | * existing extent. |
| 6777 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 6778 | * just use the extent. |
| 6779 | * |
| 6780 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6781 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6782 | len = min(len, em->len - (start - em->start)); |
| 6783 | lockstart = start + len; |
| 6784 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6785 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6786 | |
| 6787 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 6788 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 6789 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6790 | int type; |
| 6791 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6792 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6793 | |
| 6794 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6795 | type = BTRFS_ORDERED_PREALLOC; |
| 6796 | else |
| 6797 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6798 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6799 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6800 | |
| 6801 | /* |
| 6802 | * we're not going to log anything, but we do need |
| 6803 | * to make sure the current transaction stays open |
| 6804 | * while we look for nocow cross refs |
| 6805 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6806 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6807 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6808 | goto must_cow; |
| 6809 | |
| 6810 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6811 | u64 orig_start = em->orig_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6812 | u64 orig_block_len = em->orig_block_len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6813 | |
| 6814 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 6815 | free_extent_map(em); |
| 6816 | em = create_pinned_em(inode, start, len, |
| 6817 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6818 | block_start, len, |
| 6819 | orig_block_len, type); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6820 | if (IS_ERR(em)) { |
| 6821 | btrfs_end_transaction(trans, root); |
| 6822 | goto unlock_err; |
| 6823 | } |
| 6824 | } |
| 6825 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6826 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 6827 | block_start, len, len, type); |
| 6828 | btrfs_end_transaction(trans, root); |
| 6829 | if (ret) { |
| 6830 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6831 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6832 | } |
| 6833 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6834 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6835 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6836 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6837 | must_cow: |
| 6838 | /* |
| 6839 | * this will cow the extent, reset the len in case we changed |
| 6840 | * it above |
| 6841 | */ |
| 6842 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6843 | free_extent_map(em); |
| 6844 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6845 | if (IS_ERR(em)) { |
| 6846 | ret = PTR_ERR(em); |
| 6847 | goto unlock_err; |
| 6848 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6849 | len = min(len, em->len - (start - em->start)); |
| 6850 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6851 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 6852 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6853 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6854 | bh_result->b_bdev = em->bdev; |
| 6855 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6856 | if (create) { |
| 6857 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6858 | set_buffer_new(bh_result); |
| 6859 | |
| 6860 | /* |
| 6861 | * Need to update the i_size under the extent lock so buffered |
| 6862 | * readers will get the updated i_size when we unlock. |
| 6863 | */ |
| 6864 | if (start + len > i_size_read(inode)) |
| 6865 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6866 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6867 | spin_lock(&BTRFS_I(inode)->lock); |
| 6868 | BTRFS_I(inode)->outstanding_extents++; |
| 6869 | spin_unlock(&BTRFS_I(inode)->lock); |
| 6870 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6871 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6872 | lockstart + len - 1, EXTENT_DELALLOC, NULL, |
| 6873 | &cached_state, GFP_NOFS); |
| 6874 | BUG_ON(ret); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6875 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6876 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6877 | /* |
| 6878 | * In the case of write we need to clear and unlock the entire range, |
| 6879 | * in the case of read we need to unlock only the end area that we |
| 6880 | * aren't using if there is any left over space. |
| 6881 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6882 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6883 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6884 | lockend, unlock_bits, 1, 0, |
| 6885 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6886 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6887 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6888 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6889 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6890 | free_extent_map(em); |
| 6891 | |
| 6892 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6893 | |
| 6894 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6895 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6896 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 6897 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6898 | } |
| 6899 | |
| 6900 | struct btrfs_dio_private { |
| 6901 | struct inode *inode; |
| 6902 | u64 logical_offset; |
| 6903 | u64 disk_bytenr; |
| 6904 | u64 bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6905 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6906 | |
| 6907 | /* number of bios pending for this dio */ |
| 6908 | atomic_t pending_bios; |
| 6909 | |
| 6910 | /* IO errors */ |
| 6911 | int errors; |
| 6912 | |
| 6913 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6914 | }; |
| 6915 | |
| 6916 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 6917 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6918 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6919 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 6920 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6921 | struct inode *inode = dip->inode; |
| 6922 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6923 | u64 start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6924 | |
| 6925 | start = dip->logical_offset; |
| 6926 | do { |
| 6927 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 6928 | struct page *page = bvec->bv_page; |
| 6929 | char *kaddr; |
| 6930 | u32 csum = ~(u32)0; |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6931 | u64 private = ~(u32)0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6932 | unsigned long flags; |
| 6933 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6934 | if (get_state_private(&BTRFS_I(inode)->io_tree, |
| 6935 | start, &private)) |
| 6936 | goto failed; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6937 | local_irq_save(flags); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6938 | kaddr = kmap_atomic(page); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6939 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 6940 | csum, bvec->bv_len); |
| 6941 | btrfs_csum_final(csum, (char *)&csum); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6942 | kunmap_atomic(kaddr); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6943 | local_irq_restore(flags); |
| 6944 | |
| 6945 | flush_dcache_page(bvec->bv_page); |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6946 | if (csum != private) { |
| 6947 | failed: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6948 | printk(KERN_ERR "btrfs csum failed ino %llu off" |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6949 | " %llu csum %u private %u\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6950 | (unsigned long long)btrfs_ino(inode), |
| 6951 | (unsigned long long)start, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6952 | csum, (unsigned)private); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6953 | err = -EIO; |
| 6954 | } |
| 6955 | } |
| 6956 | |
| 6957 | start += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6958 | bvec++; |
| 6959 | } while (bvec <= bvec_end); |
| 6960 | |
| 6961 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6962 | dip->logical_offset + dip->bytes - 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6963 | bio->bi_private = dip->private; |
| 6964 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6965 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6966 | |
| 6967 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 6968 | if (err) |
| 6969 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6970 | dio_end_io(bio, err); |
| 6971 | } |
| 6972 | |
| 6973 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 6974 | { |
| 6975 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6976 | struct inode *inode = dip->inode; |
| 6977 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6978 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6979 | u64 ordered_offset = dip->logical_offset; |
| 6980 | u64 ordered_bytes = dip->bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6981 | int ret; |
| 6982 | |
| 6983 | if (err) |
| 6984 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6985 | again: |
| 6986 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 6987 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6988 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6989 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6990 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6991 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6992 | ordered->work.func = finish_ordered_fn; |
| 6993 | ordered->work.flags = 0; |
| 6994 | btrfs_queue_worker(&root->fs_info->endio_write_workers, |
| 6995 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6996 | out_test: |
| 6997 | /* |
| 6998 | * our bio might span multiple ordered extents. If we haven't |
| 6999 | * completed the accounting for the whole dio, go back and try again |
| 7000 | */ |
| 7001 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 7002 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 7003 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7004 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7005 | goto again; |
| 7006 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7007 | out_done: |
| 7008 | bio->bi_private = dip->private; |
| 7009 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7010 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7011 | |
| 7012 | /* If we had an error make sure to clear the uptodate flag */ |
| 7013 | if (err) |
| 7014 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7015 | dio_end_io(bio, err); |
| 7016 | } |
| 7017 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7018 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 7019 | struct bio *bio, int mirror_num, |
| 7020 | unsigned long bio_flags, u64 offset) |
| 7021 | { |
| 7022 | int ret; |
| 7023 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7024 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7025 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7026 | return 0; |
| 7027 | } |
| 7028 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7029 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 7030 | { |
| 7031 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7032 | |
| 7033 | if (err) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7034 | printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 7035 | "sector %#Lx len %u err no %d\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7036 | (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw, |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 7037 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7038 | dip->errors = 1; |
| 7039 | |
| 7040 | /* |
| 7041 | * before atomic variable goto zero, we must make sure |
| 7042 | * dip->errors is perceived to be set. |
| 7043 | */ |
| 7044 | smp_mb__before_atomic_dec(); |
| 7045 | } |
| 7046 | |
| 7047 | /* if there are more bios still pending for this dio, just exit */ |
| 7048 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 7049 | goto out; |
| 7050 | |
| 7051 | if (dip->errors) |
| 7052 | bio_io_error(dip->orig_bio); |
| 7053 | else { |
| 7054 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 7055 | bio_endio(dip->orig_bio, 0); |
| 7056 | } |
| 7057 | out: |
| 7058 | bio_put(bio); |
| 7059 | } |
| 7060 | |
| 7061 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 7062 | u64 first_sector, gfp_t gfp_flags) |
| 7063 | { |
| 7064 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 7065 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 7066 | } |
| 7067 | |
| 7068 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 7069 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7070 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7071 | { |
| 7072 | int write = rw & REQ_WRITE; |
| 7073 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7074 | int ret; |
| 7075 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7076 | if (async_submit) |
| 7077 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7078 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7079 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7080 | |
| 7081 | if (!write) { |
| 7082 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 7083 | if (ret) |
| 7084 | goto err; |
| 7085 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7086 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7087 | if (skip_sum) |
| 7088 | goto map; |
| 7089 | |
| 7090 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7091 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 7092 | inode, rw, bio, 0, 0, |
| 7093 | file_offset, |
| 7094 | __btrfs_submit_bio_start_direct_io, |
| 7095 | __btrfs_submit_bio_done); |
| 7096 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7097 | } else if (write) { |
| 7098 | /* |
| 7099 | * If we aren't doing async submit, calculate the csum of the |
| 7100 | * bio now. |
| 7101 | */ |
| 7102 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 7103 | if (ret) |
| 7104 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7105 | } else if (!skip_sum) { |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7106 | ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7107 | if (ret) |
| 7108 | goto err; |
| 7109 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7110 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7111 | map: |
| 7112 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7113 | err: |
| 7114 | bio_put(bio); |
| 7115 | return ret; |
| 7116 | } |
| 7117 | |
| 7118 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 7119 | int skip_sum) |
| 7120 | { |
| 7121 | struct inode *inode = dip->inode; |
| 7122 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7123 | struct bio *bio; |
| 7124 | struct bio *orig_bio = dip->orig_bio; |
| 7125 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 7126 | u64 start_sector = orig_bio->bi_sector; |
| 7127 | u64 file_offset = dip->logical_offset; |
| 7128 | u64 submit_len = 0; |
| 7129 | u64 map_length; |
| 7130 | int nr_pages = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7131 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7132 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7133 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7134 | map_length = orig_bio->bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7135 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7136 | &map_length, NULL, 0); |
| 7137 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 7138 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7139 | return -EIO; |
| 7140 | } |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7141 | if (map_length >= orig_bio->bi_size) { |
| 7142 | bio = orig_bio; |
| 7143 | goto submit; |
| 7144 | } |
| 7145 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7146 | /* async crcs make it difficult to collect full stripe writes. */ |
| 7147 | if (btrfs_get_alloc_profile(root, 1) & |
| 7148 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) |
| 7149 | async_submit = 0; |
| 7150 | else |
| 7151 | async_submit = 1; |
| 7152 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7153 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7154 | if (!bio) |
| 7155 | return -ENOMEM; |
| 7156 | bio->bi_private = dip; |
| 7157 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7158 | atomic_inc(&dip->pending_bios); |
| 7159 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7160 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 7161 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 7162 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 7163 | bvec->bv_offset) < bvec->bv_len)) { |
| 7164 | /* |
| 7165 | * inc the count before we submit the bio so |
| 7166 | * we know the end IO handler won't happen before |
| 7167 | * we inc the count. Otherwise, the dip might get freed |
| 7168 | * before we're done setting it up |
| 7169 | */ |
| 7170 | atomic_inc(&dip->pending_bios); |
| 7171 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7172 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7173 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7174 | if (ret) { |
| 7175 | bio_put(bio); |
| 7176 | atomic_dec(&dip->pending_bios); |
| 7177 | goto out_err; |
| 7178 | } |
| 7179 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7180 | start_sector += submit_len >> 9; |
| 7181 | file_offset += submit_len; |
| 7182 | |
| 7183 | submit_len = 0; |
| 7184 | nr_pages = 0; |
| 7185 | |
| 7186 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7187 | start_sector, GFP_NOFS); |
| 7188 | if (!bio) |
| 7189 | goto out_err; |
| 7190 | bio->bi_private = dip; |
| 7191 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7192 | |
| 7193 | map_length = orig_bio->bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7194 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7195 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7196 | &map_length, NULL, 0); |
| 7197 | if (ret) { |
| 7198 | bio_put(bio); |
| 7199 | goto out_err; |
| 7200 | } |
| 7201 | } else { |
| 7202 | submit_len += bvec->bv_len; |
| 7203 | nr_pages ++; |
| 7204 | bvec++; |
| 7205 | } |
| 7206 | } |
| 7207 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7208 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7209 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7210 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7211 | if (!ret) |
| 7212 | return 0; |
| 7213 | |
| 7214 | bio_put(bio); |
| 7215 | out_err: |
| 7216 | dip->errors = 1; |
| 7217 | /* |
| 7218 | * before atomic variable goto zero, we must |
| 7219 | * make sure dip->errors is perceived to be set. |
| 7220 | */ |
| 7221 | smp_mb__before_atomic_dec(); |
| 7222 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 7223 | bio_io_error(dip->orig_bio); |
| 7224 | |
| 7225 | /* bio_end_io() will handle error, so we needn't return it */ |
| 7226 | return 0; |
| 7227 | } |
| 7228 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7229 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 7230 | loff_t file_offset) |
| 7231 | { |
| 7232 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7233 | struct btrfs_dio_private *dip; |
| 7234 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7235 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 7236 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7237 | int ret = 0; |
| 7238 | |
| 7239 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7240 | |
| 7241 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 7242 | if (!dip) { |
| 7243 | ret = -ENOMEM; |
| 7244 | goto free_ordered; |
| 7245 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7246 | |
| 7247 | dip->private = bio->bi_private; |
| 7248 | dip->inode = inode; |
| 7249 | dip->logical_offset = file_offset; |
| 7250 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7251 | dip->bytes = 0; |
| 7252 | do { |
| 7253 | dip->bytes += bvec->bv_len; |
| 7254 | bvec++; |
| 7255 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 7256 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7257 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7258 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7259 | dip->errors = 0; |
| 7260 | dip->orig_bio = bio; |
| 7261 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7262 | |
| 7263 | if (write) |
| 7264 | bio->bi_end_io = btrfs_endio_direct_write; |
| 7265 | else |
| 7266 | bio->bi_end_io = btrfs_endio_direct_read; |
| 7267 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7268 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 7269 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7270 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7271 | free_ordered: |
| 7272 | /* |
| 7273 | * If this is a write, we need to clean up the reserved space and kill |
| 7274 | * the ordered extent. |
| 7275 | */ |
| 7276 | if (write) { |
| 7277 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 7278 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7279 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 7280 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 7281 | btrfs_free_reserved_extent(root, ordered->start, |
| 7282 | ordered->disk_len); |
| 7283 | btrfs_put_ordered_extent(ordered); |
| 7284 | btrfs_put_ordered_extent(ordered); |
| 7285 | } |
| 7286 | bio_endio(bio, ret); |
| 7287 | } |
| 7288 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7289 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 7290 | const struct iovec *iov, loff_t offset, |
| 7291 | unsigned long nr_segs) |
| 7292 | { |
| 7293 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7294 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7295 | size_t size; |
| 7296 | unsigned long addr; |
| 7297 | unsigned blocksize_mask = root->sectorsize - 1; |
| 7298 | ssize_t retval = -EINVAL; |
| 7299 | loff_t end = offset; |
| 7300 | |
| 7301 | if (offset & blocksize_mask) |
| 7302 | goto out; |
| 7303 | |
| 7304 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 7305 | for (seg = 0; seg < nr_segs; seg++) { |
| 7306 | addr = (unsigned long)iov[seg].iov_base; |
| 7307 | size = iov[seg].iov_len; |
| 7308 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7309 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7310 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7311 | |
| 7312 | /* If this is a write we don't need to check anymore */ |
| 7313 | if (rw & WRITE) |
| 7314 | continue; |
| 7315 | |
| 7316 | /* |
| 7317 | * Check to make sure we don't have duplicate iov_base's in this |
| 7318 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 7319 | * when reading back. |
| 7320 | */ |
| 7321 | for (i = seg + 1; i < nr_segs; i++) { |
| 7322 | if (iov[seg].iov_base == iov[i].iov_base) |
| 7323 | goto out; |
| 7324 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7325 | } |
| 7326 | retval = 0; |
| 7327 | out: |
| 7328 | return retval; |
| 7329 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7330 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7331 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 7332 | const struct iovec *iov, loff_t offset, |
| 7333 | unsigned long nr_segs) |
| 7334 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7335 | struct file *file = iocb->ki_filp; |
| 7336 | struct inode *inode = file->f_mapping->host; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7337 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7338 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7339 | bool wakeup = true; |
| 7340 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7341 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7342 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7343 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7344 | offset, nr_segs)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7345 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7346 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7347 | atomic_inc(&inode->i_dio_count); |
| 7348 | smp_mb__after_atomic_inc(); |
| 7349 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7350 | if (rw & WRITE) { |
| 7351 | count = iov_length(iov, nr_segs); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7352 | /* |
| 7353 | * If the write DIO is beyond the EOF, we need update |
| 7354 | * the isize, but it is protected by i_mutex. So we can |
| 7355 | * not unlock the i_mutex at this case. |
| 7356 | */ |
| 7357 | if (offset + count <= inode->i_size) { |
| 7358 | mutex_unlock(&inode->i_mutex); |
| 7359 | relock = true; |
| 7360 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7361 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 7362 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7363 | goto out; |
| 7364 | } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 7365 | &BTRFS_I(inode)->runtime_flags))) { |
| 7366 | inode_dio_done(inode); |
| 7367 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 7368 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7369 | } |
| 7370 | |
| 7371 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 7372 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 7373 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7374 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7375 | if (rw & WRITE) { |
| 7376 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 7377 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7378 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7379 | btrfs_delalloc_release_space(inode, |
| 7380 | count - (size_t)ret); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7381 | else |
| 7382 | btrfs_delalloc_release_metadata(inode, 0); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7383 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7384 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7385 | if (wakeup) |
| 7386 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7387 | if (relock) |
| 7388 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7389 | |
| 7390 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7391 | } |
| 7392 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7393 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 7394 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7395 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 7396 | __u64 start, __u64 len) |
| 7397 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7398 | int ret; |
| 7399 | |
| 7400 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 7401 | if (ret) |
| 7402 | return ret; |
| 7403 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7404 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7405 | } |
| 7406 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7407 | int btrfs_readpage(struct file *file, struct page *page) |
| 7408 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7409 | struct extent_io_tree *tree; |
| 7410 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 7411 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7412 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7413 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7414 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 7415 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7416 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7417 | |
| 7418 | |
| 7419 | if (current->flags & PF_MEMALLOC) { |
| 7420 | redirty_page_for_writepage(wbc, page); |
| 7421 | unlock_page(page); |
| 7422 | return 0; |
| 7423 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7424 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7425 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 7426 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7427 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 7428 | int btrfs_writepages(struct address_space *mapping, |
| 7429 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7430 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7431 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7432 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7433 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7434 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 7435 | } |
| 7436 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7437 | static int |
| 7438 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 7439 | struct list_head *pages, unsigned nr_pages) |
| 7440 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7441 | struct extent_io_tree *tree; |
| 7442 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7443 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 7444 | btrfs_get_extent); |
| 7445 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7446 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7447 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7448 | struct extent_io_tree *tree; |
| 7449 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7450 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7451 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7452 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 7453 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7454 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7455 | if (ret == 1) { |
| 7456 | ClearPagePrivate(page); |
| 7457 | set_page_private(page, 0); |
| 7458 | page_cache_release(page); |
| 7459 | } |
| 7460 | return ret; |
| 7461 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7462 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7463 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 7464 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 7465 | if (PageWriteback(page) || PageDirty(page)) |
| 7466 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 7467 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7468 | } |
| 7469 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7470 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 7471 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7472 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7473 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7474 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7475 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7476 | u64 page_start = page_offset(page); |
| 7477 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7478 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7479 | /* |
| 7480 | * we have the page locked, so new writeback can't start, |
| 7481 | * and the dirty bit won't be cleared while we are here. |
| 7482 | * |
| 7483 | * Wait for IO on this page so that we can safely clear |
| 7484 | * the PagePrivate2 bit and do ordered accounting |
| 7485 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7486 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7487 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7488 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7489 | if (offset) { |
| 7490 | btrfs_releasepage(page, GFP_NOFS); |
| 7491 | return; |
| 7492 | } |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7493 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 7494 | ordered = btrfs_lookup_ordered_extent(inode, page_offset(page)); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7495 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7496 | /* |
| 7497 | * IO on this page will never be started, so we need |
| 7498 | * to account for any ordered extents now |
| 7499 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7500 | clear_extent_bit(tree, page_start, page_end, |
| 7501 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7502 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 7503 | EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7504 | /* |
| 7505 | * whoever cleared the private bit is responsible |
| 7506 | * for the finish_ordered_io |
| 7507 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7508 | if (TestClearPagePrivate2(page) && |
| 7509 | btrfs_dec_test_ordered_pending(inode, &ordered, page_start, |
| 7510 | PAGE_CACHE_SIZE, 1)) { |
| 7511 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7512 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7513 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7514 | cached_state = NULL; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7515 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7516 | } |
| 7517 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 7518 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7519 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 7520 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7521 | __btrfs_releasepage(page, GFP_NOFS); |
| 7522 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7523 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7524 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7525 | ClearPagePrivate(page); |
| 7526 | set_page_private(page, 0); |
| 7527 | page_cache_release(page); |
| 7528 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7529 | } |
| 7530 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7531 | /* |
| 7532 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 7533 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 7534 | * be careful to check for EOF conditions here. We set the page up correctly |
| 7535 | * for a written page which means we get ENOSPC checking when writing into |
| 7536 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 7537 | * support these features. |
| 7538 | * |
| 7539 | * We are not allowed to take the i_mutex here so we have to play games to |
| 7540 | * protect against truncate races as the page could now be beyond EOF. Because |
| 7541 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 7542 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 7543 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 7544 | * unlock the page. |
| 7545 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7546 | 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] | 7547 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7548 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 7549 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7550 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7551 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7552 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7553 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7554 | char *kaddr; |
| 7555 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7556 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7557 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7558 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7559 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7560 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7561 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7562 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7563 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7564 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 7565 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7566 | reserved = 1; |
| 7567 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7568 | if (ret) { |
| 7569 | if (ret == -ENOMEM) |
| 7570 | ret = VM_FAULT_OOM; |
| 7571 | else /* -ENOSPC, -EIO, etc */ |
| 7572 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7573 | if (reserved) |
| 7574 | goto out; |
| 7575 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7576 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7577 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7578 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7579 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7580 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7581 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7582 | page_start = page_offset(page); |
| 7583 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7584 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7585 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7586 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7587 | /* page got truncated out from underneath us */ |
| 7588 | goto out_unlock; |
| 7589 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7590 | wait_on_page_writeback(page); |
| 7591 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7592 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7593 | set_page_extent_mapped(page); |
| 7594 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7595 | /* |
| 7596 | * we can't set the delalloc bits if there are pending ordered |
| 7597 | * extents. Drop our locks and wait for them to finish |
| 7598 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7599 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 7600 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7601 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7602 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7603 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7604 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7605 | btrfs_put_ordered_extent(ordered); |
| 7606 | goto again; |
| 7607 | } |
| 7608 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7609 | /* |
| 7610 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 7611 | * if it was already dirty, so for space accounting reasons we need to |
| 7612 | * clear any delalloc bits for the range we are fixing to save. There |
| 7613 | * is probably a better way to do this, but for now keep consistent with |
| 7614 | * prepare_pages in the normal write path. |
| 7615 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7616 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7617 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7618 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7619 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7620 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7621 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 7622 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7623 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7624 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7625 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7626 | ret = VM_FAULT_SIGBUS; |
| 7627 | goto out_unlock; |
| 7628 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7629 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7630 | |
| 7631 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7632 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7633 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7634 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7635 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7636 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7637 | if (zero_start != PAGE_CACHE_SIZE) { |
| 7638 | kaddr = kmap(page); |
| 7639 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 7640 | flush_dcache_page(page); |
| 7641 | kunmap(page); |
| 7642 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7643 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7644 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7645 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7646 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7647 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 7648 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7649 | 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] | 7650 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7651 | 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] | 7652 | |
| 7653 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7654 | if (!ret) { |
| 7655 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7656 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7657 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7658 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7659 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7660 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7661 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7662 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7663 | return ret; |
| 7664 | } |
| 7665 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7666 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7667 | { |
| 7668 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7669 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7670 | int ret; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7671 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7672 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7673 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7674 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7675 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 7676 | ret = btrfs_truncate_page(inode, inode->i_size, 0, 0); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 7677 | if (ret) |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7678 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7679 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7680 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7681 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7682 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7683 | /* |
| 7684 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 7685 | * 3 things going on here |
| 7686 | * |
| 7687 | * 1) We need to reserve space for our orphan item and the space to |
| 7688 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 7689 | * orphan item because we didn't reserve space to remove it. |
| 7690 | * |
| 7691 | * 2) We need to reserve space to update our inode. |
| 7692 | * |
| 7693 | * 3) We need to have something to cache all the space that is going to |
| 7694 | * be free'd up by the truncate operation, but also have some slack |
| 7695 | * space reserved in case it uses space during the truncate (thank you |
| 7696 | * very much snapshotting). |
| 7697 | * |
| 7698 | * And we need these to all be seperate. The fact is we can use alot of |
| 7699 | * space doing the truncate, and we have no earthly idea how much space |
| 7700 | * we will use, so we need the truncate reservation to be seperate so it |
| 7701 | * doesn't end up using space reserved for updating the inode or |
| 7702 | * removing the orphan item. We also need to be able to stop the |
| 7703 | * transaction and start a new one, which means we need to be able to |
| 7704 | * update the inode several times, and we have no idea of knowing how |
| 7705 | * many times that will be, so we can't just reserve 1 item for the |
| 7706 | * entirety of the opration, so that has to be done seperately as well. |
| 7707 | * Then there is the orphan item, which does indeed need to be held on |
| 7708 | * to for the whole operation, and we need nobody to touch this reserved |
| 7709 | * space except the orphan code. |
| 7710 | * |
| 7711 | * So that leaves us with |
| 7712 | * |
| 7713 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 7714 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 7715 | * transaction reservation. |
| 7716 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 7717 | * updating the inode. |
| 7718 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 7719 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7720 | if (!rsv) |
| 7721 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 7722 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7723 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7724 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7725 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7726 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7727 | * 1 for updating the inode. |
| 7728 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 7729 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7730 | if (IS_ERR(trans)) { |
| 7731 | err = PTR_ERR(trans); |
| 7732 | goto out; |
| 7733 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7734 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7735 | /* Migrate the slack space for the truncate to our reserve */ |
| 7736 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 7737 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7738 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7739 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7740 | /* |
| 7741 | * setattr is responsible for setting the ordered_data_close flag, |
| 7742 | * but that is only tested during the last file release. That |
| 7743 | * could happen well after the next commit, leaving a great big |
| 7744 | * window where new writes may get lost if someone chooses to write |
| 7745 | * to this file after truncating to zero |
| 7746 | * |
| 7747 | * The inode doesn't have any dirty data here, and so if we commit |
| 7748 | * this is a noop. If someone immediately starts writing to the inode |
| 7749 | * it is very likely we'll catch some of their writes in this |
| 7750 | * transaction, and the commit will find this file on the ordered |
| 7751 | * data list with good things to send down. |
| 7752 | * |
| 7753 | * This is a best effort solution, there is still a window where |
| 7754 | * using truncate to replace the contents of the file will |
| 7755 | * end up with a zero length file after a crash. |
| 7756 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7757 | if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 7758 | &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7759 | btrfs_add_ordered_operation(trans, root, inode); |
| 7760 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7761 | /* |
| 7762 | * So if we truncate and then write and fsync we normally would just |
| 7763 | * write the extents that changed, which is a problem if we need to |
| 7764 | * first truncate that entire inode. So set this flag so we write out |
| 7765 | * all of the extents in the inode to the sync log so we're completely |
| 7766 | * safe. |
| 7767 | */ |
| 7768 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7769 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7770 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7771 | while (1) { |
| 7772 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 7773 | inode->i_size, |
| 7774 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7775 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7776 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7777 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7778 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7779 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7780 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7781 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7782 | if (ret) { |
| 7783 | err = ret; |
| 7784 | break; |
| 7785 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7786 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7787 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7788 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7789 | |
| 7790 | trans = btrfs_start_transaction(root, 2); |
| 7791 | if (IS_ERR(trans)) { |
| 7792 | ret = err = PTR_ERR(trans); |
| 7793 | trans = NULL; |
| 7794 | break; |
| 7795 | } |
| 7796 | |
| 7797 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 7798 | rsv, min_size); |
| 7799 | BUG_ON(ret); /* shouldn't happen */ |
| 7800 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7801 | } |
| 7802 | |
| 7803 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7804 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7805 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7806 | if (ret) |
| 7807 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7808 | } |
| 7809 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7810 | if (trans) { |
| 7811 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 7812 | ret = btrfs_update_inode(trans, root, inode); |
| 7813 | if (ret && !err) |
| 7814 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7815 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7816 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7817 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7818 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7819 | |
| 7820 | out: |
| 7821 | btrfs_free_block_rsv(root, rsv); |
| 7822 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7823 | if (ret && !err) |
| 7824 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7825 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7826 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7827 | } |
| 7828 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7829 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7830 | * create a new subvolume directory/inode (helper for the ioctl). |
| 7831 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7832 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7833 | struct btrfs_root *new_root, u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7834 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7835 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7836 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7837 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7838 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 7839 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 7840 | new_dirid, new_dirid, |
| 7841 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 7842 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7843 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 7844 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7845 | inode->i_op = &btrfs_dir_inode_operations; |
| 7846 | inode->i_fop = &btrfs_dir_file_operations; |
| 7847 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 7848 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7849 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7850 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7851 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 7852 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7853 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 7854 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7855 | } |
| 7856 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7857 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 7858 | { |
| 7859 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7860 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7861 | |
| 7862 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 7863 | if (!ei) |
| 7864 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7865 | |
| 7866 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7867 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 7868 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7869 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7870 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7871 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7872 | ei->disk_i_size = 0; |
| 7873 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7874 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7875 | ei->index_cnt = (u64)-1; |
| 7876 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7877 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7878 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7879 | spin_lock_init(&ei->lock); |
| 7880 | ei->outstanding_extents = 0; |
| 7881 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7882 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7883 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7884 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7885 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7886 | ei->delayed_node = NULL; |
| 7887 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7888 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 7889 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 7890 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 7891 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 7892 | ei->io_tree.track_uptodate = 1; |
| 7893 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7894 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7895 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 7896 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7897 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7898 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7899 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7900 | RB_CLEAR_NODE(&ei->rb_node); |
| 7901 | |
| 7902 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7903 | } |
| 7904 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7905 | static void btrfs_i_callback(struct rcu_head *head) |
| 7906 | { |
| 7907 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7908 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 7909 | } |
| 7910 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7911 | void btrfs_destroy_inode(struct inode *inode) |
| 7912 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7913 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7914 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7915 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 7916 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7917 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7918 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 7919 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7920 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 7921 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7922 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7923 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7924 | * This can happen where we create an inode, but somebody else also |
| 7925 | * created the same inode and we need to destroy the one we already |
| 7926 | * created. |
| 7927 | */ |
| 7928 | if (!root) |
| 7929 | goto free; |
| 7930 | |
| 7931 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7932 | * Make sure we're properly removed from the ordered operation |
| 7933 | * lists. |
| 7934 | */ |
| 7935 | smp_mb(); |
| 7936 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 7937 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 7938 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 7939 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 7940 | } |
| 7941 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7942 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 7943 | &BTRFS_I(inode)->runtime_flags)) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7944 | printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n", |
| 7945 | (unsigned long long)btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7946 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7947 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7948 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7949 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7950 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 7951 | if (!ordered) |
| 7952 | break; |
| 7953 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7954 | printk(KERN_ERR "btrfs found ordered " |
| 7955 | "extent %llu %llu on inode cleanup\n", |
| 7956 | (unsigned long long)ordered->file_offset, |
| 7957 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7958 | btrfs_remove_ordered_extent(inode, ordered); |
| 7959 | btrfs_put_ordered_extent(ordered); |
| 7960 | btrfs_put_ordered_extent(ordered); |
| 7961 | } |
| 7962 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7963 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7964 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7965 | free: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7966 | btrfs_remove_delayed_node(inode); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7967 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7968 | } |
| 7969 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7970 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7971 | { |
| 7972 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7973 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 7974 | /* the snap/subvol tree is on deleting */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7975 | if (btrfs_root_refs(&root->root_item) == 0 && |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 7976 | root != root->fs_info->tree_root) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7977 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7978 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7979 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7980 | } |
| 7981 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 7982 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7983 | { |
| 7984 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 7985 | |
| 7986 | inode_init_once(&ei->vfs_inode); |
| 7987 | } |
| 7988 | |
| 7989 | void btrfs_destroy_cachep(void) |
| 7990 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 7991 | /* |
| 7992 | * Make sure all delayed rcu free inodes are flushed before we |
| 7993 | * destroy cache. |
| 7994 | */ |
| 7995 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7996 | if (btrfs_inode_cachep) |
| 7997 | kmem_cache_destroy(btrfs_inode_cachep); |
| 7998 | if (btrfs_trans_handle_cachep) |
| 7999 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 8000 | if (btrfs_transaction_cachep) |
| 8001 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8002 | if (btrfs_path_cachep) |
| 8003 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8004 | if (btrfs_free_space_cachep) |
| 8005 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8006 | if (btrfs_delalloc_work_cachep) |
| 8007 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8008 | } |
| 8009 | |
| 8010 | int btrfs_init_cachep(void) |
| 8011 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8012 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8013 | sizeof(struct btrfs_inode), 0, |
| 8014 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8015 | if (!btrfs_inode_cachep) |
| 8016 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8017 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8018 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8019 | sizeof(struct btrfs_trans_handle), 0, |
| 8020 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8021 | if (!btrfs_trans_handle_cachep) |
| 8022 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8023 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8024 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8025 | sizeof(struct btrfs_transaction), 0, |
| 8026 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8027 | if (!btrfs_transaction_cachep) |
| 8028 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8029 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8030 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8031 | sizeof(struct btrfs_path), 0, |
| 8032 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8033 | if (!btrfs_path_cachep) |
| 8034 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8035 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8036 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8037 | sizeof(struct btrfs_free_space), 0, |
| 8038 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 8039 | if (!btrfs_free_space_cachep) |
| 8040 | goto fail; |
| 8041 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8042 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 8043 | sizeof(struct btrfs_delalloc_work), 0, |
| 8044 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 8045 | NULL); |
| 8046 | if (!btrfs_delalloc_work_cachep) |
| 8047 | goto fail; |
| 8048 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8049 | return 0; |
| 8050 | fail: |
| 8051 | btrfs_destroy_cachep(); |
| 8052 | return -ENOMEM; |
| 8053 | } |
| 8054 | |
| 8055 | static int btrfs_getattr(struct vfsmount *mnt, |
| 8056 | struct dentry *dentry, struct kstat *stat) |
| 8057 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8058 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8059 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8060 | u32 blocksize = inode->i_sb->s_blocksize; |
| 8061 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8062 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8063 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 8064 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8065 | |
| 8066 | spin_lock(&BTRFS_I(inode)->lock); |
| 8067 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 8068 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8069 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8070 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8071 | return 0; |
| 8072 | } |
| 8073 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8074 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 8075 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8076 | { |
| 8077 | struct btrfs_trans_handle *trans; |
| 8078 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8079 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8080 | struct inode *new_inode = new_dentry->d_inode; |
| 8081 | struct inode *old_inode = old_dentry->d_inode; |
| 8082 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8083 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8084 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8085 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8086 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8087 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8088 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8089 | return -EPERM; |
| 8090 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8091 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8092 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8093 | return -EXDEV; |
| 8094 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8095 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 8096 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8097 | return -ENOTEMPTY; |
| 8098 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8099 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8100 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8101 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8102 | |
| 8103 | |
| 8104 | /* check for collisions, even if the name isn't there */ |
| 8105 | ret = btrfs_check_dir_item_collision(root, new_dir->i_ino, |
| 8106 | new_dentry->d_name.name, |
| 8107 | new_dentry->d_name.len); |
| 8108 | |
| 8109 | if (ret) { |
| 8110 | if (ret == -EEXIST) { |
| 8111 | /* we shouldn't get |
| 8112 | * eexist without a new_inode */ |
| 8113 | if (!new_inode) { |
| 8114 | WARN_ON(1); |
| 8115 | return ret; |
| 8116 | } |
| 8117 | } else { |
| 8118 | /* maybe -EOVERFLOW */ |
| 8119 | return ret; |
| 8120 | } |
| 8121 | } |
| 8122 | ret = 0; |
| 8123 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8124 | /* |
| 8125 | * we're using rename to replace one file with another. |
| 8126 | * and the replacement file is large. Start IO on it now so |
| 8127 | * we don't add too much work to the end of the transaction |
| 8128 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 8129 | 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] | 8130 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 8131 | filemap_flush(old_inode->i_mapping); |
| 8132 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8133 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8134 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8135 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8136 | /* |
| 8137 | * We want to reserve the absolute worst case amount of items. So if |
| 8138 | * both inodes are subvols and we need to unlink them then that would |
| 8139 | * require 4 item modifications, but if they are both normal inodes it |
| 8140 | * would require 5 item modifications, so we'll assume their normal |
| 8141 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8142 | * should cover the worst case number of items we'll modify. |
| 8143 | */ |
| 8144 | trans = btrfs_start_transaction(root, 20); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8145 | if (IS_ERR(trans)) { |
| 8146 | ret = PTR_ERR(trans); |
| 8147 | goto out_notrans; |
| 8148 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8149 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8150 | if (dest != root) |
| 8151 | btrfs_record_root_in_trans(trans, dest); |
| 8152 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8153 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8154 | if (ret) |
| 8155 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8156 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8157 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8158 | /* force full log commit if subvolume involved. */ |
| 8159 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 8160 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8161 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8162 | new_dentry->d_name.name, |
| 8163 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8164 | old_ino, |
| 8165 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8166 | if (ret) |
| 8167 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8168 | /* |
| 8169 | * this is an ugly little race, but the rename is required |
| 8170 | * to make sure that if we crash, the inode is either at the |
| 8171 | * old name or the new one. pinning the log transaction lets |
| 8172 | * us make sure we don't allow a log commit to come in after |
| 8173 | * we unlink the name but before we add the new name back in. |
| 8174 | */ |
| 8175 | btrfs_pin_log_trans(root); |
| 8176 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8177 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8178 | * make sure the inode gets flushed if it is replacing |
| 8179 | * something. |
| 8180 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8181 | 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] | 8182 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8183 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8184 | inode_inc_iversion(old_dir); |
| 8185 | inode_inc_iversion(new_dir); |
| 8186 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8187 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8188 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8189 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8190 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8191 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 8192 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 8193 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8194 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8195 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 8196 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 8197 | old_dentry->d_name.name, |
| 8198 | old_dentry->d_name.len); |
| 8199 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 8200 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 8201 | old_dentry->d_inode, |
| 8202 | old_dentry->d_name.name, |
| 8203 | old_dentry->d_name.len); |
| 8204 | if (!ret) |
| 8205 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8206 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8207 | if (ret) { |
| 8208 | btrfs_abort_transaction(trans, root, ret); |
| 8209 | goto out_fail; |
| 8210 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8211 | |
| 8212 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8213 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8214 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8215 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8216 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 8217 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 8218 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 8219 | root_objectid, |
| 8220 | new_dentry->d_name.name, |
| 8221 | new_dentry->d_name.len); |
| 8222 | BUG_ON(new_inode->i_nlink == 0); |
| 8223 | } else { |
| 8224 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 8225 | new_dentry->d_inode, |
| 8226 | new_dentry->d_name.name, |
| 8227 | new_dentry->d_name.len); |
| 8228 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8229 | if (!ret && new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8230 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8231 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8232 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8233 | if (ret) { |
| 8234 | btrfs_abort_transaction(trans, root, ret); |
| 8235 | goto out_fail; |
| 8236 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8237 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8238 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8239 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 8240 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8241 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8242 | if (ret) { |
| 8243 | btrfs_abort_transaction(trans, root, ret); |
| 8244 | goto out_fail; |
| 8245 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8246 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8247 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 8248 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 8249 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8250 | btrfs_end_log_trans(root); |
| 8251 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8252 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8253 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8254 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8255 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8256 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8257 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8258 | return ret; |
| 8259 | } |
| 8260 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8261 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 8262 | { |
| 8263 | struct btrfs_delalloc_work *delalloc_work; |
| 8264 | |
| 8265 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 8266 | work); |
| 8267 | if (delalloc_work->wait) |
| 8268 | btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1); |
| 8269 | else |
| 8270 | filemap_flush(delalloc_work->inode->i_mapping); |
| 8271 | |
| 8272 | if (delalloc_work->delay_iput) |
| 8273 | btrfs_add_delayed_iput(delalloc_work->inode); |
| 8274 | else |
| 8275 | iput(delalloc_work->inode); |
| 8276 | complete(&delalloc_work->completion); |
| 8277 | } |
| 8278 | |
| 8279 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 8280 | int wait, int delay_iput) |
| 8281 | { |
| 8282 | struct btrfs_delalloc_work *work; |
| 8283 | |
| 8284 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 8285 | if (!work) |
| 8286 | return NULL; |
| 8287 | |
| 8288 | init_completion(&work->completion); |
| 8289 | INIT_LIST_HEAD(&work->list); |
| 8290 | work->inode = inode; |
| 8291 | work->wait = wait; |
| 8292 | work->delay_iput = delay_iput; |
| 8293 | work->work.func = btrfs_run_delalloc_work; |
| 8294 | |
| 8295 | return work; |
| 8296 | } |
| 8297 | |
| 8298 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 8299 | { |
| 8300 | wait_for_completion(&work->completion); |
| 8301 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 8302 | } |
| 8303 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8304 | /* |
| 8305 | * some fairly slow code that needs optimization. This walks the list |
| 8306 | * of all the inodes with pending delalloc and forces them to disk. |
| 8307 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 8308 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8309 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8310 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8311 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8312 | struct btrfs_delalloc_work *work, *next; |
| 8313 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8314 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8315 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8316 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8317 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 8318 | return -EROFS; |
| 8319 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8320 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8321 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 8322 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 8323 | spin_lock(&root->fs_info->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8324 | list_splice_init(&root->fs_info->delalloc_inodes, &splice); |
| 8325 | while (!list_empty(&splice)) { |
| 8326 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8327 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8328 | |
| 8329 | list_del_init(&binode->delalloc_inodes); |
| 8330 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8331 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8332 | if (!inode) { |
| 8333 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 8334 | &binode->runtime_flags); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8335 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8336 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8337 | |
| 8338 | list_add_tail(&binode->delalloc_inodes, |
| 8339 | &root->fs_info->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 8340 | spin_unlock(&root->fs_info->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8341 | |
| 8342 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
| 8343 | if (unlikely(!work)) { |
| 8344 | ret = -ENOMEM; |
| 8345 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8346 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8347 | list_add_tail(&work->list, &works); |
| 8348 | btrfs_queue_worker(&root->fs_info->flush_workers, |
| 8349 | &work->work); |
| 8350 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8351 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 8352 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8353 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 8354 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8355 | |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8356 | list_for_each_entry_safe(work, next, &works, list) { |
| 8357 | list_del_init(&work->list); |
| 8358 | btrfs_wait_and_free_delalloc_work(work); |
| 8359 | } |
| 8360 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8361 | /* the filemap_flush will queue IO into the worker threads, but |
| 8362 | * we have to make sure the IO is actually started and that |
| 8363 | * ordered extents get created before we return |
| 8364 | */ |
| 8365 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8366 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8367 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8368 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8369 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 8370 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8371 | } |
| 8372 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8373 | return 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8374 | out: |
| 8375 | list_for_each_entry_safe(work, next, &works, list) { |
| 8376 | list_del_init(&work->list); |
| 8377 | btrfs_wait_and_free_delalloc_work(work); |
| 8378 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8379 | |
| 8380 | if (!list_empty_careful(&splice)) { |
| 8381 | spin_lock(&root->fs_info->delalloc_lock); |
| 8382 | list_splice_tail(&splice, &root->fs_info->delalloc_inodes); |
| 8383 | spin_unlock(&root->fs_info->delalloc_lock); |
| 8384 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8385 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8386 | } |
| 8387 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8388 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 8389 | const char *symname) |
| 8390 | { |
| 8391 | struct btrfs_trans_handle *trans; |
| 8392 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 8393 | struct btrfs_path *path; |
| 8394 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8395 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8396 | int err; |
| 8397 | int drop_inode = 0; |
| 8398 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8399 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8400 | int name_len; |
| 8401 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8402 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8403 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8404 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8405 | |
| 8406 | name_len = strlen(symname) + 1; |
| 8407 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 8408 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8409 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8410 | /* |
| 8411 | * 2 items for inode item and ref |
| 8412 | * 2 items for dir items |
| 8413 | * 1 item for xattr if selinux is on |
| 8414 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8415 | trans = btrfs_start_transaction(root, 5); |
| 8416 | if (IS_ERR(trans)) |
| 8417 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8418 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 8419 | err = btrfs_find_free_ino(root, &objectid); |
| 8420 | if (err) |
| 8421 | goto out_unlock; |
| 8422 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8423 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8424 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 8425 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8426 | if (IS_ERR(inode)) { |
| 8427 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8428 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8429 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8430 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 8431 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8432 | if (err) { |
| 8433 | drop_inode = 1; |
| 8434 | goto out_unlock; |
| 8435 | } |
| 8436 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 8437 | /* |
| 8438 | * If the active LSM wants to access the inode during |
| 8439 | * d_instantiate it needs these. Smack checks to see |
| 8440 | * if the filesystem supports xattrs by looking at the |
| 8441 | * ops vector. |
| 8442 | */ |
| 8443 | inode->i_fop = &btrfs_file_operations; |
| 8444 | inode->i_op = &btrfs_file_inode_operations; |
| 8445 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 8446 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8447 | if (err) |
| 8448 | drop_inode = 1; |
| 8449 | else { |
| 8450 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8451 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8452 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8453 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8454 | if (drop_inode) |
| 8455 | goto out_unlock; |
| 8456 | |
| 8457 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8458 | if (!path) { |
| 8459 | err = -ENOMEM; |
| 8460 | drop_inode = 1; |
| 8461 | goto out_unlock; |
| 8462 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8463 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8464 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8465 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 8466 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 8467 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 8468 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8469 | if (err) { |
| 8470 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 8471 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8472 | goto out_unlock; |
| 8473 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8474 | leaf = path->nodes[0]; |
| 8475 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 8476 | struct btrfs_file_extent_item); |
| 8477 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 8478 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8479 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 8480 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 8481 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 8482 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 8483 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 8484 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8485 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8486 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 8487 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8488 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8489 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8490 | inode->i_op = &btrfs_symlink_inode_operations; |
| 8491 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8492 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8493 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8494 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8495 | err = btrfs_update_inode(trans, root, inode); |
| 8496 | if (err) |
| 8497 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8498 | |
| 8499 | out_unlock: |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 8500 | if (!err) |
| 8501 | d_instantiate(dentry, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8502 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8503 | if (drop_inode) { |
| 8504 | inode_dec_link_count(inode); |
| 8505 | iput(inode); |
| 8506 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8507 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8508 | return err; |
| 8509 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8510 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8511 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8512 | u64 start, u64 num_bytes, u64 min_size, |
| 8513 | loff_t actual_len, u64 *alloc_hint, |
| 8514 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8515 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8516 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 8517 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8518 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8519 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8520 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8521 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8522 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8523 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8524 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8525 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8526 | if (trans) |
| 8527 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8528 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8529 | if (own_trans) { |
| 8530 | trans = btrfs_start_transaction(root, 3); |
| 8531 | if (IS_ERR(trans)) { |
| 8532 | ret = PTR_ERR(trans); |
| 8533 | break; |
| 8534 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8535 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8536 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8537 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 8538 | cur_bytes = max(cur_bytes, min_size); |
| 8539 | ret = btrfs_reserve_extent(trans, root, cur_bytes, |
Zach Brown | 24542bf | 2012-11-16 00:04:43 +0000 | [diff] [blame] | 8540 | min_size, 0, *alloc_hint, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8541 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8542 | if (own_trans) |
| 8543 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8544 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8545 | } |
| 8546 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8547 | ret = insert_reserved_file_extent(trans, inode, |
| 8548 | cur_offset, ins.objectid, |
| 8549 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 8550 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8551 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8552 | if (ret) { |
| 8553 | btrfs_abort_transaction(trans, root, ret); |
| 8554 | if (own_trans) |
| 8555 | btrfs_end_transaction(trans, root); |
| 8556 | break; |
| 8557 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 8558 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8559 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8560 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8561 | em = alloc_extent_map(); |
| 8562 | if (!em) { |
| 8563 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 8564 | &BTRFS_I(inode)->runtime_flags); |
| 8565 | goto next; |
| 8566 | } |
| 8567 | |
| 8568 | em->start = cur_offset; |
| 8569 | em->orig_start = cur_offset; |
| 8570 | em->len = ins.offset; |
| 8571 | em->block_start = ins.objectid; |
| 8572 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 8573 | em->orig_block_len = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8574 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 8575 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 8576 | em->generation = trans->transid; |
| 8577 | |
| 8578 | while (1) { |
| 8579 | write_lock(&em_tree->lock); |
| 8580 | ret = add_extent_mapping(em_tree, em); |
| 8581 | if (!ret) |
| 8582 | list_move(&em->list, |
| 8583 | &em_tree->modified_extents); |
| 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 | 10556cb2 | 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 | b293f02e | 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 | }; |