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> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 43 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 44 | #include "ctree.h" |
| 45 | #include "disk-io.h" |
| 46 | #include "transaction.h" |
| 47 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 48 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 49 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 50 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 51 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 52 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 53 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 54 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 55 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 56 | #include "inode-map.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 57 | |
| 58 | struct btrfs_iget_args { |
| 59 | u64 ino; |
| 60 | struct btrfs_root *root; |
| 61 | }; |
| 62 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 63 | static const struct inode_operations btrfs_dir_inode_operations; |
| 64 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 65 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 66 | static const struct inode_operations btrfs_special_inode_operations; |
| 67 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 68 | static const struct address_space_operations btrfs_aops; |
| 69 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 70 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 71 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 72 | |
| 73 | static struct kmem_cache *btrfs_inode_cachep; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 74 | static struct kmem_cache *btrfs_delalloc_work_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 75 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 76 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 77 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 78 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 79 | |
| 80 | #define S_SHIFT 12 |
| 81 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 82 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 83 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 84 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 85 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 86 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 87 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 88 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 89 | }; |
| 90 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 91 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 92 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 93 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 94 | static noinline int cow_file_range(struct inode *inode, |
| 95 | struct page *locked_page, |
| 96 | u64 start, u64 end, int *page_started, |
| 97 | unsigned long *nr_written, int unlock); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 98 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 99 | u64 len, u64 orig_start, |
| 100 | u64 block_start, u64 block_len, |
| 101 | u64 orig_block_len, int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 102 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 103 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 104 | struct inode *inode, struct inode *dir, |
| 105 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 106 | { |
| 107 | int err; |
| 108 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 109 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 110 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 111 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 112 | return err; |
| 113 | } |
| 114 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 115 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 116 | * this does all the hard work for inserting an inline extent into |
| 117 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 118 | * no overlapping inline items exist in the btree |
| 119 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 120 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 121 | struct btrfs_root *root, struct inode *inode, |
| 122 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 123 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 124 | struct page **compressed_pages) |
| 125 | { |
| 126 | struct btrfs_key key; |
| 127 | struct btrfs_path *path; |
| 128 | struct extent_buffer *leaf; |
| 129 | struct page *page = NULL; |
| 130 | char *kaddr; |
| 131 | unsigned long ptr; |
| 132 | struct btrfs_file_extent_item *ei; |
| 133 | int err = 0; |
| 134 | int ret; |
| 135 | size_t cur_size = size; |
| 136 | size_t datasize; |
| 137 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 138 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 139 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 140 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 141 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 142 | path = btrfs_alloc_path(); |
| 143 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 144 | return -ENOMEM; |
| 145 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 146 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 147 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 148 | key.objectid = btrfs_ino(inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 149 | key.offset = start; |
| 150 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 151 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 152 | |
| 153 | inode_add_bytes(inode, size); |
| 154 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 155 | datasize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 156 | if (ret) { |
| 157 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 158 | goto fail; |
| 159 | } |
| 160 | leaf = path->nodes[0]; |
| 161 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 162 | struct btrfs_file_extent_item); |
| 163 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 164 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 165 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 166 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 167 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 168 | ptr = btrfs_file_extent_inline_start(ei); |
| 169 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 170 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | struct page *cpage; |
| 172 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 173 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 174 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 175 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 176 | PAGE_CACHE_SIZE); |
| 177 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 178 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 179 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 180 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 181 | |
| 182 | i++; |
| 183 | ptr += cur_size; |
| 184 | compressed_size -= cur_size; |
| 185 | } |
| 186 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 187 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 188 | } else { |
| 189 | page = find_get_page(inode->i_mapping, |
| 190 | start >> PAGE_CACHE_SHIFT); |
| 191 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 192 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 193 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 194 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 195 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 196 | page_cache_release(page); |
| 197 | } |
| 198 | btrfs_mark_buffer_dirty(leaf); |
| 199 | btrfs_free_path(path); |
| 200 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 201 | /* |
| 202 | * we're an inline extent, so nobody can |
| 203 | * extend the file past i_size without locking |
| 204 | * a page we already have locked. |
| 205 | * |
| 206 | * We must do any isize and inode updates |
| 207 | * before we unlock the pages. Otherwise we |
| 208 | * could end up racing with unlink. |
| 209 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 210 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 211 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 212 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 213 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 214 | fail: |
| 215 | btrfs_free_path(path); |
| 216 | return err; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /* |
| 221 | * conditionally insert an inline extent into the file. This |
| 222 | * does the checks required to make sure the data is small enough |
| 223 | * to fit as an inline extent. |
| 224 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 225 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 226 | struct btrfs_root *root, |
| 227 | struct inode *inode, u64 start, u64 end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 228 | size_t compressed_size, int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 229 | struct page **compressed_pages) |
| 230 | { |
| 231 | u64 isize = i_size_read(inode); |
| 232 | u64 actual_end = min(end + 1, isize); |
| 233 | u64 inline_len = actual_end - start; |
| 234 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 235 | ~((u64)root->sectorsize - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 236 | u64 data_len = inline_len; |
| 237 | int ret; |
| 238 | |
| 239 | if (compressed_size) |
| 240 | data_len = compressed_size; |
| 241 | |
| 242 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 243 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 244 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 245 | (!compressed_size && |
| 246 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 247 | end + 1 < isize || |
| 248 | data_len > root->fs_info->max_inline) { |
| 249 | return 1; |
| 250 | } |
| 251 | |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 252 | ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 253 | if (ret) |
| 254 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 255 | |
| 256 | if (isize > actual_end) |
| 257 | inline_len = min_t(u64, isize, actual_end); |
| 258 | ret = insert_inline_extent(trans, root, inode, start, |
| 259 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 260 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 261 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 262 | btrfs_abort_transaction(trans, root, ret); |
| 263 | return ret; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 264 | } else if (ret == -ENOSPC) { |
| 265 | return 1; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 266 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 267 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 268 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 269 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 273 | struct async_extent { |
| 274 | u64 start; |
| 275 | u64 ram_size; |
| 276 | u64 compressed_size; |
| 277 | struct page **pages; |
| 278 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 279 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 280 | struct list_head list; |
| 281 | }; |
| 282 | |
| 283 | struct async_cow { |
| 284 | struct inode *inode; |
| 285 | struct btrfs_root *root; |
| 286 | struct page *locked_page; |
| 287 | u64 start; |
| 288 | u64 end; |
| 289 | struct list_head extents; |
| 290 | struct btrfs_work work; |
| 291 | }; |
| 292 | |
| 293 | static noinline int add_async_extent(struct async_cow *cow, |
| 294 | u64 start, u64 ram_size, |
| 295 | u64 compressed_size, |
| 296 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 297 | unsigned long nr_pages, |
| 298 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 299 | { |
| 300 | struct async_extent *async_extent; |
| 301 | |
| 302 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 303 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 304 | async_extent->start = start; |
| 305 | async_extent->ram_size = ram_size; |
| 306 | async_extent->compressed_size = compressed_size; |
| 307 | async_extent->pages = pages; |
| 308 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 309 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 310 | list_add_tail(&async_extent->list, &cow->extents); |
| 311 | return 0; |
| 312 | } |
| 313 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 314 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 315 | * we create compressed extents in two phases. The first |
| 316 | * phase compresses a range of pages that have already been |
| 317 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 318 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 319 | * This is done inside an ordered work queue, and the compression |
| 320 | * is spread across many cpus. The actual IO submission is step |
| 321 | * two, and the ordered work queue takes care of making sure that |
| 322 | * happens in the same order things were put onto the queue by |
| 323 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 324 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 325 | * If this code finds it can't get good compression, it puts an |
| 326 | * entry onto the work queue to write the uncompressed bytes. This |
| 327 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 328 | * are written in the same order that the flusher thread sent them |
| 329 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 330 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 331 | static noinline int compress_file_range(struct inode *inode, |
| 332 | struct page *locked_page, |
| 333 | u64 start, u64 end, |
| 334 | struct async_cow *async_cow, |
| 335 | int *num_added) |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 336 | { |
| 337 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 338 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 339 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 340 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 341 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 342 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 343 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 344 | struct page **pages = NULL; |
| 345 | unsigned long nr_pages; |
| 346 | unsigned long nr_pages_ret = 0; |
| 347 | unsigned long total_compressed = 0; |
| 348 | unsigned long total_in = 0; |
| 349 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 350 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 351 | int i; |
| 352 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 353 | int compress_type = root->fs_info->compress_type; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 354 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 355 | /* if this is a small write inside eof, kick off a defrag */ |
| 356 | if ((end - start + 1) < 16 * 1024 && |
| 357 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 358 | btrfs_add_inode_defrag(NULL, inode); |
| 359 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 360 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 361 | again: |
| 362 | will_compress = 0; |
| 363 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 364 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 365 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 366 | /* |
| 367 | * we don't want to send crud past the end of i_size through |
| 368 | * compression, that's just a waste of CPU time. So, if the |
| 369 | * end of the file is before the start of our current |
| 370 | * requested range of bytes, we bail out to the uncompressed |
| 371 | * cleanup code that can deal with all of this. |
| 372 | * |
| 373 | * It isn't really the fastest way to fix things, but this is a |
| 374 | * very uncommon corner. |
| 375 | */ |
| 376 | if (actual_end <= start) |
| 377 | goto cleanup_and_bail_uncompressed; |
| 378 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 379 | total_compressed = actual_end - start; |
| 380 | |
| 381 | /* we want to make sure that amount of ram required to uncompress |
| 382 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 383 | * of a compressed extent to 128k. This is a crucial number |
| 384 | * because it also controls how easily we can spread reads across |
| 385 | * cpus for decompression. |
| 386 | * |
| 387 | * We also want to make sure the amount of IO required to do |
| 388 | * a random read is reasonably small, so we limit the size of |
| 389 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 390 | */ |
| 391 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 392 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 393 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 394 | total_in = 0; |
| 395 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 396 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 397 | /* |
| 398 | * we do compression for mount -o compress and when the |
| 399 | * inode has not been flagged as nocompress. This flag can |
| 400 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 401 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 402 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 403 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 404 | (BTRFS_I(inode)->force_compress) || |
| 405 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 406 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 407 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 408 | if (!pages) { |
| 409 | /* just bail out to the uncompressed code */ |
| 410 | goto cont; |
| 411 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 412 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 413 | if (BTRFS_I(inode)->force_compress) |
| 414 | compress_type = BTRFS_I(inode)->force_compress; |
| 415 | |
| 416 | ret = btrfs_compress_pages(compress_type, |
| 417 | inode->i_mapping, start, |
| 418 | total_compressed, pages, |
| 419 | nr_pages, &nr_pages_ret, |
| 420 | &total_in, |
| 421 | &total_compressed, |
| 422 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 423 | |
| 424 | if (!ret) { |
| 425 | unsigned long offset = total_compressed & |
| 426 | (PAGE_CACHE_SIZE - 1); |
| 427 | struct page *page = pages[nr_pages_ret - 1]; |
| 428 | char *kaddr; |
| 429 | |
| 430 | /* zero the tail end of the last page, we might be |
| 431 | * sending it down to disk |
| 432 | */ |
| 433 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 434 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 435 | memset(kaddr + offset, 0, |
| 436 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 437 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 438 | } |
| 439 | will_compress = 1; |
| 440 | } |
| 441 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 442 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 443 | if (start == 0) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 444 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 445 | if (IS_ERR(trans)) { |
| 446 | ret = PTR_ERR(trans); |
| 447 | trans = NULL; |
| 448 | goto cleanup_and_out; |
| 449 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 450 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 451 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 452 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 453 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 454 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 455 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 456 | */ |
| 457 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 458 | start, end, 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 459 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 460 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 461 | ret = cow_file_range_inline(trans, root, inode, |
| 462 | start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 463 | total_compressed, |
| 464 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 465 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 466 | if (ret <= 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 467 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 468 | * inline extent creation worked or returned error, |
| 469 | * we don't need to create any more async work items. |
| 470 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 471 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 472 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 473 | &BTRFS_I(inode)->io_tree, |
| 474 | start, end, NULL, |
| 475 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 476 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 477 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 478 | |
| 479 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 480 | goto free_pages_out; |
| 481 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 482 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | if (will_compress) { |
| 486 | /* |
| 487 | * we aren't doing an inline extent round the compressed size |
| 488 | * up to a block size boundary so the allocator does sane |
| 489 | * things |
| 490 | */ |
| 491 | total_compressed = (total_compressed + blocksize - 1) & |
| 492 | ~(blocksize - 1); |
| 493 | |
| 494 | /* |
| 495 | * one last check to make sure the compression is really a |
| 496 | * win, compare the page count read with the blocks on disk |
| 497 | */ |
| 498 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 499 | ~(PAGE_CACHE_SIZE - 1); |
| 500 | if (total_compressed >= total_in) { |
| 501 | will_compress = 0; |
| 502 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 503 | num_bytes = total_in; |
| 504 | } |
| 505 | } |
| 506 | if (!will_compress && pages) { |
| 507 | /* |
| 508 | * the compression code ran but failed to make things smaller, |
| 509 | * free any pages it allocated and our page pointer array |
| 510 | */ |
| 511 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 512 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 513 | page_cache_release(pages[i]); |
| 514 | } |
| 515 | kfree(pages); |
| 516 | pages = NULL; |
| 517 | total_compressed = 0; |
| 518 | nr_pages_ret = 0; |
| 519 | |
| 520 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 521 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 522 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 523 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 524 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 525 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 526 | if (will_compress) { |
| 527 | *num_added += 1; |
| 528 | |
| 529 | /* the async work queues will take care of doing actual |
| 530 | * allocation on disk for these compressed pages, |
| 531 | * and will submit them to the elevator. |
| 532 | */ |
| 533 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 534 | total_compressed, pages, nr_pages_ret, |
| 535 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 536 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 537 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 538 | start += num_bytes; |
| 539 | pages = NULL; |
| 540 | cond_resched(); |
| 541 | goto again; |
| 542 | } |
| 543 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 544 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 545 | /* |
| 546 | * No compression, but we still need to write the pages in |
| 547 | * the file we've been given so far. redirty the locked |
| 548 | * page if it corresponds to our extent and set things up |
| 549 | * for the async work queue to run cow_file_range to do |
| 550 | * the normal delalloc dance |
| 551 | */ |
| 552 | if (page_offset(locked_page) >= start && |
| 553 | page_offset(locked_page) <= end) { |
| 554 | __set_page_dirty_nobuffers(locked_page); |
| 555 | /* unlocked later on in the async handlers */ |
| 556 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 557 | add_async_extent(async_cow, start, end - start + 1, |
| 558 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 559 | *num_added += 1; |
| 560 | } |
| 561 | |
| 562 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 563 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 564 | |
| 565 | free_pages_out: |
| 566 | for (i = 0; i < nr_pages_ret; i++) { |
| 567 | WARN_ON(pages[i]->mapping); |
| 568 | page_cache_release(pages[i]); |
| 569 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 570 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 571 | |
| 572 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 573 | |
| 574 | cleanup_and_out: |
| 575 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 576 | start, end, NULL, |
| 577 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 578 | EXTENT_CLEAR_DIRTY | |
| 579 | EXTENT_CLEAR_DELALLOC | |
| 580 | EXTENT_SET_WRITEBACK | |
| 581 | EXTENT_END_WRITEBACK); |
| 582 | if (!trans || IS_ERR(trans)) |
| 583 | btrfs_error(root->fs_info, ret, "Failed to join transaction"); |
| 584 | else |
| 585 | btrfs_abort_transaction(trans, root, ret); |
| 586 | goto free_pages_out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /* |
| 590 | * phase two of compressed writeback. This is the ordered portion |
| 591 | * of the code, which only gets called in the order the work was |
| 592 | * queued. We walk all the async extents created by compress_file_range |
| 593 | * and send them down to the disk. |
| 594 | */ |
| 595 | static noinline int submit_compressed_extents(struct inode *inode, |
| 596 | struct async_cow *async_cow) |
| 597 | { |
| 598 | struct async_extent *async_extent; |
| 599 | u64 alloc_hint = 0; |
| 600 | struct btrfs_trans_handle *trans; |
| 601 | struct btrfs_key ins; |
| 602 | struct extent_map *em; |
| 603 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 604 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 605 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 606 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 607 | |
| 608 | if (list_empty(&async_cow->extents)) |
| 609 | return 0; |
| 610 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 611 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 612 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 613 | async_extent = list_entry(async_cow->extents.next, |
| 614 | struct async_extent, list); |
| 615 | list_del(&async_extent->list); |
| 616 | |
| 617 | io_tree = &BTRFS_I(inode)->io_tree; |
| 618 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 619 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 620 | /* did the compression code fall back to uncompressed IO? */ |
| 621 | if (!async_extent->pages) { |
| 622 | int page_started = 0; |
| 623 | unsigned long nr_written = 0; |
| 624 | |
| 625 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 626 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 627 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 628 | |
| 629 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 630 | ret = cow_file_range(inode, async_cow->locked_page, |
| 631 | async_extent->start, |
| 632 | async_extent->start + |
| 633 | async_extent->ram_size - 1, |
| 634 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 635 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 636 | /* JDM XXX */ |
| 637 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 638 | /* |
| 639 | * if page_started, cow_file_range inserted an |
| 640 | * inline extent and took care of all the unlocking |
| 641 | * and IO for us. Otherwise, we need to submit |
| 642 | * all those pages down to the drive. |
| 643 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 644 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 645 | extent_write_locked_range(io_tree, |
| 646 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 647 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 648 | async_extent->ram_size - 1, |
| 649 | btrfs_get_extent, |
| 650 | WB_SYNC_ALL); |
| 651 | kfree(async_extent); |
| 652 | cond_resched(); |
| 653 | continue; |
| 654 | } |
| 655 | |
| 656 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 657 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 658 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 659 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 660 | if (IS_ERR(trans)) { |
| 661 | ret = PTR_ERR(trans); |
| 662 | } else { |
| 663 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 664 | ret = btrfs_reserve_extent(trans, root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 665 | async_extent->compressed_size, |
| 666 | async_extent->compressed_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 667 | 0, alloc_hint, &ins, 1); |
Miao Xie | 962197b | 2012-09-11 23:27:35 -0600 | [diff] [blame] | 668 | if (ret && ret != -ENOSPC) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 669 | btrfs_abort_transaction(trans, root, ret); |
| 670 | btrfs_end_transaction(trans, root); |
| 671 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 672 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 673 | if (ret) { |
| 674 | int i; |
| 675 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 676 | WARN_ON(async_extent->pages[i]->mapping); |
| 677 | page_cache_release(async_extent->pages[i]); |
| 678 | } |
| 679 | kfree(async_extent->pages); |
| 680 | async_extent->nr_pages = 0; |
| 681 | async_extent->pages = NULL; |
| 682 | unlock_extent(io_tree, async_extent->start, |
| 683 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 684 | async_extent->ram_size - 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 685 | if (ret == -ENOSPC) |
| 686 | goto retry; |
| 687 | goto out_free; /* JDM: Requeue? */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 688 | } |
| 689 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 690 | /* |
| 691 | * here we're doing allocation and writeback of the |
| 692 | * compressed pages |
| 693 | */ |
| 694 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 695 | async_extent->start + |
| 696 | async_extent->ram_size - 1, 0); |
| 697 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 698 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 699 | BUG_ON(!em); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 700 | em->start = async_extent->start; |
| 701 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 702 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 703 | em->mod_start = em->start; |
| 704 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 705 | |
| 706 | em->block_start = ins.objectid; |
| 707 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 708 | em->orig_block_len = ins.offset; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 709 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 710 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 711 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 712 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 713 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 714 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 715 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 716 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 717 | ret = add_extent_mapping(em_tree, em); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 718 | if (!ret) |
| 719 | list_move(&em->list, |
| 720 | &em_tree->modified_extents); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 721 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 722 | if (ret != -EEXIST) { |
| 723 | free_extent_map(em); |
| 724 | break; |
| 725 | } |
| 726 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 727 | async_extent->start + |
| 728 | async_extent->ram_size - 1, 0); |
| 729 | } |
| 730 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 731 | ret = btrfs_add_ordered_extent_compress(inode, |
| 732 | async_extent->start, |
| 733 | ins.objectid, |
| 734 | async_extent->ram_size, |
| 735 | ins.offset, |
| 736 | BTRFS_ORDERED_COMPRESSED, |
| 737 | async_extent->compress_type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 738 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 739 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 740 | /* |
| 741 | * clear dirty, set writeback and unlock the pages. |
| 742 | */ |
| 743 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 744 | &BTRFS_I(inode)->io_tree, |
| 745 | async_extent->start, |
| 746 | async_extent->start + |
| 747 | async_extent->ram_size - 1, |
| 748 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 749 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 750 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 751 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 752 | |
| 753 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 754 | async_extent->start, |
| 755 | async_extent->ram_size, |
| 756 | ins.objectid, |
| 757 | ins.offset, async_extent->pages, |
| 758 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 759 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 760 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 761 | alloc_hint = ins.objectid + ins.offset; |
| 762 | kfree(async_extent); |
| 763 | cond_resched(); |
| 764 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 765 | ret = 0; |
| 766 | out: |
| 767 | return ret; |
| 768 | out_free: |
| 769 | kfree(async_extent); |
| 770 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 771 | } |
| 772 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 773 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 774 | u64 num_bytes) |
| 775 | { |
| 776 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 777 | struct extent_map *em; |
| 778 | u64 alloc_hint = 0; |
| 779 | |
| 780 | read_lock(&em_tree->lock); |
| 781 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 782 | if (em) { |
| 783 | /* |
| 784 | * if block start isn't an actual block number then find the |
| 785 | * first block in this inode and use that as a hint. If that |
| 786 | * block is also bogus then just don't worry about it. |
| 787 | */ |
| 788 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 789 | free_extent_map(em); |
| 790 | em = search_extent_mapping(em_tree, 0, 0); |
| 791 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 792 | alloc_hint = em->block_start; |
| 793 | if (em) |
| 794 | free_extent_map(em); |
| 795 | } else { |
| 796 | alloc_hint = em->block_start; |
| 797 | free_extent_map(em); |
| 798 | } |
| 799 | } |
| 800 | read_unlock(&em_tree->lock); |
| 801 | |
| 802 | return alloc_hint; |
| 803 | } |
| 804 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 805 | /* |
| 806 | * when extent_io.c finds a delayed allocation range in the file, |
| 807 | * the call backs end up in this code. The basic idea is to |
| 808 | * allocate extents on disk for the range, and create ordered data structs |
| 809 | * in ram to track those extents. |
| 810 | * |
| 811 | * locked_page is the page that writepage had locked already. We use |
| 812 | * it to make sure we don't do extra locks or unlocks. |
| 813 | * |
| 814 | * *page_started is set to one if we unlock locked_page and do everything |
| 815 | * required to start IO on it. It may be clean and already done with |
| 816 | * IO when we return. |
| 817 | */ |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 818 | static noinline int __cow_file_range(struct btrfs_trans_handle *trans, |
| 819 | struct inode *inode, |
| 820 | struct btrfs_root *root, |
| 821 | struct page *locked_page, |
| 822 | u64 start, u64 end, int *page_started, |
| 823 | unsigned long *nr_written, |
| 824 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 825 | { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 826 | u64 alloc_hint = 0; |
| 827 | u64 num_bytes; |
| 828 | unsigned long ram_size; |
| 829 | u64 disk_num_bytes; |
| 830 | u64 cur_alloc_size; |
| 831 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 832 | struct btrfs_key ins; |
| 833 | struct extent_map *em; |
| 834 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 835 | int ret = 0; |
| 836 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 837 | BUG_ON(btrfs_is_free_space_inode(inode)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 838 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 839 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 840 | num_bytes = max(blocksize, num_bytes); |
| 841 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 842 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 843 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 844 | if (num_bytes < 64 * 1024 && |
| 845 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 846 | btrfs_add_inode_defrag(trans, inode); |
| 847 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 848 | if (start == 0) { |
| 849 | /* lets try to make an inline extent */ |
| 850 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 851 | start, end, 0, 0, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 852 | if (ret == 0) { |
| 853 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 854 | &BTRFS_I(inode)->io_tree, |
| 855 | start, end, NULL, |
| 856 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 857 | EXTENT_CLEAR_UNLOCK | |
| 858 | EXTENT_CLEAR_DELALLOC | |
| 859 | EXTENT_CLEAR_DIRTY | |
| 860 | EXTENT_SET_WRITEBACK | |
| 861 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 862 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 863 | *nr_written = *nr_written + |
| 864 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 865 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 866 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 867 | } else if (ret < 0) { |
| 868 | btrfs_abort_transaction(trans, root, ret); |
| 869 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 870 | } |
| 871 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 872 | |
| 873 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 874 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 875 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 876 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 877 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 878 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 879 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 880 | unsigned long op; |
| 881 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 882 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 883 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 884 | root->sectorsize, 0, alloc_hint, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 885 | &ins, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 886 | if (ret < 0) { |
| 887 | btrfs_abort_transaction(trans, root, ret); |
| 888 | goto out_unlock; |
| 889 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 890 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 891 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 892 | BUG_ON(!em); /* -ENOMEM */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 893 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 894 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 895 | ram_size = ins.offset; |
| 896 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 897 | em->mod_start = em->start; |
| 898 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 899 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 900 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 901 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 902 | em->orig_block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 903 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 904 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 905 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 906 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 907 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 908 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 909 | ret = add_extent_mapping(em_tree, em); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 910 | if (!ret) |
| 911 | list_move(&em->list, |
| 912 | &em_tree->modified_extents); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 913 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 914 | if (ret != -EEXIST) { |
| 915 | free_extent_map(em); |
| 916 | break; |
| 917 | } |
| 918 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 919 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 920 | } |
| 921 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 922 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 923 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 924 | ram_size, cur_alloc_size, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 925 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 926 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 927 | if (root->root_key.objectid == |
| 928 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 929 | ret = btrfs_reloc_clone_csums(inode, start, |
| 930 | cur_alloc_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 931 | if (ret) { |
| 932 | btrfs_abort_transaction(trans, root, ret); |
| 933 | goto out_unlock; |
| 934 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 935 | } |
| 936 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 937 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 938 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 939 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 940 | /* we're not doing compressed IO, don't unlock the first |
| 941 | * page (which the caller expects to stay locked), don't |
| 942 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 943 | * |
| 944 | * Do set the Private2 bit so we know this page was properly |
| 945 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 946 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 947 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 948 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 949 | EXTENT_SET_PRIVATE2; |
| 950 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 951 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 952 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 953 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 954 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 955 | num_bytes -= cur_alloc_size; |
| 956 | alloc_hint = ins.objectid + ins.offset; |
| 957 | start += cur_alloc_size; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 958 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 959 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 960 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 961 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 962 | out_unlock: |
| 963 | extent_clear_unlock_delalloc(inode, |
| 964 | &BTRFS_I(inode)->io_tree, |
Josef Bacik | beb42dd | 2012-05-30 15:35:17 -0400 | [diff] [blame] | 965 | start, end, locked_page, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 966 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 967 | EXTENT_CLEAR_UNLOCK | |
| 968 | EXTENT_CLEAR_DELALLOC | |
| 969 | EXTENT_CLEAR_DIRTY | |
| 970 | EXTENT_SET_WRITEBACK | |
| 971 | EXTENT_END_WRITEBACK); |
| 972 | |
| 973 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 974 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 975 | |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 976 | static noinline int cow_file_range(struct inode *inode, |
| 977 | struct page *locked_page, |
| 978 | u64 start, u64 end, int *page_started, |
| 979 | unsigned long *nr_written, |
| 980 | int unlock) |
| 981 | { |
| 982 | struct btrfs_trans_handle *trans; |
| 983 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 984 | int ret; |
| 985 | |
| 986 | trans = btrfs_join_transaction(root); |
| 987 | if (IS_ERR(trans)) { |
| 988 | extent_clear_unlock_delalloc(inode, |
| 989 | &BTRFS_I(inode)->io_tree, |
| 990 | start, end, locked_page, |
| 991 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 992 | EXTENT_CLEAR_UNLOCK | |
| 993 | EXTENT_CLEAR_DELALLOC | |
| 994 | EXTENT_CLEAR_DIRTY | |
| 995 | EXTENT_SET_WRITEBACK | |
| 996 | EXTENT_END_WRITEBACK); |
| 997 | return PTR_ERR(trans); |
| 998 | } |
| 999 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 1000 | |
| 1001 | ret = __cow_file_range(trans, inode, root, locked_page, start, end, |
| 1002 | page_started, nr_written, unlock); |
| 1003 | |
| 1004 | btrfs_end_transaction(trans, root); |
| 1005 | |
| 1006 | return ret; |
| 1007 | } |
| 1008 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1009 | /* |
| 1010 | * work queue call back to started compression on a file and pages |
| 1011 | */ |
| 1012 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1013 | { |
| 1014 | struct async_cow *async_cow; |
| 1015 | int num_added = 0; |
| 1016 | async_cow = container_of(work, struct async_cow, work); |
| 1017 | |
| 1018 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1019 | async_cow->start, async_cow->end, async_cow, |
| 1020 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1021 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1022 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1023 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1024 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * work queue call back to submit previously compressed pages |
| 1029 | */ |
| 1030 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1031 | { |
| 1032 | struct async_cow *async_cow; |
| 1033 | struct btrfs_root *root; |
| 1034 | unsigned long nr_pages; |
| 1035 | |
| 1036 | async_cow = container_of(work, struct async_cow, work); |
| 1037 | |
| 1038 | root = async_cow->root; |
| 1039 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1040 | PAGE_CACHE_SHIFT; |
| 1041 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1042 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1043 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1044 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1045 | wake_up(&root->fs_info->async_submit_wait); |
| 1046 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1047 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1048 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1049 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1050 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1051 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1052 | { |
| 1053 | struct async_cow *async_cow; |
| 1054 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1055 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1056 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1057 | kfree(async_cow); |
| 1058 | } |
| 1059 | |
| 1060 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1061 | u64 start, u64 end, int *page_started, |
| 1062 | unsigned long *nr_written) |
| 1063 | { |
| 1064 | struct async_cow *async_cow; |
| 1065 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1066 | unsigned long nr_pages; |
| 1067 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1068 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1069 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1070 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1071 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1072 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1073 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1074 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1075 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1076 | async_cow->root = root; |
| 1077 | async_cow->locked_page = locked_page; |
| 1078 | async_cow->start = start; |
| 1079 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1080 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1081 | cur_end = end; |
| 1082 | else |
| 1083 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1084 | |
| 1085 | async_cow->end = cur_end; |
| 1086 | INIT_LIST_HEAD(&async_cow->extents); |
| 1087 | |
| 1088 | async_cow->work.func = async_cow_start; |
| 1089 | async_cow->work.ordered_func = async_cow_submit; |
| 1090 | async_cow->work.ordered_free = async_cow_free; |
| 1091 | async_cow->work.flags = 0; |
| 1092 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1093 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1094 | PAGE_CACHE_SHIFT; |
| 1095 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1096 | |
| 1097 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 1098 | &async_cow->work); |
| 1099 | |
| 1100 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1101 | wait_event(root->fs_info->async_submit_wait, |
| 1102 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1103 | limit)); |
| 1104 | } |
| 1105 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1106 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1107 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1108 | wait_event(root->fs_info->async_submit_wait, |
| 1109 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1110 | 0)); |
| 1111 | } |
| 1112 | |
| 1113 | *nr_written += nr_pages; |
| 1114 | start = cur_end + 1; |
| 1115 | } |
| 1116 | *page_started = 1; |
| 1117 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1118 | } |
| 1119 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1120 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1121 | u64 bytenr, u64 num_bytes) |
| 1122 | { |
| 1123 | int ret; |
| 1124 | struct btrfs_ordered_sum *sums; |
| 1125 | LIST_HEAD(list); |
| 1126 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1127 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1128 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1129 | if (ret == 0 && list_empty(&list)) |
| 1130 | return 0; |
| 1131 | |
| 1132 | while (!list_empty(&list)) { |
| 1133 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1134 | list_del(&sums->list); |
| 1135 | kfree(sums); |
| 1136 | } |
| 1137 | return 1; |
| 1138 | } |
| 1139 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1140 | /* |
| 1141 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1142 | * of the extents that exist in the file, and COWs the file as required. |
| 1143 | * |
| 1144 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1145 | * blocks on disk |
| 1146 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1147 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1148 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1149 | u64 start, u64 end, int *page_started, int force, |
| 1150 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1151 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1152 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1153 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1154 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1155 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1156 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1157 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1158 | u64 cow_start; |
| 1159 | u64 cur_offset; |
| 1160 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1161 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1162 | u64 disk_bytenr; |
| 1163 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1164 | u64 disk_num_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1165 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1166 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1167 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1168 | int nocow; |
| 1169 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1170 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1171 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1172 | |
| 1173 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1174 | if (!path) { |
| 1175 | extent_clear_unlock_delalloc(inode, |
| 1176 | &BTRFS_I(inode)->io_tree, |
| 1177 | start, end, locked_page, |
| 1178 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1179 | EXTENT_CLEAR_UNLOCK | |
| 1180 | EXTENT_CLEAR_DELALLOC | |
| 1181 | EXTENT_CLEAR_DIRTY | |
| 1182 | EXTENT_SET_WRITEBACK | |
| 1183 | EXTENT_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1184 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1185 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1186 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1187 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1188 | |
| 1189 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1190 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1191 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1192 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1193 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1194 | if (IS_ERR(trans)) { |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1195 | extent_clear_unlock_delalloc(inode, |
| 1196 | &BTRFS_I(inode)->io_tree, |
| 1197 | start, end, locked_page, |
| 1198 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1199 | EXTENT_CLEAR_UNLOCK | |
| 1200 | EXTENT_CLEAR_DELALLOC | |
| 1201 | EXTENT_CLEAR_DIRTY | |
| 1202 | EXTENT_SET_WRITEBACK | |
| 1203 | EXTENT_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1204 | btrfs_free_path(path); |
| 1205 | return PTR_ERR(trans); |
| 1206 | } |
| 1207 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1208 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1209 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1210 | cow_start = (u64)-1; |
| 1211 | cur_offset = start; |
| 1212 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1213 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1214 | cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1215 | if (ret < 0) { |
| 1216 | btrfs_abort_transaction(trans, root, ret); |
| 1217 | goto error; |
| 1218 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1219 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1220 | leaf = path->nodes[0]; |
| 1221 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1222 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1223 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1224 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1225 | path->slots[0]--; |
| 1226 | } |
| 1227 | check_prev = 0; |
| 1228 | next_slot: |
| 1229 | leaf = path->nodes[0]; |
| 1230 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1231 | ret = btrfs_next_leaf(root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1232 | if (ret < 0) { |
| 1233 | btrfs_abort_transaction(trans, root, ret); |
| 1234 | goto error; |
| 1235 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1236 | if (ret > 0) |
| 1237 | break; |
| 1238 | leaf = path->nodes[0]; |
| 1239 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1240 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1241 | nocow = 0; |
| 1242 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1243 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1244 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1245 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1246 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1247 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1248 | found_key.offset > end) |
| 1249 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1250 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1251 | if (found_key.offset > cur_offset) { |
| 1252 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1253 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1254 | goto out_check; |
| 1255 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1256 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1257 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1258 | struct btrfs_file_extent_item); |
| 1259 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1260 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1261 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1262 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1263 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1264 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1265 | extent_end = found_key.offset + |
| 1266 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1267 | disk_num_bytes = |
| 1268 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1269 | if (extent_end <= start) { |
| 1270 | path->slots[0]++; |
| 1271 | goto next_slot; |
| 1272 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1273 | if (disk_bytenr == 0) |
| 1274 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1275 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1276 | btrfs_file_extent_encryption(leaf, fi) || |
| 1277 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1278 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1279 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1280 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1281 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1282 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1283 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1284 | found_key.offset - |
| 1285 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1286 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1287 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1288 | disk_bytenr += cur_offset - found_key.offset; |
| 1289 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1290 | /* |
| 1291 | * force cow if csum exists in the range. |
| 1292 | * this ensure that csum for a given extent are |
| 1293 | * either valid or do not exist. |
| 1294 | */ |
| 1295 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1296 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1297 | nocow = 1; |
| 1298 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1299 | extent_end = found_key.offset + |
| 1300 | btrfs_file_extent_inline_len(leaf, fi); |
| 1301 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1302 | } else { |
| 1303 | BUG_ON(1); |
| 1304 | } |
| 1305 | out_check: |
| 1306 | if (extent_end <= start) { |
| 1307 | path->slots[0]++; |
| 1308 | goto next_slot; |
| 1309 | } |
| 1310 | if (!nocow) { |
| 1311 | if (cow_start == (u64)-1) |
| 1312 | cow_start = cur_offset; |
| 1313 | cur_offset = extent_end; |
| 1314 | if (cur_offset > end) |
| 1315 | break; |
| 1316 | path->slots[0]++; |
| 1317 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1318 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1319 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1320 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1321 | if (cow_start != (u64)-1) { |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1322 | ret = __cow_file_range(trans, inode, root, locked_page, |
| 1323 | cow_start, found_key.offset - 1, |
| 1324 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1325 | if (ret) { |
| 1326 | btrfs_abort_transaction(trans, root, ret); |
| 1327 | goto error; |
| 1328 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1329 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1330 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1331 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1332 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1333 | struct extent_map *em; |
| 1334 | struct extent_map_tree *em_tree; |
| 1335 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1336 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1337 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1338 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1339 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1340 | em->len = num_bytes; |
| 1341 | em->block_len = num_bytes; |
| 1342 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1343 | em->orig_block_len = disk_num_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1344 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1345 | em->mod_start = em->start; |
| 1346 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1347 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1348 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1349 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1350 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1351 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1352 | ret = add_extent_mapping(em_tree, em); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1353 | if (!ret) |
| 1354 | list_move(&em->list, |
| 1355 | &em_tree->modified_extents); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1356 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1357 | if (ret != -EEXIST) { |
| 1358 | free_extent_map(em); |
| 1359 | break; |
| 1360 | } |
| 1361 | btrfs_drop_extent_cache(inode, em->start, |
| 1362 | em->start + em->len - 1, 0); |
| 1363 | } |
| 1364 | type = BTRFS_ORDERED_PREALLOC; |
| 1365 | } else { |
| 1366 | type = BTRFS_ORDERED_NOCOW; |
| 1367 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1368 | |
| 1369 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1370 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1371 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1372 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1373 | if (root->root_key.objectid == |
| 1374 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1375 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1376 | num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1377 | if (ret) { |
| 1378 | btrfs_abort_transaction(trans, root, ret); |
| 1379 | goto error; |
| 1380 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1381 | } |
| 1382 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1383 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1384 | cur_offset, cur_offset + num_bytes - 1, |
| 1385 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1386 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1387 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1388 | cur_offset = extent_end; |
| 1389 | if (cur_offset > end) |
| 1390 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1391 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1392 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1393 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1394 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1395 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1396 | cur_offset = end; |
| 1397 | } |
| 1398 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1399 | if (cow_start != (u64)-1) { |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1400 | ret = __cow_file_range(trans, inode, root, locked_page, |
| 1401 | cow_start, end, |
| 1402 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1403 | if (ret) { |
| 1404 | btrfs_abort_transaction(trans, root, ret); |
| 1405 | goto error; |
| 1406 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1409 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1410 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1411 | if (!ret) |
| 1412 | ret = err; |
| 1413 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1414 | if (ret && cur_offset < end) |
| 1415 | extent_clear_unlock_delalloc(inode, |
| 1416 | &BTRFS_I(inode)->io_tree, |
| 1417 | cur_offset, end, locked_page, |
| 1418 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1419 | EXTENT_CLEAR_UNLOCK | |
| 1420 | EXTENT_CLEAR_DELALLOC | |
| 1421 | EXTENT_CLEAR_DIRTY | |
| 1422 | EXTENT_SET_WRITEBACK | |
| 1423 | EXTENT_END_WRITEBACK); |
| 1424 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1425 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1426 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1427 | } |
| 1428 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1429 | /* |
| 1430 | * extent_io.c call back to do delayed allocation processing |
| 1431 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1432 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1433 | u64 start, u64 end, int *page_started, |
| 1434 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1435 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1436 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1437 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1438 | |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1439 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1440 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1441 | page_started, 1, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1442 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1443 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1444 | page_started, 0, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1445 | } else if (!btrfs_test_opt(root, COMPRESS) && |
| 1446 | !(BTRFS_I(inode)->force_compress) && |
| 1447 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1448 | ret = cow_file_range(inode, locked_page, start, end, |
| 1449 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1450 | } else { |
| 1451 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1452 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1453 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1454 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1455 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1456 | return ret; |
| 1457 | } |
| 1458 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1459 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1460 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1461 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1462 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1463 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1464 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1465 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1466 | spin_lock(&BTRFS_I(inode)->lock); |
| 1467 | BTRFS_I(inode)->outstanding_extents++; |
| 1468 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | /* |
| 1472 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1473 | * extents so we can keep track of new extents that are just merged onto old |
| 1474 | * extents, such as when we are doing sequential writes, so we can properly |
| 1475 | * account for the metadata space we'll need. |
| 1476 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1477 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1478 | struct extent_state *new, |
| 1479 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1480 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1481 | /* not delalloc, ignore it */ |
| 1482 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1483 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1484 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1485 | spin_lock(&BTRFS_I(inode)->lock); |
| 1486 | BTRFS_I(inode)->outstanding_extents--; |
| 1487 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1488 | } |
| 1489 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1490 | /* |
| 1491 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1492 | * bytes in this file, and to maintain the list of inodes that |
| 1493 | * have pending delalloc work to be done. |
| 1494 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1495 | static void btrfs_set_bit_hook(struct inode *inode, |
| 1496 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1497 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1498 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1499 | /* |
| 1500 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1501 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1502 | * bit, which is only set or cleared with irqs on |
| 1503 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1504 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1505 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1506 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1507 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1508 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1509 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1510 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1511 | } else { |
| 1512 | spin_lock(&BTRFS_I(inode)->lock); |
| 1513 | BTRFS_I(inode)->outstanding_extents++; |
| 1514 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1515 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1516 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1517 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1518 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1519 | spin_lock(&BTRFS_I(inode)->lock); |
| 1520 | BTRFS_I(inode)->delalloc_bytes += len; |
| 1521 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1522 | &BTRFS_I(inode)->runtime_flags)) { |
| 1523 | spin_lock(&root->fs_info->delalloc_lock); |
| 1524 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1525 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1526 | &root->fs_info->delalloc_inodes); |
| 1527 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1528 | &BTRFS_I(inode)->runtime_flags); |
| 1529 | } |
| 1530 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1531 | } |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1532 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1533 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1534 | } |
| 1535 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1536 | /* |
| 1537 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1538 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1539 | static void btrfs_clear_bit_hook(struct inode *inode, |
| 1540 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1541 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1542 | /* |
| 1543 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1544 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1545 | * bit, which is only set or cleared with irqs on |
| 1546 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1547 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1548 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1549 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1550 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1551 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1552 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1553 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1554 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1555 | spin_lock(&BTRFS_I(inode)->lock); |
| 1556 | BTRFS_I(inode)->outstanding_extents--; |
| 1557 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1558 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1559 | |
| 1560 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1561 | btrfs_delalloc_release_metadata(inode, len); |
| 1562 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1563 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 1564 | && do_list) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1565 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1566 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1567 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1568 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1569 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1570 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1571 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1572 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1573 | &BTRFS_I(inode)->runtime_flags)) { |
| 1574 | spin_lock(&root->fs_info->delalloc_lock); |
| 1575 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1576 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1577 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1578 | &BTRFS_I(inode)->runtime_flags); |
| 1579 | } |
| 1580 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1581 | } |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1582 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1583 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1584 | } |
| 1585 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1586 | /* |
| 1587 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1588 | * we don't create bios that span stripes or chunks |
| 1589 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1590 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1591 | size_t size, struct bio *bio, |
| 1592 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1593 | { |
| 1594 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1595 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1596 | u64 length = 0; |
| 1597 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1598 | int ret; |
| 1599 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1600 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1601 | return 0; |
| 1602 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1603 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1604 | map_length = length; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1605 | ret = btrfs_map_block(root->fs_info, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1606 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1607 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1608 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1609 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1610 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1611 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1612 | } |
| 1613 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1614 | /* |
| 1615 | * in order to insert checksums into the metadata in large chunks, |
| 1616 | * we wait until bio submission time. All the pages in the bio are |
| 1617 | * checksummed and sums are attached onto the ordered extent record. |
| 1618 | * |
| 1619 | * At IO completion time the cums attached on the ordered extent record |
| 1620 | * are inserted into the btree |
| 1621 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1622 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1623 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1624 | unsigned long bio_flags, |
| 1625 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1626 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1627 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1628 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1629 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1630 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1631 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1632 | return 0; |
| 1633 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1634 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1635 | /* |
| 1636 | * in order to insert checksums into the metadata in large chunks, |
| 1637 | * we wait until bio submission time. All the pages in the bio are |
| 1638 | * checksummed and sums are attached onto the ordered extent record. |
| 1639 | * |
| 1640 | * At IO completion time the cums attached on the ordered extent record |
| 1641 | * are inserted into the btree |
| 1642 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1643 | 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] | 1644 | int mirror_num, unsigned long bio_flags, |
| 1645 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1646 | { |
| 1647 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1648 | int ret; |
| 1649 | |
| 1650 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1651 | if (ret) |
| 1652 | bio_endio(bio, ret); |
| 1653 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1654 | } |
| 1655 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1656 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1657 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1658 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1659 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1660 | 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] | 1661 | int mirror_num, unsigned long bio_flags, |
| 1662 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1663 | { |
| 1664 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1665 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1666 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1667 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1668 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1669 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1670 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1671 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1672 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1673 | metadata = 2; |
| 1674 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1675 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1676 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1677 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1678 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1679 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1680 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1681 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1682 | mirror_num, |
| 1683 | bio_flags); |
| 1684 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1685 | } else if (!skip_sum) { |
| 1686 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1687 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1688 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1689 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1690 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1691 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1692 | /* csum items have already been cloned */ |
| 1693 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1694 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1695 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1696 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1697 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1698 | bio_flags, bio_offset, |
| 1699 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1700 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1701 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1702 | } else if (!skip_sum) { |
| 1703 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1704 | if (ret) |
| 1705 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1706 | } |
| 1707 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1708 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1709 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1710 | |
| 1711 | out: |
| 1712 | if (ret < 0) |
| 1713 | bio_endio(bio, ret); |
| 1714 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1715 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1716 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1717 | /* |
| 1718 | * given a list of ordered sums record them in the inode. This happens |
| 1719 | * at IO completion time based on sums calculated at bio submission time. |
| 1720 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1721 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1722 | struct inode *inode, u64 file_offset, |
| 1723 | struct list_head *list) |
| 1724 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1725 | struct btrfs_ordered_sum *sum; |
| 1726 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1727 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1728 | btrfs_csum_file_blocks(trans, |
| 1729 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1730 | } |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1734 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1735 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1736 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1737 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1738 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1739 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1740 | } |
| 1741 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1742 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1743 | struct btrfs_writepage_fixup { |
| 1744 | struct page *page; |
| 1745 | struct btrfs_work work; |
| 1746 | }; |
| 1747 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1748 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1749 | { |
| 1750 | struct btrfs_writepage_fixup *fixup; |
| 1751 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1752 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1753 | struct page *page; |
| 1754 | struct inode *inode; |
| 1755 | u64 page_start; |
| 1756 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1757 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1758 | |
| 1759 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1760 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1761 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1762 | lock_page(page); |
| 1763 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1764 | ClearPageChecked(page); |
| 1765 | goto out_page; |
| 1766 | } |
| 1767 | |
| 1768 | inode = page->mapping->host; |
| 1769 | page_start = page_offset(page); |
| 1770 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1771 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1772 | 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] | 1773 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1774 | |
| 1775 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1776 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1777 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1778 | |
| 1779 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1780 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1781 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1782 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1783 | unlock_page(page); |
| 1784 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1785 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1786 | goto again; |
| 1787 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1788 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1789 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1790 | if (ret) { |
| 1791 | mapping_set_error(page->mapping, ret); |
| 1792 | end_extent_writepage(page, ret, page_start, page_end); |
| 1793 | ClearPageChecked(page); |
| 1794 | goto out; |
| 1795 | } |
| 1796 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1797 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1798 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1799 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1800 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1801 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1802 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1803 | out_page: |
| 1804 | unlock_page(page); |
| 1805 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1806 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | /* |
| 1810 | * There are a few paths in the higher layers of the kernel that directly |
| 1811 | * set the page dirty bit without asking the filesystem if it is a |
| 1812 | * good idea. This causes problems because we want to make sure COW |
| 1813 | * properly happens and the data=ordered rules are followed. |
| 1814 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1815 | * 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] | 1816 | * hasn't been properly setup for IO. We kick off an async process |
| 1817 | * to fix it up. The async helper will wait for ordered extents, set |
| 1818 | * the delalloc bit and make it safe to write the page. |
| 1819 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1820 | 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] | 1821 | { |
| 1822 | struct inode *inode = page->mapping->host; |
| 1823 | struct btrfs_writepage_fixup *fixup; |
| 1824 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1825 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1826 | /* this page is properly in the ordered list */ |
| 1827 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1828 | return 0; |
| 1829 | |
| 1830 | if (PageChecked(page)) |
| 1831 | return -EAGAIN; |
| 1832 | |
| 1833 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1834 | if (!fixup) |
| 1835 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1836 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1837 | SetPageChecked(page); |
| 1838 | page_cache_get(page); |
| 1839 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1840 | fixup->page = page; |
| 1841 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1842 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1843 | } |
| 1844 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1845 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1846 | struct inode *inode, u64 file_pos, |
| 1847 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1848 | u64 num_bytes, u64 ram_bytes, |
| 1849 | u8 compression, u8 encryption, |
| 1850 | u16 other_encoding, int extent_type) |
| 1851 | { |
| 1852 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1853 | struct btrfs_file_extent_item *fi; |
| 1854 | struct btrfs_path *path; |
| 1855 | struct extent_buffer *leaf; |
| 1856 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1857 | int ret; |
| 1858 | |
| 1859 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1860 | if (!path) |
| 1861 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1862 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1863 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1864 | |
| 1865 | /* |
| 1866 | * we may be replacing one extent in the tree with another. |
| 1867 | * The new extent is pinned in the extent map, and we don't want |
| 1868 | * to drop it from the cache until it is completely in the btree. |
| 1869 | * |
| 1870 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1871 | * the caller is expected to unpin it and allow it to be merged |
| 1872 | * with the others. |
| 1873 | */ |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1874 | ret = btrfs_drop_extents(trans, root, inode, file_pos, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 1875 | file_pos + num_bytes, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1876 | if (ret) |
| 1877 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1878 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1879 | ins.objectid = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1880 | ins.offset = file_pos; |
| 1881 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1882 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1883 | if (ret) |
| 1884 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1885 | leaf = path->nodes[0]; |
| 1886 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1887 | struct btrfs_file_extent_item); |
| 1888 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1889 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1890 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1891 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1892 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1893 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1894 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1895 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1896 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1897 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1898 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1899 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 1900 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1901 | |
| 1902 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1903 | |
| 1904 | ins.objectid = disk_bytenr; |
| 1905 | ins.offset = disk_num_bytes; |
| 1906 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1907 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1908 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1909 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1910 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1911 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1912 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1913 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1914 | } |
| 1915 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1916 | /* |
| 1917 | * helper function for btrfs_finish_ordered_io, this |
| 1918 | * just reads in some of the csum leaves to prime them into ram |
| 1919 | * before we start the transaction. It limits the amount of btree |
| 1920 | * reads required while inside the transaction. |
| 1921 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1922 | /* as ordered data IO finishes, this gets called so we can finish |
| 1923 | * an ordered extent if the range of bytes in the file it covers are |
| 1924 | * fully written. |
| 1925 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1926 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1927 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1928 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1929 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1930 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1931 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1932 | struct extent_state *cached_state = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1933 | int compress_type = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1934 | int ret; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1935 | bool nolock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1936 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1937 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1938 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1939 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 1940 | ret = -EIO; |
| 1941 | goto out; |
| 1942 | } |
| 1943 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1944 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1945 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 1946 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1947 | if (nolock) |
| 1948 | trans = btrfs_join_transaction_nolock(root); |
| 1949 | else |
| 1950 | trans = btrfs_join_transaction(root); |
| 1951 | if (IS_ERR(trans)) { |
| 1952 | ret = PTR_ERR(trans); |
| 1953 | trans = NULL; |
| 1954 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1955 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 1956 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 1957 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 1958 | if (ret) /* -ENOMEM or corruption */ |
| 1959 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1960 | goto out; |
| 1961 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1962 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1963 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 1964 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1965 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1966 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1967 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1968 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1969 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1970 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1971 | if (IS_ERR(trans)) { |
| 1972 | ret = PTR_ERR(trans); |
| 1973 | trans = NULL; |
| 1974 | goto out_unlock; |
| 1975 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1976 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1977 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1978 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1979 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1980 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1981 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1982 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1983 | ordered_extent->file_offset, |
| 1984 | ordered_extent->file_offset + |
| 1985 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1986 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 1987 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1988 | ret = insert_reserved_file_extent(trans, inode, |
| 1989 | ordered_extent->file_offset, |
| 1990 | ordered_extent->start, |
| 1991 | ordered_extent->disk_len, |
| 1992 | ordered_extent->len, |
| 1993 | ordered_extent->len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1994 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1995 | BTRFS_FILE_EXTENT_REG); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1996 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1997 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1998 | ordered_extent->file_offset, ordered_extent->len, |
| 1999 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2000 | if (ret < 0) { |
| 2001 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2002 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2003 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2004 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2005 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2006 | &ordered_extent->list); |
| 2007 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2008 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2009 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2010 | if (ret) { /* -ENOMEM or corruption */ |
| 2011 | btrfs_abort_transaction(trans, root, ret); |
| 2012 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2013 | } |
| 2014 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2015 | out_unlock: |
| 2016 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2017 | ordered_extent->file_offset + |
| 2018 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2019 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2020 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2021 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2022 | if (trans) |
| 2023 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2024 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2025 | if (ret) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2026 | clear_extent_uptodate(io_tree, ordered_extent->file_offset, |
| 2027 | ordered_extent->file_offset + |
| 2028 | ordered_extent->len - 1, NULL, GFP_NOFS); |
| 2029 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2030 | /* |
| 2031 | * If the ordered extent had an IOERR or something else went |
| 2032 | * wrong we need to return the space for this ordered extent |
| 2033 | * back to the allocator. |
| 2034 | */ |
| 2035 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2036 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2037 | btrfs_free_reserved_extent(root, ordered_extent->start, |
| 2038 | ordered_extent->disk_len); |
| 2039 | } |
| 2040 | |
| 2041 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2042 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2043 | * This needs to be done to make sure anybody waiting knows we are done |
| 2044 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2045 | */ |
| 2046 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2047 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2048 | /* once for us */ |
| 2049 | btrfs_put_ordered_extent(ordered_extent); |
| 2050 | /* once for the tree */ |
| 2051 | btrfs_put_ordered_extent(ordered_extent); |
| 2052 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2053 | return ret; |
| 2054 | } |
| 2055 | |
| 2056 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2057 | { |
| 2058 | struct btrfs_ordered_extent *ordered_extent; |
| 2059 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2060 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2061 | } |
| 2062 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2063 | 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] | 2064 | struct extent_state *state, int uptodate) |
| 2065 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2066 | struct inode *inode = page->mapping->host; |
| 2067 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2068 | struct btrfs_ordered_extent *ordered_extent = NULL; |
| 2069 | struct btrfs_workers *workers; |
| 2070 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2071 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2072 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2073 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2074 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2075 | end - start + 1, uptodate)) |
| 2076 | return 0; |
| 2077 | |
| 2078 | ordered_extent->work.func = finish_ordered_fn; |
| 2079 | ordered_extent->work.flags = 0; |
| 2080 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2081 | if (btrfs_is_free_space_inode(inode)) |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2082 | workers = &root->fs_info->endio_freespace_worker; |
| 2083 | else |
| 2084 | workers = &root->fs_info->endio_write_workers; |
| 2085 | btrfs_queue_worker(workers, &ordered_extent->work); |
| 2086 | |
| 2087 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2088 | } |
| 2089 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2090 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2091 | * 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] | 2092 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2093 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2094 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2095 | 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] | 2096 | struct extent_state *state, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2097 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2098 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2099 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2100 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2101 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 2102 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2103 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2104 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2105 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2106 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2107 | if (PageChecked(page)) { |
| 2108 | ClearPageChecked(page); |
| 2109 | goto good; |
| 2110 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2111 | |
| 2112 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 2113 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2114 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2115 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2116 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2117 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2118 | GFP_NOFS); |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 2122 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2123 | private = state->private; |
| 2124 | ret = 0; |
| 2125 | } else { |
| 2126 | ret = get_state_private(io_tree, start, &private); |
| 2127 | } |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2128 | kaddr = kmap_atomic(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2129 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2130 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2131 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2132 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 2133 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2134 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2135 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2136 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2137 | kunmap_atomic(kaddr); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2138 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2139 | return 0; |
| 2140 | |
| 2141 | zeroit: |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 2142 | 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] | 2143 | "private %llu\n", |
| 2144 | (unsigned long long)btrfs_ino(page->mapping->host), |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 2145 | (unsigned long long)start, csum, |
| 2146 | (unsigned long long)private); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2147 | memset(kaddr + offset, 1, end - start + 1); |
| 2148 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2149 | kunmap_atomic(kaddr); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2150 | if (private == 0) |
| 2151 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2152 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2153 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2154 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2155 | struct delayed_iput { |
| 2156 | struct list_head list; |
| 2157 | struct inode *inode; |
| 2158 | }; |
| 2159 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2160 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2161 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2162 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2163 | { |
| 2164 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2165 | struct delayed_iput *delayed; |
| 2166 | |
| 2167 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2168 | return; |
| 2169 | |
| 2170 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2171 | delayed->inode = inode; |
| 2172 | |
| 2173 | spin_lock(&fs_info->delayed_iput_lock); |
| 2174 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2175 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2176 | } |
| 2177 | |
| 2178 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2179 | { |
| 2180 | LIST_HEAD(list); |
| 2181 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2182 | struct delayed_iput *delayed; |
| 2183 | int empty; |
| 2184 | |
| 2185 | spin_lock(&fs_info->delayed_iput_lock); |
| 2186 | empty = list_empty(&fs_info->delayed_iputs); |
| 2187 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2188 | if (empty) |
| 2189 | return; |
| 2190 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2191 | spin_lock(&fs_info->delayed_iput_lock); |
| 2192 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2193 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2194 | |
| 2195 | while (!list_empty(&list)) { |
| 2196 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2197 | list_del(&delayed->list); |
| 2198 | iput(delayed->inode); |
| 2199 | kfree(delayed); |
| 2200 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2203 | enum btrfs_orphan_cleanup_state { |
| 2204 | ORPHAN_CLEANUP_STARTED = 1, |
| 2205 | ORPHAN_CLEANUP_DONE = 2, |
| 2206 | }; |
| 2207 | |
| 2208 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 2209 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2210 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2211 | * structure. |
| 2212 | */ |
| 2213 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2214 | struct btrfs_root *root) |
| 2215 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2216 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2217 | int ret; |
| 2218 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2219 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2220 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2221 | return; |
| 2222 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2223 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2224 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2225 | spin_unlock(&root->orphan_lock); |
| 2226 | return; |
| 2227 | } |
| 2228 | |
| 2229 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 2230 | spin_unlock(&root->orphan_lock); |
| 2231 | return; |
| 2232 | } |
| 2233 | |
| 2234 | block_rsv = root->orphan_block_rsv; |
| 2235 | root->orphan_block_rsv = NULL; |
| 2236 | spin_unlock(&root->orphan_lock); |
| 2237 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2238 | if (root->orphan_item_inserted && |
| 2239 | btrfs_root_refs(&root->root_item) > 0) { |
| 2240 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2241 | root->root_key.objectid); |
| 2242 | BUG_ON(ret); |
| 2243 | root->orphan_item_inserted = 0; |
| 2244 | } |
| 2245 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2246 | if (block_rsv) { |
| 2247 | WARN_ON(block_rsv->size > 0); |
| 2248 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2253 | * This creates an orphan entry for the given inode in case something goes |
| 2254 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2255 | * |
| 2256 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2257 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2258 | */ |
| 2259 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2260 | { |
| 2261 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2262 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2263 | int reserve = 0; |
| 2264 | int insert = 0; |
| 2265 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2266 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2267 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 2268 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2269 | if (!block_rsv) |
| 2270 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2271 | } |
| 2272 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2273 | spin_lock(&root->orphan_lock); |
| 2274 | if (!root->orphan_block_rsv) { |
| 2275 | root->orphan_block_rsv = block_rsv; |
| 2276 | } else if (block_rsv) { |
| 2277 | btrfs_free_block_rsv(root, block_rsv); |
| 2278 | block_rsv = NULL; |
| 2279 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2280 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2281 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2282 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2283 | #if 0 |
| 2284 | /* |
| 2285 | * For proper ENOSPC handling, we should do orphan |
| 2286 | * cleanup when mounting. But this introduces backward |
| 2287 | * compatibility issue. |
| 2288 | */ |
| 2289 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2290 | insert = 2; |
| 2291 | else |
| 2292 | insert = 1; |
| 2293 | #endif |
| 2294 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 2295 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2296 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2297 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 2298 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 2299 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2300 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2301 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2302 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2303 | /* grab metadata reservation from transaction handle */ |
| 2304 | if (reserve) { |
| 2305 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2306 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2310 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2311 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2312 | if (ret && ret != -EEXIST) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2313 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2314 | &BTRFS_I(inode)->runtime_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2315 | btrfs_abort_transaction(trans, root, ret); |
| 2316 | return ret; |
| 2317 | } |
| 2318 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2322 | if (insert >= 2) { |
| 2323 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2324 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2325 | if (ret && ret != -EEXIST) { |
| 2326 | btrfs_abort_transaction(trans, root, ret); |
| 2327 | return ret; |
| 2328 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2329 | } |
| 2330 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | /* |
| 2334 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2335 | * item for this particular inode. |
| 2336 | */ |
| 2337 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2338 | { |
| 2339 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2340 | int delete_item = 0; |
| 2341 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2342 | int ret = 0; |
| 2343 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2344 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2345 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2346 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2347 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2348 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 2349 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 2350 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2351 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2352 | spin_unlock(&root->orphan_lock); |
| 2353 | |
| 2354 | if (trans && delete_item) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2355 | ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2356 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2357 | } |
| 2358 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2359 | if (release_rsv) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2360 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2361 | atomic_dec(&root->orphan_inodes); |
| 2362 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2363 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2364 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | /* |
| 2368 | * this cleans up any orphans that may be left on the list from the last use |
| 2369 | * of this root. |
| 2370 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2371 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2372 | { |
| 2373 | struct btrfs_path *path; |
| 2374 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2375 | struct btrfs_key key, found_key; |
| 2376 | struct btrfs_trans_handle *trans; |
| 2377 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 2378 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2379 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2380 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2381 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2382 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2383 | |
| 2384 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2385 | if (!path) { |
| 2386 | ret = -ENOMEM; |
| 2387 | goto out; |
| 2388 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2389 | path->reada = -1; |
| 2390 | |
| 2391 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2392 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2393 | key.offset = (u64)-1; |
| 2394 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2395 | while (1) { |
| 2396 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2397 | if (ret < 0) |
| 2398 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2399 | |
| 2400 | /* |
| 2401 | * 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] | 2402 | * 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] | 2403 | * find the key and see if we have stuff that matches |
| 2404 | */ |
| 2405 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2406 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2407 | if (path->slots[0] == 0) |
| 2408 | break; |
| 2409 | path->slots[0]--; |
| 2410 | } |
| 2411 | |
| 2412 | /* pull out the item */ |
| 2413 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2414 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2415 | |
| 2416 | /* make sure the item matches what we want */ |
| 2417 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2418 | break; |
| 2419 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2420 | break; |
| 2421 | |
| 2422 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2423 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2424 | |
| 2425 | /* |
| 2426 | * this is where we are basically btrfs_lookup, without the |
| 2427 | * crossing root thing. we store the inode number in the |
| 2428 | * offset of the orphan item. |
| 2429 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 2430 | |
| 2431 | if (found_key.offset == last_objectid) { |
| 2432 | printk(KERN_ERR "btrfs: Error removing orphan entry, " |
| 2433 | "stopping orphan cleanup\n"); |
| 2434 | ret = -EINVAL; |
| 2435 | goto out; |
| 2436 | } |
| 2437 | |
| 2438 | last_objectid = found_key.offset; |
| 2439 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2440 | found_key.objectid = found_key.offset; |
| 2441 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2442 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2443 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2444 | ret = PTR_RET(inode); |
| 2445 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2446 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2447 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 2448 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 2449 | struct btrfs_root *dead_root; |
| 2450 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2451 | int is_dead_root = 0; |
| 2452 | |
| 2453 | /* |
| 2454 | * this is an orphan in the tree root. Currently these |
| 2455 | * could come from 2 sources: |
| 2456 | * a) a snapshot deletion in progress |
| 2457 | * b) a free space cache inode |
| 2458 | * We need to distinguish those two, as the snapshot |
| 2459 | * orphan must not get deleted. |
| 2460 | * find_dead_roots already ran before us, so if this |
| 2461 | * is a snapshot deletion, we should find the root |
| 2462 | * in the dead_roots list |
| 2463 | */ |
| 2464 | spin_lock(&fs_info->trans_lock); |
| 2465 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 2466 | root_list) { |
| 2467 | if (dead_root->root_key.objectid == |
| 2468 | found_key.objectid) { |
| 2469 | is_dead_root = 1; |
| 2470 | break; |
| 2471 | } |
| 2472 | } |
| 2473 | spin_unlock(&fs_info->trans_lock); |
| 2474 | if (is_dead_root) { |
| 2475 | /* prevent this orphan from being found again */ |
| 2476 | key.offset = found_key.objectid - 1; |
| 2477 | continue; |
| 2478 | } |
| 2479 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2480 | /* |
| 2481 | * Inode is already gone but the orphan item is still there, |
| 2482 | * kill the orphan item. |
| 2483 | */ |
| 2484 | if (ret == -ESTALE) { |
| 2485 | trans = btrfs_start_transaction(root, 1); |
| 2486 | if (IS_ERR(trans)) { |
| 2487 | ret = PTR_ERR(trans); |
| 2488 | goto out; |
| 2489 | } |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2490 | printk(KERN_ERR "auto deleting %Lu\n", |
| 2491 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2492 | ret = btrfs_del_orphan_item(trans, root, |
| 2493 | found_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2494 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2495 | btrfs_end_transaction(trans, root); |
| 2496 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2497 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2498 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2499 | /* |
| 2500 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2501 | * the proper thing when we hit it |
| 2502 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2503 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2504 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame^] | 2505 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2506 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2507 | /* if we have links, this was a truncate, lets do that */ |
| 2508 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 2509 | if (!S_ISREG(inode->i_mode)) { |
| 2510 | WARN_ON(1); |
| 2511 | iput(inode); |
| 2512 | continue; |
| 2513 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2514 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 2515 | |
| 2516 | /* 1 for the orphan item deletion. */ |
| 2517 | trans = btrfs_start_transaction(root, 1); |
| 2518 | if (IS_ERR(trans)) { |
| 2519 | ret = PTR_ERR(trans); |
| 2520 | goto out; |
| 2521 | } |
| 2522 | ret = btrfs_orphan_add(trans, inode); |
| 2523 | btrfs_end_transaction(trans, root); |
| 2524 | if (ret) |
| 2525 | goto out; |
| 2526 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2527 | ret = btrfs_truncate(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2528 | } else { |
| 2529 | nr_unlink++; |
| 2530 | } |
| 2531 | |
| 2532 | /* this will do delete_inode and everything for us */ |
| 2533 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2534 | if (ret) |
| 2535 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2536 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 2537 | /* release the path since we're done with it */ |
| 2538 | btrfs_release_path(path); |
| 2539 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2540 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 2541 | |
| 2542 | if (root->orphan_block_rsv) |
| 2543 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 2544 | (u64)-1); |
| 2545 | |
| 2546 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2547 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2548 | if (!IS_ERR(trans)) |
| 2549 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2550 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2551 | |
| 2552 | if (nr_unlink) |
| 2553 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2554 | if (nr_truncate) |
| 2555 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2556 | |
| 2557 | out: |
| 2558 | if (ret) |
| 2559 | printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret); |
| 2560 | btrfs_free_path(path); |
| 2561 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2562 | } |
| 2563 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2564 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2565 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2566 | * don't find any xattrs, we know there can't be any acls. |
| 2567 | * |
| 2568 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2569 | */ |
| 2570 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2571 | int slot, u64 objectid) |
| 2572 | { |
| 2573 | u32 nritems = btrfs_header_nritems(leaf); |
| 2574 | struct btrfs_key found_key; |
| 2575 | int scanned = 0; |
| 2576 | |
| 2577 | slot++; |
| 2578 | while (slot < nritems) { |
| 2579 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2580 | |
| 2581 | /* we found a different objectid, there must not be acls */ |
| 2582 | if (found_key.objectid != objectid) |
| 2583 | return 0; |
| 2584 | |
| 2585 | /* we found an xattr, assume we've got an acl */ |
| 2586 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2587 | return 1; |
| 2588 | |
| 2589 | /* |
| 2590 | * we found a key greater than an xattr key, there can't |
| 2591 | * be any acls later on |
| 2592 | */ |
| 2593 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2594 | return 0; |
| 2595 | |
| 2596 | slot++; |
| 2597 | scanned++; |
| 2598 | |
| 2599 | /* |
| 2600 | * it goes inode, inode backrefs, xattrs, extents, |
| 2601 | * so if there are a ton of hard links to an inode there can |
| 2602 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2603 | * this is just an optimization |
| 2604 | */ |
| 2605 | if (scanned >= 8) |
| 2606 | break; |
| 2607 | } |
| 2608 | /* we hit the end of the leaf before we found an xattr or |
| 2609 | * something larger than an xattr. We have to assume the inode |
| 2610 | * has acls |
| 2611 | */ |
| 2612 | return 1; |
| 2613 | } |
| 2614 | |
| 2615 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2616 | * read an inode from the btree into the in-memory inode |
| 2617 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2618 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2619 | { |
| 2620 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2621 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2622 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2623 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2624 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2625 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2626 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2627 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2628 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2629 | bool filled = false; |
| 2630 | |
| 2631 | ret = btrfs_fill_inode(inode, &rdev); |
| 2632 | if (!ret) |
| 2633 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2634 | |
| 2635 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 2636 | if (!path) |
| 2637 | goto make_bad; |
| 2638 | |
Josef Bacik | d90c732 | 2011-05-17 09:50:54 -0400 | [diff] [blame] | 2639 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2640 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2641 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2642 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2643 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2644 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2645 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2646 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2647 | |
| 2648 | if (filled) |
| 2649 | goto cache_acl; |
| 2650 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2651 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2652 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2653 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2654 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 2655 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 2656 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2657 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2658 | |
| 2659 | tspec = btrfs_inode_atime(inode_item); |
| 2660 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2661 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2662 | |
| 2663 | tspec = btrfs_inode_mtime(inode_item); |
| 2664 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2665 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2666 | |
| 2667 | tspec = btrfs_inode_ctime(inode_item); |
| 2668 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2669 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2670 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2671 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2672 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2673 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 2674 | |
| 2675 | /* |
| 2676 | * If we were modified in the current generation and evicted from memory |
| 2677 | * and then re-read we need to do a full sync since we don't have any |
| 2678 | * idea about which extents were modified before we were evicted from |
| 2679 | * cache. |
| 2680 | */ |
| 2681 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 2682 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 2683 | &BTRFS_I(inode)->runtime_flags); |
| 2684 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 2685 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2686 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2687 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2688 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2689 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2690 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2691 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2692 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2693 | /* |
| 2694 | * try to precache a NULL acl entry for files that don't have |
| 2695 | * any xattrs or acls |
| 2696 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2697 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
| 2698 | btrfs_ino(inode)); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 2699 | if (!maybe_acls) |
| 2700 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2701 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2702 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2703 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2704 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2705 | case S_IFREG: |
| 2706 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2707 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2708 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2709 | inode->i_fop = &btrfs_file_operations; |
| 2710 | inode->i_op = &btrfs_file_inode_operations; |
| 2711 | break; |
| 2712 | case S_IFDIR: |
| 2713 | inode->i_fop = &btrfs_dir_file_operations; |
| 2714 | if (root == root->fs_info->tree_root) |
| 2715 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2716 | else |
| 2717 | inode->i_op = &btrfs_dir_inode_operations; |
| 2718 | break; |
| 2719 | case S_IFLNK: |
| 2720 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2721 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2722 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2723 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2724 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2725 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2726 | init_special_inode(inode, inode->i_mode, rdev); |
| 2727 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2728 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2729 | |
| 2730 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2731 | return; |
| 2732 | |
| 2733 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2734 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2735 | make_bad_inode(inode); |
| 2736 | } |
| 2737 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2738 | /* |
| 2739 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2740 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2741 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2742 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2743 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2744 | struct inode *inode) |
| 2745 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 2746 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2747 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 2748 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2749 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 2750 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 2751 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 2752 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 2753 | &token); |
| 2754 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 2755 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2756 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 2757 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2758 | inode->i_atime.tv_sec, &token); |
| 2759 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2760 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2761 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 2762 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2763 | inode->i_mtime.tv_sec, &token); |
| 2764 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2765 | inode->i_mtime.tv_nsec, &token); |
| 2766 | |
| 2767 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2768 | inode->i_ctime.tv_sec, &token); |
| 2769 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2770 | inode->i_ctime.tv_nsec, &token); |
| 2771 | |
| 2772 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 2773 | &token); |
| 2774 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 2775 | &token); |
| 2776 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 2777 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 2778 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 2779 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 2780 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2781 | } |
| 2782 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2783 | /* |
| 2784 | * copy everything in the in-memory inode into the btree. |
| 2785 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2786 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2787 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2788 | { |
| 2789 | struct btrfs_inode_item *inode_item; |
| 2790 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2791 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2792 | int ret; |
| 2793 | |
| 2794 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2795 | if (!path) |
| 2796 | return -ENOMEM; |
| 2797 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2798 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2799 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 2800 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2801 | if (ret) { |
| 2802 | if (ret > 0) |
| 2803 | ret = -ENOENT; |
| 2804 | goto failed; |
| 2805 | } |
| 2806 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2807 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2808 | leaf = path->nodes[0]; |
| 2809 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2810 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2811 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2812 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2813 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2814 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2815 | ret = 0; |
| 2816 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2817 | btrfs_free_path(path); |
| 2818 | return ret; |
| 2819 | } |
| 2820 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2821 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2822 | * copy everything in the in-memory inode into the btree. |
| 2823 | */ |
| 2824 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2825 | struct btrfs_root *root, struct inode *inode) |
| 2826 | { |
| 2827 | int ret; |
| 2828 | |
| 2829 | /* |
| 2830 | * If the inode is a free space inode, we can deadlock during commit |
| 2831 | * if we put it into the delayed code. |
| 2832 | * |
| 2833 | * The data relocation inode should also be directly updated |
| 2834 | * without delay |
| 2835 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2836 | if (!btrfs_is_free_space_inode(inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2837 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 2838 | btrfs_update_root_times(trans, root); |
| 2839 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2840 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 2841 | if (!ret) |
| 2842 | btrfs_set_inode_last_trans(trans, inode); |
| 2843 | return ret; |
| 2844 | } |
| 2845 | |
| 2846 | return btrfs_update_inode_item(trans, root, inode); |
| 2847 | } |
| 2848 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 2849 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 2850 | struct btrfs_root *root, |
| 2851 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2852 | { |
| 2853 | int ret; |
| 2854 | |
| 2855 | ret = btrfs_update_inode(trans, root, inode); |
| 2856 | if (ret == -ENOSPC) |
| 2857 | return btrfs_update_inode_item(trans, root, inode); |
| 2858 | return ret; |
| 2859 | } |
| 2860 | |
| 2861 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2862 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2863 | * recovery code. It remove a link in a directory with a given name, and |
| 2864 | * also drops the back refs in the inode to the directory |
| 2865 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2866 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2867 | struct btrfs_root *root, |
| 2868 | struct inode *dir, struct inode *inode, |
| 2869 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2870 | { |
| 2871 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2872 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2873 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2874 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2875 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2876 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2877 | u64 ino = btrfs_ino(inode); |
| 2878 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2879 | |
| 2880 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2881 | if (!path) { |
| 2882 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 2883 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2884 | } |
| 2885 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2886 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2887 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2888 | name, name_len, -1); |
| 2889 | if (IS_ERR(di)) { |
| 2890 | ret = PTR_ERR(di); |
| 2891 | goto err; |
| 2892 | } |
| 2893 | if (!di) { |
| 2894 | ret = -ENOENT; |
| 2895 | goto err; |
| 2896 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2897 | leaf = path->nodes[0]; |
| 2898 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2899 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2900 | if (ret) |
| 2901 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2902 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2903 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2904 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 2905 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2906 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2907 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2908 | "inode %llu parent %llu\n", name_len, name, |
| 2909 | (unsigned long long)ino, (unsigned long long)dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2910 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2911 | goto err; |
| 2912 | } |
| 2913 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2914 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2915 | if (ret) { |
| 2916 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2917 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2918 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2919 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2920 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2921 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2922 | if (ret != 0 && ret != -ENOENT) { |
| 2923 | btrfs_abort_transaction(trans, root, ret); |
| 2924 | goto err; |
| 2925 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2926 | |
| 2927 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2928 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 2929 | if (ret == -ENOENT) |
| 2930 | ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2931 | err: |
| 2932 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2933 | if (ret) |
| 2934 | goto out; |
| 2935 | |
| 2936 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 2937 | inode_inc_iversion(inode); |
| 2938 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2939 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 2940 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2941 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2942 | return ret; |
| 2943 | } |
| 2944 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2945 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2946 | struct btrfs_root *root, |
| 2947 | struct inode *dir, struct inode *inode, |
| 2948 | const char *name, int name_len) |
| 2949 | { |
| 2950 | int ret; |
| 2951 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 2952 | if (!ret) { |
| 2953 | btrfs_drop_nlink(inode); |
| 2954 | ret = btrfs_update_inode(trans, root, inode); |
| 2955 | } |
| 2956 | return ret; |
| 2957 | } |
| 2958 | |
| 2959 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2960 | /* helper to check if there is any shared block in the path */ |
| 2961 | static int check_path_shared(struct btrfs_root *root, |
| 2962 | struct btrfs_path *path) |
| 2963 | { |
| 2964 | struct extent_buffer *eb; |
| 2965 | int level; |
Dan Carpenter | 0e4dcbef | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 2966 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2967 | |
| 2968 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2969 | int ret; |
| 2970 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2971 | if (!path->nodes[level]) |
| 2972 | break; |
| 2973 | eb = path->nodes[level]; |
| 2974 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2975 | continue; |
| 2976 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2977 | &refs, NULL); |
| 2978 | if (refs > 1) |
| 2979 | return 1; |
| 2980 | } |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2981 | return 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | /* |
| 2985 | * helper to start transaction for unlink and rmdir. |
| 2986 | * |
| 2987 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2988 | * so in enospc case, we should make sure they will free space before |
| 2989 | * allowing them to use the global metadata reservation. |
| 2990 | */ |
| 2991 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2992 | struct dentry *dentry) |
| 2993 | { |
| 2994 | struct btrfs_trans_handle *trans; |
| 2995 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2996 | struct btrfs_path *path; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2997 | struct btrfs_dir_item *di; |
| 2998 | struct inode *inode = dentry->d_inode; |
| 2999 | u64 index; |
| 3000 | int check_link = 1; |
| 3001 | int err = -ENOSPC; |
| 3002 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3003 | u64 ino = btrfs_ino(inode); |
| 3004 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3005 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3006 | /* |
| 3007 | * 1 for the possible orphan item |
| 3008 | * 1 for the dir item |
| 3009 | * 1 for the dir index |
| 3010 | * 1 for the inode ref |
| 3011 | * 1 for the inode ref in the tree log |
| 3012 | * 2 for the dir entries in the log |
| 3013 | * 1 for the inode |
| 3014 | */ |
| 3015 | trans = btrfs_start_transaction(root, 8); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3016 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3017 | return trans; |
| 3018 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3019 | if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3020 | return ERR_PTR(-ENOSPC); |
| 3021 | |
| 3022 | /* check if there is someone else holds reference */ |
| 3023 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 3024 | return ERR_PTR(-ENOSPC); |
| 3025 | |
| 3026 | if (atomic_read(&inode->i_count) > 2) |
| 3027 | return ERR_PTR(-ENOSPC); |
| 3028 | |
| 3029 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 3030 | return ERR_PTR(-ENOSPC); |
| 3031 | |
| 3032 | path = btrfs_alloc_path(); |
| 3033 | if (!path) { |
| 3034 | root->fs_info->enospc_unlink = 0; |
| 3035 | return ERR_PTR(-ENOMEM); |
| 3036 | } |
| 3037 | |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 3038 | /* 1 for the orphan item */ |
| 3039 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3040 | if (IS_ERR(trans)) { |
| 3041 | btrfs_free_path(path); |
| 3042 | root->fs_info->enospc_unlink = 0; |
| 3043 | return trans; |
| 3044 | } |
| 3045 | |
| 3046 | path->skip_locking = 1; |
| 3047 | path->search_commit_root = 1; |
| 3048 | |
| 3049 | ret = btrfs_lookup_inode(trans, root, path, |
| 3050 | &BTRFS_I(dir)->location, 0); |
| 3051 | if (ret < 0) { |
| 3052 | err = ret; |
| 3053 | goto out; |
| 3054 | } |
| 3055 | if (ret == 0) { |
| 3056 | if (check_path_shared(root, path)) |
| 3057 | goto out; |
| 3058 | } else { |
| 3059 | check_link = 0; |
| 3060 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3061 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3062 | |
| 3063 | ret = btrfs_lookup_inode(trans, root, path, |
| 3064 | &BTRFS_I(inode)->location, 0); |
| 3065 | if (ret < 0) { |
| 3066 | err = ret; |
| 3067 | goto out; |
| 3068 | } |
| 3069 | if (ret == 0) { |
| 3070 | if (check_path_shared(root, path)) |
| 3071 | goto out; |
| 3072 | } else { |
| 3073 | check_link = 0; |
| 3074 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3075 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3076 | |
| 3077 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 3078 | ret = btrfs_lookup_file_extent(trans, root, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3079 | ino, (u64)-1, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3080 | if (ret < 0) { |
| 3081 | err = ret; |
| 3082 | goto out; |
| 3083 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3084 | BUG_ON(ret == 0); /* Corruption */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3085 | if (check_path_shared(root, path)) |
| 3086 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3087 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | if (!check_link) { |
| 3091 | err = 0; |
| 3092 | goto out; |
| 3093 | } |
| 3094 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3095 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3096 | dentry->d_name.name, dentry->d_name.len, 0); |
| 3097 | if (IS_ERR(di)) { |
| 3098 | err = PTR_ERR(di); |
| 3099 | goto out; |
| 3100 | } |
| 3101 | if (di) { |
| 3102 | if (check_path_shared(root, path)) |
| 3103 | goto out; |
| 3104 | } else { |
| 3105 | err = 0; |
| 3106 | goto out; |
| 3107 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3108 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3109 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 3110 | ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name, |
| 3111 | dentry->d_name.len, ino, dir_ino, 0, |
| 3112 | &index); |
| 3113 | if (ret) { |
| 3114 | err = ret; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3115 | goto out; |
| 3116 | } |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 3117 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3118 | if (check_path_shared(root, path)) |
| 3119 | goto out; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 3120 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3121 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3122 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3123 | /* |
| 3124 | * This is a commit root search, if we can lookup inode item and other |
| 3125 | * relative items in the commit root, it means the transaction of |
| 3126 | * dir/file creation has been committed, and the dir index item that we |
| 3127 | * delay to insert has also been inserted into the commit root. So |
| 3128 | * we needn't worry about the delayed insertion of the dir index item |
| 3129 | * here. |
| 3130 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3131 | di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3132 | dentry->d_name.name, dentry->d_name.len, 0); |
| 3133 | if (IS_ERR(di)) { |
| 3134 | err = PTR_ERR(di); |
| 3135 | goto out; |
| 3136 | } |
| 3137 | BUG_ON(ret == -ENOENT); |
| 3138 | if (check_path_shared(root, path)) |
| 3139 | goto out; |
| 3140 | |
| 3141 | err = 0; |
| 3142 | out: |
| 3143 | btrfs_free_path(path); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 3144 | /* Migrate the orphan reservation over */ |
| 3145 | if (!err) |
| 3146 | err = btrfs_block_rsv_migrate(trans->block_rsv, |
| 3147 | &root->fs_info->global_block_rsv, |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3148 | trans->bytes_reserved); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 3149 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3150 | if (err) { |
| 3151 | btrfs_end_transaction(trans, root); |
| 3152 | root->fs_info->enospc_unlink = 0; |
| 3153 | return ERR_PTR(err); |
| 3154 | } |
| 3155 | |
| 3156 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 3157 | return trans; |
| 3158 | } |
| 3159 | |
| 3160 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 3161 | struct btrfs_root *root) |
| 3162 | { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3163 | if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) { |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3164 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 3165 | trans->bytes_reserved); |
| 3166 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3167 | BUG_ON(!root->fs_info->enospc_unlink); |
| 3168 | root->fs_info->enospc_unlink = 0; |
| 3169 | } |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 3170 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3171 | } |
| 3172 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3173 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3174 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3175 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3176 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3177 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3178 | int ret; |
| 3179 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3180 | trans = __unlink_start_trans(dir, dentry); |
| 3181 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3182 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3183 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3184 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3185 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3186 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3187 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3188 | if (ret) |
| 3189 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3190 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3191 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3192 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3193 | if (ret) |
| 3194 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3195 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3196 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3197 | out: |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3198 | __unlink_end_trans(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3199 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3200 | return ret; |
| 3201 | } |
| 3202 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3203 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3204 | struct btrfs_root *root, |
| 3205 | struct inode *dir, u64 objectid, |
| 3206 | const char *name, int name_len) |
| 3207 | { |
| 3208 | struct btrfs_path *path; |
| 3209 | struct extent_buffer *leaf; |
| 3210 | struct btrfs_dir_item *di; |
| 3211 | struct btrfs_key key; |
| 3212 | u64 index; |
| 3213 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3214 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3215 | |
| 3216 | path = btrfs_alloc_path(); |
| 3217 | if (!path) |
| 3218 | return -ENOMEM; |
| 3219 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3220 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3221 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3222 | if (IS_ERR_OR_NULL(di)) { |
| 3223 | if (!di) |
| 3224 | ret = -ENOENT; |
| 3225 | else |
| 3226 | ret = PTR_ERR(di); |
| 3227 | goto out; |
| 3228 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3229 | |
| 3230 | leaf = path->nodes[0]; |
| 3231 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3232 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3233 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3234 | if (ret) { |
| 3235 | btrfs_abort_transaction(trans, root, ret); |
| 3236 | goto out; |
| 3237 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3238 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3239 | |
| 3240 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3241 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3242 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3243 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3244 | if (ret != -ENOENT) { |
| 3245 | btrfs_abort_transaction(trans, root, ret); |
| 3246 | goto out; |
| 3247 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3248 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3249 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3250 | if (IS_ERR_OR_NULL(di)) { |
| 3251 | if (!di) |
| 3252 | ret = -ENOENT; |
| 3253 | else |
| 3254 | ret = PTR_ERR(di); |
| 3255 | btrfs_abort_transaction(trans, root, ret); |
| 3256 | goto out; |
| 3257 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3258 | |
| 3259 | leaf = path->nodes[0]; |
| 3260 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3261 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3262 | index = key.offset; |
| 3263 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3264 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3265 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3266 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3267 | if (ret) { |
| 3268 | btrfs_abort_transaction(trans, root, ret); |
| 3269 | goto out; |
| 3270 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3271 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3272 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3273 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3274 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3275 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3276 | if (ret) |
| 3277 | btrfs_abort_transaction(trans, root, ret); |
| 3278 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3279 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3280 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3281 | } |
| 3282 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3283 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3284 | { |
| 3285 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3286 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3287 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3288 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3289 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3290 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3291 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3292 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 3293 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3294 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3295 | trans = __unlink_start_trans(dir, dentry); |
| 3296 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3297 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3298 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3299 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3300 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3301 | BTRFS_I(inode)->location.objectid, |
| 3302 | dentry->d_name.name, |
| 3303 | dentry->d_name.len); |
| 3304 | goto out; |
| 3305 | } |
| 3306 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3307 | err = btrfs_orphan_add(trans, inode); |
| 3308 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3309 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3310 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3311 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3312 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3313 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3314 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3315 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3316 | out: |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3317 | __unlink_end_trans(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3318 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3319 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3320 | return err; |
| 3321 | } |
| 3322 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3323 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3324 | * this can truncate away extent items, csum items and directory items. |
| 3325 | * 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] | 3326 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3327 | * |
| 3328 | * csum items that cross the new i_size are truncated to the new size |
| 3329 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3330 | * |
| 3331 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3332 | * 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] | 3333 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3334 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3335 | struct btrfs_root *root, |
| 3336 | struct inode *inode, |
| 3337 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3338 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3339 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3340 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3341 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3342 | struct btrfs_key key; |
| 3343 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3344 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3345 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3346 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3347 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3348 | u64 mask = root->sectorsize - 1; |
| 3349 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3350 | int found_extent; |
| 3351 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3352 | int pending_del_nr = 0; |
| 3353 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3354 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3355 | int ret; |
| 3356 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3357 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3358 | |
| 3359 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3360 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 3361 | path = btrfs_alloc_path(); |
| 3362 | if (!path) |
| 3363 | return -ENOMEM; |
| 3364 | path->reada = -1; |
| 3365 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3366 | /* |
| 3367 | * We want to drop from the next block forward in case this new size is |
| 3368 | * not block aligned since we will be keeping the last block of the |
| 3369 | * extent just the way it is. |
| 3370 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3371 | if (root->ref_cows || root == root->fs_info->tree_root) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3372 | btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3373 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3374 | /* |
| 3375 | * This function is also used to drop the items in the log tree before |
| 3376 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 3377 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 3378 | * items. |
| 3379 | */ |
| 3380 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 3381 | btrfs_kill_delayed_inode_items(inode); |
| 3382 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3383 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3384 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3385 | key.type = (u8)-1; |
| 3386 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3387 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3388 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3389 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3390 | if (ret < 0) { |
| 3391 | err = ret; |
| 3392 | goto out; |
| 3393 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3394 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3395 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3396 | /* there are no items in the tree for us to truncate, we're |
| 3397 | * done |
| 3398 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3399 | if (path->slots[0] == 0) |
| 3400 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3401 | path->slots[0]--; |
| 3402 | } |
| 3403 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3404 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3405 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3406 | leaf = path->nodes[0]; |
| 3407 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3408 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3409 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3410 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3411 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3412 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3413 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3414 | break; |
| 3415 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3416 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3417 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3418 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3419 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3420 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 3421 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3422 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3423 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3424 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3425 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3426 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3427 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3428 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3429 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3430 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3431 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3432 | } else { |
| 3433 | if (item_end < new_size) |
| 3434 | break; |
| 3435 | if (found_key.offset >= new_size) |
| 3436 | del_item = 1; |
| 3437 | else |
| 3438 | del_item = 0; |
| 3439 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3440 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3441 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3442 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3443 | goto delete; |
| 3444 | |
| 3445 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3446 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3447 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 3448 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3449 | u64 orig_num_bytes = |
| 3450 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3451 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3452 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3453 | extent_num_bytes = extent_num_bytes & |
| 3454 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3455 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3456 | extent_num_bytes); |
| 3457 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3458 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3459 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3460 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3461 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3462 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3463 | extent_num_bytes = |
| 3464 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3465 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3466 | extent_offset = found_key.offset - |
| 3467 | btrfs_file_extent_offset(leaf, fi); |
| 3468 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3469 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3470 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3471 | if (extent_start != 0) { |
| 3472 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3473 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3474 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3475 | } |
| 3476 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3477 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3478 | /* |
| 3479 | * we can't truncate inline items that have had |
| 3480 | * special encodings |
| 3481 | */ |
| 3482 | if (!del_item && |
| 3483 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3484 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3485 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3486 | u32 size = new_size - found_key.offset; |
| 3487 | |
| 3488 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3489 | inode_sub_bytes(inode, item_end + 1 - |
| 3490 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3491 | } |
| 3492 | size = |
| 3493 | btrfs_file_extent_calc_inline_size(size); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3494 | btrfs_truncate_item(trans, root, path, |
| 3495 | size, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3496 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3497 | inode_sub_bytes(inode, item_end + 1 - |
| 3498 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3499 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3500 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3501 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3502 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3503 | if (!pending_del_nr) { |
| 3504 | /* no pending yet, add ourselves */ |
| 3505 | pending_del_slot = path->slots[0]; |
| 3506 | pending_del_nr = 1; |
| 3507 | } else if (pending_del_nr && |
| 3508 | path->slots[0] + 1 == pending_del_slot) { |
| 3509 | /* hop on the pending chunk */ |
| 3510 | pending_del_nr++; |
| 3511 | pending_del_slot = path->slots[0]; |
| 3512 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3513 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3514 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3515 | } else { |
| 3516 | break; |
| 3517 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3518 | if (found_extent && (root->ref_cows || |
| 3519 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3520 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3521 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3522 | extent_num_bytes, 0, |
| 3523 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3524 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3525 | BUG_ON(ret); |
| 3526 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3527 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3528 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3529 | break; |
| 3530 | |
| 3531 | if (path->slots[0] == 0 || |
| 3532 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3533 | if (pending_del_nr) { |
| 3534 | ret = btrfs_del_items(trans, root, path, |
| 3535 | pending_del_slot, |
| 3536 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3537 | if (ret) { |
| 3538 | btrfs_abort_transaction(trans, |
| 3539 | root, ret); |
| 3540 | goto error; |
| 3541 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3542 | pending_del_nr = 0; |
| 3543 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3544 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3545 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3546 | } else { |
| 3547 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3548 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3549 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3550 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3551 | if (pending_del_nr) { |
| 3552 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3553 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3554 | if (ret) |
| 3555 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3556 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3557 | error: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3558 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3559 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3560 | } |
| 3561 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3562 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 3563 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 3564 | * @inode - inode that we're zeroing |
| 3565 | * @from - the offset to start zeroing |
| 3566 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 3567 | * offset |
| 3568 | * @front - zero up to the offset instead of from the offset on |
| 3569 | * |
| 3570 | * This will find the page for the "from" offset and cow the page and zero the |
| 3571 | * 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] | 3572 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 3573 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 3574 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3575 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 3576 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3577 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3578 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3579 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3580 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3581 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3582 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3583 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3584 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3585 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 3586 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3587 | int ret = 0; |
| 3588 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3589 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3590 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 3591 | if ((offset & (blocksize - 1)) == 0 && |
| 3592 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3593 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3594 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3595 | if (ret) |
| 3596 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3597 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3598 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 3599 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3600 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3601 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 3602 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3603 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3604 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3605 | |
| 3606 | page_start = page_offset(page); |
| 3607 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3608 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3609 | if (!PageUptodate(page)) { |
| 3610 | ret = btrfs_readpage(NULL, page); |
| 3611 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3612 | if (page->mapping != mapping) { |
| 3613 | unlock_page(page); |
| 3614 | page_cache_release(page); |
| 3615 | goto again; |
| 3616 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3617 | if (!PageUptodate(page)) { |
| 3618 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3619 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3620 | } |
| 3621 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3622 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3623 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 3624 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3625 | set_page_extent_mapped(page); |
| 3626 | |
| 3627 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3628 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3629 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3630 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3631 | unlock_page(page); |
| 3632 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3633 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3634 | btrfs_put_ordered_extent(ordered); |
| 3635 | goto again; |
| 3636 | } |
| 3637 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3638 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 3639 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 3640 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3641 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3642 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3643 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3644 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3645 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3646 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3647 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3648 | goto out_unlock; |
| 3649 | } |
| 3650 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3651 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 3652 | if (!len) |
| 3653 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3654 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 3655 | if (front) |
| 3656 | memset(kaddr, 0, offset); |
| 3657 | else |
| 3658 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3659 | flush_dcache_page(page); |
| 3660 | kunmap(page); |
| 3661 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3662 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3663 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3664 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3665 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3666 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3667 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3668 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3669 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3670 | unlock_page(page); |
| 3671 | page_cache_release(page); |
| 3672 | out: |
| 3673 | return ret; |
| 3674 | } |
| 3675 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 3676 | /* |
| 3677 | * This function puts in dummy file extents for the area we're creating a hole |
| 3678 | * for. So if we are truncating this file to a larger size we need to insert |
| 3679 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 3680 | * the range between oldsize and size |
| 3681 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3682 | 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] | 3683 | { |
| 3684 | struct btrfs_trans_handle *trans; |
| 3685 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3686 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3687 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3688 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3689 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3690 | u64 mask = root->sectorsize - 1; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3691 | u64 hole_start = (oldsize + mask) & ~mask; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3692 | u64 block_end = (size + mask) & ~mask; |
| 3693 | u64 last_byte; |
| 3694 | u64 cur_offset; |
| 3695 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3696 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3697 | |
| 3698 | if (size <= hole_start) |
| 3699 | return 0; |
| 3700 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3701 | while (1) { |
| 3702 | struct btrfs_ordered_extent *ordered; |
| 3703 | btrfs_wait_ordered_range(inode, hole_start, |
| 3704 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3705 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 3706 | &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3707 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3708 | if (!ordered) |
| 3709 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3710 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3711 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3712 | btrfs_put_ordered_extent(ordered); |
| 3713 | } |
| 3714 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3715 | cur_offset = hole_start; |
| 3716 | while (1) { |
| 3717 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3718 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3719 | if (IS_ERR(em)) { |
| 3720 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 3721 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3722 | break; |
| 3723 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3724 | last_byte = min(extent_map_end(em), block_end); |
| 3725 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3726 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3727 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3728 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3729 | |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3730 | trans = btrfs_start_transaction(root, 3); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3731 | if (IS_ERR(trans)) { |
| 3732 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3733 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3734 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3735 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3736 | err = btrfs_drop_extents(trans, root, inode, |
| 3737 | cur_offset, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 3738 | cur_offset + hole_size, 1); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3739 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3740 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3741 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3742 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3743 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3744 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3745 | err = btrfs_insert_file_extent(trans, root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3746 | btrfs_ino(inode), cur_offset, 0, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3747 | 0, hole_size, 0, hole_size, |
| 3748 | 0, 0, 0); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3749 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3750 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3751 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3752 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3753 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3754 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3755 | btrfs_drop_extent_cache(inode, cur_offset, |
| 3756 | cur_offset + hole_size - 1, 0); |
| 3757 | hole_em = alloc_extent_map(); |
| 3758 | if (!hole_em) { |
| 3759 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3760 | &BTRFS_I(inode)->runtime_flags); |
| 3761 | goto next; |
| 3762 | } |
| 3763 | hole_em->start = cur_offset; |
| 3764 | hole_em->len = hole_size; |
| 3765 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3766 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3767 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 3768 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 3769 | hole_em->orig_block_len = 0; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3770 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 3771 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
| 3772 | hole_em->generation = trans->transid; |
| 3773 | |
| 3774 | while (1) { |
| 3775 | write_lock(&em_tree->lock); |
| 3776 | err = add_extent_mapping(em_tree, hole_em); |
| 3777 | if (!err) |
| 3778 | list_move(&hole_em->list, |
| 3779 | &em_tree->modified_extents); |
| 3780 | write_unlock(&em_tree->lock); |
| 3781 | if (err != -EEXIST) |
| 3782 | break; |
| 3783 | btrfs_drop_extent_cache(inode, cur_offset, |
| 3784 | cur_offset + |
| 3785 | hole_size - 1, 0); |
| 3786 | } |
| 3787 | free_extent_map(hole_em); |
| 3788 | next: |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3789 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3790 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3791 | } |
| 3792 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3793 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3794 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3795 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3796 | break; |
| 3797 | } |
| 3798 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3799 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3800 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3801 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3802 | return err; |
| 3803 | } |
| 3804 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 3805 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3806 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 3807 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3808 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3809 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 3810 | loff_t newsize = attr->ia_size; |
| 3811 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3812 | int ret; |
| 3813 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3814 | if (newsize == oldsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3815 | return 0; |
| 3816 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 3817 | /* |
| 3818 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 3819 | * special case where we need to update the times despite not having |
| 3820 | * these flags set. For all other operations the VFS set these flags |
| 3821 | * explicitly if it wants a timestamp update. |
| 3822 | */ |
| 3823 | if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) |
| 3824 | inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb); |
| 3825 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3826 | if (newsize > oldsize) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3827 | truncate_pagecache(inode, oldsize, newsize); |
| 3828 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 3829 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3830 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3831 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 3832 | trans = btrfs_start_transaction(root, 1); |
| 3833 | if (IS_ERR(trans)) |
| 3834 | return PTR_ERR(trans); |
| 3835 | |
| 3836 | i_size_write(inode, newsize); |
| 3837 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 3838 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 3839 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3840 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3841 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3842 | /* |
| 3843 | * We're truncating a file that used to have good data down to |
| 3844 | * zero. Make sure it gets into the ordered flush list so that |
| 3845 | * any new writes get down to disk quickly. |
| 3846 | */ |
| 3847 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3848 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 3849 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3850 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3851 | /* |
| 3852 | * 1 for the orphan item we're going to add |
| 3853 | * 1 for the orphan item deletion. |
| 3854 | */ |
| 3855 | trans = btrfs_start_transaction(root, 2); |
| 3856 | if (IS_ERR(trans)) |
| 3857 | return PTR_ERR(trans); |
| 3858 | |
| 3859 | /* |
| 3860 | * We need to do this in case we fail at _any_ point during the |
| 3861 | * actual truncate. Once we do the truncate_setsize we could |
| 3862 | * invalidate pages which forces any outstanding ordered io to |
| 3863 | * be instantly completed which will give us extents that need |
| 3864 | * to be truncated. If we fail to get an orphan inode down we |
| 3865 | * could have left over extents that were never meant to live, |
| 3866 | * so we need to garuntee from this point on that everything |
| 3867 | * will be consistent. |
| 3868 | */ |
| 3869 | ret = btrfs_orphan_add(trans, inode); |
| 3870 | btrfs_end_transaction(trans, root); |
| 3871 | if (ret) |
| 3872 | return ret; |
| 3873 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3874 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3875 | truncate_setsize(inode, newsize); |
| 3876 | ret = btrfs_truncate(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3877 | if (ret && inode->i_nlink) |
| 3878 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3881 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3884 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3885 | { |
| 3886 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3887 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3888 | int err; |
| 3889 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3890 | if (btrfs_root_readonly(root)) |
| 3891 | return -EROFS; |
| 3892 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3893 | err = inode_change_ok(inode, attr); |
| 3894 | if (err) |
| 3895 | return err; |
| 3896 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3897 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 3898 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3899 | if (err) |
| 3900 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3901 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3902 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3903 | if (attr->ia_valid) { |
| 3904 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3905 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 3906 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3907 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 3908 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3909 | err = btrfs_acl_chmod(inode); |
| 3910 | } |
| 3911 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3912 | return err; |
| 3913 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3914 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3915 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3916 | { |
| 3917 | struct btrfs_trans_handle *trans; |
| 3918 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3919 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 3920 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3921 | int ret; |
| 3922 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3923 | trace_btrfs_inode_evict(inode); |
| 3924 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3925 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3926 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3927 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3928 | goto no_delete; |
| 3929 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3930 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3931 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3932 | goto no_delete; |
| 3933 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3934 | /* 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] | 3935 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3936 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3937 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 3938 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3939 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3940 | goto no_delete; |
| 3941 | } |
| 3942 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3943 | if (inode->i_nlink > 0) { |
| 3944 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3945 | goto no_delete; |
| 3946 | } |
| 3947 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 3948 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 3949 | if (ret) { |
| 3950 | btrfs_orphan_del(NULL, inode); |
| 3951 | goto no_delete; |
| 3952 | } |
| 3953 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3954 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3955 | if (!rsv) { |
| 3956 | btrfs_orphan_del(NULL, inode); |
| 3957 | goto no_delete; |
| 3958 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 3959 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 3960 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3961 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3962 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3963 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3964 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3965 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 3966 | * This is a bit simpler than btrfs_truncate since we've already |
| 3967 | * reserved our space for our orphan item in the unlink, so we just |
| 3968 | * need to reserve some slack space in case we add bytes and update |
| 3969 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3970 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3971 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 3972 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 3973 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3974 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3975 | /* |
| 3976 | * Try and steal from the global reserve since we will |
| 3977 | * likely not use this space anyway, we want to try as |
| 3978 | * hard as possible to get this to work. |
| 3979 | */ |
| 3980 | if (ret) |
| 3981 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 3982 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3983 | if (ret) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3984 | printk(KERN_WARNING "Could not get space for a " |
Josef Bacik | 482e6dc | 2011-08-19 10:31:56 -0400 | [diff] [blame] | 3985 | "delete, will truncate on mount %d\n", ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3986 | btrfs_orphan_del(NULL, inode); |
| 3987 | btrfs_free_block_rsv(root, rsv); |
| 3988 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3989 | } |
| 3990 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 3991 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3992 | if (IS_ERR(trans)) { |
| 3993 | btrfs_orphan_del(NULL, inode); |
| 3994 | btrfs_free_block_rsv(root, rsv); |
| 3995 | goto no_delete; |
| 3996 | } |
| 3997 | |
| 3998 | trans->block_rsv = rsv; |
| 3999 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4000 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4001 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4002 | break; |
| 4003 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4004 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4005 | btrfs_end_transaction(trans, root); |
| 4006 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4007 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4008 | } |
| 4009 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4010 | btrfs_free_block_rsv(root, rsv); |
| 4011 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4012 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4013 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4014 | ret = btrfs_orphan_del(trans, inode); |
| 4015 | BUG_ON(ret); |
| 4016 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4017 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4018 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4019 | if (!(root == root->fs_info->tree_root || |
| 4020 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4021 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4022 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4023 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4024 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4025 | no_delete: |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 4026 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4027 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | /* |
| 4031 | * this returns the key found in the dir entry in the location pointer. |
| 4032 | * If no dir entries were found, location->objectid is 0. |
| 4033 | */ |
| 4034 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 4035 | struct btrfs_key *location) |
| 4036 | { |
| 4037 | const char *name = dentry->d_name.name; |
| 4038 | int namelen = dentry->d_name.len; |
| 4039 | struct btrfs_dir_item *di; |
| 4040 | struct btrfs_path *path; |
| 4041 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4042 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4043 | |
| 4044 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4045 | if (!path) |
| 4046 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4047 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4048 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4049 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4050 | if (IS_ERR(di)) |
| 4051 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4052 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4053 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4054 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4055 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4056 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4057 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4058 | btrfs_free_path(path); |
| 4059 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4060 | out_err: |
| 4061 | location->objectid = 0; |
| 4062 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | /* |
| 4066 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 4067 | * needs to be changed to reflect the root directory of the tree root. This |
| 4068 | * is kind of like crossing a mount point. |
| 4069 | */ |
| 4070 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4071 | struct inode *dir, |
| 4072 | struct dentry *dentry, |
| 4073 | struct btrfs_key *location, |
| 4074 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4075 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4076 | struct btrfs_path *path; |
| 4077 | struct btrfs_root *new_root; |
| 4078 | struct btrfs_root_ref *ref; |
| 4079 | struct extent_buffer *leaf; |
| 4080 | int ret; |
| 4081 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4082 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4083 | path = btrfs_alloc_path(); |
| 4084 | if (!path) { |
| 4085 | err = -ENOMEM; |
| 4086 | goto out; |
| 4087 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4088 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4089 | err = -ENOENT; |
| 4090 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 4091 | BTRFS_I(dir)->root->root_key.objectid, |
| 4092 | location->objectid); |
| 4093 | if (ret) { |
| 4094 | if (ret < 0) |
| 4095 | err = ret; |
| 4096 | goto out; |
| 4097 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4098 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4099 | leaf = path->nodes[0]; |
| 4100 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4101 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4102 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 4103 | goto out; |
| 4104 | |
| 4105 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 4106 | (unsigned long)(ref + 1), |
| 4107 | dentry->d_name.len); |
| 4108 | if (ret) |
| 4109 | goto out; |
| 4110 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4111 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4112 | |
| 4113 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 4114 | if (IS_ERR(new_root)) { |
| 4115 | err = PTR_ERR(new_root); |
| 4116 | goto out; |
| 4117 | } |
| 4118 | |
| 4119 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 4120 | err = -ENOENT; |
| 4121 | goto out; |
| 4122 | } |
| 4123 | |
| 4124 | *sub_root = new_root; |
| 4125 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 4126 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4127 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4128 | err = 0; |
| 4129 | out: |
| 4130 | btrfs_free_path(path); |
| 4131 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4132 | } |
| 4133 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4134 | static void inode_tree_add(struct inode *inode) |
| 4135 | { |
| 4136 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4137 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4138 | struct rb_node **p; |
| 4139 | struct rb_node *parent; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4140 | u64 ino = btrfs_ino(inode); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4141 | again: |
| 4142 | p = &root->inode_tree.rb_node; |
| 4143 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4144 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 4145 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4146 | return; |
| 4147 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4148 | spin_lock(&root->inode_lock); |
| 4149 | while (*p) { |
| 4150 | parent = *p; |
| 4151 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 4152 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4153 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4154 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4155 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4156 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4157 | else { |
| 4158 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 4159 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4160 | rb_erase(parent, &root->inode_tree); |
| 4161 | RB_CLEAR_NODE(parent); |
| 4162 | spin_unlock(&root->inode_lock); |
| 4163 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4164 | } |
| 4165 | } |
| 4166 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 4167 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 4168 | spin_unlock(&root->inode_lock); |
| 4169 | } |
| 4170 | |
| 4171 | static void inode_tree_del(struct inode *inode) |
| 4172 | { |
| 4173 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4174 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4175 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4176 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4177 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4178 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4179 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4180 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4181 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4182 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4183 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4184 | /* |
| 4185 | * Free space cache has inodes in the tree root, but the tree root has a |
| 4186 | * root_refs of 0, so this could end up dropping the tree root as a |
| 4187 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 4188 | * make sure we don't drop it. |
| 4189 | */ |
| 4190 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 4191 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4192 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 4193 | spin_lock(&root->inode_lock); |
| 4194 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 4195 | spin_unlock(&root->inode_lock); |
| 4196 | if (empty) |
| 4197 | btrfs_add_dead_root(root); |
| 4198 | } |
| 4199 | } |
| 4200 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4201 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4202 | { |
| 4203 | struct rb_node *node; |
| 4204 | struct rb_node *prev; |
| 4205 | struct btrfs_inode *entry; |
| 4206 | struct inode *inode; |
| 4207 | u64 objectid = 0; |
| 4208 | |
| 4209 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4210 | |
| 4211 | spin_lock(&root->inode_lock); |
| 4212 | again: |
| 4213 | node = root->inode_tree.rb_node; |
| 4214 | prev = NULL; |
| 4215 | while (node) { |
| 4216 | prev = node; |
| 4217 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4218 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4219 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4220 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4221 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4222 | node = node->rb_right; |
| 4223 | else |
| 4224 | break; |
| 4225 | } |
| 4226 | if (!node) { |
| 4227 | while (prev) { |
| 4228 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4229 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4230 | node = prev; |
| 4231 | break; |
| 4232 | } |
| 4233 | prev = rb_next(prev); |
| 4234 | } |
| 4235 | } |
| 4236 | while (node) { |
| 4237 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4238 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4239 | inode = igrab(&entry->vfs_inode); |
| 4240 | if (inode) { |
| 4241 | spin_unlock(&root->inode_lock); |
| 4242 | if (atomic_read(&inode->i_count) > 1) |
| 4243 | d_prune_aliases(inode); |
| 4244 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4245 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4246 | * the inode cache when its usage count |
| 4247 | * hits zero. |
| 4248 | */ |
| 4249 | iput(inode); |
| 4250 | cond_resched(); |
| 4251 | spin_lock(&root->inode_lock); |
| 4252 | goto again; |
| 4253 | } |
| 4254 | |
| 4255 | if (cond_resched_lock(&root->inode_lock)) |
| 4256 | goto again; |
| 4257 | |
| 4258 | node = rb_next(node); |
| 4259 | } |
| 4260 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4261 | } |
| 4262 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4263 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4264 | { |
| 4265 | struct btrfs_iget_args *args = p; |
| 4266 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4267 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4268 | return 0; |
| 4269 | } |
| 4270 | |
| 4271 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4272 | { |
| 4273 | struct btrfs_iget_args *args = opaque; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4274 | return args->ino == btrfs_ino(inode) && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4275 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4276 | } |
| 4277 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4278 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4279 | u64 objectid, |
| 4280 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4281 | { |
| 4282 | struct inode *inode; |
| 4283 | struct btrfs_iget_args args; |
| 4284 | args.ino = objectid; |
| 4285 | args.root = root; |
| 4286 | |
| 4287 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 4288 | btrfs_init_locked_inode, |
| 4289 | (void *)&args); |
| 4290 | return inode; |
| 4291 | } |
| 4292 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4293 | /* Get an inode object given its location and corresponding root. |
| 4294 | * Returns in *is_new if the inode was read from disk |
| 4295 | */ |
| 4296 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4297 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4298 | { |
| 4299 | struct inode *inode; |
| 4300 | |
| 4301 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4302 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4303 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4304 | |
| 4305 | if (inode->i_state & I_NEW) { |
| 4306 | BTRFS_I(inode)->root = root; |
| 4307 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4308 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4309 | if (!is_bad_inode(inode)) { |
| 4310 | inode_tree_add(inode); |
| 4311 | unlock_new_inode(inode); |
| 4312 | if (new) |
| 4313 | *new = 1; |
| 4314 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4315 | unlock_new_inode(inode); |
| 4316 | iput(inode); |
| 4317 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4318 | } |
| 4319 | } |
| 4320 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4321 | return inode; |
| 4322 | } |
| 4323 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4324 | static struct inode *new_simple_dir(struct super_block *s, |
| 4325 | struct btrfs_key *key, |
| 4326 | struct btrfs_root *root) |
| 4327 | { |
| 4328 | struct inode *inode = new_inode(s); |
| 4329 | |
| 4330 | if (!inode) |
| 4331 | return ERR_PTR(-ENOMEM); |
| 4332 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4333 | BTRFS_I(inode)->root = root; |
| 4334 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4335 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4336 | |
| 4337 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4338 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4339 | inode->i_fop = &simple_dir_operations; |
| 4340 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 4341 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 4342 | |
| 4343 | return inode; |
| 4344 | } |
| 4345 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4346 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4347 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4348 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4349 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4350 | struct btrfs_root *sub_root = root; |
| 4351 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4352 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4353 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4354 | |
| 4355 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 4356 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4357 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4358 | if (unlikely(d_need_lookup(dentry))) { |
| 4359 | memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key)); |
| 4360 | kfree(dentry->d_fsdata); |
| 4361 | dentry->d_fsdata = NULL; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 4362 | /* This thing is hashed, drop it for now */ |
| 4363 | d_drop(dentry); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4364 | } else { |
| 4365 | ret = btrfs_inode_by_name(dir, dentry, &location); |
| 4366 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4367 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4368 | if (ret < 0) |
| 4369 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4370 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4371 | if (location.objectid == 0) |
| 4372 | return NULL; |
| 4373 | |
| 4374 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4375 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4376 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4377 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4378 | |
| 4379 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4380 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4381 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4382 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4383 | &location, &sub_root); |
| 4384 | if (ret < 0) { |
| 4385 | if (ret != -ENOENT) |
| 4386 | inode = ERR_PTR(ret); |
| 4387 | else |
| 4388 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4389 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4390 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4391 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4392 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4393 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 4394 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4395 | down_read(&root->fs_info->cleanup_work_sem); |
| 4396 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4397 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4398 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4399 | if (ret) |
| 4400 | inode = ERR_PTR(ret); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4401 | } |
| 4402 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4403 | return inode; |
| 4404 | } |
| 4405 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 4406 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4407 | { |
| 4408 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4409 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4410 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4411 | if (!inode && !IS_ROOT(dentry)) |
| 4412 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4413 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4414 | if (inode) { |
| 4415 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4416 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4417 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4418 | |
| 4419 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 4420 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4421 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4422 | return 0; |
| 4423 | } |
| 4424 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4425 | static void btrfs_dentry_release(struct dentry *dentry) |
| 4426 | { |
| 4427 | if (dentry->d_fsdata) |
| 4428 | kfree(dentry->d_fsdata); |
| 4429 | } |
| 4430 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4431 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 4432 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4433 | { |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 4434 | struct dentry *ret; |
| 4435 | |
| 4436 | ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); |
| 4437 | if (unlikely(d_need_lookup(dentry))) { |
| 4438 | spin_lock(&dentry->d_lock); |
| 4439 | dentry->d_flags &= ~DCACHE_NEED_LOOKUP; |
| 4440 | spin_unlock(&dentry->d_lock); |
| 4441 | } |
| 4442 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4443 | } |
| 4444 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4445 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4446 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 4447 | }; |
| 4448 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 4449 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 4450 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4451 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4452 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4453 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4454 | struct btrfs_item *item; |
| 4455 | struct btrfs_dir_item *di; |
| 4456 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4457 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4458 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4459 | struct list_head ins_list; |
| 4460 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4461 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4462 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4463 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4464 | unsigned char d_type; |
| 4465 | int over = 0; |
| 4466 | u32 di_cur; |
| 4467 | u32 di_total; |
| 4468 | u32 di_len; |
| 4469 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4470 | char tmp_name[32]; |
| 4471 | char *name_ptr; |
| 4472 | int name_len; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4473 | int is_curr = 0; /* filp->f_pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4474 | |
| 4475 | /* FIXME, use a real flag for deciding about the key type */ |
| 4476 | if (root->fs_info->tree_root == root) |
| 4477 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4478 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4479 | /* special case for "." */ |
| 4480 | if (filp->f_pos == 0) { |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 4481 | over = filldir(dirent, ".", 1, |
| 4482 | filp->f_pos, btrfs_ino(inode), DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4483 | if (over) |
| 4484 | return 0; |
| 4485 | filp->f_pos = 1; |
| 4486 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4487 | /* special case for .., just use the back ref */ |
| 4488 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4489 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4490 | over = filldir(dirent, "..", 2, |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 4491 | filp->f_pos, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4492 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4493 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4494 | filp->f_pos = 2; |
| 4495 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4496 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4497 | if (!path) |
| 4498 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4499 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 4500 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4501 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4502 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4503 | INIT_LIST_HEAD(&ins_list); |
| 4504 | INIT_LIST_HEAD(&del_list); |
| 4505 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 4506 | } |
| 4507 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4508 | btrfs_set_key_type(&key, key_type); |
| 4509 | key.offset = filp->f_pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4510 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4511 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4512 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4513 | if (ret < 0) |
| 4514 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4515 | |
| 4516 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4517 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4518 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4519 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4520 | ret = btrfs_next_leaf(root, path); |
| 4521 | if (ret < 0) |
| 4522 | goto err; |
| 4523 | else if (ret > 0) |
| 4524 | break; |
| 4525 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4526 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4527 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4528 | item = btrfs_item_nr(leaf, slot); |
| 4529 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4530 | |
| 4531 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4532 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4533 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4534 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4535 | if (found_key.offset < filp->f_pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4536 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4537 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 4538 | btrfs_should_delete_dir_index(&del_list, |
| 4539 | found_key.offset)) |
| 4540 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4541 | |
| 4542 | filp->f_pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4543 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4544 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4545 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 4546 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4547 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4548 | |
| 4549 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4550 | struct btrfs_key location; |
| 4551 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 4552 | if (verify_dir_item(root, leaf, di)) |
| 4553 | break; |
| 4554 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4555 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4556 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4557 | name_ptr = tmp_name; |
| 4558 | } else { |
| 4559 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4560 | if (!name_ptr) { |
| 4561 | ret = -ENOMEM; |
| 4562 | goto err; |
| 4563 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4564 | } |
| 4565 | read_extent_buffer(leaf, name_ptr, |
| 4566 | (unsigned long)(di + 1), name_len); |
| 4567 | |
| 4568 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4569 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4570 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4571 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4572 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 4573 | * skip it. |
| 4574 | * |
| 4575 | * In contrast to old kernels, we insert the snapshot's |
| 4576 | * dir item and dir index after it has been created, so |
| 4577 | * we won't find a reference to our own snapshot. We |
| 4578 | * still keep the following code for backward |
| 4579 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4580 | */ |
| 4581 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4582 | location.objectid == root->root_key.objectid) { |
| 4583 | over = 0; |
| 4584 | goto skip; |
| 4585 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4586 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4587 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4588 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4589 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4590 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4591 | if (name_ptr != tmp_name) |
| 4592 | kfree(name_ptr); |
| 4593 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4594 | if (over) |
| 4595 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4596 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4597 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4598 | di_cur += di_len; |
| 4599 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4600 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4601 | next: |
| 4602 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4603 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4604 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4605 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4606 | if (is_curr) |
| 4607 | filp->f_pos++; |
| 4608 | ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir, |
| 4609 | &ins_list); |
| 4610 | if (ret) |
| 4611 | goto nopos; |
| 4612 | } |
| 4613 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4614 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4615 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4616 | /* |
| 4617 | * 32-bit glibc will use getdents64, but then strtol - |
| 4618 | * so the last number we can serve is this. |
| 4619 | */ |
| 4620 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4621 | else |
| 4622 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4623 | nopos: |
| 4624 | ret = 0; |
| 4625 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4626 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 4627 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4628 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4629 | return ret; |
| 4630 | } |
| 4631 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4632 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4633 | { |
| 4634 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4635 | struct btrfs_trans_handle *trans; |
| 4636 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4637 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4638 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4639 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4640 | return 0; |
| 4641 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 4642 | 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] | 4643 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4644 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4645 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4646 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4647 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4648 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4649 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4650 | if (IS_ERR(trans)) |
| 4651 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 4652 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4653 | } |
| 4654 | return ret; |
| 4655 | } |
| 4656 | |
| 4657 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4658 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4659 | * inode changes. But, it is most likely to find the inode in cache. |
| 4660 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4661 | * to keep or drop this code. |
| 4662 | */ |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4663 | int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4664 | { |
| 4665 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4666 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4667 | int ret; |
| 4668 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4669 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4670 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4671 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4672 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4673 | if (IS_ERR(trans)) |
| 4674 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4675 | |
| 4676 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4677 | if (ret && ret == -ENOSPC) { |
| 4678 | /* whoops, lets try again with the full transaction */ |
| 4679 | btrfs_end_transaction(trans, root); |
| 4680 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4681 | if (IS_ERR(trans)) |
| 4682 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4683 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4684 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4685 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4686 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4687 | if (BTRFS_I(inode)->delayed_node) |
| 4688 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4689 | |
| 4690 | return ret; |
| 4691 | } |
| 4692 | |
| 4693 | /* |
| 4694 | * This is a copy of file_update_time. We need this so we can return error on |
| 4695 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 4696 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 4697 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 4698 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4699 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 4700 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4701 | |
| 4702 | if (btrfs_root_readonly(root)) |
| 4703 | return -EROFS; |
| 4704 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 4705 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4706 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 4707 | if (flags & S_CTIME) |
| 4708 | inode->i_ctime = *now; |
| 4709 | if (flags & S_MTIME) |
| 4710 | inode->i_mtime = *now; |
| 4711 | if (flags & S_ATIME) |
| 4712 | inode->i_atime = *now; |
| 4713 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4714 | } |
| 4715 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4716 | /* |
| 4717 | * find the highest existing sequence number in a directory |
| 4718 | * and then set the in-memory index_cnt variable to reflect |
| 4719 | * free sequence numbers |
| 4720 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4721 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4722 | { |
| 4723 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4724 | struct btrfs_key key, found_key; |
| 4725 | struct btrfs_path *path; |
| 4726 | struct extent_buffer *leaf; |
| 4727 | int ret; |
| 4728 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4729 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4730 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4731 | key.offset = (u64)-1; |
| 4732 | |
| 4733 | path = btrfs_alloc_path(); |
| 4734 | if (!path) |
| 4735 | return -ENOMEM; |
| 4736 | |
| 4737 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4738 | if (ret < 0) |
| 4739 | goto out; |
| 4740 | /* FIXME: we should be able to handle this */ |
| 4741 | if (ret == 0) |
| 4742 | goto out; |
| 4743 | ret = 0; |
| 4744 | |
| 4745 | /* |
| 4746 | * MAGIC NUMBER EXPLANATION: |
| 4747 | * since we search a directory based on f_pos we have to start at 2 |
| 4748 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4749 | * else has to start at 2 |
| 4750 | */ |
| 4751 | if (path->slots[0] == 0) { |
| 4752 | BTRFS_I(inode)->index_cnt = 2; |
| 4753 | goto out; |
| 4754 | } |
| 4755 | |
| 4756 | path->slots[0]--; |
| 4757 | |
| 4758 | leaf = path->nodes[0]; |
| 4759 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4760 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4761 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4762 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4763 | BTRFS_I(inode)->index_cnt = 2; |
| 4764 | goto out; |
| 4765 | } |
| 4766 | |
| 4767 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4768 | out: |
| 4769 | btrfs_free_path(path); |
| 4770 | return ret; |
| 4771 | } |
| 4772 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4773 | /* |
| 4774 | * helper to find a free sequence number in a given directory. This current |
| 4775 | * code is very simple, later versions will do smarter things in the btree |
| 4776 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4777 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4778 | { |
| 4779 | int ret = 0; |
| 4780 | |
| 4781 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4782 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 4783 | if (ret) { |
| 4784 | ret = btrfs_set_inode_index_count(dir); |
| 4785 | if (ret) |
| 4786 | return ret; |
| 4787 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4788 | } |
| 4789 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4790 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4791 | BTRFS_I(dir)->index_cnt++; |
| 4792 | |
| 4793 | return ret; |
| 4794 | } |
| 4795 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4796 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4797 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4798 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4799 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 4800 | u64 ref_objectid, u64 objectid, |
| 4801 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4802 | { |
| 4803 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4804 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4805 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4806 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4807 | struct btrfs_inode_ref *ref; |
| 4808 | struct btrfs_key key[2]; |
| 4809 | u32 sizes[2]; |
| 4810 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4811 | int ret; |
| 4812 | int owner; |
| 4813 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4814 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4815 | if (!path) |
| 4816 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4817 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4818 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4819 | if (!inode) { |
| 4820 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4821 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4822 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4823 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4824 | /* |
| 4825 | * we have to initialize this early, so we can reclaim the inode |
| 4826 | * number if we fail afterwards in this function. |
| 4827 | */ |
| 4828 | inode->i_ino = objectid; |
| 4829 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4830 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4831 | trace_btrfs_inode_request(dir); |
| 4832 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4833 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4834 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4835 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4836 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4837 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4838 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4839 | } |
| 4840 | /* |
| 4841 | * index_cnt is ignored for everything but a dir, |
| 4842 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4843 | * number |
| 4844 | */ |
| 4845 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4846 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4847 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 4848 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4849 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4850 | /* |
| 4851 | * We could have gotten an inode number from somebody who was fsynced |
| 4852 | * and then removed in this same transaction, so let's just set full |
| 4853 | * sync since it will be a full sync anyway and this will blow away the |
| 4854 | * old info in the log. |
| 4855 | */ |
| 4856 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 4857 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 4858 | if (S_ISDIR(mode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4859 | owner = 0; |
| 4860 | else |
| 4861 | owner = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4862 | |
| 4863 | key[0].objectid = objectid; |
| 4864 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4865 | key[0].offset = 0; |
| 4866 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 4867 | /* |
| 4868 | * Start new inodes with an inode_ref. This is slightly more |
| 4869 | * efficient for small numbers of hard links since they will |
| 4870 | * be packed into one item. Extended refs will kick in if we |
| 4871 | * add more hard links than can fit in the ref item. |
| 4872 | */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4873 | key[1].objectid = objectid; |
| 4874 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4875 | key[1].offset = ref_objectid; |
| 4876 | |
| 4877 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4878 | sizes[1] = name_len + sizeof(*ref); |
| 4879 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4880 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4881 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4882 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4883 | goto fail; |
| 4884 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 4885 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4886 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4887 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4888 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4889 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 4890 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 4891 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4892 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4893 | |
| 4894 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4895 | struct btrfs_inode_ref); |
| 4896 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4897 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4898 | ptr = (unsigned long)(ref + 1); |
| 4899 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4900 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4901 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4902 | btrfs_free_path(path); |
| 4903 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4904 | location = &BTRFS_I(inode)->location; |
| 4905 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4906 | location->offset = 0; |
| 4907 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4908 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4909 | btrfs_inherit_iflags(inode, dir); |
| 4910 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 4911 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4912 | if (btrfs_test_opt(root, NODATASUM)) |
| 4913 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 4914 | if (btrfs_test_opt(root, NODATACOW)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4915 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4916 | } |
| 4917 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4918 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4919 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4920 | |
| 4921 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 4922 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4923 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4924 | btrfs_update_root_times(trans, root); |
| 4925 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4926 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4927 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4928 | if (dir) |
| 4929 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4930 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4931 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4932 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4936 | { |
| 4937 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4938 | } |
| 4939 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4940 | /* |
| 4941 | * utility function to add 'inode' into 'parent_inode' with |
| 4942 | * a give name and a given sequence number. |
| 4943 | * if 'add_backref' is true, also insert a backref from the |
| 4944 | * inode to the parent directory. |
| 4945 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4946 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4947 | struct inode *parent_inode, struct inode *inode, |
| 4948 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4949 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4950 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4951 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4952 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4953 | u64 ino = btrfs_ino(inode); |
| 4954 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4955 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4956 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4957 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4958 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4959 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4960 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4961 | key.offset = 0; |
| 4962 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4963 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4964 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4965 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4966 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4967 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4968 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4969 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 4970 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4971 | } |
| 4972 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4973 | /* Nothing to clean up yet */ |
| 4974 | if (ret) |
| 4975 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4976 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4977 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4978 | parent_inode, &key, |
| 4979 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 4980 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4981 | goto fail_dir_item; |
| 4982 | else if (ret) { |
| 4983 | btrfs_abort_transaction(trans, root, ret); |
| 4984 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4985 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4986 | |
| 4987 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 4988 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4989 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4990 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 4991 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 4992 | if (ret) |
| 4993 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4994 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 4995 | |
| 4996 | fail_dir_item: |
| 4997 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4998 | u64 local_index; |
| 4999 | int err; |
| 5000 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 5001 | key.objectid, root->root_key.objectid, |
| 5002 | parent_ino, &local_index, name, name_len); |
| 5003 | |
| 5004 | } else if (add_backref) { |
| 5005 | u64 local_index; |
| 5006 | int err; |
| 5007 | |
| 5008 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 5009 | ino, parent_ino, &local_index); |
| 5010 | } |
| 5011 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5015 | struct inode *dir, struct dentry *dentry, |
| 5016 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5017 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5018 | int err = btrfs_add_link(trans, dir, inode, |
| 5019 | dentry->d_name.name, dentry->d_name.len, |
| 5020 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5021 | if (err > 0) |
| 5022 | err = -EEXIST; |
| 5023 | return err; |
| 5024 | } |
| 5025 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5026 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 5027 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5028 | { |
| 5029 | struct btrfs_trans_handle *trans; |
| 5030 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5031 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5032 | int err; |
| 5033 | int drop_inode = 0; |
| 5034 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5035 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5036 | |
| 5037 | if (!new_valid_dev(rdev)) |
| 5038 | return -EINVAL; |
| 5039 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5040 | /* |
| 5041 | * 2 for inode item and ref |
| 5042 | * 2 for dir items |
| 5043 | * 1 for xattr if selinux is on |
| 5044 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5045 | trans = btrfs_start_transaction(root, 5); |
| 5046 | if (IS_ERR(trans)) |
| 5047 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5048 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5049 | err = btrfs_find_free_ino(root, &objectid); |
| 5050 | if (err) |
| 5051 | goto out_unlock; |
| 5052 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5053 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5054 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5055 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5056 | if (IS_ERR(inode)) { |
| 5057 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5058 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5059 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5060 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5061 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5062 | if (err) { |
| 5063 | drop_inode = 1; |
| 5064 | goto out_unlock; |
| 5065 | } |
| 5066 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5067 | /* |
| 5068 | * If the active LSM wants to access the inode during |
| 5069 | * d_instantiate it needs these. Smack checks to see |
| 5070 | * if the filesystem supports xattrs by looking at the |
| 5071 | * ops vector. |
| 5072 | */ |
| 5073 | |
| 5074 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5075 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5076 | if (err) |
| 5077 | drop_inode = 1; |
| 5078 | else { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5079 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 5080 | btrfs_update_inode(trans, root, inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5081 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5082 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5083 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5084 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5085 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5086 | if (drop_inode) { |
| 5087 | inode_dec_link_count(inode); |
| 5088 | iput(inode); |
| 5089 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5090 | return err; |
| 5091 | } |
| 5092 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5093 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 5094 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5095 | { |
| 5096 | struct btrfs_trans_handle *trans; |
| 5097 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5098 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5099 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5100 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5101 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5102 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5103 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5104 | /* |
| 5105 | * 2 for inode item and ref |
| 5106 | * 2 for dir items |
| 5107 | * 1 for xattr if selinux is on |
| 5108 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5109 | trans = btrfs_start_transaction(root, 5); |
| 5110 | if (IS_ERR(trans)) |
| 5111 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5112 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5113 | err = btrfs_find_free_ino(root, &objectid); |
| 5114 | if (err) |
| 5115 | goto out_unlock; |
| 5116 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5117 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5118 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5119 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5120 | if (IS_ERR(inode)) { |
| 5121 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5122 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5123 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5124 | drop_inode_on_err = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5125 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5126 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5127 | if (err) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5128 | goto out_unlock; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5129 | |
Filipe Brandenburger | 9185aa5 | 2012-11-30 03:40:08 +0000 | [diff] [blame] | 5130 | err = btrfs_update_inode(trans, root, inode); |
| 5131 | if (err) |
| 5132 | goto out_unlock; |
| 5133 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5134 | /* |
| 5135 | * If the active LSM wants to access the inode during |
| 5136 | * d_instantiate it needs these. Smack checks to see |
| 5137 | * if the filesystem supports xattrs by looking at the |
| 5138 | * ops vector. |
| 5139 | */ |
| 5140 | inode->i_fop = &btrfs_file_operations; |
| 5141 | inode->i_op = &btrfs_file_inode_operations; |
| 5142 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5143 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5144 | if (err) |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5145 | goto out_unlock; |
| 5146 | |
| 5147 | inode->i_mapping->a_ops = &btrfs_aops; |
| 5148 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 5149 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 5150 | d_instantiate(dentry, inode); |
| 5151 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5152 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5153 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5154 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5155 | inode_dec_link_count(inode); |
| 5156 | iput(inode); |
| 5157 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5158 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5159 | return err; |
| 5160 | } |
| 5161 | |
| 5162 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 5163 | struct dentry *dentry) |
| 5164 | { |
| 5165 | struct btrfs_trans_handle *trans; |
| 5166 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5167 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5168 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5169 | int err; |
| 5170 | int drop_inode = 0; |
| 5171 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5172 | /* do not allow sys_link's with other subvols of the same device */ |
| 5173 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 5174 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5175 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5176 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 5177 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5178 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5179 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5180 | if (err) |
| 5181 | goto fail; |
| 5182 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5183 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5184 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5185 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5186 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5187 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5188 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5189 | if (IS_ERR(trans)) { |
| 5190 | err = PTR_ERR(trans); |
| 5191 | goto fail; |
| 5192 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5193 | |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5194 | btrfs_inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5195 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5196 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 5197 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 5198 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5199 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5200 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5201 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5202 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5203 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5204 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 5205 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5206 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5207 | if (err) |
| 5208 | goto fail; |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5209 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5210 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5211 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5212 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5213 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5214 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5215 | if (drop_inode) { |
| 5216 | inode_dec_link_count(inode); |
| 5217 | iput(inode); |
| 5218 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5219 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5220 | return err; |
| 5221 | } |
| 5222 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 5223 | 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] | 5224 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5225 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5226 | struct btrfs_trans_handle *trans; |
| 5227 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5228 | int err = 0; |
| 5229 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5230 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5231 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5232 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5233 | /* |
| 5234 | * 2 items for inode and ref |
| 5235 | * 2 items for dir items |
| 5236 | * 1 for xattr if selinux is on |
| 5237 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5238 | trans = btrfs_start_transaction(root, 5); |
| 5239 | if (IS_ERR(trans)) |
| 5240 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5241 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5242 | err = btrfs_find_free_ino(root, &objectid); |
| 5243 | if (err) |
| 5244 | goto out_fail; |
| 5245 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5246 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5247 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5248 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5249 | if (IS_ERR(inode)) { |
| 5250 | err = PTR_ERR(inode); |
| 5251 | goto out_fail; |
| 5252 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5253 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5254 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5255 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5256 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5257 | if (err) |
| 5258 | goto out_fail; |
| 5259 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5260 | inode->i_op = &btrfs_dir_inode_operations; |
| 5261 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5262 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5263 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5264 | err = btrfs_update_inode(trans, root, inode); |
| 5265 | if (err) |
| 5266 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5267 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5268 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 5269 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5270 | if (err) |
| 5271 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5272 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5273 | d_instantiate(dentry, inode); |
| 5274 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5275 | |
| 5276 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5277 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5278 | if (drop_on_err) |
| 5279 | iput(inode); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5280 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5281 | return err; |
| 5282 | } |
| 5283 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5284 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 5285 | * and an extent that you want to insert, deal with overlap and insert |
| 5286 | * the new extent into the tree. |
| 5287 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5288 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 5289 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5290 | struct extent_map *em, |
| 5291 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5292 | { |
| 5293 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5294 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5295 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 5296 | start_diff = map_start - em->start; |
| 5297 | em->start = map_start; |
| 5298 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5299 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 5300 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5301 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5302 | em->block_len -= start_diff; |
| 5303 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5304 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5305 | } |
| 5306 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5307 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 5308 | struct inode *inode, struct page *page, |
| 5309 | size_t pg_offset, u64 extent_offset, |
| 5310 | struct btrfs_file_extent_item *item) |
| 5311 | { |
| 5312 | int ret; |
| 5313 | struct extent_buffer *leaf = path->nodes[0]; |
| 5314 | char *tmp; |
| 5315 | size_t max_size; |
| 5316 | unsigned long inline_size; |
| 5317 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5318 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5319 | |
| 5320 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5321 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5322 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 5323 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 5324 | btrfs_item_nr(leaf, path->slots[0])); |
| 5325 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 5326 | if (!tmp) |
| 5327 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5328 | ptr = btrfs_file_extent_inline_start(item); |
| 5329 | |
| 5330 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 5331 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 5332 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5333 | ret = btrfs_decompress(compress_type, tmp, page, |
| 5334 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5335 | if (ret) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 5336 | char *kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5337 | unsigned long copy_size = min_t(u64, |
| 5338 | PAGE_CACHE_SIZE - pg_offset, |
| 5339 | max_size - extent_offset); |
| 5340 | memset(kaddr + pg_offset, 0, copy_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 5341 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5342 | } |
| 5343 | kfree(tmp); |
| 5344 | return 0; |
| 5345 | } |
| 5346 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5347 | /* |
| 5348 | * 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] | 5349 | * the ugly parts come from merging extents from the disk with the in-ram |
| 5350 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5351 | * where the in-ram extents might be locked pending data=ordered completion. |
| 5352 | * |
| 5353 | * This also copies inline extents directly into the page. |
| 5354 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5355 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5356 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5357 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5358 | int create) |
| 5359 | { |
| 5360 | int ret; |
| 5361 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5362 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5363 | u64 extent_start = 0; |
| 5364 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5365 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5366 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5367 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5368 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5369 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5370 | struct extent_buffer *leaf; |
| 5371 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5372 | struct extent_map *em = NULL; |
| 5373 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5374 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5375 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5376 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5377 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5378 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5379 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5380 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5381 | if (em) |
| 5382 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5383 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5384 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5385 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5386 | if (em->start > start || em->start + em->len <= start) |
| 5387 | free_extent_map(em); |
| 5388 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5389 | free_extent_map(em); |
| 5390 | else |
| 5391 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5392 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5393 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5394 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5395 | err = -ENOMEM; |
| 5396 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5397 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5398 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5399 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 5400 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5401 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5402 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5403 | |
| 5404 | if (!path) { |
| 5405 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5406 | if (!path) { |
| 5407 | err = -ENOMEM; |
| 5408 | goto out; |
| 5409 | } |
| 5410 | /* |
| 5411 | * Chances are we'll be called again, so go ahead and do |
| 5412 | * readahead |
| 5413 | */ |
| 5414 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5415 | } |
| 5416 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5417 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 5418 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5419 | if (ret < 0) { |
| 5420 | err = ret; |
| 5421 | goto out; |
| 5422 | } |
| 5423 | |
| 5424 | if (ret != 0) { |
| 5425 | if (path->slots[0] == 0) |
| 5426 | goto not_found; |
| 5427 | path->slots[0]--; |
| 5428 | } |
| 5429 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5430 | leaf = path->nodes[0]; |
| 5431 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5432 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5433 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5434 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5435 | found_type = btrfs_key_type(&found_key); |
| 5436 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5437 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 5438 | goto not_found; |
| 5439 | } |
| 5440 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5441 | found_type = btrfs_file_extent_type(leaf, item); |
| 5442 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5443 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5444 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5445 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5446 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5447 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5448 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5449 | size_t size; |
| 5450 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5451 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 5452 | ~((u64)root->sectorsize - 1); |
| 5453 | } |
| 5454 | |
| 5455 | if (start >= extent_end) { |
| 5456 | path->slots[0]++; |
| 5457 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 5458 | ret = btrfs_next_leaf(root, path); |
| 5459 | if (ret < 0) { |
| 5460 | err = ret; |
| 5461 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5462 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5463 | if (ret > 0) |
| 5464 | goto not_found; |
| 5465 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5466 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5467 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5468 | if (found_key.objectid != objectid || |
| 5469 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 5470 | goto not_found; |
| 5471 | if (start + len <= found_key.offset) |
| 5472 | goto not_found; |
| 5473 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 5474 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5475 | em->len = found_key.offset - start; |
| 5476 | goto not_found_em; |
| 5477 | } |
| 5478 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5479 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5480 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5481 | em->start = extent_start; |
| 5482 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5483 | em->orig_start = extent_start - |
| 5484 | btrfs_file_extent_offset(leaf, item); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 5485 | em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 5486 | item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5487 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 5488 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5489 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5490 | goto insert; |
| 5491 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5492 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5493 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5494 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5495 | em->block_start = bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 5496 | em->block_len = em->orig_block_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5497 | } else { |
| 5498 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 5499 | em->block_start = bytenr; |
| 5500 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5501 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 5502 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5503 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5504 | goto insert; |
| 5505 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5506 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5507 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5508 | size_t size; |
| 5509 | size_t extent_offset; |
| 5510 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5511 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5512 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5513 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5514 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5515 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5516 | goto out; |
| 5517 | } |
| 5518 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5519 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5520 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5521 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5522 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5523 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5524 | em->len = (copy_size + root->sectorsize - 1) & |
| 5525 | ~((u64)root->sectorsize - 1); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 5526 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 5527 | em->orig_start = em->start; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5528 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5529 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5530 | em->compress_type = compress_type; |
| 5531 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5532 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5533 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5534 | if (btrfs_file_extent_compression(leaf, item) != |
| 5535 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5536 | ret = uncompress_inline(path, inode, page, |
| 5537 | pg_offset, |
| 5538 | extent_offset, item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5539 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5540 | } else { |
| 5541 | map = kmap(page); |
| 5542 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 5543 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 5544 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 5545 | memset(map + pg_offset + copy_size, 0, |
| 5546 | PAGE_CACHE_SIZE - pg_offset - |
| 5547 | copy_size); |
| 5548 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5549 | kunmap(page); |
| 5550 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5551 | flush_dcache_page(page); |
| 5552 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 5553 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5554 | if (!trans) { |
| 5555 | kunmap(page); |
| 5556 | free_extent_map(em); |
| 5557 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5558 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5559 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5560 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5561 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5562 | if (IS_ERR(trans)) |
| 5563 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5564 | goto again; |
| 5565 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5566 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5567 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5568 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5569 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5570 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5571 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5572 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 5573 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5574 | goto insert; |
| 5575 | } else { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 5576 | WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5577 | } |
| 5578 | not_found: |
| 5579 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 5580 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5581 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5582 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5583 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5584 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5585 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5586 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5587 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5588 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 5589 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 5590 | (unsigned long long)em->len, |
| 5591 | (unsigned long long)start, |
| 5592 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5593 | err = -EIO; |
| 5594 | goto out; |
| 5595 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5596 | |
| 5597 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5598 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5599 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5600 | /* it is possible that someone inserted the extent into the tree |
| 5601 | * while we had the lock dropped. It is also possible that |
| 5602 | * an overlapping map exists in the tree |
| 5603 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5604 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5605 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5606 | |
| 5607 | ret = 0; |
| 5608 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5609 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5610 | if (existing && (existing->start > start || |
| 5611 | existing->start + existing->len <= start)) { |
| 5612 | free_extent_map(existing); |
| 5613 | existing = NULL; |
| 5614 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5615 | if (!existing) { |
| 5616 | existing = lookup_extent_mapping(em_tree, em->start, |
| 5617 | em->len); |
| 5618 | if (existing) { |
| 5619 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5620 | em, start, |
| 5621 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5622 | free_extent_map(existing); |
| 5623 | if (err) { |
| 5624 | free_extent_map(em); |
| 5625 | em = NULL; |
| 5626 | } |
| 5627 | } else { |
| 5628 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5629 | free_extent_map(em); |
| 5630 | em = NULL; |
| 5631 | } |
| 5632 | } else { |
| 5633 | free_extent_map(em); |
| 5634 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5635 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5636 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5637 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5638 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5639 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5640 | |
Tsutomu Itoh | f0bd95e | 2012-10-01 03:08:37 -0600 | [diff] [blame] | 5641 | if (em) |
| 5642 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5643 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5644 | if (path) |
| 5645 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5646 | if (trans) { |
| 5647 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5648 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5649 | err = ret; |
| 5650 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5651 | if (err) { |
| 5652 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5653 | return ERR_PTR(err); |
| 5654 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5655 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5656 | return em; |
| 5657 | } |
| 5658 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5659 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 5660 | size_t pg_offset, u64 start, u64 len, |
| 5661 | int create) |
| 5662 | { |
| 5663 | struct extent_map *em; |
| 5664 | struct extent_map *hole_em = NULL; |
| 5665 | u64 range_start = start; |
| 5666 | u64 end; |
| 5667 | u64 found; |
| 5668 | u64 found_end; |
| 5669 | int err = 0; |
| 5670 | |
| 5671 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 5672 | if (IS_ERR(em)) |
| 5673 | return em; |
| 5674 | if (em) { |
| 5675 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 5676 | * if our em maps to |
| 5677 | * - a hole or |
| 5678 | * - a pre-alloc extent, |
| 5679 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5680 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 5681 | if (em->block_start != EXTENT_MAP_HOLE && |
| 5682 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5683 | return em; |
| 5684 | else |
| 5685 | hole_em = em; |
| 5686 | } |
| 5687 | |
| 5688 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 5689 | end = start + len; |
| 5690 | if (end < start) |
| 5691 | end = (u64)-1; |
| 5692 | else |
| 5693 | end -= 1; |
| 5694 | |
| 5695 | em = NULL; |
| 5696 | |
| 5697 | /* ok, we didn't find anything, lets look for delalloc */ |
| 5698 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 5699 | end, len, EXTENT_DELALLOC, 1); |
| 5700 | found_end = range_start + found; |
| 5701 | if (found_end < range_start) |
| 5702 | found_end = (u64)-1; |
| 5703 | |
| 5704 | /* |
| 5705 | * we didn't find anything useful, return |
| 5706 | * the original results from get_extent() |
| 5707 | */ |
| 5708 | if (range_start > end || found_end <= start) { |
| 5709 | em = hole_em; |
| 5710 | hole_em = NULL; |
| 5711 | goto out; |
| 5712 | } |
| 5713 | |
| 5714 | /* adjust the range_start to make sure it doesn't |
| 5715 | * go backwards from the start they passed in |
| 5716 | */ |
| 5717 | range_start = max(start,range_start); |
| 5718 | found = found_end - range_start; |
| 5719 | |
| 5720 | if (found > 0) { |
| 5721 | u64 hole_start = start; |
| 5722 | u64 hole_len = len; |
| 5723 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5724 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5725 | if (!em) { |
| 5726 | err = -ENOMEM; |
| 5727 | goto out; |
| 5728 | } |
| 5729 | /* |
| 5730 | * when btrfs_get_extent can't find anything it |
| 5731 | * returns one huge hole |
| 5732 | * |
| 5733 | * make sure what it found really fits our range, and |
| 5734 | * adjust to make sure it is based on the start from |
| 5735 | * the caller |
| 5736 | */ |
| 5737 | if (hole_em) { |
| 5738 | u64 calc_end = extent_map_end(hole_em); |
| 5739 | |
| 5740 | if (calc_end <= start || (hole_em->start > end)) { |
| 5741 | free_extent_map(hole_em); |
| 5742 | hole_em = NULL; |
| 5743 | } else { |
| 5744 | hole_start = max(hole_em->start, start); |
| 5745 | hole_len = calc_end - hole_start; |
| 5746 | } |
| 5747 | } |
| 5748 | em->bdev = NULL; |
| 5749 | if (hole_em && range_start > hole_start) { |
| 5750 | /* our hole starts before our delalloc, so we |
| 5751 | * have to return just the parts of the hole |
| 5752 | * that go until the delalloc starts |
| 5753 | */ |
| 5754 | em->len = min(hole_len, |
| 5755 | range_start - hole_start); |
| 5756 | em->start = hole_start; |
| 5757 | em->orig_start = hole_start; |
| 5758 | /* |
| 5759 | * don't adjust block start at all, |
| 5760 | * it is fixed at EXTENT_MAP_HOLE |
| 5761 | */ |
| 5762 | em->block_start = hole_em->block_start; |
| 5763 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 5764 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 5765 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5766 | } else { |
| 5767 | em->start = range_start; |
| 5768 | em->len = found; |
| 5769 | em->orig_start = range_start; |
| 5770 | em->block_start = EXTENT_MAP_DELALLOC; |
| 5771 | em->block_len = found; |
| 5772 | } |
| 5773 | } else if (hole_em) { |
| 5774 | return hole_em; |
| 5775 | } |
| 5776 | out: |
| 5777 | |
| 5778 | free_extent_map(hole_em); |
| 5779 | if (err) { |
| 5780 | free_extent_map(em); |
| 5781 | return ERR_PTR(err); |
| 5782 | } |
| 5783 | return em; |
| 5784 | } |
| 5785 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5786 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 5787 | u64 start, u64 len) |
| 5788 | { |
| 5789 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5790 | struct btrfs_trans_handle *trans; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 5791 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5792 | struct btrfs_key ins; |
| 5793 | u64 alloc_hint; |
| 5794 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5795 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5796 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5797 | if (IS_ERR(trans)) |
| 5798 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5799 | |
| 5800 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5801 | |
| 5802 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 5803 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 5804 | alloc_hint, &ins, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5805 | if (ret) { |
| 5806 | em = ERR_PTR(ret); |
| 5807 | goto out; |
| 5808 | } |
| 5809 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 5810 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
| 5811 | ins.offset, ins.offset, 0); |
| 5812 | if (IS_ERR(em)) |
| 5813 | goto out; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5814 | |
| 5815 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 5816 | ins.offset, ins.offset, 0); |
| 5817 | if (ret) { |
| 5818 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 5819 | em = ERR_PTR(ret); |
| 5820 | } |
| 5821 | out: |
| 5822 | btrfs_end_transaction(trans, root); |
| 5823 | return em; |
| 5824 | } |
| 5825 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5826 | /* |
| 5827 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 5828 | * block must be cow'd |
| 5829 | */ |
| 5830 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 5831 | struct inode *inode, u64 offset, u64 len) |
| 5832 | { |
| 5833 | struct btrfs_path *path; |
| 5834 | int ret; |
| 5835 | struct extent_buffer *leaf; |
| 5836 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5837 | struct btrfs_file_extent_item *fi; |
| 5838 | struct btrfs_key key; |
| 5839 | u64 disk_bytenr; |
| 5840 | u64 backref_offset; |
| 5841 | u64 extent_end; |
| 5842 | u64 num_bytes; |
| 5843 | int slot; |
| 5844 | int found_type; |
| 5845 | |
| 5846 | path = btrfs_alloc_path(); |
| 5847 | if (!path) |
| 5848 | return -ENOMEM; |
| 5849 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5850 | ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5851 | offset, 0); |
| 5852 | if (ret < 0) |
| 5853 | goto out; |
| 5854 | |
| 5855 | slot = path->slots[0]; |
| 5856 | if (ret == 1) { |
| 5857 | if (slot == 0) { |
| 5858 | /* can't find the item, must cow */ |
| 5859 | ret = 0; |
| 5860 | goto out; |
| 5861 | } |
| 5862 | slot--; |
| 5863 | } |
| 5864 | ret = 0; |
| 5865 | leaf = path->nodes[0]; |
| 5866 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5867 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5868 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5869 | /* not our file or wrong item type, must cow */ |
| 5870 | goto out; |
| 5871 | } |
| 5872 | |
| 5873 | if (key.offset > offset) { |
| 5874 | /* Wrong offset, must cow */ |
| 5875 | goto out; |
| 5876 | } |
| 5877 | |
| 5878 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5879 | found_type = btrfs_file_extent_type(leaf, fi); |
| 5880 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 5881 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 5882 | /* not a regular extent, must cow */ |
| 5883 | goto out; |
| 5884 | } |
| 5885 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5886 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 5887 | |
| 5888 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 5889 | if (extent_end < offset + len) { |
| 5890 | /* extent doesn't include our full range, must cow */ |
| 5891 | goto out; |
| 5892 | } |
| 5893 | |
| 5894 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 5895 | goto out; |
| 5896 | |
| 5897 | /* |
| 5898 | * look for other files referencing this extent, if we |
| 5899 | * find any we must cow |
| 5900 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5901 | if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5902 | key.offset - backref_offset, disk_bytenr)) |
| 5903 | goto out; |
| 5904 | |
| 5905 | /* |
| 5906 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 5907 | * in this extent we are about to write. If there |
| 5908 | * are any csums in that range we have to cow in order |
| 5909 | * to keep the csums correct |
| 5910 | */ |
| 5911 | disk_bytenr += backref_offset; |
| 5912 | disk_bytenr += offset - key.offset; |
| 5913 | num_bytes = min(offset + len, extent_end) - offset; |
| 5914 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 5915 | goto out; |
| 5916 | /* |
| 5917 | * all of the above have passed, it is safe to overwrite this extent |
| 5918 | * without cow |
| 5919 | */ |
| 5920 | ret = 1; |
| 5921 | out: |
| 5922 | btrfs_free_path(path); |
| 5923 | return ret; |
| 5924 | } |
| 5925 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5926 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 5927 | struct extent_state **cached_state, int writing) |
| 5928 | { |
| 5929 | struct btrfs_ordered_extent *ordered; |
| 5930 | int ret = 0; |
| 5931 | |
| 5932 | while (1) { |
| 5933 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 5934 | 0, cached_state); |
| 5935 | /* |
| 5936 | * We're concerned with the entire range that we're going to be |
| 5937 | * doing DIO to, so we need to make sure theres no ordered |
| 5938 | * extents in this range. |
| 5939 | */ |
| 5940 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 5941 | lockend - lockstart + 1); |
| 5942 | |
| 5943 | /* |
| 5944 | * We need to make sure there are no buffered pages in this |
| 5945 | * range either, we could have raced between the invalidate in |
| 5946 | * generic_file_direct_write and locking the extent. The |
| 5947 | * invalidate needs to happen so that reads after a write do not |
| 5948 | * get stale data. |
| 5949 | */ |
| 5950 | if (!ordered && (!writing || |
| 5951 | !test_range_bit(&BTRFS_I(inode)->io_tree, |
| 5952 | lockstart, lockend, EXTENT_UPTODATE, 0, |
| 5953 | *cached_state))) |
| 5954 | break; |
| 5955 | |
| 5956 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 5957 | cached_state, GFP_NOFS); |
| 5958 | |
| 5959 | if (ordered) { |
| 5960 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 5961 | btrfs_put_ordered_extent(ordered); |
| 5962 | } else { |
| 5963 | /* Screw you mmap */ |
| 5964 | ret = filemap_write_and_wait_range(inode->i_mapping, |
| 5965 | lockstart, |
| 5966 | lockend); |
| 5967 | if (ret) |
| 5968 | break; |
| 5969 | |
| 5970 | /* |
| 5971 | * If we found a page that couldn't be invalidated just |
| 5972 | * fall back to buffered. |
| 5973 | */ |
| 5974 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 5975 | lockstart >> PAGE_CACHE_SHIFT, |
| 5976 | lockend >> PAGE_CACHE_SHIFT); |
| 5977 | if (ret) |
| 5978 | break; |
| 5979 | } |
| 5980 | |
| 5981 | cond_resched(); |
| 5982 | } |
| 5983 | |
| 5984 | return ret; |
| 5985 | } |
| 5986 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 5987 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 5988 | u64 len, u64 orig_start, |
| 5989 | u64 block_start, u64 block_len, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 5990 | u64 orig_block_len, int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 5991 | { |
| 5992 | struct extent_map_tree *em_tree; |
| 5993 | struct extent_map *em; |
| 5994 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5995 | int ret; |
| 5996 | |
| 5997 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 5998 | em = alloc_extent_map(); |
| 5999 | if (!em) |
| 6000 | return ERR_PTR(-ENOMEM); |
| 6001 | |
| 6002 | em->start = start; |
| 6003 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 6004 | em->mod_start = start; |
| 6005 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6006 | em->len = len; |
| 6007 | em->block_len = block_len; |
| 6008 | em->block_start = block_start; |
| 6009 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6010 | em->orig_block_len = orig_block_len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6011 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6012 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6013 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 6014 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6015 | |
| 6016 | do { |
| 6017 | btrfs_drop_extent_cache(inode, em->start, |
| 6018 | em->start + em->len - 1, 0); |
| 6019 | write_lock(&em_tree->lock); |
| 6020 | ret = add_extent_mapping(em_tree, em); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6021 | if (!ret) |
| 6022 | list_move(&em->list, |
| 6023 | &em_tree->modified_extents); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6024 | write_unlock(&em_tree->lock); |
| 6025 | } while (ret == -EEXIST); |
| 6026 | |
| 6027 | if (ret) { |
| 6028 | free_extent_map(em); |
| 6029 | return ERR_PTR(ret); |
| 6030 | } |
| 6031 | |
| 6032 | return em; |
| 6033 | } |
| 6034 | |
| 6035 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6036 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 6037 | struct buffer_head *bh_result, int create) |
| 6038 | { |
| 6039 | struct extent_map *em; |
| 6040 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6041 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6042 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6043 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6044 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6045 | struct btrfs_trans_handle *trans; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6046 | int unlock_bits = EXTENT_LOCKED; |
| 6047 | int ret; |
| 6048 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6049 | if (create) { |
| 6050 | ret = btrfs_delalloc_reserve_space(inode, len); |
| 6051 | if (ret) |
| 6052 | return ret; |
| 6053 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6054 | } else { |
| 6055 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6056 | } |
| 6057 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6058 | lockstart = start; |
| 6059 | lockend = start + len - 1; |
| 6060 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6061 | /* |
| 6062 | * If this errors out it's because we couldn't invalidate pagecache for |
| 6063 | * this range and we need to fallback to buffered. |
| 6064 | */ |
| 6065 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 6066 | return -ENOTBLK; |
| 6067 | |
| 6068 | if (create) { |
| 6069 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6070 | lockend, EXTENT_DELALLOC, NULL, |
| 6071 | &cached_state, GFP_NOFS); |
| 6072 | if (ret) |
| 6073 | goto unlock_err; |
| 6074 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6075 | |
| 6076 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6077 | if (IS_ERR(em)) { |
| 6078 | ret = PTR_ERR(em); |
| 6079 | goto unlock_err; |
| 6080 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6081 | |
| 6082 | /* |
| 6083 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 6084 | * io. INLINE is special, and we could probably kludge it in here, but |
| 6085 | * it's still buffered so for safety lets just fall back to the generic |
| 6086 | * buffered path. |
| 6087 | * |
| 6088 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 6089 | * decompress it, so there will be buffering required no matter what we |
| 6090 | * do, so go ahead and fallback to buffered. |
| 6091 | * |
| 6092 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 6093 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 6094 | * the generic code. |
| 6095 | */ |
| 6096 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 6097 | em->block_start == EXTENT_MAP_INLINE) { |
| 6098 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6099 | ret = -ENOTBLK; |
| 6100 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6101 | } |
| 6102 | |
| 6103 | /* Just a good old fashioned hole, return */ |
| 6104 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 6105 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 6106 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6107 | ret = 0; |
| 6108 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6109 | } |
| 6110 | |
| 6111 | /* |
| 6112 | * We don't allocate a new extent in the following cases |
| 6113 | * |
| 6114 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 6115 | * existing extent. |
| 6116 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 6117 | * just use the extent. |
| 6118 | * |
| 6119 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6120 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6121 | len = min(len, em->len - (start - em->start)); |
| 6122 | lockstart = start + len; |
| 6123 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6124 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6125 | |
| 6126 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 6127 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 6128 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6129 | int type; |
| 6130 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6131 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6132 | |
| 6133 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6134 | type = BTRFS_ORDERED_PREALLOC; |
| 6135 | else |
| 6136 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6137 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6138 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6139 | |
| 6140 | /* |
| 6141 | * we're not going to log anything, but we do need |
| 6142 | * to make sure the current transaction stays open |
| 6143 | * while we look for nocow cross refs |
| 6144 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6145 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6146 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6147 | goto must_cow; |
| 6148 | |
| 6149 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6150 | u64 orig_start = em->orig_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6151 | u64 orig_block_len = em->orig_block_len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6152 | |
| 6153 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 6154 | free_extent_map(em); |
| 6155 | em = create_pinned_em(inode, start, len, |
| 6156 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6157 | block_start, len, |
| 6158 | orig_block_len, type); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6159 | if (IS_ERR(em)) { |
| 6160 | btrfs_end_transaction(trans, root); |
| 6161 | goto unlock_err; |
| 6162 | } |
| 6163 | } |
| 6164 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6165 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 6166 | block_start, len, len, type); |
| 6167 | btrfs_end_transaction(trans, root); |
| 6168 | if (ret) { |
| 6169 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6170 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6171 | } |
| 6172 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6173 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6174 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6175 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6176 | must_cow: |
| 6177 | /* |
| 6178 | * this will cow the extent, reset the len in case we changed |
| 6179 | * it above |
| 6180 | */ |
| 6181 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6182 | free_extent_map(em); |
| 6183 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6184 | if (IS_ERR(em)) { |
| 6185 | ret = PTR_ERR(em); |
| 6186 | goto unlock_err; |
| 6187 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6188 | len = min(len, em->len - (start - em->start)); |
| 6189 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6190 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 6191 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6192 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6193 | bh_result->b_bdev = em->bdev; |
| 6194 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6195 | if (create) { |
| 6196 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6197 | set_buffer_new(bh_result); |
| 6198 | |
| 6199 | /* |
| 6200 | * Need to update the i_size under the extent lock so buffered |
| 6201 | * readers will get the updated i_size when we unlock. |
| 6202 | */ |
| 6203 | if (start + len > i_size_read(inode)) |
| 6204 | i_size_write(inode, start + len); |
| 6205 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6206 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6207 | /* |
| 6208 | * In the case of write we need to clear and unlock the entire range, |
| 6209 | * in the case of read we need to unlock only the end area that we |
| 6210 | * aren't using if there is any left over space. |
| 6211 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6212 | if (lockstart < lockend) { |
| 6213 | if (create && len < lockend - lockstart) { |
| 6214 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 6215 | lockstart + len - 1, |
| 6216 | unlock_bits | EXTENT_DEFRAG, 1, 0, |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6217 | &cached_state, GFP_NOFS); |
| 6218 | /* |
| 6219 | * Beside unlock, we also need to cleanup reserved space |
| 6220 | * for the left range by attaching EXTENT_DO_ACCOUNTING. |
| 6221 | */ |
| 6222 | clear_extent_bit(&BTRFS_I(inode)->io_tree, |
| 6223 | lockstart + len, lockend, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 6224 | unlock_bits | EXTENT_DO_ACCOUNTING | |
| 6225 | EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6226 | } else { |
| 6227 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6228 | lockend, unlock_bits, 1, 0, |
| 6229 | &cached_state, GFP_NOFS); |
| 6230 | } |
| 6231 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6232 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6233 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6234 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6235 | free_extent_map(em); |
| 6236 | |
| 6237 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6238 | |
| 6239 | unlock_err: |
| 6240 | if (create) |
| 6241 | unlock_bits |= EXTENT_DO_ACCOUNTING; |
| 6242 | |
| 6243 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6244 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 6245 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6246 | } |
| 6247 | |
| 6248 | struct btrfs_dio_private { |
| 6249 | struct inode *inode; |
| 6250 | u64 logical_offset; |
| 6251 | u64 disk_bytenr; |
| 6252 | u64 bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6253 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6254 | |
| 6255 | /* number of bios pending for this dio */ |
| 6256 | atomic_t pending_bios; |
| 6257 | |
| 6258 | /* IO errors */ |
| 6259 | int errors; |
| 6260 | |
| 6261 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6262 | }; |
| 6263 | |
| 6264 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 6265 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6266 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6267 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 6268 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6269 | struct inode *inode = dip->inode; |
| 6270 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6271 | u64 start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6272 | |
| 6273 | start = dip->logical_offset; |
| 6274 | do { |
| 6275 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 6276 | struct page *page = bvec->bv_page; |
| 6277 | char *kaddr; |
| 6278 | u32 csum = ~(u32)0; |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6279 | u64 private = ~(u32)0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6280 | unsigned long flags; |
| 6281 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6282 | if (get_state_private(&BTRFS_I(inode)->io_tree, |
| 6283 | start, &private)) |
| 6284 | goto failed; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6285 | local_irq_save(flags); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6286 | kaddr = kmap_atomic(page); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6287 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 6288 | csum, bvec->bv_len); |
| 6289 | btrfs_csum_final(csum, (char *)&csum); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6290 | kunmap_atomic(kaddr); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6291 | local_irq_restore(flags); |
| 6292 | |
| 6293 | flush_dcache_page(bvec->bv_page); |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6294 | if (csum != private) { |
| 6295 | failed: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6296 | printk(KERN_ERR "btrfs csum failed ino %llu off" |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6297 | " %llu csum %u private %u\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6298 | (unsigned long long)btrfs_ino(inode), |
| 6299 | (unsigned long long)start, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6300 | csum, (unsigned)private); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6301 | err = -EIO; |
| 6302 | } |
| 6303 | } |
| 6304 | |
| 6305 | start += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6306 | bvec++; |
| 6307 | } while (bvec <= bvec_end); |
| 6308 | |
| 6309 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6310 | dip->logical_offset + dip->bytes - 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6311 | bio->bi_private = dip->private; |
| 6312 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6313 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6314 | |
| 6315 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 6316 | if (err) |
| 6317 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6318 | dio_end_io(bio, err); |
| 6319 | } |
| 6320 | |
| 6321 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 6322 | { |
| 6323 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6324 | struct inode *inode = dip->inode; |
| 6325 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6326 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6327 | u64 ordered_offset = dip->logical_offset; |
| 6328 | u64 ordered_bytes = dip->bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6329 | int ret; |
| 6330 | |
| 6331 | if (err) |
| 6332 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6333 | again: |
| 6334 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 6335 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6336 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6337 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6338 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6339 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6340 | ordered->work.func = finish_ordered_fn; |
| 6341 | ordered->work.flags = 0; |
| 6342 | btrfs_queue_worker(&root->fs_info->endio_write_workers, |
| 6343 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6344 | out_test: |
| 6345 | /* |
| 6346 | * our bio might span multiple ordered extents. If we haven't |
| 6347 | * completed the accounting for the whole dio, go back and try again |
| 6348 | */ |
| 6349 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 6350 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 6351 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6352 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6353 | goto again; |
| 6354 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6355 | out_done: |
| 6356 | bio->bi_private = dip->private; |
| 6357 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6358 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6359 | |
| 6360 | /* If we had an error make sure to clear the uptodate flag */ |
| 6361 | if (err) |
| 6362 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6363 | dio_end_io(bio, err); |
| 6364 | } |
| 6365 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6366 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 6367 | struct bio *bio, int mirror_num, |
| 6368 | unsigned long bio_flags, u64 offset) |
| 6369 | { |
| 6370 | int ret; |
| 6371 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6372 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6373 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6374 | return 0; |
| 6375 | } |
| 6376 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6377 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 6378 | { |
| 6379 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6380 | |
| 6381 | if (err) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6382 | printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6383 | "sector %#Lx len %u err no %d\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6384 | (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw, |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6385 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6386 | dip->errors = 1; |
| 6387 | |
| 6388 | /* |
| 6389 | * before atomic variable goto zero, we must make sure |
| 6390 | * dip->errors is perceived to be set. |
| 6391 | */ |
| 6392 | smp_mb__before_atomic_dec(); |
| 6393 | } |
| 6394 | |
| 6395 | /* if there are more bios still pending for this dio, just exit */ |
| 6396 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 6397 | goto out; |
| 6398 | |
| 6399 | if (dip->errors) |
| 6400 | bio_io_error(dip->orig_bio); |
| 6401 | else { |
| 6402 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 6403 | bio_endio(dip->orig_bio, 0); |
| 6404 | } |
| 6405 | out: |
| 6406 | bio_put(bio); |
| 6407 | } |
| 6408 | |
| 6409 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 6410 | u64 first_sector, gfp_t gfp_flags) |
| 6411 | { |
| 6412 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 6413 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 6414 | } |
| 6415 | |
| 6416 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 6417 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6418 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6419 | { |
| 6420 | int write = rw & REQ_WRITE; |
| 6421 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6422 | int ret; |
| 6423 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 6424 | if (async_submit) |
| 6425 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 6426 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6427 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6428 | |
| 6429 | if (!write) { |
| 6430 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 6431 | if (ret) |
| 6432 | goto err; |
| 6433 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6434 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6435 | if (skip_sum) |
| 6436 | goto map; |
| 6437 | |
| 6438 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6439 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 6440 | inode, rw, bio, 0, 0, |
| 6441 | file_offset, |
| 6442 | __btrfs_submit_bio_start_direct_io, |
| 6443 | __btrfs_submit_bio_done); |
| 6444 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6445 | } else if (write) { |
| 6446 | /* |
| 6447 | * If we aren't doing async submit, calculate the csum of the |
| 6448 | * bio now. |
| 6449 | */ |
| 6450 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 6451 | if (ret) |
| 6452 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 6453 | } else if (!skip_sum) { |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6454 | ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 6455 | if (ret) |
| 6456 | goto err; |
| 6457 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6458 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6459 | map: |
| 6460 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6461 | err: |
| 6462 | bio_put(bio); |
| 6463 | return ret; |
| 6464 | } |
| 6465 | |
| 6466 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 6467 | int skip_sum) |
| 6468 | { |
| 6469 | struct inode *inode = dip->inode; |
| 6470 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6471 | struct bio *bio; |
| 6472 | struct bio *orig_bio = dip->orig_bio; |
| 6473 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 6474 | u64 start_sector = orig_bio->bi_sector; |
| 6475 | u64 file_offset = dip->logical_offset; |
| 6476 | u64 submit_len = 0; |
| 6477 | u64 map_length; |
| 6478 | int nr_pages = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6479 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6480 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6481 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6482 | map_length = orig_bio->bi_size; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 6483 | ret = btrfs_map_block(root->fs_info, READ, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6484 | &map_length, NULL, 0); |
| 6485 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6486 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6487 | return -EIO; |
| 6488 | } |
| 6489 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6490 | if (map_length >= orig_bio->bi_size) { |
| 6491 | bio = orig_bio; |
| 6492 | goto submit; |
| 6493 | } |
| 6494 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6495 | async_submit = 1; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6496 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 6497 | if (!bio) |
| 6498 | return -ENOMEM; |
| 6499 | bio->bi_private = dip; |
| 6500 | bio->bi_end_io = btrfs_end_dio_bio; |
| 6501 | atomic_inc(&dip->pending_bios); |
| 6502 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6503 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 6504 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 6505 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 6506 | bvec->bv_offset) < bvec->bv_len)) { |
| 6507 | /* |
| 6508 | * inc the count before we submit the bio so |
| 6509 | * we know the end IO handler won't happen before |
| 6510 | * we inc the count. Otherwise, the dip might get freed |
| 6511 | * before we're done setting it up |
| 6512 | */ |
| 6513 | atomic_inc(&dip->pending_bios); |
| 6514 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 6515 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6516 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6517 | if (ret) { |
| 6518 | bio_put(bio); |
| 6519 | atomic_dec(&dip->pending_bios); |
| 6520 | goto out_err; |
| 6521 | } |
| 6522 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6523 | start_sector += submit_len >> 9; |
| 6524 | file_offset += submit_len; |
| 6525 | |
| 6526 | submit_len = 0; |
| 6527 | nr_pages = 0; |
| 6528 | |
| 6529 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 6530 | start_sector, GFP_NOFS); |
| 6531 | if (!bio) |
| 6532 | goto out_err; |
| 6533 | bio->bi_private = dip; |
| 6534 | bio->bi_end_io = btrfs_end_dio_bio; |
| 6535 | |
| 6536 | map_length = orig_bio->bi_size; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 6537 | ret = btrfs_map_block(root->fs_info, READ, |
| 6538 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6539 | &map_length, NULL, 0); |
| 6540 | if (ret) { |
| 6541 | bio_put(bio); |
| 6542 | goto out_err; |
| 6543 | } |
| 6544 | } else { |
| 6545 | submit_len += bvec->bv_len; |
| 6546 | nr_pages ++; |
| 6547 | bvec++; |
| 6548 | } |
| 6549 | } |
| 6550 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6551 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6552 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6553 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6554 | if (!ret) |
| 6555 | return 0; |
| 6556 | |
| 6557 | bio_put(bio); |
| 6558 | out_err: |
| 6559 | dip->errors = 1; |
| 6560 | /* |
| 6561 | * before atomic variable goto zero, we must |
| 6562 | * make sure dip->errors is perceived to be set. |
| 6563 | */ |
| 6564 | smp_mb__before_atomic_dec(); |
| 6565 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 6566 | bio_io_error(dip->orig_bio); |
| 6567 | |
| 6568 | /* bio_end_io() will handle error, so we needn't return it */ |
| 6569 | return 0; |
| 6570 | } |
| 6571 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6572 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 6573 | loff_t file_offset) |
| 6574 | { |
| 6575 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6576 | struct btrfs_dio_private *dip; |
| 6577 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6578 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 6579 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6580 | int ret = 0; |
| 6581 | |
| 6582 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 6583 | |
| 6584 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 6585 | if (!dip) { |
| 6586 | ret = -ENOMEM; |
| 6587 | goto free_ordered; |
| 6588 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6589 | |
| 6590 | dip->private = bio->bi_private; |
| 6591 | dip->inode = inode; |
| 6592 | dip->logical_offset = file_offset; |
| 6593 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6594 | dip->bytes = 0; |
| 6595 | do { |
| 6596 | dip->bytes += bvec->bv_len; |
| 6597 | bvec++; |
| 6598 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 6599 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6600 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6601 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6602 | dip->errors = 0; |
| 6603 | dip->orig_bio = bio; |
| 6604 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6605 | |
| 6606 | if (write) |
| 6607 | bio->bi_end_io = btrfs_endio_direct_write; |
| 6608 | else |
| 6609 | bio->bi_end_io = btrfs_endio_direct_read; |
| 6610 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6611 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 6612 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6613 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6614 | free_ordered: |
| 6615 | /* |
| 6616 | * If this is a write, we need to clean up the reserved space and kill |
| 6617 | * the ordered extent. |
| 6618 | */ |
| 6619 | if (write) { |
| 6620 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 6621 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6622 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 6623 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 6624 | btrfs_free_reserved_extent(root, ordered->start, |
| 6625 | ordered->disk_len); |
| 6626 | btrfs_put_ordered_extent(ordered); |
| 6627 | btrfs_put_ordered_extent(ordered); |
| 6628 | } |
| 6629 | bio_endio(bio, ret); |
| 6630 | } |
| 6631 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6632 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 6633 | const struct iovec *iov, loff_t offset, |
| 6634 | unsigned long nr_segs) |
| 6635 | { |
| 6636 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6637 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6638 | size_t size; |
| 6639 | unsigned long addr; |
| 6640 | unsigned blocksize_mask = root->sectorsize - 1; |
| 6641 | ssize_t retval = -EINVAL; |
| 6642 | loff_t end = offset; |
| 6643 | |
| 6644 | if (offset & blocksize_mask) |
| 6645 | goto out; |
| 6646 | |
| 6647 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 6648 | for (seg = 0; seg < nr_segs; seg++) { |
| 6649 | addr = (unsigned long)iov[seg].iov_base; |
| 6650 | size = iov[seg].iov_len; |
| 6651 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6652 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6653 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6654 | |
| 6655 | /* If this is a write we don't need to check anymore */ |
| 6656 | if (rw & WRITE) |
| 6657 | continue; |
| 6658 | |
| 6659 | /* |
| 6660 | * Check to make sure we don't have duplicate iov_base's in this |
| 6661 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 6662 | * when reading back. |
| 6663 | */ |
| 6664 | for (i = seg + 1; i < nr_segs; i++) { |
| 6665 | if (iov[seg].iov_base == iov[i].iov_base) |
| 6666 | goto out; |
| 6667 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6668 | } |
| 6669 | retval = 0; |
| 6670 | out: |
| 6671 | return retval; |
| 6672 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6673 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6674 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 6675 | const struct iovec *iov, loff_t offset, |
| 6676 | unsigned long nr_segs) |
| 6677 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6678 | struct file *file = iocb->ki_filp; |
| 6679 | struct inode *inode = file->f_mapping->host; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6680 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6681 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6682 | offset, nr_segs)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6683 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6684 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6685 | return __blockdev_direct_IO(rw, iocb, inode, |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6686 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 6687 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
| 6688 | btrfs_submit_direct, 0); |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6689 | } |
| 6690 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 6691 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 6692 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6693 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 6694 | __u64 start, __u64 len) |
| 6695 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 6696 | int ret; |
| 6697 | |
| 6698 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 6699 | if (ret) |
| 6700 | return ret; |
| 6701 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6702 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6703 | } |
| 6704 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6705 | int btrfs_readpage(struct file *file, struct page *page) |
| 6706 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6707 | struct extent_io_tree *tree; |
| 6708 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 6709 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6710 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6711 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6712 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 6713 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6714 | struct extent_io_tree *tree; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6715 | |
| 6716 | |
| 6717 | if (current->flags & PF_MEMALLOC) { |
| 6718 | redirty_page_for_writepage(wbc, page); |
| 6719 | unlock_page(page); |
| 6720 | return 0; |
| 6721 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6722 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6723 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 6724 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6725 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6726 | int btrfs_writepages(struct address_space *mapping, |
| 6727 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6728 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6729 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6730 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6731 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6732 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 6733 | } |
| 6734 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6735 | static int |
| 6736 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 6737 | struct list_head *pages, unsigned nr_pages) |
| 6738 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6739 | struct extent_io_tree *tree; |
| 6740 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6741 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 6742 | btrfs_get_extent); |
| 6743 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6744 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6745 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6746 | struct extent_io_tree *tree; |
| 6747 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6748 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6749 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6750 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 6751 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6752 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6753 | if (ret == 1) { |
| 6754 | ClearPagePrivate(page); |
| 6755 | set_page_private(page, 0); |
| 6756 | page_cache_release(page); |
| 6757 | } |
| 6758 | return ret; |
| 6759 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6760 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6761 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 6762 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 6763 | if (PageWriteback(page) || PageDirty(page)) |
| 6764 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 6765 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6766 | } |
| 6767 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6768 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 6769 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6770 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6771 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6772 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6773 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6774 | u64 page_start = page_offset(page); |
| 6775 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6776 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6777 | /* |
| 6778 | * we have the page locked, so new writeback can't start, |
| 6779 | * and the dirty bit won't be cleared while we are here. |
| 6780 | * |
| 6781 | * Wait for IO on this page so that we can safely clear |
| 6782 | * the PagePrivate2 bit and do ordered accounting |
| 6783 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6784 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6785 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6786 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6787 | if (offset) { |
| 6788 | btrfs_releasepage(page, GFP_NOFS); |
| 6789 | return; |
| 6790 | } |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6791 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 6792 | ordered = btrfs_lookup_ordered_extent(inode, page_offset(page)); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6793 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6794 | /* |
| 6795 | * IO on this page will never be started, so we need |
| 6796 | * to account for any ordered extents now |
| 6797 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6798 | clear_extent_bit(tree, page_start, page_end, |
| 6799 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 6800 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 6801 | EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6802 | /* |
| 6803 | * whoever cleared the private bit is responsible |
| 6804 | * for the finish_ordered_io |
| 6805 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6806 | if (TestClearPagePrivate2(page) && |
| 6807 | btrfs_dec_test_ordered_pending(inode, &ordered, page_start, |
| 6808 | PAGE_CACHE_SIZE, 1)) { |
| 6809 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6810 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6811 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6812 | cached_state = NULL; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6813 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6814 | } |
| 6815 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6816 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 6817 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 6818 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6819 | __btrfs_releasepage(page, GFP_NOFS); |
| 6820 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6821 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6822 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6823 | ClearPagePrivate(page); |
| 6824 | set_page_private(page, 0); |
| 6825 | page_cache_release(page); |
| 6826 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6827 | } |
| 6828 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6829 | /* |
| 6830 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 6831 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 6832 | * be careful to check for EOF conditions here. We set the page up correctly |
| 6833 | * for a written page which means we get ENOSPC checking when writing into |
| 6834 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 6835 | * support these features. |
| 6836 | * |
| 6837 | * We are not allowed to take the i_mutex here so we have to play games to |
| 6838 | * protect against truncate races as the page could now be beyond EOF. Because |
| 6839 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 6840 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 6841 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 6842 | * unlock the page. |
| 6843 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6844 | 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] | 6845 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6846 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 6847 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6848 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6849 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 6850 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6851 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6852 | char *kaddr; |
| 6853 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6854 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6855 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6856 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6857 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6858 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6859 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 6860 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6861 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6862 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6863 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6864 | reserved = 1; |
| 6865 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6866 | if (ret) { |
| 6867 | if (ret == -ENOMEM) |
| 6868 | ret = VM_FAULT_OOM; |
| 6869 | else /* -ENOSPC, -EIO, etc */ |
| 6870 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6871 | if (reserved) |
| 6872 | goto out; |
| 6873 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6874 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6875 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6876 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6877 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6878 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6879 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6880 | page_start = page_offset(page); |
| 6881 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6882 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6883 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6884 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6885 | /* page got truncated out from underneath us */ |
| 6886 | goto out_unlock; |
| 6887 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6888 | wait_on_page_writeback(page); |
| 6889 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6890 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6891 | set_page_extent_mapped(page); |
| 6892 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6893 | /* |
| 6894 | * we can't set the delalloc bits if there are pending ordered |
| 6895 | * extents. Drop our locks and wait for them to finish |
| 6896 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6897 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6898 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6899 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6900 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6901 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6902 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6903 | btrfs_put_ordered_extent(ordered); |
| 6904 | goto again; |
| 6905 | } |
| 6906 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6907 | /* |
| 6908 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 6909 | * if it was already dirty, so for space accounting reasons we need to |
| 6910 | * clear any delalloc bits for the range we are fixing to save. There |
| 6911 | * is probably a better way to do this, but for now keep consistent with |
| 6912 | * prepare_pages in the normal write path. |
| 6913 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6914 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 6915 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 6916 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6917 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6918 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6919 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 6920 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6921 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6922 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6923 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6924 | ret = VM_FAULT_SIGBUS; |
| 6925 | goto out_unlock; |
| 6926 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6927 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6928 | |
| 6929 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6930 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6931 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6932 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6933 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6934 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6935 | if (zero_start != PAGE_CACHE_SIZE) { |
| 6936 | kaddr = kmap(page); |
| 6937 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 6938 | flush_dcache_page(page); |
| 6939 | kunmap(page); |
| 6940 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6941 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6942 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6943 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6944 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6945 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 6946 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 6947 | 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] | 6948 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6949 | 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] | 6950 | |
| 6951 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 6952 | if (!ret) { |
| 6953 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6954 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 6955 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6956 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6957 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6958 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6959 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 6960 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6961 | return ret; |
| 6962 | } |
| 6963 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6964 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6965 | { |
| 6966 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6967 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6968 | int ret; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6969 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6970 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6971 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 6972 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6973 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 6974 | ret = btrfs_truncate_page(inode, inode->i_size, 0, 0); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 6975 | if (ret) |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6976 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6977 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6978 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6979 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6980 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6981 | /* |
| 6982 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 6983 | * 3 things going on here |
| 6984 | * |
| 6985 | * 1) We need to reserve space for our orphan item and the space to |
| 6986 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 6987 | * orphan item because we didn't reserve space to remove it. |
| 6988 | * |
| 6989 | * 2) We need to reserve space to update our inode. |
| 6990 | * |
| 6991 | * 3) We need to have something to cache all the space that is going to |
| 6992 | * be free'd up by the truncate operation, but also have some slack |
| 6993 | * space reserved in case it uses space during the truncate (thank you |
| 6994 | * very much snapshotting). |
| 6995 | * |
| 6996 | * And we need these to all be seperate. The fact is we can use alot of |
| 6997 | * space doing the truncate, and we have no earthly idea how much space |
| 6998 | * we will use, so we need the truncate reservation to be seperate so it |
| 6999 | * doesn't end up using space reserved for updating the inode or |
| 7000 | * removing the orphan item. We also need to be able to stop the |
| 7001 | * transaction and start a new one, which means we need to be able to |
| 7002 | * update the inode several times, and we have no idea of knowing how |
| 7003 | * many times that will be, so we can't just reserve 1 item for the |
| 7004 | * entirety of the opration, so that has to be done seperately as well. |
| 7005 | * Then there is the orphan item, which does indeed need to be held on |
| 7006 | * to for the whole operation, and we need nobody to touch this reserved |
| 7007 | * space except the orphan code. |
| 7008 | * |
| 7009 | * So that leaves us with |
| 7010 | * |
| 7011 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 7012 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 7013 | * transaction reservation. |
| 7014 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 7015 | * updating the inode. |
| 7016 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 7017 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7018 | if (!rsv) |
| 7019 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 7020 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7021 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7022 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7023 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7024 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7025 | * 1 for updating the inode. |
| 7026 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 7027 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7028 | if (IS_ERR(trans)) { |
| 7029 | err = PTR_ERR(trans); |
| 7030 | goto out; |
| 7031 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7032 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7033 | /* Migrate the slack space for the truncate to our reserve */ |
| 7034 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 7035 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7036 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7037 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7038 | /* |
| 7039 | * setattr is responsible for setting the ordered_data_close flag, |
| 7040 | * but that is only tested during the last file release. That |
| 7041 | * could happen well after the next commit, leaving a great big |
| 7042 | * window where new writes may get lost if someone chooses to write |
| 7043 | * to this file after truncating to zero |
| 7044 | * |
| 7045 | * The inode doesn't have any dirty data here, and so if we commit |
| 7046 | * this is a noop. If someone immediately starts writing to the inode |
| 7047 | * it is very likely we'll catch some of their writes in this |
| 7048 | * transaction, and the commit will find this file on the ordered |
| 7049 | * data list with good things to send down. |
| 7050 | * |
| 7051 | * This is a best effort solution, there is still a window where |
| 7052 | * using truncate to replace the contents of the file will |
| 7053 | * end up with a zero length file after a crash. |
| 7054 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7055 | if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 7056 | &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7057 | btrfs_add_ordered_operation(trans, root, inode); |
| 7058 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7059 | /* |
| 7060 | * So if we truncate and then write and fsync we normally would just |
| 7061 | * write the extents that changed, which is a problem if we need to |
| 7062 | * first truncate that entire inode. So set this flag so we write out |
| 7063 | * all of the extents in the inode to the sync log so we're completely |
| 7064 | * safe. |
| 7065 | */ |
| 7066 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7067 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7068 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7069 | while (1) { |
| 7070 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 7071 | inode->i_size, |
| 7072 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7073 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7074 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7075 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7076 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7077 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7078 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7079 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7080 | if (ret) { |
| 7081 | err = ret; |
| 7082 | break; |
| 7083 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7084 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7085 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7086 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7087 | |
| 7088 | trans = btrfs_start_transaction(root, 2); |
| 7089 | if (IS_ERR(trans)) { |
| 7090 | ret = err = PTR_ERR(trans); |
| 7091 | trans = NULL; |
| 7092 | break; |
| 7093 | } |
| 7094 | |
| 7095 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 7096 | rsv, min_size); |
| 7097 | BUG_ON(ret); /* shouldn't happen */ |
| 7098 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7099 | } |
| 7100 | |
| 7101 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7102 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7103 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7104 | if (ret) |
| 7105 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7106 | } |
| 7107 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7108 | if (trans) { |
| 7109 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 7110 | ret = btrfs_update_inode(trans, root, inode); |
| 7111 | if (ret && !err) |
| 7112 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7113 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7114 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7115 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7116 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7117 | |
| 7118 | out: |
| 7119 | btrfs_free_block_rsv(root, rsv); |
| 7120 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7121 | if (ret && !err) |
| 7122 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7123 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7124 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7125 | } |
| 7126 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7127 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7128 | * create a new subvolume directory/inode (helper for the ioctl). |
| 7129 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7130 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7131 | struct btrfs_root *new_root, u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7132 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7133 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7134 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7135 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7136 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 7137 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 7138 | new_dirid, new_dirid, |
| 7139 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 7140 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7141 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 7142 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7143 | inode->i_op = &btrfs_dir_inode_operations; |
| 7144 | inode->i_fop = &btrfs_dir_file_operations; |
| 7145 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 7146 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7147 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7148 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7149 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 7150 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7151 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 7152 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7153 | } |
| 7154 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7155 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 7156 | { |
| 7157 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7158 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7159 | |
| 7160 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 7161 | if (!ei) |
| 7162 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7163 | |
| 7164 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7165 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 7166 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7167 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7168 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7169 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7170 | ei->disk_i_size = 0; |
| 7171 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7172 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7173 | ei->index_cnt = (u64)-1; |
| 7174 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7175 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7176 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7177 | spin_lock_init(&ei->lock); |
| 7178 | ei->outstanding_extents = 0; |
| 7179 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7180 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7181 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7182 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7183 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7184 | ei->delayed_node = NULL; |
| 7185 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7186 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 7187 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 7188 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 7189 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 7190 | ei->io_tree.track_uptodate = 1; |
| 7191 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7192 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7193 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 7194 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7195 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7196 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7197 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7198 | RB_CLEAR_NODE(&ei->rb_node); |
| 7199 | |
| 7200 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7201 | } |
| 7202 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7203 | static void btrfs_i_callback(struct rcu_head *head) |
| 7204 | { |
| 7205 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7206 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 7207 | } |
| 7208 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7209 | void btrfs_destroy_inode(struct inode *inode) |
| 7210 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7211 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7212 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7213 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 7214 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7215 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7216 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 7217 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7218 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 7219 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7220 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7221 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7222 | * This can happen where we create an inode, but somebody else also |
| 7223 | * created the same inode and we need to destroy the one we already |
| 7224 | * created. |
| 7225 | */ |
| 7226 | if (!root) |
| 7227 | goto free; |
| 7228 | |
| 7229 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7230 | * Make sure we're properly removed from the ordered operation |
| 7231 | * lists. |
| 7232 | */ |
| 7233 | smp_mb(); |
| 7234 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 7235 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 7236 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 7237 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 7238 | } |
| 7239 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7240 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 7241 | &BTRFS_I(inode)->runtime_flags)) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7242 | printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n", |
| 7243 | (unsigned long long)btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7244 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7245 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7246 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7247 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7248 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 7249 | if (!ordered) |
| 7250 | break; |
| 7251 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7252 | printk(KERN_ERR "btrfs found ordered " |
| 7253 | "extent %llu %llu on inode cleanup\n", |
| 7254 | (unsigned long long)ordered->file_offset, |
| 7255 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7256 | btrfs_remove_ordered_extent(inode, ordered); |
| 7257 | btrfs_put_ordered_extent(ordered); |
| 7258 | btrfs_put_ordered_extent(ordered); |
| 7259 | } |
| 7260 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7261 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7262 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7263 | free: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7264 | btrfs_remove_delayed_node(inode); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7265 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7266 | } |
| 7267 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7268 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7269 | { |
| 7270 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7271 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7272 | if (btrfs_root_refs(&root->root_item) == 0 && |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 7273 | !btrfs_is_free_space_inode(inode)) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7274 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7275 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7276 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7277 | } |
| 7278 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 7279 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7280 | { |
| 7281 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 7282 | |
| 7283 | inode_init_once(&ei->vfs_inode); |
| 7284 | } |
| 7285 | |
| 7286 | void btrfs_destroy_cachep(void) |
| 7287 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 7288 | /* |
| 7289 | * Make sure all delayed rcu free inodes are flushed before we |
| 7290 | * destroy cache. |
| 7291 | */ |
| 7292 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7293 | if (btrfs_inode_cachep) |
| 7294 | kmem_cache_destroy(btrfs_inode_cachep); |
| 7295 | if (btrfs_trans_handle_cachep) |
| 7296 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 7297 | if (btrfs_transaction_cachep) |
| 7298 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7299 | if (btrfs_path_cachep) |
| 7300 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 7301 | if (btrfs_free_space_cachep) |
| 7302 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7303 | if (btrfs_delalloc_work_cachep) |
| 7304 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7305 | } |
| 7306 | |
| 7307 | int btrfs_init_cachep(void) |
| 7308 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7309 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7310 | sizeof(struct btrfs_inode), 0, |
| 7311 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7312 | if (!btrfs_inode_cachep) |
| 7313 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7314 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7315 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7316 | sizeof(struct btrfs_trans_handle), 0, |
| 7317 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7318 | if (!btrfs_trans_handle_cachep) |
| 7319 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7320 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7321 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7322 | sizeof(struct btrfs_transaction), 0, |
| 7323 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7324 | if (!btrfs_transaction_cachep) |
| 7325 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7326 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7327 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7328 | sizeof(struct btrfs_path), 0, |
| 7329 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7330 | if (!btrfs_path_cachep) |
| 7331 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7332 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7333 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 7334 | sizeof(struct btrfs_free_space), 0, |
| 7335 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 7336 | if (!btrfs_free_space_cachep) |
| 7337 | goto fail; |
| 7338 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7339 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 7340 | sizeof(struct btrfs_delalloc_work), 0, |
| 7341 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 7342 | NULL); |
| 7343 | if (!btrfs_delalloc_work_cachep) |
| 7344 | goto fail; |
| 7345 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7346 | return 0; |
| 7347 | fail: |
| 7348 | btrfs_destroy_cachep(); |
| 7349 | return -ENOMEM; |
| 7350 | } |
| 7351 | |
| 7352 | static int btrfs_getattr(struct vfsmount *mnt, |
| 7353 | struct dentry *dentry, struct kstat *stat) |
| 7354 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 7355 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7356 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 7357 | u32 blocksize = inode->i_sb->s_blocksize; |
| 7358 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7359 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 7360 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 7361 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 7362 | |
| 7363 | spin_lock(&BTRFS_I(inode)->lock); |
| 7364 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 7365 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 7366 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 7367 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7368 | return 0; |
| 7369 | } |
| 7370 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7371 | /* |
| 7372 | * If a file is moved, it will inherit the cow and compression flags of the new |
| 7373 | * directory. |
| 7374 | */ |
| 7375 | static void fixup_inode_flags(struct inode *dir, struct inode *inode) |
| 7376 | { |
| 7377 | struct btrfs_inode *b_dir = BTRFS_I(dir); |
| 7378 | struct btrfs_inode *b_inode = BTRFS_I(inode); |
| 7379 | |
| 7380 | if (b_dir->flags & BTRFS_INODE_NODATACOW) |
| 7381 | b_inode->flags |= BTRFS_INODE_NODATACOW; |
| 7382 | else |
| 7383 | b_inode->flags &= ~BTRFS_INODE_NODATACOW; |
| 7384 | |
Liu Bo | bc17823 | 2012-06-14 02:23:18 -0600 | [diff] [blame] | 7385 | if (b_dir->flags & BTRFS_INODE_COMPRESS) { |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7386 | b_inode->flags |= BTRFS_INODE_COMPRESS; |
Liu Bo | bc17823 | 2012-06-14 02:23:18 -0600 | [diff] [blame] | 7387 | b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 7388 | } else { |
| 7389 | b_inode->flags &= ~(BTRFS_INODE_COMPRESS | |
| 7390 | BTRFS_INODE_NOCOMPRESS); |
| 7391 | } |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7392 | } |
| 7393 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7394 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 7395 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7396 | { |
| 7397 | struct btrfs_trans_handle *trans; |
| 7398 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7399 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7400 | struct inode *new_inode = new_dentry->d_inode; |
| 7401 | struct inode *old_inode = old_dentry->d_inode; |
| 7402 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7403 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7404 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7405 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7406 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7407 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7408 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7409 | return -EPERM; |
| 7410 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7411 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7412 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7413 | return -EXDEV; |
| 7414 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7415 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 7416 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7417 | return -ENOTEMPTY; |
| 7418 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7419 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7420 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7421 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 7422 | |
| 7423 | |
| 7424 | /* check for collisions, even if the name isn't there */ |
| 7425 | ret = btrfs_check_dir_item_collision(root, new_dir->i_ino, |
| 7426 | new_dentry->d_name.name, |
| 7427 | new_dentry->d_name.len); |
| 7428 | |
| 7429 | if (ret) { |
| 7430 | if (ret == -EEXIST) { |
| 7431 | /* we shouldn't get |
| 7432 | * eexist without a new_inode */ |
| 7433 | if (!new_inode) { |
| 7434 | WARN_ON(1); |
| 7435 | return ret; |
| 7436 | } |
| 7437 | } else { |
| 7438 | /* maybe -EOVERFLOW */ |
| 7439 | return ret; |
| 7440 | } |
| 7441 | } |
| 7442 | ret = 0; |
| 7443 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7444 | /* |
| 7445 | * we're using rename to replace one file with another. |
| 7446 | * and the replacement file is large. Start IO on it now so |
| 7447 | * we don't add too much work to the end of the transaction |
| 7448 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 7449 | 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] | 7450 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 7451 | filemap_flush(old_inode->i_mapping); |
| 7452 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7453 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7454 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7455 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7456 | /* |
| 7457 | * We want to reserve the absolute worst case amount of items. So if |
| 7458 | * both inodes are subvols and we need to unlink them then that would |
| 7459 | * require 4 item modifications, but if they are both normal inodes it |
| 7460 | * would require 5 item modifications, so we'll assume their normal |
| 7461 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 7462 | * should cover the worst case number of items we'll modify. |
| 7463 | */ |
| 7464 | trans = btrfs_start_transaction(root, 20); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 7465 | if (IS_ERR(trans)) { |
| 7466 | ret = PTR_ERR(trans); |
| 7467 | goto out_notrans; |
| 7468 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7469 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7470 | if (dest != root) |
| 7471 | btrfs_record_root_in_trans(trans, dest); |
| 7472 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7473 | ret = btrfs_set_inode_index(new_dir, &index); |
| 7474 | if (ret) |
| 7475 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7476 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7477 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7478 | /* force full log commit if subvolume involved. */ |
| 7479 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 7480 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7481 | ret = btrfs_insert_inode_ref(trans, dest, |
| 7482 | new_dentry->d_name.name, |
| 7483 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7484 | old_ino, |
| 7485 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7486 | if (ret) |
| 7487 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7488 | /* |
| 7489 | * this is an ugly little race, but the rename is required |
| 7490 | * to make sure that if we crash, the inode is either at the |
| 7491 | * old name or the new one. pinning the log transaction lets |
| 7492 | * us make sure we don't allow a log commit to come in after |
| 7493 | * we unlink the name but before we add the new name back in. |
| 7494 | */ |
| 7495 | btrfs_pin_log_trans(root); |
| 7496 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7497 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7498 | * make sure the inode gets flushed if it is replacing |
| 7499 | * something. |
| 7500 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7501 | 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] | 7502 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7503 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 7504 | inode_inc_iversion(old_dir); |
| 7505 | inode_inc_iversion(new_dir); |
| 7506 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7507 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 7508 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 7509 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7510 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7511 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 7512 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 7513 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7514 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7515 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 7516 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 7517 | old_dentry->d_name.name, |
| 7518 | old_dentry->d_name.len); |
| 7519 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 7520 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 7521 | old_dentry->d_inode, |
| 7522 | old_dentry->d_name.name, |
| 7523 | old_dentry->d_name.len); |
| 7524 | if (!ret) |
| 7525 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7526 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7527 | if (ret) { |
| 7528 | btrfs_abort_transaction(trans, root, ret); |
| 7529 | goto out_fail; |
| 7530 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7531 | |
| 7532 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 7533 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7534 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7535 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7536 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 7537 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 7538 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 7539 | root_objectid, |
| 7540 | new_dentry->d_name.name, |
| 7541 | new_dentry->d_name.len); |
| 7542 | BUG_ON(new_inode->i_nlink == 0); |
| 7543 | } else { |
| 7544 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 7545 | new_dentry->d_inode, |
| 7546 | new_dentry->d_name.name, |
| 7547 | new_dentry->d_name.len); |
| 7548 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7549 | if (!ret && new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7550 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7551 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7552 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7553 | if (ret) { |
| 7554 | btrfs_abort_transaction(trans, root, ret); |
| 7555 | goto out_fail; |
| 7556 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7557 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7558 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7559 | fixup_inode_flags(new_dir, old_inode); |
| 7560 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7561 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 7562 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7563 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7564 | if (ret) { |
| 7565 | btrfs_abort_transaction(trans, root, ret); |
| 7566 | goto out_fail; |
| 7567 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7568 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7569 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 7570 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 7571 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7572 | btrfs_end_log_trans(root); |
| 7573 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7574 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7575 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 7576 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7577 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7578 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7579 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7580 | return ret; |
| 7581 | } |
| 7582 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7583 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 7584 | { |
| 7585 | struct btrfs_delalloc_work *delalloc_work; |
| 7586 | |
| 7587 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 7588 | work); |
| 7589 | if (delalloc_work->wait) |
| 7590 | btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1); |
| 7591 | else |
| 7592 | filemap_flush(delalloc_work->inode->i_mapping); |
| 7593 | |
| 7594 | if (delalloc_work->delay_iput) |
| 7595 | btrfs_add_delayed_iput(delalloc_work->inode); |
| 7596 | else |
| 7597 | iput(delalloc_work->inode); |
| 7598 | complete(&delalloc_work->completion); |
| 7599 | } |
| 7600 | |
| 7601 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 7602 | int wait, int delay_iput) |
| 7603 | { |
| 7604 | struct btrfs_delalloc_work *work; |
| 7605 | |
| 7606 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 7607 | if (!work) |
| 7608 | return NULL; |
| 7609 | |
| 7610 | init_completion(&work->completion); |
| 7611 | INIT_LIST_HEAD(&work->list); |
| 7612 | work->inode = inode; |
| 7613 | work->wait = wait; |
| 7614 | work->delay_iput = delay_iput; |
| 7615 | work->work.func = btrfs_run_delalloc_work; |
| 7616 | |
| 7617 | return work; |
| 7618 | } |
| 7619 | |
| 7620 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 7621 | { |
| 7622 | wait_for_completion(&work->completion); |
| 7623 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 7624 | } |
| 7625 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7626 | /* |
| 7627 | * some fairly slow code that needs optimization. This walks the list |
| 7628 | * of all the inodes with pending delalloc and forces them to disk. |
| 7629 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7630 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7631 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7632 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7633 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7634 | struct btrfs_delalloc_work *work, *next; |
| 7635 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 7636 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7637 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7638 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7639 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 7640 | return -EROFS; |
| 7641 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7642 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 7643 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 7644 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7645 | spin_lock(&root->fs_info->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 7646 | list_splice_init(&root->fs_info->delalloc_inodes, &splice); |
| 7647 | while (!list_empty(&splice)) { |
| 7648 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7649 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 7650 | |
| 7651 | list_del_init(&binode->delalloc_inodes); |
| 7652 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7653 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 7654 | if (!inode) { |
| 7655 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 7656 | &binode->runtime_flags); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 7657 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 7658 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 7659 | |
| 7660 | list_add_tail(&binode->delalloc_inodes, |
| 7661 | &root->fs_info->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7662 | spin_unlock(&root->fs_info->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 7663 | |
| 7664 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
| 7665 | if (unlikely(!work)) { |
| 7666 | ret = -ENOMEM; |
| 7667 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7668 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 7669 | list_add_tail(&work->list, &works); |
| 7670 | btrfs_queue_worker(&root->fs_info->flush_workers, |
| 7671 | &work->work); |
| 7672 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7673 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7674 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7675 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7676 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7677 | |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 7678 | list_for_each_entry_safe(work, next, &works, list) { |
| 7679 | list_del_init(&work->list); |
| 7680 | btrfs_wait_and_free_delalloc_work(work); |
| 7681 | } |
| 7682 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7683 | /* the filemap_flush will queue IO into the worker threads, but |
| 7684 | * we have to make sure the IO is actually started and that |
| 7685 | * ordered extents get created before we return |
| 7686 | */ |
| 7687 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7688 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7689 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7690 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7691 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 7692 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7693 | } |
| 7694 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 7695 | return 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7696 | out: |
| 7697 | list_for_each_entry_safe(work, next, &works, list) { |
| 7698 | list_del_init(&work->list); |
| 7699 | btrfs_wait_and_free_delalloc_work(work); |
| 7700 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 7701 | |
| 7702 | if (!list_empty_careful(&splice)) { |
| 7703 | spin_lock(&root->fs_info->delalloc_lock); |
| 7704 | list_splice_tail(&splice, &root->fs_info->delalloc_inodes); |
| 7705 | spin_unlock(&root->fs_info->delalloc_lock); |
| 7706 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7707 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7708 | } |
| 7709 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7710 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 7711 | const char *symname) |
| 7712 | { |
| 7713 | struct btrfs_trans_handle *trans; |
| 7714 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 7715 | struct btrfs_path *path; |
| 7716 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7717 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7718 | int err; |
| 7719 | int drop_inode = 0; |
| 7720 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7721 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7722 | int name_len; |
| 7723 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7724 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7725 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7726 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7727 | |
| 7728 | name_len = strlen(symname) + 1; |
| 7729 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 7730 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7731 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7732 | /* |
| 7733 | * 2 items for inode item and ref |
| 7734 | * 2 items for dir items |
| 7735 | * 1 item for xattr if selinux is on |
| 7736 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7737 | trans = btrfs_start_transaction(root, 5); |
| 7738 | if (IS_ERR(trans)) |
| 7739 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7740 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 7741 | err = btrfs_find_free_ino(root, &objectid); |
| 7742 | if (err) |
| 7743 | goto out_unlock; |
| 7744 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7745 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7746 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7747 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7748 | if (IS_ERR(inode)) { |
| 7749 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7750 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7751 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7752 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 7753 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7754 | if (err) { |
| 7755 | drop_inode = 1; |
| 7756 | goto out_unlock; |
| 7757 | } |
| 7758 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 7759 | /* |
| 7760 | * If the active LSM wants to access the inode during |
| 7761 | * d_instantiate it needs these. Smack checks to see |
| 7762 | * if the filesystem supports xattrs by looking at the |
| 7763 | * ops vector. |
| 7764 | */ |
| 7765 | inode->i_fop = &btrfs_file_operations; |
| 7766 | inode->i_op = &btrfs_file_inode_operations; |
| 7767 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 7768 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7769 | if (err) |
| 7770 | drop_inode = 1; |
| 7771 | else { |
| 7772 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7773 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7774 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7775 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7776 | if (drop_inode) |
| 7777 | goto out_unlock; |
| 7778 | |
| 7779 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 7780 | if (!path) { |
| 7781 | err = -ENOMEM; |
| 7782 | drop_inode = 1; |
| 7783 | goto out_unlock; |
| 7784 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7785 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7786 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7787 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 7788 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 7789 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 7790 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7791 | if (err) { |
| 7792 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 7793 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7794 | goto out_unlock; |
| 7795 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7796 | leaf = path->nodes[0]; |
| 7797 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 7798 | struct btrfs_file_extent_item); |
| 7799 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 7800 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7801 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7802 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 7803 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 7804 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 7805 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 7806 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7807 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7808 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 7809 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7810 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7811 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7812 | inode->i_op = &btrfs_symlink_inode_operations; |
| 7813 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7814 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7815 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7816 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7817 | err = btrfs_update_inode(trans, root, inode); |
| 7818 | if (err) |
| 7819 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7820 | |
| 7821 | out_unlock: |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 7822 | if (!err) |
| 7823 | d_instantiate(dentry, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7824 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7825 | if (drop_inode) { |
| 7826 | inode_dec_link_count(inode); |
| 7827 | iput(inode); |
| 7828 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7829 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7830 | return err; |
| 7831 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7832 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7833 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7834 | u64 start, u64 num_bytes, u64 min_size, |
| 7835 | loff_t actual_len, u64 *alloc_hint, |
| 7836 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7837 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7838 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 7839 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7840 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7841 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7842 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7843 | u64 i_size; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7844 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7845 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7846 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7847 | if (trans) |
| 7848 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7849 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7850 | if (own_trans) { |
| 7851 | trans = btrfs_start_transaction(root, 3); |
| 7852 | if (IS_ERR(trans)) { |
| 7853 | ret = PTR_ERR(trans); |
| 7854 | break; |
| 7855 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7856 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7857 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7858 | ret = btrfs_reserve_extent(trans, root, num_bytes, min_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 7859 | 0, *alloc_hint, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7860 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7861 | if (own_trans) |
| 7862 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7863 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7864 | } |
| 7865 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7866 | ret = insert_reserved_file_extent(trans, inode, |
| 7867 | cur_offset, ins.objectid, |
| 7868 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 7869 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7870 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7871 | if (ret) { |
| 7872 | btrfs_abort_transaction(trans, root, ret); |
| 7873 | if (own_trans) |
| 7874 | btrfs_end_transaction(trans, root); |
| 7875 | break; |
| 7876 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 7877 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7878 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7879 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7880 | em = alloc_extent_map(); |
| 7881 | if (!em) { |
| 7882 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 7883 | &BTRFS_I(inode)->runtime_flags); |
| 7884 | goto next; |
| 7885 | } |
| 7886 | |
| 7887 | em->start = cur_offset; |
| 7888 | em->orig_start = cur_offset; |
| 7889 | em->len = ins.offset; |
| 7890 | em->block_start = ins.objectid; |
| 7891 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7892 | em->orig_block_len = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7893 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 7894 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 7895 | em->generation = trans->transid; |
| 7896 | |
| 7897 | while (1) { |
| 7898 | write_lock(&em_tree->lock); |
| 7899 | ret = add_extent_mapping(em_tree, em); |
| 7900 | if (!ret) |
| 7901 | list_move(&em->list, |
| 7902 | &em_tree->modified_extents); |
| 7903 | write_unlock(&em_tree->lock); |
| 7904 | if (ret != -EEXIST) |
| 7905 | break; |
| 7906 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7907 | cur_offset + ins.offset - 1, |
| 7908 | 0); |
| 7909 | } |
| 7910 | free_extent_map(em); |
| 7911 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7912 | num_bytes -= ins.offset; |
| 7913 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7914 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7915 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 7916 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7917 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7918 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7919 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7920 | (actual_len > inode->i_size) && |
| 7921 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7922 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7923 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7924 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7925 | i_size = cur_offset; |
| 7926 | i_size_write(inode, i_size); |
| 7927 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7928 | } |
| 7929 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7930 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7931 | |
| 7932 | if (ret) { |
| 7933 | btrfs_abort_transaction(trans, root, ret); |
| 7934 | if (own_trans) |
| 7935 | btrfs_end_transaction(trans, root); |
| 7936 | break; |
| 7937 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7938 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7939 | if (own_trans) |
| 7940 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7941 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7942 | return ret; |
| 7943 | } |
| 7944 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7945 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7946 | u64 start, u64 num_bytes, u64 min_size, |
| 7947 | loff_t actual_len, u64 *alloc_hint) |
| 7948 | { |
| 7949 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7950 | min_size, actual_len, alloc_hint, |
| 7951 | NULL); |
| 7952 | } |
| 7953 | |
| 7954 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 7955 | struct btrfs_trans_handle *trans, int mode, |
| 7956 | u64 start, u64 num_bytes, u64 min_size, |
| 7957 | loff_t actual_len, u64 *alloc_hint) |
| 7958 | { |
| 7959 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7960 | min_size, actual_len, alloc_hint, trans); |
| 7961 | } |
| 7962 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7963 | static int btrfs_set_page_dirty(struct page *page) |
| 7964 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7965 | return __set_page_dirty_nobuffers(page); |
| 7966 | } |
| 7967 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 7968 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7969 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7970 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 7971 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7972 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 7973 | if (mask & MAY_WRITE && |
| 7974 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 7975 | if (btrfs_root_readonly(root)) |
| 7976 | return -EROFS; |
| 7977 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 7978 | return -EACCES; |
| 7979 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 7980 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7981 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7982 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7983 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7984 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7985 | .lookup = btrfs_lookup, |
| 7986 | .create = btrfs_create, |
| 7987 | .unlink = btrfs_unlink, |
| 7988 | .link = btrfs_link, |
| 7989 | .mkdir = btrfs_mkdir, |
| 7990 | .rmdir = btrfs_rmdir, |
| 7991 | .rename = btrfs_rename, |
| 7992 | .symlink = btrfs_symlink, |
| 7993 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7994 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7995 | .setxattr = btrfs_setxattr, |
| 7996 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7997 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7998 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7999 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8000 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8001 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8002 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8003 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8004 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8005 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8006 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8007 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 8008 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8009 | .llseek = generic_file_llseek, |
| 8010 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 8011 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8012 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8013 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8014 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8015 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 8016 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8017 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8018 | }; |
| 8019 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8020 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8021 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 8022 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 8023 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8024 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8025 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8026 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 8027 | .set_bit_hook = btrfs_set_bit_hook, |
| 8028 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8029 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 8030 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8031 | }; |
| 8032 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 8033 | /* |
| 8034 | * btrfs doesn't support the bmap operation because swapfiles |
| 8035 | * use bmap to make a mapping of extents in the file. They assume |
| 8036 | * these extents won't change over the life of the file and they |
| 8037 | * use the bmap result to do IO directly to the drive. |
| 8038 | * |
| 8039 | * the btrfs bmap call would return logical addresses that aren't |
| 8040 | * suitable for IO and they also will change frequently as COW |
| 8041 | * operations happen. So, swapfile + btrfs == corruption. |
| 8042 | * |
| 8043 | * For now we're avoiding this by dropping bmap. |
| 8044 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8045 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8046 | .readpage = btrfs_readpage, |
| 8047 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8048 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8049 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8050 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8051 | .invalidatepage = btrfs_invalidatepage, |
| 8052 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8053 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 8054 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8055 | }; |
| 8056 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8057 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8058 | .readpage = btrfs_readpage, |
| 8059 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 8060 | .invalidatepage = btrfs_invalidatepage, |
| 8061 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8062 | }; |
| 8063 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8064 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8065 | .getattr = btrfs_getattr, |
| 8066 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8067 | .setxattr = btrfs_setxattr, |
| 8068 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 8069 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8070 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8071 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8072 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8073 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8074 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8075 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8076 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8077 | .getattr = btrfs_getattr, |
| 8078 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8079 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8080 | .setxattr = btrfs_setxattr, |
| 8081 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8082 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8083 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8084 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8085 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8086 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8087 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8088 | .readlink = generic_readlink, |
| 8089 | .follow_link = page_follow_link_light, |
| 8090 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 8091 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 8092 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8093 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 8094 | .setxattr = btrfs_setxattr, |
| 8095 | .getxattr = btrfs_getxattr, |
| 8096 | .listxattr = btrfs_listxattr, |
| 8097 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8098 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8099 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8100 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8101 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 8102 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8103 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 8104 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8105 | }; |