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> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 42 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 43 | #include "ctree.h" |
| 44 | #include "disk-io.h" |
| 45 | #include "transaction.h" |
| 46 | #include "btrfs_inode.h" |
| 47 | #include "ioctl.h" |
| 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; |
| 74 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 75 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 76 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 77 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 78 | |
| 79 | #define S_SHIFT 12 |
| 80 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 81 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 82 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 83 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 84 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 85 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 86 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 87 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 88 | }; |
| 89 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 90 | static int btrfs_setsize(struct inode *inode, loff_t newsize); |
| 91 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 92 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 93 | static noinline int cow_file_range(struct inode *inode, |
| 94 | struct page *locked_page, |
| 95 | u64 start, u64 end, int *page_started, |
| 96 | unsigned long *nr_written, int unlock); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 97 | static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 98 | struct btrfs_root *root, struct inode *inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 99 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 100 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 101 | struct inode *inode, struct inode *dir, |
| 102 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 103 | { |
| 104 | int err; |
| 105 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 106 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 107 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 108 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 109 | return err; |
| 110 | } |
| 111 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 112 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 113 | * this does all the hard work for inserting an inline extent into |
| 114 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 115 | * no overlapping inline items exist in the btree |
| 116 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 117 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 118 | struct btrfs_root *root, struct inode *inode, |
| 119 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 120 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 121 | struct page **compressed_pages) |
| 122 | { |
| 123 | struct btrfs_key key; |
| 124 | struct btrfs_path *path; |
| 125 | struct extent_buffer *leaf; |
| 126 | struct page *page = NULL; |
| 127 | char *kaddr; |
| 128 | unsigned long ptr; |
| 129 | struct btrfs_file_extent_item *ei; |
| 130 | int err = 0; |
| 131 | int ret; |
| 132 | size_t cur_size = size; |
| 133 | size_t datasize; |
| 134 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 135 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 136 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 137 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 138 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 139 | path = btrfs_alloc_path(); |
| 140 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 141 | return -ENOMEM; |
| 142 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 143 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 144 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 145 | key.objectid = btrfs_ino(inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 146 | key.offset = start; |
| 147 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 148 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 149 | |
| 150 | inode_add_bytes(inode, size); |
| 151 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 152 | datasize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 153 | if (ret) { |
| 154 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 155 | goto fail; |
| 156 | } |
| 157 | leaf = path->nodes[0]; |
| 158 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 159 | struct btrfs_file_extent_item); |
| 160 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 161 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 162 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 163 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 164 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 165 | ptr = btrfs_file_extent_inline_start(ei); |
| 166 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 167 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 168 | struct page *cpage; |
| 169 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 170 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 172 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 173 | PAGE_CACHE_SIZE); |
| 174 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 175 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 176 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 177 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 178 | |
| 179 | i++; |
| 180 | ptr += cur_size; |
| 181 | compressed_size -= cur_size; |
| 182 | } |
| 183 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 184 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 185 | } else { |
| 186 | page = find_get_page(inode->i_mapping, |
| 187 | start >> PAGE_CACHE_SHIFT); |
| 188 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 189 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 190 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 191 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 192 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 193 | page_cache_release(page); |
| 194 | } |
| 195 | btrfs_mark_buffer_dirty(leaf); |
| 196 | btrfs_free_path(path); |
| 197 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 198 | /* |
| 199 | * we're an inline extent, so nobody can |
| 200 | * extend the file past i_size without locking |
| 201 | * a page we already have locked. |
| 202 | * |
| 203 | * We must do any isize and inode updates |
| 204 | * before we unlock the pages. Otherwise we |
| 205 | * could end up racing with unlink. |
| 206 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 207 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 208 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 209 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 210 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 211 | fail: |
| 212 | btrfs_free_path(path); |
| 213 | return err; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /* |
| 218 | * conditionally insert an inline extent into the file. This |
| 219 | * does the checks required to make sure the data is small enough |
| 220 | * to fit as an inline extent. |
| 221 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 222 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 223 | struct btrfs_root *root, |
| 224 | struct inode *inode, u64 start, u64 end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 225 | size_t compressed_size, int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 226 | struct page **compressed_pages) |
| 227 | { |
| 228 | u64 isize = i_size_read(inode); |
| 229 | u64 actual_end = min(end + 1, isize); |
| 230 | u64 inline_len = actual_end - start; |
| 231 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 232 | ~((u64)root->sectorsize - 1); |
| 233 | u64 hint_byte; |
| 234 | u64 data_len = inline_len; |
| 235 | int ret; |
| 236 | |
| 237 | if (compressed_size) |
| 238 | data_len = compressed_size; |
| 239 | |
| 240 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 241 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 242 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 243 | (!compressed_size && |
| 244 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 245 | end + 1 < isize || |
| 246 | data_len > root->fs_info->max_inline) { |
| 247 | return 1; |
| 248 | } |
| 249 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 250 | ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 251 | &hint_byte, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 252 | if (ret) |
| 253 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 254 | |
| 255 | if (isize > actual_end) |
| 256 | inline_len = min_t(u64, isize, actual_end); |
| 257 | ret = insert_inline_extent(trans, root, inode, start, |
| 258 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 259 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 260 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 261 | btrfs_abort_transaction(trans, root, ret); |
| 262 | return ret; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 263 | } else if (ret == -ENOSPC) { |
| 264 | return 1; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 265 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 266 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 267 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 268 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 272 | struct async_extent { |
| 273 | u64 start; |
| 274 | u64 ram_size; |
| 275 | u64 compressed_size; |
| 276 | struct page **pages; |
| 277 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 278 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 279 | struct list_head list; |
| 280 | }; |
| 281 | |
| 282 | struct async_cow { |
| 283 | struct inode *inode; |
| 284 | struct btrfs_root *root; |
| 285 | struct page *locked_page; |
| 286 | u64 start; |
| 287 | u64 end; |
| 288 | struct list_head extents; |
| 289 | struct btrfs_work work; |
| 290 | }; |
| 291 | |
| 292 | static noinline int add_async_extent(struct async_cow *cow, |
| 293 | u64 start, u64 ram_size, |
| 294 | u64 compressed_size, |
| 295 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 296 | unsigned long nr_pages, |
| 297 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 298 | { |
| 299 | struct async_extent *async_extent; |
| 300 | |
| 301 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 302 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 303 | async_extent->start = start; |
| 304 | async_extent->ram_size = ram_size; |
| 305 | async_extent->compressed_size = compressed_size; |
| 306 | async_extent->pages = pages; |
| 307 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 308 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 309 | list_add_tail(&async_extent->list, &cow->extents); |
| 310 | return 0; |
| 311 | } |
| 312 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 313 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 314 | * we create compressed extents in two phases. The first |
| 315 | * phase compresses a range of pages that have already been |
| 316 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 317 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 318 | * This is done inside an ordered work queue, and the compression |
| 319 | * is spread across many cpus. The actual IO submission is step |
| 320 | * two, and the ordered work queue takes care of making sure that |
| 321 | * happens in the same order things were put onto the queue by |
| 322 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 323 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 324 | * If this code finds it can't get good compression, it puts an |
| 325 | * entry onto the work queue to write the uncompressed bytes. This |
| 326 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 327 | * are written in the same order that the flusher thread sent them |
| 328 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 329 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 330 | static noinline int compress_file_range(struct inode *inode, |
| 331 | struct page *locked_page, |
| 332 | u64 start, u64 end, |
| 333 | struct async_cow *async_cow, |
| 334 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 335 | { |
| 336 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 337 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 338 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 339 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 340 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 341 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 342 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 343 | struct page **pages = NULL; |
| 344 | unsigned long nr_pages; |
| 345 | unsigned long nr_pages_ret = 0; |
| 346 | unsigned long total_compressed = 0; |
| 347 | unsigned long total_in = 0; |
| 348 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 349 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 350 | int i; |
| 351 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 352 | int compress_type = root->fs_info->compress_type; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 353 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 354 | /* if this is a small write inside eof, kick off a defrag */ |
| 355 | if ((end - start + 1) < 16 * 1024 && |
| 356 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 357 | btrfs_add_inode_defrag(NULL, inode); |
| 358 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 359 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 360 | again: |
| 361 | will_compress = 0; |
| 362 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 363 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 364 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 365 | /* |
| 366 | * we don't want to send crud past the end of i_size through |
| 367 | * compression, that's just a waste of CPU time. So, if the |
| 368 | * end of the file is before the start of our current |
| 369 | * requested range of bytes, we bail out to the uncompressed |
| 370 | * cleanup code that can deal with all of this. |
| 371 | * |
| 372 | * It isn't really the fastest way to fix things, but this is a |
| 373 | * very uncommon corner. |
| 374 | */ |
| 375 | if (actual_end <= start) |
| 376 | goto cleanup_and_bail_uncompressed; |
| 377 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 378 | total_compressed = actual_end - start; |
| 379 | |
| 380 | /* we want to make sure that amount of ram required to uncompress |
| 381 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 382 | * of a compressed extent to 128k. This is a crucial number |
| 383 | * because it also controls how easily we can spread reads across |
| 384 | * cpus for decompression. |
| 385 | * |
| 386 | * We also want to make sure the amount of IO required to do |
| 387 | * a random read is reasonably small, so we limit the size of |
| 388 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 389 | */ |
| 390 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 391 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 392 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 393 | total_in = 0; |
| 394 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 395 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 396 | /* |
| 397 | * we do compression for mount -o compress and when the |
| 398 | * inode has not been flagged as nocompress. This flag can |
| 399 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 400 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 401 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 402 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 403 | (BTRFS_I(inode)->force_compress) || |
| 404 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 405 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 406 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 407 | if (!pages) { |
| 408 | /* just bail out to the uncompressed code */ |
| 409 | goto cont; |
| 410 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 411 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 412 | if (BTRFS_I(inode)->force_compress) |
| 413 | compress_type = BTRFS_I(inode)->force_compress; |
| 414 | |
| 415 | ret = btrfs_compress_pages(compress_type, |
| 416 | inode->i_mapping, start, |
| 417 | total_compressed, pages, |
| 418 | nr_pages, &nr_pages_ret, |
| 419 | &total_in, |
| 420 | &total_compressed, |
| 421 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 422 | |
| 423 | if (!ret) { |
| 424 | unsigned long offset = total_compressed & |
| 425 | (PAGE_CACHE_SIZE - 1); |
| 426 | struct page *page = pages[nr_pages_ret - 1]; |
| 427 | char *kaddr; |
| 428 | |
| 429 | /* zero the tail end of the last page, we might be |
| 430 | * sending it down to disk |
| 431 | */ |
| 432 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 433 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 434 | memset(kaddr + offset, 0, |
| 435 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 436 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 437 | } |
| 438 | will_compress = 1; |
| 439 | } |
| 440 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 441 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 442 | if (start == 0) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 443 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 444 | if (IS_ERR(trans)) { |
| 445 | ret = PTR_ERR(trans); |
| 446 | trans = NULL; |
| 447 | goto cleanup_and_out; |
| 448 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 449 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 450 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 451 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 452 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 453 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 454 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 455 | */ |
| 456 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 457 | start, end, 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 458 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 459 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 460 | ret = cow_file_range_inline(trans, root, inode, |
| 461 | start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 462 | total_compressed, |
| 463 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 464 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 465 | if (ret <= 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 466 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 467 | * inline extent creation worked or returned error, |
| 468 | * we don't need to create any more async work items. |
| 469 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 470 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 471 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 472 | &BTRFS_I(inode)->io_tree, |
| 473 | start, end, NULL, |
| 474 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 475 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 476 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 477 | |
| 478 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 479 | goto free_pages_out; |
| 480 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 481 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | if (will_compress) { |
| 485 | /* |
| 486 | * we aren't doing an inline extent round the compressed size |
| 487 | * up to a block size boundary so the allocator does sane |
| 488 | * things |
| 489 | */ |
| 490 | total_compressed = (total_compressed + blocksize - 1) & |
| 491 | ~(blocksize - 1); |
| 492 | |
| 493 | /* |
| 494 | * one last check to make sure the compression is really a |
| 495 | * win, compare the page count read with the blocks on disk |
| 496 | */ |
| 497 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 498 | ~(PAGE_CACHE_SIZE - 1); |
| 499 | if (total_compressed >= total_in) { |
| 500 | will_compress = 0; |
| 501 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 502 | num_bytes = total_in; |
| 503 | } |
| 504 | } |
| 505 | if (!will_compress && pages) { |
| 506 | /* |
| 507 | * the compression code ran but failed to make things smaller, |
| 508 | * free any pages it allocated and our page pointer array |
| 509 | */ |
| 510 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 511 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 512 | page_cache_release(pages[i]); |
| 513 | } |
| 514 | kfree(pages); |
| 515 | pages = NULL; |
| 516 | total_compressed = 0; |
| 517 | nr_pages_ret = 0; |
| 518 | |
| 519 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 520 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 521 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 522 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 523 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 524 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 525 | if (will_compress) { |
| 526 | *num_added += 1; |
| 527 | |
| 528 | /* the async work queues will take care of doing actual |
| 529 | * allocation on disk for these compressed pages, |
| 530 | * and will submit them to the elevator. |
| 531 | */ |
| 532 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 533 | total_compressed, pages, nr_pages_ret, |
| 534 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 535 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 536 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 537 | start += num_bytes; |
| 538 | pages = NULL; |
| 539 | cond_resched(); |
| 540 | goto again; |
| 541 | } |
| 542 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 543 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 544 | /* |
| 545 | * No compression, but we still need to write the pages in |
| 546 | * the file we've been given so far. redirty the locked |
| 547 | * page if it corresponds to our extent and set things up |
| 548 | * for the async work queue to run cow_file_range to do |
| 549 | * the normal delalloc dance |
| 550 | */ |
| 551 | if (page_offset(locked_page) >= start && |
| 552 | page_offset(locked_page) <= end) { |
| 553 | __set_page_dirty_nobuffers(locked_page); |
| 554 | /* unlocked later on in the async handlers */ |
| 555 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 556 | add_async_extent(async_cow, start, end - start + 1, |
| 557 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 558 | *num_added += 1; |
| 559 | } |
| 560 | |
| 561 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 562 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 563 | |
| 564 | free_pages_out: |
| 565 | for (i = 0; i < nr_pages_ret; i++) { |
| 566 | WARN_ON(pages[i]->mapping); |
| 567 | page_cache_release(pages[i]); |
| 568 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 569 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 570 | |
| 571 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 572 | |
| 573 | cleanup_and_out: |
| 574 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 575 | start, end, NULL, |
| 576 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 577 | EXTENT_CLEAR_DIRTY | |
| 578 | EXTENT_CLEAR_DELALLOC | |
| 579 | EXTENT_SET_WRITEBACK | |
| 580 | EXTENT_END_WRITEBACK); |
| 581 | if (!trans || IS_ERR(trans)) |
| 582 | btrfs_error(root->fs_info, ret, "Failed to join transaction"); |
| 583 | else |
| 584 | btrfs_abort_transaction(trans, root, ret); |
| 585 | goto free_pages_out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* |
| 589 | * phase two of compressed writeback. This is the ordered portion |
| 590 | * of the code, which only gets called in the order the work was |
| 591 | * queued. We walk all the async extents created by compress_file_range |
| 592 | * and send them down to the disk. |
| 593 | */ |
| 594 | static noinline int submit_compressed_extents(struct inode *inode, |
| 595 | struct async_cow *async_cow) |
| 596 | { |
| 597 | struct async_extent *async_extent; |
| 598 | u64 alloc_hint = 0; |
| 599 | struct btrfs_trans_handle *trans; |
| 600 | struct btrfs_key ins; |
| 601 | struct extent_map *em; |
| 602 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 603 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 604 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 605 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 606 | |
| 607 | if (list_empty(&async_cow->extents)) |
| 608 | return 0; |
| 609 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 610 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 611 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 612 | async_extent = list_entry(async_cow->extents.next, |
| 613 | struct async_extent, list); |
| 614 | list_del(&async_extent->list); |
| 615 | |
| 616 | io_tree = &BTRFS_I(inode)->io_tree; |
| 617 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 618 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 619 | /* did the compression code fall back to uncompressed IO? */ |
| 620 | if (!async_extent->pages) { |
| 621 | int page_started = 0; |
| 622 | unsigned long nr_written = 0; |
| 623 | |
| 624 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 625 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 626 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 627 | |
| 628 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 629 | ret = cow_file_range(inode, async_cow->locked_page, |
| 630 | async_extent->start, |
| 631 | async_extent->start + |
| 632 | async_extent->ram_size - 1, |
| 633 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 634 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 635 | /* JDM XXX */ |
| 636 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 637 | /* |
| 638 | * if page_started, cow_file_range inserted an |
| 639 | * inline extent and took care of all the unlocking |
| 640 | * and IO for us. Otherwise, we need to submit |
| 641 | * all those pages down to the drive. |
| 642 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 643 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 644 | extent_write_locked_range(io_tree, |
| 645 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 646 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 647 | async_extent->ram_size - 1, |
| 648 | btrfs_get_extent, |
| 649 | WB_SYNC_ALL); |
| 650 | kfree(async_extent); |
| 651 | cond_resched(); |
| 652 | continue; |
| 653 | } |
| 654 | |
| 655 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 656 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 657 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 658 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 659 | if (IS_ERR(trans)) { |
| 660 | ret = PTR_ERR(trans); |
| 661 | } else { |
| 662 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 663 | ret = btrfs_reserve_extent(trans, root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 664 | async_extent->compressed_size, |
| 665 | async_extent->compressed_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 666 | 0, alloc_hint, &ins, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 667 | if (ret) |
| 668 | btrfs_abort_transaction(trans, root, ret); |
| 669 | btrfs_end_transaction(trans, root); |
| 670 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 671 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 672 | if (ret) { |
| 673 | int i; |
| 674 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 675 | WARN_ON(async_extent->pages[i]->mapping); |
| 676 | page_cache_release(async_extent->pages[i]); |
| 677 | } |
| 678 | kfree(async_extent->pages); |
| 679 | async_extent->nr_pages = 0; |
| 680 | async_extent->pages = NULL; |
| 681 | unlock_extent(io_tree, async_extent->start, |
| 682 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 683 | async_extent->ram_size - 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 684 | if (ret == -ENOSPC) |
| 685 | goto retry; |
| 686 | goto out_free; /* JDM: Requeue? */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 687 | } |
| 688 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 689 | /* |
| 690 | * here we're doing allocation and writeback of the |
| 691 | * compressed pages |
| 692 | */ |
| 693 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 694 | async_extent->start + |
| 695 | async_extent->ram_size - 1, 0); |
| 696 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 697 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 698 | BUG_ON(!em); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 699 | em->start = async_extent->start; |
| 700 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 701 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 702 | |
| 703 | em->block_start = ins.objectid; |
| 704 | em->block_len = ins.offset; |
| 705 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 706 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 707 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 708 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 709 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 710 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 711 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 712 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 713 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 714 | if (ret != -EEXIST) { |
| 715 | free_extent_map(em); |
| 716 | break; |
| 717 | } |
| 718 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 719 | async_extent->start + |
| 720 | async_extent->ram_size - 1, 0); |
| 721 | } |
| 722 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 723 | ret = btrfs_add_ordered_extent_compress(inode, |
| 724 | async_extent->start, |
| 725 | ins.objectid, |
| 726 | async_extent->ram_size, |
| 727 | ins.offset, |
| 728 | BTRFS_ORDERED_COMPRESSED, |
| 729 | async_extent->compress_type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 730 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 731 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 732 | /* |
| 733 | * clear dirty, set writeback and unlock the pages. |
| 734 | */ |
| 735 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 736 | &BTRFS_I(inode)->io_tree, |
| 737 | async_extent->start, |
| 738 | async_extent->start + |
| 739 | async_extent->ram_size - 1, |
| 740 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 741 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 742 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 743 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 744 | |
| 745 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 746 | async_extent->start, |
| 747 | async_extent->ram_size, |
| 748 | ins.objectid, |
| 749 | ins.offset, async_extent->pages, |
| 750 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 751 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 752 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 753 | alloc_hint = ins.objectid + ins.offset; |
| 754 | kfree(async_extent); |
| 755 | cond_resched(); |
| 756 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 757 | ret = 0; |
| 758 | out: |
| 759 | return ret; |
| 760 | out_free: |
| 761 | kfree(async_extent); |
| 762 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 763 | } |
| 764 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 765 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 766 | u64 num_bytes) |
| 767 | { |
| 768 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 769 | struct extent_map *em; |
| 770 | u64 alloc_hint = 0; |
| 771 | |
| 772 | read_lock(&em_tree->lock); |
| 773 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 774 | if (em) { |
| 775 | /* |
| 776 | * if block start isn't an actual block number then find the |
| 777 | * first block in this inode and use that as a hint. If that |
| 778 | * block is also bogus then just don't worry about it. |
| 779 | */ |
| 780 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 781 | free_extent_map(em); |
| 782 | em = search_extent_mapping(em_tree, 0, 0); |
| 783 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 784 | alloc_hint = em->block_start; |
| 785 | if (em) |
| 786 | free_extent_map(em); |
| 787 | } else { |
| 788 | alloc_hint = em->block_start; |
| 789 | free_extent_map(em); |
| 790 | } |
| 791 | } |
| 792 | read_unlock(&em_tree->lock); |
| 793 | |
| 794 | return alloc_hint; |
| 795 | } |
| 796 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 797 | /* |
| 798 | * when extent_io.c finds a delayed allocation range in the file, |
| 799 | * the call backs end up in this code. The basic idea is to |
| 800 | * allocate extents on disk for the range, and create ordered data structs |
| 801 | * in ram to track those extents. |
| 802 | * |
| 803 | * locked_page is the page that writepage had locked already. We use |
| 804 | * it to make sure we don't do extra locks or unlocks. |
| 805 | * |
| 806 | * *page_started is set to one if we unlock locked_page and do everything |
| 807 | * required to start IO on it. It may be clean and already done with |
| 808 | * IO when we return. |
| 809 | */ |
| 810 | static noinline int cow_file_range(struct inode *inode, |
| 811 | struct page *locked_page, |
| 812 | u64 start, u64 end, int *page_started, |
| 813 | unsigned long *nr_written, |
| 814 | int unlock) |
| 815 | { |
| 816 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 817 | struct btrfs_trans_handle *trans; |
| 818 | u64 alloc_hint = 0; |
| 819 | u64 num_bytes; |
| 820 | unsigned long ram_size; |
| 821 | u64 disk_num_bytes; |
| 822 | u64 cur_alloc_size; |
| 823 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 824 | struct btrfs_key ins; |
| 825 | struct extent_map *em; |
| 826 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 827 | int ret = 0; |
| 828 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 829 | BUG_ON(btrfs_is_free_space_inode(inode)); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 830 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 831 | if (IS_ERR(trans)) { |
| 832 | extent_clear_unlock_delalloc(inode, |
| 833 | &BTRFS_I(inode)->io_tree, |
Josef Bacik | beb42dd | 2012-05-30 15:35:17 -0400 | [diff] [blame] | 834 | start, end, locked_page, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 835 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 836 | EXTENT_CLEAR_UNLOCK | |
| 837 | EXTENT_CLEAR_DELALLOC | |
| 838 | EXTENT_CLEAR_DIRTY | |
| 839 | EXTENT_SET_WRITEBACK | |
| 840 | EXTENT_END_WRITEBACK); |
| 841 | return PTR_ERR(trans); |
| 842 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 843 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 844 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 845 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 846 | num_bytes = max(blocksize, num_bytes); |
| 847 | disk_num_bytes = num_bytes; |
| 848 | ret = 0; |
| 849 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 850 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 851 | if (num_bytes < 64 * 1024 && |
| 852 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 853 | btrfs_add_inode_defrag(trans, inode); |
| 854 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 855 | if (start == 0) { |
| 856 | /* lets try to make an inline extent */ |
| 857 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 858 | start, end, 0, 0, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 859 | if (ret == 0) { |
| 860 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 861 | &BTRFS_I(inode)->io_tree, |
| 862 | start, end, NULL, |
| 863 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 864 | EXTENT_CLEAR_UNLOCK | |
| 865 | EXTENT_CLEAR_DELALLOC | |
| 866 | EXTENT_CLEAR_DIRTY | |
| 867 | EXTENT_SET_WRITEBACK | |
| 868 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 869 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 870 | *nr_written = *nr_written + |
| 871 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 872 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 873 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 874 | } else if (ret < 0) { |
| 875 | btrfs_abort_transaction(trans, root, ret); |
| 876 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 877 | } |
| 878 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 879 | |
| 880 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 881 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 882 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 883 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 884 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 885 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 886 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 887 | unsigned long op; |
| 888 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 889 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 890 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 891 | root->sectorsize, 0, alloc_hint, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 892 | &ins, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 893 | if (ret < 0) { |
| 894 | btrfs_abort_transaction(trans, root, ret); |
| 895 | goto out_unlock; |
| 896 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 897 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 898 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 899 | BUG_ON(!em); /* -ENOMEM */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 900 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 901 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 902 | ram_size = ins.offset; |
| 903 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 904 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 905 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 906 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 907 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 908 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 909 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 910 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 911 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 912 | ret = add_extent_mapping(em_tree, em); |
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 | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 958 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 959 | ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 960 | out: |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 961 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 962 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 963 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 964 | out_unlock: |
| 965 | extent_clear_unlock_delalloc(inode, |
| 966 | &BTRFS_I(inode)->io_tree, |
Josef Bacik | beb42dd | 2012-05-30 15:35:17 -0400 | [diff] [blame] | 967 | start, end, locked_page, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 968 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 969 | EXTENT_CLEAR_UNLOCK | |
| 970 | EXTENT_CLEAR_DELALLOC | |
| 971 | EXTENT_CLEAR_DIRTY | |
| 972 | EXTENT_SET_WRITEBACK | |
| 973 | EXTENT_END_WRITEBACK); |
| 974 | |
| 975 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 976 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 977 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 978 | /* |
| 979 | * work queue call back to started compression on a file and pages |
| 980 | */ |
| 981 | static noinline void async_cow_start(struct btrfs_work *work) |
| 982 | { |
| 983 | struct async_cow *async_cow; |
| 984 | int num_added = 0; |
| 985 | async_cow = container_of(work, struct async_cow, work); |
| 986 | |
| 987 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 988 | async_cow->start, async_cow->end, async_cow, |
| 989 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 990 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 991 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 992 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 993 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* |
| 997 | * work queue call back to submit previously compressed pages |
| 998 | */ |
| 999 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1000 | { |
| 1001 | struct async_cow *async_cow; |
| 1002 | struct btrfs_root *root; |
| 1003 | unsigned long nr_pages; |
| 1004 | |
| 1005 | async_cow = container_of(work, struct async_cow, work); |
| 1006 | |
| 1007 | root = async_cow->root; |
| 1008 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1009 | PAGE_CACHE_SHIFT; |
| 1010 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1011 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1012 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1013 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1014 | wake_up(&root->fs_info->async_submit_wait); |
| 1015 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1016 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1017 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1018 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1019 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1020 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1021 | { |
| 1022 | struct async_cow *async_cow; |
| 1023 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1024 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1025 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1026 | kfree(async_cow); |
| 1027 | } |
| 1028 | |
| 1029 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1030 | u64 start, u64 end, int *page_started, |
| 1031 | unsigned long *nr_written) |
| 1032 | { |
| 1033 | struct async_cow *async_cow; |
| 1034 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1035 | unsigned long nr_pages; |
| 1036 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1037 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1038 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1039 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1040 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1041 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1042 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1043 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1044 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1045 | async_cow->root = root; |
| 1046 | async_cow->locked_page = locked_page; |
| 1047 | async_cow->start = start; |
| 1048 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1049 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1050 | cur_end = end; |
| 1051 | else |
| 1052 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1053 | |
| 1054 | async_cow->end = cur_end; |
| 1055 | INIT_LIST_HEAD(&async_cow->extents); |
| 1056 | |
| 1057 | async_cow->work.func = async_cow_start; |
| 1058 | async_cow->work.ordered_func = async_cow_submit; |
| 1059 | async_cow->work.ordered_free = async_cow_free; |
| 1060 | async_cow->work.flags = 0; |
| 1061 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1062 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1063 | PAGE_CACHE_SHIFT; |
| 1064 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1065 | |
| 1066 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 1067 | &async_cow->work); |
| 1068 | |
| 1069 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1070 | wait_event(root->fs_info->async_submit_wait, |
| 1071 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1072 | limit)); |
| 1073 | } |
| 1074 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1075 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1076 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1077 | wait_event(root->fs_info->async_submit_wait, |
| 1078 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1079 | 0)); |
| 1080 | } |
| 1081 | |
| 1082 | *nr_written += nr_pages; |
| 1083 | start = cur_end + 1; |
| 1084 | } |
| 1085 | *page_started = 1; |
| 1086 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1087 | } |
| 1088 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1089 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1090 | u64 bytenr, u64 num_bytes) |
| 1091 | { |
| 1092 | int ret; |
| 1093 | struct btrfs_ordered_sum *sums; |
| 1094 | LIST_HEAD(list); |
| 1095 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1096 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1097 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1098 | if (ret == 0 && list_empty(&list)) |
| 1099 | return 0; |
| 1100 | |
| 1101 | while (!list_empty(&list)) { |
| 1102 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1103 | list_del(&sums->list); |
| 1104 | kfree(sums); |
| 1105 | } |
| 1106 | return 1; |
| 1107 | } |
| 1108 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1109 | /* |
| 1110 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1111 | * of the extents that exist in the file, and COWs the file as required. |
| 1112 | * |
| 1113 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1114 | * blocks on disk |
| 1115 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1116 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1117 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1118 | u64 start, u64 end, int *page_started, int force, |
| 1119 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1120 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1121 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1122 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1123 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1124 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1125 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1126 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1127 | u64 cow_start; |
| 1128 | u64 cur_offset; |
| 1129 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1130 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1131 | u64 disk_bytenr; |
| 1132 | u64 num_bytes; |
| 1133 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1134 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1135 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1136 | int nocow; |
| 1137 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1138 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1139 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1140 | |
| 1141 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1142 | if (!path) { |
| 1143 | extent_clear_unlock_delalloc(inode, |
| 1144 | &BTRFS_I(inode)->io_tree, |
| 1145 | start, end, locked_page, |
| 1146 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1147 | EXTENT_CLEAR_UNLOCK | |
| 1148 | EXTENT_CLEAR_DELALLOC | |
| 1149 | EXTENT_CLEAR_DIRTY | |
| 1150 | EXTENT_SET_WRITEBACK | |
| 1151 | EXTENT_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1152 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1153 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1154 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1155 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1156 | |
| 1157 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1158 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1159 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1160 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1161 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1162 | if (IS_ERR(trans)) { |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1163 | extent_clear_unlock_delalloc(inode, |
| 1164 | &BTRFS_I(inode)->io_tree, |
| 1165 | start, end, locked_page, |
| 1166 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1167 | EXTENT_CLEAR_UNLOCK | |
| 1168 | EXTENT_CLEAR_DELALLOC | |
| 1169 | EXTENT_CLEAR_DIRTY | |
| 1170 | EXTENT_SET_WRITEBACK | |
| 1171 | EXTENT_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1172 | btrfs_free_path(path); |
| 1173 | return PTR_ERR(trans); |
| 1174 | } |
| 1175 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1176 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1177 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1178 | cow_start = (u64)-1; |
| 1179 | cur_offset = start; |
| 1180 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1181 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1182 | cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1183 | if (ret < 0) { |
| 1184 | btrfs_abort_transaction(trans, root, ret); |
| 1185 | goto error; |
| 1186 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1187 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1188 | leaf = path->nodes[0]; |
| 1189 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1190 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1191 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1192 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1193 | path->slots[0]--; |
| 1194 | } |
| 1195 | check_prev = 0; |
| 1196 | next_slot: |
| 1197 | leaf = path->nodes[0]; |
| 1198 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1199 | ret = btrfs_next_leaf(root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1200 | if (ret < 0) { |
| 1201 | btrfs_abort_transaction(trans, root, ret); |
| 1202 | goto error; |
| 1203 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1204 | if (ret > 0) |
| 1205 | break; |
| 1206 | leaf = path->nodes[0]; |
| 1207 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1208 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1209 | nocow = 0; |
| 1210 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1211 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1212 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1213 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1214 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1215 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1216 | found_key.offset > end) |
| 1217 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1218 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1219 | if (found_key.offset > cur_offset) { |
| 1220 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1221 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1222 | goto out_check; |
| 1223 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1224 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1225 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1226 | struct btrfs_file_extent_item); |
| 1227 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1228 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1229 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1230 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1231 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1232 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1233 | extent_end = found_key.offset + |
| 1234 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1235 | if (extent_end <= start) { |
| 1236 | path->slots[0]++; |
| 1237 | goto next_slot; |
| 1238 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1239 | if (disk_bytenr == 0) |
| 1240 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1241 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1242 | btrfs_file_extent_encryption(leaf, fi) || |
| 1243 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1244 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1245 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1246 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1247 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1248 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1249 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1250 | found_key.offset - |
| 1251 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1252 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1253 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1254 | disk_bytenr += cur_offset - found_key.offset; |
| 1255 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1256 | /* |
| 1257 | * force cow if csum exists in the range. |
| 1258 | * this ensure that csum for a given extent are |
| 1259 | * either valid or do not exist. |
| 1260 | */ |
| 1261 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1262 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1263 | nocow = 1; |
| 1264 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1265 | extent_end = found_key.offset + |
| 1266 | btrfs_file_extent_inline_len(leaf, fi); |
| 1267 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1268 | } else { |
| 1269 | BUG_ON(1); |
| 1270 | } |
| 1271 | out_check: |
| 1272 | if (extent_end <= start) { |
| 1273 | path->slots[0]++; |
| 1274 | goto next_slot; |
| 1275 | } |
| 1276 | if (!nocow) { |
| 1277 | if (cow_start == (u64)-1) |
| 1278 | cow_start = cur_offset; |
| 1279 | cur_offset = extent_end; |
| 1280 | if (cur_offset > end) |
| 1281 | break; |
| 1282 | path->slots[0]++; |
| 1283 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1284 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1285 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1286 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1287 | if (cow_start != (u64)-1) { |
| 1288 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1289 | found_key.offset - 1, page_started, |
| 1290 | nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1291 | if (ret) { |
| 1292 | btrfs_abort_transaction(trans, root, ret); |
| 1293 | goto error; |
| 1294 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1295 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1296 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1297 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1298 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1299 | struct extent_map *em; |
| 1300 | struct extent_map_tree *em_tree; |
| 1301 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1302 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1303 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1304 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1305 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1306 | em->len = num_bytes; |
| 1307 | em->block_len = num_bytes; |
| 1308 | em->block_start = disk_bytenr; |
| 1309 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1310 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame^] | 1311 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1312 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1313 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1314 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1315 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1316 | if (ret != -EEXIST) { |
| 1317 | free_extent_map(em); |
| 1318 | break; |
| 1319 | } |
| 1320 | btrfs_drop_extent_cache(inode, em->start, |
| 1321 | em->start + em->len - 1, 0); |
| 1322 | } |
| 1323 | type = BTRFS_ORDERED_PREALLOC; |
| 1324 | } else { |
| 1325 | type = BTRFS_ORDERED_NOCOW; |
| 1326 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1327 | |
| 1328 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1329 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1330 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1331 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1332 | if (root->root_key.objectid == |
| 1333 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1334 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1335 | num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1336 | if (ret) { |
| 1337 | btrfs_abort_transaction(trans, root, ret); |
| 1338 | goto error; |
| 1339 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1340 | } |
| 1341 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1342 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1343 | cur_offset, cur_offset + num_bytes - 1, |
| 1344 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1345 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1346 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1347 | cur_offset = extent_end; |
| 1348 | if (cur_offset > end) |
| 1349 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1350 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1351 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1352 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1353 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1354 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1355 | cur_offset = end; |
| 1356 | } |
| 1357 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1358 | if (cow_start != (u64)-1) { |
| 1359 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1360 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1361 | if (ret) { |
| 1362 | btrfs_abort_transaction(trans, root, ret); |
| 1363 | goto error; |
| 1364 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1365 | } |
| 1366 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1367 | error: |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1368 | if (nolock) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1369 | err = btrfs_end_transaction_nolock(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1370 | } else { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1371 | err = btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1372 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1373 | if (!ret) |
| 1374 | ret = err; |
| 1375 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1376 | if (ret && cur_offset < end) |
| 1377 | extent_clear_unlock_delalloc(inode, |
| 1378 | &BTRFS_I(inode)->io_tree, |
| 1379 | cur_offset, end, locked_page, |
| 1380 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1381 | EXTENT_CLEAR_UNLOCK | |
| 1382 | EXTENT_CLEAR_DELALLOC | |
| 1383 | EXTENT_CLEAR_DIRTY | |
| 1384 | EXTENT_SET_WRITEBACK | |
| 1385 | EXTENT_END_WRITEBACK); |
| 1386 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1387 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1388 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1389 | } |
| 1390 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1391 | /* |
| 1392 | * extent_io.c call back to do delayed allocation processing |
| 1393 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1394 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1395 | u64 start, u64 end, int *page_started, |
| 1396 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1397 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1398 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1399 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1400 | |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1401 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1402 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1403 | page_started, 1, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1404 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1405 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1406 | page_started, 0, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1407 | } else if (!btrfs_test_opt(root, COMPRESS) && |
| 1408 | !(BTRFS_I(inode)->force_compress) && |
| 1409 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1410 | ret = cow_file_range(inode, locked_page, start, end, |
| 1411 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1412 | } else { |
| 1413 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1414 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1415 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1416 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1417 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1418 | return ret; |
| 1419 | } |
| 1420 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1421 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1422 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1423 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1424 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1425 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1426 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1427 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1428 | spin_lock(&BTRFS_I(inode)->lock); |
| 1429 | BTRFS_I(inode)->outstanding_extents++; |
| 1430 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1435 | * extents so we can keep track of new extents that are just merged onto old |
| 1436 | * extents, such as when we are doing sequential writes, so we can properly |
| 1437 | * account for the metadata space we'll need. |
| 1438 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1439 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1440 | struct extent_state *new, |
| 1441 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1442 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1443 | /* not delalloc, ignore it */ |
| 1444 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1445 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1446 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1447 | spin_lock(&BTRFS_I(inode)->lock); |
| 1448 | BTRFS_I(inode)->outstanding_extents--; |
| 1449 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1450 | } |
| 1451 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1452 | /* |
| 1453 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1454 | * bytes in this file, and to maintain the list of inodes that |
| 1455 | * have pending delalloc work to be done. |
| 1456 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1457 | static void btrfs_set_bit_hook(struct inode *inode, |
| 1458 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1459 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1460 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1461 | /* |
| 1462 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1463 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1464 | * bit, which is only set or cleared with irqs on |
| 1465 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1466 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1467 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1468 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1469 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1470 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1471 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1472 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1473 | } else { |
| 1474 | spin_lock(&BTRFS_I(inode)->lock); |
| 1475 | BTRFS_I(inode)->outstanding_extents++; |
| 1476 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1477 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1478 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1479 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1480 | BTRFS_I(inode)->delalloc_bytes += len; |
| 1481 | root->fs_info->delalloc_bytes += len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1482 | if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1483 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1484 | &root->fs_info->delalloc_inodes); |
| 1485 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1486 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1487 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1488 | } |
| 1489 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1490 | /* |
| 1491 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1492 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1493 | static void btrfs_clear_bit_hook(struct inode *inode, |
| 1494 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1495 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1496 | /* |
| 1497 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1498 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1499 | * bit, which is only set or cleared with irqs on |
| 1500 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1501 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1502 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1503 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1504 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1505 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1506 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1507 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1508 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1509 | spin_lock(&BTRFS_I(inode)->lock); |
| 1510 | BTRFS_I(inode)->outstanding_extents--; |
| 1511 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1512 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1513 | |
| 1514 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1515 | btrfs_delalloc_release_metadata(inode, len); |
| 1516 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1517 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 1518 | && do_list) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1519 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1520 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1521 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1522 | root->fs_info->delalloc_bytes -= len; |
| 1523 | BTRFS_I(inode)->delalloc_bytes -= len; |
| 1524 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1525 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1526 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1527 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1528 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1529 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1530 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1531 | } |
| 1532 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1533 | /* |
| 1534 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1535 | * we don't create bios that span stripes or chunks |
| 1536 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1537 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1538 | size_t size, struct bio *bio, |
| 1539 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1540 | { |
| 1541 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1542 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1543 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1544 | u64 length = 0; |
| 1545 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1546 | int ret; |
| 1547 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1548 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1549 | return 0; |
| 1550 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1551 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1552 | map_tree = &root->fs_info->mapping_tree; |
| 1553 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1554 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1555 | &map_length, NULL, 0); |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1556 | /* Will always return 0 or 1 with map_multi == NULL */ |
| 1557 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1558 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1559 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1560 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1561 | } |
| 1562 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1563 | /* |
| 1564 | * in order to insert checksums into the metadata in large chunks, |
| 1565 | * we wait until bio submission time. All the pages in the bio are |
| 1566 | * checksummed and sums are attached onto the ordered extent record. |
| 1567 | * |
| 1568 | * At IO completion time the cums attached on the ordered extent record |
| 1569 | * are inserted into the btree |
| 1570 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1571 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1572 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1573 | unsigned long bio_flags, |
| 1574 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1575 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1576 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1577 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1578 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1579 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1580 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1581 | return 0; |
| 1582 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1583 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1584 | /* |
| 1585 | * in order to insert checksums into the metadata in large chunks, |
| 1586 | * we wait until bio submission time. All the pages in the bio are |
| 1587 | * checksummed and sums are attached onto the ordered extent record. |
| 1588 | * |
| 1589 | * At IO completion time the cums attached on the ordered extent record |
| 1590 | * are inserted into the btree |
| 1591 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1592 | 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] | 1593 | int mirror_num, unsigned long bio_flags, |
| 1594 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1595 | { |
| 1596 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1597 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1598 | } |
| 1599 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1600 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1601 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1602 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1603 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1604 | 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] | 1605 | int mirror_num, unsigned long bio_flags, |
| 1606 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1607 | { |
| 1608 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1609 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1610 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1611 | int metadata = 0; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1612 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1613 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1614 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1615 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1616 | metadata = 2; |
| 1617 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1618 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1619 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1620 | if (ret) |
| 1621 | return ret; |
| 1622 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1623 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1624 | return btrfs_submit_compressed_read(inode, bio, |
| 1625 | mirror_num, bio_flags); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1626 | } else if (!skip_sum) { |
| 1627 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1628 | if (ret) |
| 1629 | return ret; |
| 1630 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1631 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1632 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1633 | /* csum items have already been cloned */ |
| 1634 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1635 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1636 | /* we're doing a write, do the async checksumming */ |
| 1637 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1638 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1639 | bio_flags, bio_offset, |
| 1640 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1641 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1642 | } |
| 1643 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1644 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1645 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1646 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1647 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1648 | /* |
| 1649 | * given a list of ordered sums record them in the inode. This happens |
| 1650 | * at IO completion time based on sums calculated at bio submission time. |
| 1651 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1652 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1653 | struct inode *inode, u64 file_offset, |
| 1654 | struct list_head *list) |
| 1655 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1656 | struct btrfs_ordered_sum *sum; |
| 1657 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1658 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1659 | btrfs_csum_file_blocks(trans, |
| 1660 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1661 | } |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1665 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1666 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1667 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1668 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1669 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1670 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1671 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1672 | } |
| 1673 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1674 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1675 | struct btrfs_writepage_fixup { |
| 1676 | struct page *page; |
| 1677 | struct btrfs_work work; |
| 1678 | }; |
| 1679 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1680 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1681 | { |
| 1682 | struct btrfs_writepage_fixup *fixup; |
| 1683 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1684 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1685 | struct page *page; |
| 1686 | struct inode *inode; |
| 1687 | u64 page_start; |
| 1688 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1689 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1690 | |
| 1691 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1692 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1693 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1694 | lock_page(page); |
| 1695 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1696 | ClearPageChecked(page); |
| 1697 | goto out_page; |
| 1698 | } |
| 1699 | |
| 1700 | inode = page->mapping->host; |
| 1701 | page_start = page_offset(page); |
| 1702 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1703 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1704 | 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] | 1705 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1706 | |
| 1707 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1708 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1709 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1710 | |
| 1711 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1712 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1713 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1714 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1715 | unlock_page(page); |
| 1716 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1717 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1718 | goto again; |
| 1719 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1720 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1721 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1722 | if (ret) { |
| 1723 | mapping_set_error(page->mapping, ret); |
| 1724 | end_extent_writepage(page, ret, page_start, page_end); |
| 1725 | ClearPageChecked(page); |
| 1726 | goto out; |
| 1727 | } |
| 1728 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1729 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1730 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1731 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1732 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1733 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1734 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1735 | out_page: |
| 1736 | unlock_page(page); |
| 1737 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1738 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | * There are a few paths in the higher layers of the kernel that directly |
| 1743 | * set the page dirty bit without asking the filesystem if it is a |
| 1744 | * good idea. This causes problems because we want to make sure COW |
| 1745 | * properly happens and the data=ordered rules are followed. |
| 1746 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1747 | * 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] | 1748 | * hasn't been properly setup for IO. We kick off an async process |
| 1749 | * to fix it up. The async helper will wait for ordered extents, set |
| 1750 | * the delalloc bit and make it safe to write the page. |
| 1751 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1752 | 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] | 1753 | { |
| 1754 | struct inode *inode = page->mapping->host; |
| 1755 | struct btrfs_writepage_fixup *fixup; |
| 1756 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1757 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1758 | /* this page is properly in the ordered list */ |
| 1759 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1760 | return 0; |
| 1761 | |
| 1762 | if (PageChecked(page)) |
| 1763 | return -EAGAIN; |
| 1764 | |
| 1765 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1766 | if (!fixup) |
| 1767 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1768 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1769 | SetPageChecked(page); |
| 1770 | page_cache_get(page); |
| 1771 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1772 | fixup->page = page; |
| 1773 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1774 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1775 | } |
| 1776 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1777 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1778 | struct inode *inode, u64 file_pos, |
| 1779 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1780 | u64 num_bytes, u64 ram_bytes, |
| 1781 | u8 compression, u8 encryption, |
| 1782 | u16 other_encoding, int extent_type) |
| 1783 | { |
| 1784 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1785 | struct btrfs_file_extent_item *fi; |
| 1786 | struct btrfs_path *path; |
| 1787 | struct extent_buffer *leaf; |
| 1788 | struct btrfs_key ins; |
| 1789 | u64 hint; |
| 1790 | int ret; |
| 1791 | |
| 1792 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1793 | if (!path) |
| 1794 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1795 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1796 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1797 | |
| 1798 | /* |
| 1799 | * we may be replacing one extent in the tree with another. |
| 1800 | * The new extent is pinned in the extent map, and we don't want |
| 1801 | * to drop it from the cache until it is completely in the btree. |
| 1802 | * |
| 1803 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1804 | * the caller is expected to unpin it and allow it to be merged |
| 1805 | * with the others. |
| 1806 | */ |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1807 | ret = btrfs_drop_extents(trans, root, inode, file_pos, |
| 1808 | file_pos + num_bytes, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1809 | &hint, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1810 | if (ret) |
| 1811 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1812 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1813 | ins.objectid = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1814 | ins.offset = file_pos; |
| 1815 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1816 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1817 | if (ret) |
| 1818 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1819 | leaf = path->nodes[0]; |
| 1820 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1821 | struct btrfs_file_extent_item); |
| 1822 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1823 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1824 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1825 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1826 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1827 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1828 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1829 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1830 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1831 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1832 | |
| 1833 | btrfs_unlock_up_safe(path, 1); |
| 1834 | btrfs_set_lock_blocking(leaf); |
| 1835 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1836 | btrfs_mark_buffer_dirty(leaf); |
| 1837 | |
| 1838 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1839 | |
| 1840 | ins.objectid = disk_bytenr; |
| 1841 | ins.offset = disk_num_bytes; |
| 1842 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1843 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1844 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1845 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1846 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1847 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1848 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1849 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1850 | } |
| 1851 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1852 | /* |
| 1853 | * helper function for btrfs_finish_ordered_io, this |
| 1854 | * just reads in some of the csum leaves to prime them into ram |
| 1855 | * before we start the transaction. It limits the amount of btree |
| 1856 | * reads required while inside the transaction. |
| 1857 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1858 | /* as ordered data IO finishes, this gets called so we can finish |
| 1859 | * an ordered extent if the range of bytes in the file it covers are |
| 1860 | * fully written. |
| 1861 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1862 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1863 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1864 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1865 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1866 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1867 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1868 | struct extent_state *cached_state = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1869 | int compress_type = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1870 | int ret; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1871 | bool nolock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1872 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1873 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1874 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1875 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 1876 | ret = -EIO; |
| 1877 | goto out; |
| 1878 | } |
| 1879 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1880 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1881 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1882 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1883 | if (!ret) { |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1884 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1885 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1886 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1887 | trans = btrfs_join_transaction(root); |
Liu Bo | d280e5b | 2012-08-21 21:13:25 -0600 | [diff] [blame] | 1888 | if (IS_ERR(trans)) { |
| 1889 | ret = PTR_ERR(trans); |
| 1890 | trans = NULL; |
| 1891 | goto out; |
| 1892 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1893 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 1894 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1895 | if (ret) /* -ENOMEM or corruption */ |
| 1896 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1897 | } |
| 1898 | goto out; |
| 1899 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1900 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1901 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 1902 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1903 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1904 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1905 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1906 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1907 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1908 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1909 | if (IS_ERR(trans)) { |
| 1910 | ret = PTR_ERR(trans); |
| 1911 | trans = NULL; |
| 1912 | goto out_unlock; |
| 1913 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1914 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1915 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1916 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1917 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1918 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1919 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1920 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1921 | ordered_extent->file_offset, |
| 1922 | ordered_extent->file_offset + |
| 1923 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1924 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 1925 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1926 | ret = insert_reserved_file_extent(trans, inode, |
| 1927 | ordered_extent->file_offset, |
| 1928 | ordered_extent->start, |
| 1929 | ordered_extent->disk_len, |
| 1930 | ordered_extent->len, |
| 1931 | ordered_extent->len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1932 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1933 | BTRFS_FILE_EXTENT_REG); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1934 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1935 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1936 | ordered_extent->file_offset, ordered_extent->len, |
| 1937 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1938 | if (ret < 0) { |
| 1939 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1940 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1941 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1942 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1943 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1944 | &ordered_extent->list); |
| 1945 | |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 1946 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
Miao Xie | a39f752 | 2011-09-11 10:52:25 -0400 | [diff] [blame] | 1947 | if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 1948 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1949 | if (ret) { /* -ENOMEM or corruption */ |
| 1950 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1951 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1952 | } |
Josef Bacik | 7c73531 | 2012-08-13 15:43:26 -0400 | [diff] [blame] | 1953 | } else { |
| 1954 | btrfs_set_inode_last_trans(trans, inode); |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 1955 | } |
| 1956 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1957 | out_unlock: |
| 1958 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 1959 | ordered_extent->file_offset + |
| 1960 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1961 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 1962 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1963 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 1964 | if (trans) { |
| 1965 | if (nolock) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1966 | btrfs_end_transaction_nolock(trans, root); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 1967 | else |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1968 | btrfs_end_transaction(trans, root); |
| 1969 | } |
| 1970 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1971 | if (ret) |
| 1972 | clear_extent_uptodate(io_tree, ordered_extent->file_offset, |
| 1973 | ordered_extent->file_offset + |
| 1974 | ordered_extent->len - 1, NULL, GFP_NOFS); |
| 1975 | |
| 1976 | /* |
| 1977 | * This needs to be dont to make sure anybody waiting knows we are done |
| 1978 | * upating everything for this ordered extent. |
| 1979 | */ |
| 1980 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 1981 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1982 | /* once for us */ |
| 1983 | btrfs_put_ordered_extent(ordered_extent); |
| 1984 | /* once for the tree */ |
| 1985 | btrfs_put_ordered_extent(ordered_extent); |
| 1986 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1987 | return ret; |
| 1988 | } |
| 1989 | |
| 1990 | static void finish_ordered_fn(struct btrfs_work *work) |
| 1991 | { |
| 1992 | struct btrfs_ordered_extent *ordered_extent; |
| 1993 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 1994 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1995 | } |
| 1996 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1997 | 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] | 1998 | struct extent_state *state, int uptodate) |
| 1999 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2000 | struct inode *inode = page->mapping->host; |
| 2001 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2002 | struct btrfs_ordered_extent *ordered_extent = NULL; |
| 2003 | struct btrfs_workers *workers; |
| 2004 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2005 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2006 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2007 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2008 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2009 | end - start + 1, uptodate)) |
| 2010 | return 0; |
| 2011 | |
| 2012 | ordered_extent->work.func = finish_ordered_fn; |
| 2013 | ordered_extent->work.flags = 0; |
| 2014 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2015 | if (btrfs_is_free_space_inode(inode)) |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2016 | workers = &root->fs_info->endio_freespace_worker; |
| 2017 | else |
| 2018 | workers = &root->fs_info->endio_write_workers; |
| 2019 | btrfs_queue_worker(workers, &ordered_extent->work); |
| 2020 | |
| 2021 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2022 | } |
| 2023 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2024 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2025 | * 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] | 2026 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2027 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2028 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2029 | 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] | 2030 | struct extent_state *state, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2031 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 2032 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2033 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2034 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2035 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 2036 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2037 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2038 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2039 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2040 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2041 | if (PageChecked(page)) { |
| 2042 | ClearPageChecked(page); |
| 2043 | goto good; |
| 2044 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2045 | |
| 2046 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 2047 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2048 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2049 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2050 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2051 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2052 | GFP_NOFS); |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 2056 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2057 | private = state->private; |
| 2058 | ret = 0; |
| 2059 | } else { |
| 2060 | ret = get_state_private(io_tree, start, &private); |
| 2061 | } |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2062 | kaddr = kmap_atomic(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2063 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2064 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2065 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2066 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 2067 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2068 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2069 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2070 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2071 | kunmap_atomic(kaddr); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2072 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2073 | return 0; |
| 2074 | |
| 2075 | zeroit: |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 2076 | 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] | 2077 | "private %llu\n", |
| 2078 | (unsigned long long)btrfs_ino(page->mapping->host), |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 2079 | (unsigned long long)start, csum, |
| 2080 | (unsigned long long)private); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2081 | memset(kaddr + offset, 1, end - start + 1); |
| 2082 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2083 | kunmap_atomic(kaddr); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2084 | if (private == 0) |
| 2085 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2086 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2087 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2088 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2089 | struct delayed_iput { |
| 2090 | struct list_head list; |
| 2091 | struct inode *inode; |
| 2092 | }; |
| 2093 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2094 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2095 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2096 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2097 | { |
| 2098 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2099 | struct delayed_iput *delayed; |
| 2100 | |
| 2101 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2102 | return; |
| 2103 | |
| 2104 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2105 | delayed->inode = inode; |
| 2106 | |
| 2107 | spin_lock(&fs_info->delayed_iput_lock); |
| 2108 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2109 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2110 | } |
| 2111 | |
| 2112 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2113 | { |
| 2114 | LIST_HEAD(list); |
| 2115 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2116 | struct delayed_iput *delayed; |
| 2117 | int empty; |
| 2118 | |
| 2119 | spin_lock(&fs_info->delayed_iput_lock); |
| 2120 | empty = list_empty(&fs_info->delayed_iputs); |
| 2121 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2122 | if (empty) |
| 2123 | return; |
| 2124 | |
| 2125 | down_read(&root->fs_info->cleanup_work_sem); |
| 2126 | spin_lock(&fs_info->delayed_iput_lock); |
| 2127 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2128 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2129 | |
| 2130 | while (!list_empty(&list)) { |
| 2131 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2132 | list_del(&delayed->list); |
| 2133 | iput(delayed->inode); |
| 2134 | kfree(delayed); |
| 2135 | } |
| 2136 | up_read(&root->fs_info->cleanup_work_sem); |
| 2137 | } |
| 2138 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2139 | enum btrfs_orphan_cleanup_state { |
| 2140 | ORPHAN_CLEANUP_STARTED = 1, |
| 2141 | ORPHAN_CLEANUP_DONE = 2, |
| 2142 | }; |
| 2143 | |
| 2144 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 2145 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2146 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2147 | * structure. |
| 2148 | */ |
| 2149 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2150 | struct btrfs_root *root) |
| 2151 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2152 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2153 | int ret; |
| 2154 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2155 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2156 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2157 | return; |
| 2158 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2159 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2160 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2161 | spin_unlock(&root->orphan_lock); |
| 2162 | return; |
| 2163 | } |
| 2164 | |
| 2165 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 2166 | spin_unlock(&root->orphan_lock); |
| 2167 | return; |
| 2168 | } |
| 2169 | |
| 2170 | block_rsv = root->orphan_block_rsv; |
| 2171 | root->orphan_block_rsv = NULL; |
| 2172 | spin_unlock(&root->orphan_lock); |
| 2173 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2174 | if (root->orphan_item_inserted && |
| 2175 | btrfs_root_refs(&root->root_item) > 0) { |
| 2176 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2177 | root->root_key.objectid); |
| 2178 | BUG_ON(ret); |
| 2179 | root->orphan_item_inserted = 0; |
| 2180 | } |
| 2181 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2182 | if (block_rsv) { |
| 2183 | WARN_ON(block_rsv->size > 0); |
| 2184 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2185 | } |
| 2186 | } |
| 2187 | |
| 2188 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2189 | * This creates an orphan entry for the given inode in case something goes |
| 2190 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2191 | * |
| 2192 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2193 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2194 | */ |
| 2195 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2196 | { |
| 2197 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2198 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2199 | int reserve = 0; |
| 2200 | int insert = 0; |
| 2201 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2202 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2203 | if (!root->orphan_block_rsv) { |
| 2204 | block_rsv = btrfs_alloc_block_rsv(root); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2205 | if (!block_rsv) |
| 2206 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2207 | } |
| 2208 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2209 | spin_lock(&root->orphan_lock); |
| 2210 | if (!root->orphan_block_rsv) { |
| 2211 | root->orphan_block_rsv = block_rsv; |
| 2212 | } else if (block_rsv) { |
| 2213 | btrfs_free_block_rsv(root, block_rsv); |
| 2214 | block_rsv = NULL; |
| 2215 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2216 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2217 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2218 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2219 | #if 0 |
| 2220 | /* |
| 2221 | * For proper ENOSPC handling, we should do orphan |
| 2222 | * cleanup when mounting. But this introduces backward |
| 2223 | * compatibility issue. |
| 2224 | */ |
| 2225 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2226 | insert = 2; |
| 2227 | else |
| 2228 | insert = 1; |
| 2229 | #endif |
| 2230 | insert = 1; |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2231 | atomic_dec(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2232 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2233 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 2234 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 2235 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2236 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2237 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2238 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2239 | /* grab metadata reservation from transaction handle */ |
| 2240 | if (reserve) { |
| 2241 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2242 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2246 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2247 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2248 | if (ret && ret != -EEXIST) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2249 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2250 | &BTRFS_I(inode)->runtime_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2251 | btrfs_abort_transaction(trans, root, ret); |
| 2252 | return ret; |
| 2253 | } |
| 2254 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2258 | if (insert >= 2) { |
| 2259 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2260 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2261 | if (ret && ret != -EEXIST) { |
| 2262 | btrfs_abort_transaction(trans, root, ret); |
| 2263 | return ret; |
| 2264 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2265 | } |
| 2266 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | /* |
| 2270 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2271 | * item for this particular inode. |
| 2272 | */ |
| 2273 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2274 | { |
| 2275 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2276 | int delete_item = 0; |
| 2277 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2278 | int ret = 0; |
| 2279 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2280 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2281 | if (test_and_clear_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 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2284 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 2285 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 2286 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2287 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2288 | spin_unlock(&root->orphan_lock); |
| 2289 | |
| 2290 | if (trans && delete_item) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2291 | ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2292 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2293 | } |
| 2294 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2295 | if (release_rsv) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2296 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2297 | atomic_dec(&root->orphan_inodes); |
| 2298 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2299 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2300 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | /* |
| 2304 | * this cleans up any orphans that may be left on the list from the last use |
| 2305 | * of this root. |
| 2306 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2307 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2308 | { |
| 2309 | struct btrfs_path *path; |
| 2310 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2311 | struct btrfs_key key, found_key; |
| 2312 | struct btrfs_trans_handle *trans; |
| 2313 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 2314 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2315 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2316 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2317 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2318 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2319 | |
| 2320 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2321 | if (!path) { |
| 2322 | ret = -ENOMEM; |
| 2323 | goto out; |
| 2324 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2325 | path->reada = -1; |
| 2326 | |
| 2327 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2328 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2329 | key.offset = (u64)-1; |
| 2330 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2331 | while (1) { |
| 2332 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2333 | if (ret < 0) |
| 2334 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2335 | |
| 2336 | /* |
| 2337 | * 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] | 2338 | * 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] | 2339 | * find the key and see if we have stuff that matches |
| 2340 | */ |
| 2341 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2342 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2343 | if (path->slots[0] == 0) |
| 2344 | break; |
| 2345 | path->slots[0]--; |
| 2346 | } |
| 2347 | |
| 2348 | /* pull out the item */ |
| 2349 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2350 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2351 | |
| 2352 | /* make sure the item matches what we want */ |
| 2353 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2354 | break; |
| 2355 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2356 | break; |
| 2357 | |
| 2358 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2359 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2360 | |
| 2361 | /* |
| 2362 | * this is where we are basically btrfs_lookup, without the |
| 2363 | * crossing root thing. we store the inode number in the |
| 2364 | * offset of the orphan item. |
| 2365 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 2366 | |
| 2367 | if (found_key.offset == last_objectid) { |
| 2368 | printk(KERN_ERR "btrfs: Error removing orphan entry, " |
| 2369 | "stopping orphan cleanup\n"); |
| 2370 | ret = -EINVAL; |
| 2371 | goto out; |
| 2372 | } |
| 2373 | |
| 2374 | last_objectid = found_key.offset; |
| 2375 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2376 | found_key.objectid = found_key.offset; |
| 2377 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2378 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2379 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2380 | ret = PTR_RET(inode); |
| 2381 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2382 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2383 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 2384 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 2385 | struct btrfs_root *dead_root; |
| 2386 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2387 | int is_dead_root = 0; |
| 2388 | |
| 2389 | /* |
| 2390 | * this is an orphan in the tree root. Currently these |
| 2391 | * could come from 2 sources: |
| 2392 | * a) a snapshot deletion in progress |
| 2393 | * b) a free space cache inode |
| 2394 | * We need to distinguish those two, as the snapshot |
| 2395 | * orphan must not get deleted. |
| 2396 | * find_dead_roots already ran before us, so if this |
| 2397 | * is a snapshot deletion, we should find the root |
| 2398 | * in the dead_roots list |
| 2399 | */ |
| 2400 | spin_lock(&fs_info->trans_lock); |
| 2401 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 2402 | root_list) { |
| 2403 | if (dead_root->root_key.objectid == |
| 2404 | found_key.objectid) { |
| 2405 | is_dead_root = 1; |
| 2406 | break; |
| 2407 | } |
| 2408 | } |
| 2409 | spin_unlock(&fs_info->trans_lock); |
| 2410 | if (is_dead_root) { |
| 2411 | /* prevent this orphan from being found again */ |
| 2412 | key.offset = found_key.objectid - 1; |
| 2413 | continue; |
| 2414 | } |
| 2415 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2416 | /* |
| 2417 | * Inode is already gone but the orphan item is still there, |
| 2418 | * kill the orphan item. |
| 2419 | */ |
| 2420 | if (ret == -ESTALE) { |
| 2421 | trans = btrfs_start_transaction(root, 1); |
| 2422 | if (IS_ERR(trans)) { |
| 2423 | ret = PTR_ERR(trans); |
| 2424 | goto out; |
| 2425 | } |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2426 | printk(KERN_ERR "auto deleting %Lu\n", |
| 2427 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2428 | ret = btrfs_del_orphan_item(trans, root, |
| 2429 | found_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2430 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2431 | btrfs_end_transaction(trans, root); |
| 2432 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2433 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2434 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2435 | /* |
| 2436 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2437 | * the proper thing when we hit it |
| 2438 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2439 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2440 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2441 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2442 | /* if we have links, this was a truncate, lets do that */ |
| 2443 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 2444 | if (!S_ISREG(inode->i_mode)) { |
| 2445 | WARN_ON(1); |
| 2446 | iput(inode); |
| 2447 | continue; |
| 2448 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2449 | nr_truncate++; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2450 | ret = btrfs_truncate(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2451 | } else { |
| 2452 | nr_unlink++; |
| 2453 | } |
| 2454 | |
| 2455 | /* this will do delete_inode and everything for us */ |
| 2456 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2457 | if (ret) |
| 2458 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2459 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 2460 | /* release the path since we're done with it */ |
| 2461 | btrfs_release_path(path); |
| 2462 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2463 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 2464 | |
| 2465 | if (root->orphan_block_rsv) |
| 2466 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 2467 | (u64)-1); |
| 2468 | |
| 2469 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2470 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2471 | if (!IS_ERR(trans)) |
| 2472 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2473 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2474 | |
| 2475 | if (nr_unlink) |
| 2476 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2477 | if (nr_truncate) |
| 2478 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2479 | |
| 2480 | out: |
| 2481 | if (ret) |
| 2482 | printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret); |
| 2483 | btrfs_free_path(path); |
| 2484 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2485 | } |
| 2486 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2487 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2488 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2489 | * don't find any xattrs, we know there can't be any acls. |
| 2490 | * |
| 2491 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2492 | */ |
| 2493 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2494 | int slot, u64 objectid) |
| 2495 | { |
| 2496 | u32 nritems = btrfs_header_nritems(leaf); |
| 2497 | struct btrfs_key found_key; |
| 2498 | int scanned = 0; |
| 2499 | |
| 2500 | slot++; |
| 2501 | while (slot < nritems) { |
| 2502 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2503 | |
| 2504 | /* we found a different objectid, there must not be acls */ |
| 2505 | if (found_key.objectid != objectid) |
| 2506 | return 0; |
| 2507 | |
| 2508 | /* we found an xattr, assume we've got an acl */ |
| 2509 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2510 | return 1; |
| 2511 | |
| 2512 | /* |
| 2513 | * we found a key greater than an xattr key, there can't |
| 2514 | * be any acls later on |
| 2515 | */ |
| 2516 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2517 | return 0; |
| 2518 | |
| 2519 | slot++; |
| 2520 | scanned++; |
| 2521 | |
| 2522 | /* |
| 2523 | * it goes inode, inode backrefs, xattrs, extents, |
| 2524 | * so if there are a ton of hard links to an inode there can |
| 2525 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2526 | * this is just an optimization |
| 2527 | */ |
| 2528 | if (scanned >= 8) |
| 2529 | break; |
| 2530 | } |
| 2531 | /* we hit the end of the leaf before we found an xattr or |
| 2532 | * something larger than an xattr. We have to assume the inode |
| 2533 | * has acls |
| 2534 | */ |
| 2535 | return 1; |
| 2536 | } |
| 2537 | |
| 2538 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2539 | * read an inode from the btree into the in-memory inode |
| 2540 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2541 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2542 | { |
| 2543 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2544 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2545 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2546 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2547 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2548 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2549 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2550 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2551 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2552 | bool filled = false; |
| 2553 | |
| 2554 | ret = btrfs_fill_inode(inode, &rdev); |
| 2555 | if (!ret) |
| 2556 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2557 | |
| 2558 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 2559 | if (!path) |
| 2560 | goto make_bad; |
| 2561 | |
Josef Bacik | d90c732 | 2011-05-17 09:50:54 -0400 | [diff] [blame] | 2562 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2563 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2564 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2565 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2566 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2567 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2568 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2569 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2570 | |
| 2571 | if (filled) |
| 2572 | goto cache_acl; |
| 2573 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2574 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2575 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2576 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2577 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2578 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2579 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2580 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2581 | |
| 2582 | tspec = btrfs_inode_atime(inode_item); |
| 2583 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2584 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2585 | |
| 2586 | tspec = btrfs_inode_mtime(inode_item); |
| 2587 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2588 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2589 | |
| 2590 | tspec = btrfs_inode_ctime(inode_item); |
| 2591 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2592 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2593 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2594 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2595 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2596 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 2597 | |
| 2598 | /* |
| 2599 | * If we were modified in the current generation and evicted from memory |
| 2600 | * and then re-read we need to do a full sync since we don't have any |
| 2601 | * idea about which extents were modified before we were evicted from |
| 2602 | * cache. |
| 2603 | */ |
| 2604 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 2605 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 2606 | &BTRFS_I(inode)->runtime_flags); |
| 2607 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 2608 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2609 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2610 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2611 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2612 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2613 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2614 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2615 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2616 | /* |
| 2617 | * try to precache a NULL acl entry for files that don't have |
| 2618 | * any xattrs or acls |
| 2619 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2620 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
| 2621 | btrfs_ino(inode)); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 2622 | if (!maybe_acls) |
| 2623 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2624 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2625 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2626 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2627 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2628 | case S_IFREG: |
| 2629 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2630 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2631 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2632 | inode->i_fop = &btrfs_file_operations; |
| 2633 | inode->i_op = &btrfs_file_inode_operations; |
| 2634 | break; |
| 2635 | case S_IFDIR: |
| 2636 | inode->i_fop = &btrfs_dir_file_operations; |
| 2637 | if (root == root->fs_info->tree_root) |
| 2638 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2639 | else |
| 2640 | inode->i_op = &btrfs_dir_inode_operations; |
| 2641 | break; |
| 2642 | case S_IFLNK: |
| 2643 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2644 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2645 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2646 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2647 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2648 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2649 | init_special_inode(inode, inode->i_mode, rdev); |
| 2650 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2651 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2652 | |
| 2653 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2654 | return; |
| 2655 | |
| 2656 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2657 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2658 | make_bad_inode(inode); |
| 2659 | } |
| 2660 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2661 | /* |
| 2662 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2663 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2664 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2665 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2666 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2667 | struct inode *inode) |
| 2668 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2669 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2670 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2671 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2672 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2673 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2674 | |
| 2675 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2676 | inode->i_atime.tv_sec); |
| 2677 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2678 | inode->i_atime.tv_nsec); |
| 2679 | |
| 2680 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2681 | inode->i_mtime.tv_sec); |
| 2682 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2683 | inode->i_mtime.tv_nsec); |
| 2684 | |
| 2685 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2686 | inode->i_ctime.tv_sec); |
| 2687 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2688 | inode->i_ctime.tv_nsec); |
| 2689 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2690 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2691 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 2692 | btrfs_set_inode_sequence(leaf, item, inode->i_version); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2693 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2694 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2695 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 2696 | btrfs_set_inode_block_group(leaf, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2697 | } |
| 2698 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2699 | /* |
| 2700 | * copy everything in the in-memory inode into the btree. |
| 2701 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2702 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2703 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2704 | { |
| 2705 | struct btrfs_inode_item *inode_item; |
| 2706 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2707 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2708 | int ret; |
| 2709 | |
| 2710 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2711 | if (!path) |
| 2712 | return -ENOMEM; |
| 2713 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2714 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2715 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 2716 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2717 | if (ret) { |
| 2718 | if (ret > 0) |
| 2719 | ret = -ENOENT; |
| 2720 | goto failed; |
| 2721 | } |
| 2722 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2723 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2724 | leaf = path->nodes[0]; |
| 2725 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2726 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2727 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2728 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2729 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2730 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2731 | ret = 0; |
| 2732 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2733 | btrfs_free_path(path); |
| 2734 | return ret; |
| 2735 | } |
| 2736 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2737 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2738 | * copy everything in the in-memory inode into the btree. |
| 2739 | */ |
| 2740 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2741 | struct btrfs_root *root, struct inode *inode) |
| 2742 | { |
| 2743 | int ret; |
| 2744 | |
| 2745 | /* |
| 2746 | * If the inode is a free space inode, we can deadlock during commit |
| 2747 | * if we put it into the delayed code. |
| 2748 | * |
| 2749 | * The data relocation inode should also be directly updated |
| 2750 | * without delay |
| 2751 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2752 | if (!btrfs_is_free_space_inode(inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2753 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 2754 | btrfs_update_root_times(trans, root); |
| 2755 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2756 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 2757 | if (!ret) |
| 2758 | btrfs_set_inode_last_trans(trans, inode); |
| 2759 | return ret; |
| 2760 | } |
| 2761 | |
| 2762 | return btrfs_update_inode_item(trans, root, inode); |
| 2763 | } |
| 2764 | |
| 2765 | static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 2766 | struct btrfs_root *root, struct inode *inode) |
| 2767 | { |
| 2768 | int ret; |
| 2769 | |
| 2770 | ret = btrfs_update_inode(trans, root, inode); |
| 2771 | if (ret == -ENOSPC) |
| 2772 | return btrfs_update_inode_item(trans, root, inode); |
| 2773 | return ret; |
| 2774 | } |
| 2775 | |
| 2776 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2777 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2778 | * recovery code. It remove a link in a directory with a given name, and |
| 2779 | * also drops the back refs in the inode to the directory |
| 2780 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2781 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2782 | struct btrfs_root *root, |
| 2783 | struct inode *dir, struct inode *inode, |
| 2784 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2785 | { |
| 2786 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2787 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2788 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2789 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2790 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2791 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2792 | u64 ino = btrfs_ino(inode); |
| 2793 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2794 | |
| 2795 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2796 | if (!path) { |
| 2797 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 2798 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2799 | } |
| 2800 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2801 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2802 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2803 | name, name_len, -1); |
| 2804 | if (IS_ERR(di)) { |
| 2805 | ret = PTR_ERR(di); |
| 2806 | goto err; |
| 2807 | } |
| 2808 | if (!di) { |
| 2809 | ret = -ENOENT; |
| 2810 | goto err; |
| 2811 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2812 | leaf = path->nodes[0]; |
| 2813 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2814 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2815 | if (ret) |
| 2816 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2817 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2818 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2819 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 2820 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2821 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2822 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2823 | "inode %llu parent %llu\n", name_len, name, |
| 2824 | (unsigned long long)ino, (unsigned long long)dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2825 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2826 | goto err; |
| 2827 | } |
| 2828 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2829 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2830 | if (ret) { |
| 2831 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2832 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2833 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2834 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2835 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2836 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2837 | if (ret != 0 && ret != -ENOENT) { |
| 2838 | btrfs_abort_transaction(trans, root, ret); |
| 2839 | goto err; |
| 2840 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2841 | |
| 2842 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2843 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 2844 | if (ret == -ENOENT) |
| 2845 | ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2846 | err: |
| 2847 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2848 | if (ret) |
| 2849 | goto out; |
| 2850 | |
| 2851 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 2852 | inode_inc_iversion(inode); |
| 2853 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2854 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 2855 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2856 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2857 | return ret; |
| 2858 | } |
| 2859 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2860 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2861 | struct btrfs_root *root, |
| 2862 | struct inode *dir, struct inode *inode, |
| 2863 | const char *name, int name_len) |
| 2864 | { |
| 2865 | int ret; |
| 2866 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 2867 | if (!ret) { |
| 2868 | btrfs_drop_nlink(inode); |
| 2869 | ret = btrfs_update_inode(trans, root, inode); |
| 2870 | } |
| 2871 | return ret; |
| 2872 | } |
| 2873 | |
| 2874 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2875 | /* helper to check if there is any shared block in the path */ |
| 2876 | static int check_path_shared(struct btrfs_root *root, |
| 2877 | struct btrfs_path *path) |
| 2878 | { |
| 2879 | struct extent_buffer *eb; |
| 2880 | int level; |
Dan Carpenter | 0e4dcbef | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 2881 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2882 | |
| 2883 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2884 | int ret; |
| 2885 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2886 | if (!path->nodes[level]) |
| 2887 | break; |
| 2888 | eb = path->nodes[level]; |
| 2889 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2890 | continue; |
| 2891 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2892 | &refs, NULL); |
| 2893 | if (refs > 1) |
| 2894 | return 1; |
| 2895 | } |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2896 | return 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | /* |
| 2900 | * helper to start transaction for unlink and rmdir. |
| 2901 | * |
| 2902 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2903 | * so in enospc case, we should make sure they will free space before |
| 2904 | * allowing them to use the global metadata reservation. |
| 2905 | */ |
| 2906 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2907 | struct dentry *dentry) |
| 2908 | { |
| 2909 | struct btrfs_trans_handle *trans; |
| 2910 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2911 | struct btrfs_path *path; |
| 2912 | struct btrfs_inode_ref *ref; |
| 2913 | struct btrfs_dir_item *di; |
| 2914 | struct inode *inode = dentry->d_inode; |
| 2915 | u64 index; |
| 2916 | int check_link = 1; |
| 2917 | int err = -ENOSPC; |
| 2918 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2919 | u64 ino = btrfs_ino(inode); |
| 2920 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2921 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 2922 | /* |
| 2923 | * 1 for the possible orphan item |
| 2924 | * 1 for the dir item |
| 2925 | * 1 for the dir index |
| 2926 | * 1 for the inode ref |
| 2927 | * 1 for the inode ref in the tree log |
| 2928 | * 2 for the dir entries in the log |
| 2929 | * 1 for the inode |
| 2930 | */ |
| 2931 | trans = btrfs_start_transaction(root, 8); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2932 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 2933 | return trans; |
| 2934 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2935 | if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2936 | return ERR_PTR(-ENOSPC); |
| 2937 | |
| 2938 | /* check if there is someone else holds reference */ |
| 2939 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 2940 | return ERR_PTR(-ENOSPC); |
| 2941 | |
| 2942 | if (atomic_read(&inode->i_count) > 2) |
| 2943 | return ERR_PTR(-ENOSPC); |
| 2944 | |
| 2945 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 2946 | return ERR_PTR(-ENOSPC); |
| 2947 | |
| 2948 | path = btrfs_alloc_path(); |
| 2949 | if (!path) { |
| 2950 | root->fs_info->enospc_unlink = 0; |
| 2951 | return ERR_PTR(-ENOMEM); |
| 2952 | } |
| 2953 | |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 2954 | /* 1 for the orphan item */ |
| 2955 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2956 | if (IS_ERR(trans)) { |
| 2957 | btrfs_free_path(path); |
| 2958 | root->fs_info->enospc_unlink = 0; |
| 2959 | return trans; |
| 2960 | } |
| 2961 | |
| 2962 | path->skip_locking = 1; |
| 2963 | path->search_commit_root = 1; |
| 2964 | |
| 2965 | ret = btrfs_lookup_inode(trans, root, path, |
| 2966 | &BTRFS_I(dir)->location, 0); |
| 2967 | if (ret < 0) { |
| 2968 | err = ret; |
| 2969 | goto out; |
| 2970 | } |
| 2971 | if (ret == 0) { |
| 2972 | if (check_path_shared(root, path)) |
| 2973 | goto out; |
| 2974 | } else { |
| 2975 | check_link = 0; |
| 2976 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2977 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2978 | |
| 2979 | ret = btrfs_lookup_inode(trans, root, path, |
| 2980 | &BTRFS_I(inode)->location, 0); |
| 2981 | if (ret < 0) { |
| 2982 | err = ret; |
| 2983 | goto out; |
| 2984 | } |
| 2985 | if (ret == 0) { |
| 2986 | if (check_path_shared(root, path)) |
| 2987 | goto out; |
| 2988 | } else { |
| 2989 | check_link = 0; |
| 2990 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2991 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2992 | |
| 2993 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 2994 | ret = btrfs_lookup_file_extent(trans, root, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2995 | ino, (u64)-1, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2996 | if (ret < 0) { |
| 2997 | err = ret; |
| 2998 | goto out; |
| 2999 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3000 | BUG_ON(ret == 0); /* Corruption */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3001 | if (check_path_shared(root, path)) |
| 3002 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3003 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | if (!check_link) { |
| 3007 | err = 0; |
| 3008 | goto out; |
| 3009 | } |
| 3010 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3011 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3012 | dentry->d_name.name, dentry->d_name.len, 0); |
| 3013 | if (IS_ERR(di)) { |
| 3014 | err = PTR_ERR(di); |
| 3015 | goto out; |
| 3016 | } |
| 3017 | if (di) { |
| 3018 | if (check_path_shared(root, path)) |
| 3019 | goto out; |
| 3020 | } else { |
| 3021 | err = 0; |
| 3022 | goto out; |
| 3023 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3024 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3025 | |
| 3026 | ref = btrfs_lookup_inode_ref(trans, root, path, |
| 3027 | dentry->d_name.name, dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3028 | ino, dir_ino, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3029 | if (IS_ERR(ref)) { |
| 3030 | err = PTR_ERR(ref); |
| 3031 | goto out; |
| 3032 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3033 | BUG_ON(!ref); /* Logic error */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3034 | if (check_path_shared(root, path)) |
| 3035 | goto out; |
| 3036 | index = btrfs_inode_ref_index(path->nodes[0], ref); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3037 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3038 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3039 | /* |
| 3040 | * This is a commit root search, if we can lookup inode item and other |
| 3041 | * relative items in the commit root, it means the transaction of |
| 3042 | * dir/file creation has been committed, and the dir index item that we |
| 3043 | * delay to insert has also been inserted into the commit root. So |
| 3044 | * we needn't worry about the delayed insertion of the dir index item |
| 3045 | * here. |
| 3046 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3047 | di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3048 | dentry->d_name.name, dentry->d_name.len, 0); |
| 3049 | if (IS_ERR(di)) { |
| 3050 | err = PTR_ERR(di); |
| 3051 | goto out; |
| 3052 | } |
| 3053 | BUG_ON(ret == -ENOENT); |
| 3054 | if (check_path_shared(root, path)) |
| 3055 | goto out; |
| 3056 | |
| 3057 | err = 0; |
| 3058 | out: |
| 3059 | btrfs_free_path(path); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 3060 | /* Migrate the orphan reservation over */ |
| 3061 | if (!err) |
| 3062 | err = btrfs_block_rsv_migrate(trans->block_rsv, |
| 3063 | &root->fs_info->global_block_rsv, |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3064 | trans->bytes_reserved); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 3065 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3066 | if (err) { |
| 3067 | btrfs_end_transaction(trans, root); |
| 3068 | root->fs_info->enospc_unlink = 0; |
| 3069 | return ERR_PTR(err); |
| 3070 | } |
| 3071 | |
| 3072 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 3073 | return trans; |
| 3074 | } |
| 3075 | |
| 3076 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 3077 | struct btrfs_root *root) |
| 3078 | { |
| 3079 | if (trans->block_rsv == &root->fs_info->global_block_rsv) { |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3080 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 3081 | trans->bytes_reserved); |
| 3082 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3083 | BUG_ON(!root->fs_info->enospc_unlink); |
| 3084 | root->fs_info->enospc_unlink = 0; |
| 3085 | } |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 3086 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3087 | } |
| 3088 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3089 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3090 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3091 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3092 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3093 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3094 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3095 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3096 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3097 | trans = __unlink_start_trans(dir, dentry); |
| 3098 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3099 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3100 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3101 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3102 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3103 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3104 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3105 | if (ret) |
| 3106 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3107 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3108 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3109 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3110 | if (ret) |
| 3111 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3112 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3113 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3114 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3115 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3116 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3117 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3118 | return ret; |
| 3119 | } |
| 3120 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3121 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3122 | struct btrfs_root *root, |
| 3123 | struct inode *dir, u64 objectid, |
| 3124 | const char *name, int name_len) |
| 3125 | { |
| 3126 | struct btrfs_path *path; |
| 3127 | struct extent_buffer *leaf; |
| 3128 | struct btrfs_dir_item *di; |
| 3129 | struct btrfs_key key; |
| 3130 | u64 index; |
| 3131 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3132 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3133 | |
| 3134 | path = btrfs_alloc_path(); |
| 3135 | if (!path) |
| 3136 | return -ENOMEM; |
| 3137 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3138 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3139 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3140 | if (IS_ERR_OR_NULL(di)) { |
| 3141 | if (!di) |
| 3142 | ret = -ENOENT; |
| 3143 | else |
| 3144 | ret = PTR_ERR(di); |
| 3145 | goto out; |
| 3146 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3147 | |
| 3148 | leaf = path->nodes[0]; |
| 3149 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3150 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3151 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3152 | if (ret) { |
| 3153 | btrfs_abort_transaction(trans, root, ret); |
| 3154 | goto out; |
| 3155 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3156 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3157 | |
| 3158 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3159 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3160 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3161 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3162 | if (ret != -ENOENT) { |
| 3163 | btrfs_abort_transaction(trans, root, ret); |
| 3164 | goto out; |
| 3165 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3166 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3167 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3168 | if (IS_ERR_OR_NULL(di)) { |
| 3169 | if (!di) |
| 3170 | ret = -ENOENT; |
| 3171 | else |
| 3172 | ret = PTR_ERR(di); |
| 3173 | btrfs_abort_transaction(trans, root, ret); |
| 3174 | goto out; |
| 3175 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3176 | |
| 3177 | leaf = path->nodes[0]; |
| 3178 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3179 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3180 | index = key.offset; |
| 3181 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3182 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3183 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3184 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3185 | if (ret) { |
| 3186 | btrfs_abort_transaction(trans, root, ret); |
| 3187 | goto out; |
| 3188 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3189 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3190 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3191 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3192 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3193 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3194 | if (ret) |
| 3195 | btrfs_abort_transaction(trans, root, ret); |
| 3196 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3197 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3198 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3199 | } |
| 3200 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3201 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3202 | { |
| 3203 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3204 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3205 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3206 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3207 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3208 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 3209 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3210 | btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3211 | return -ENOTEMPTY; |
| 3212 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3213 | trans = __unlink_start_trans(dir, dentry); |
| 3214 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3215 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3216 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3217 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3218 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3219 | BTRFS_I(inode)->location.objectid, |
| 3220 | dentry->d_name.name, |
| 3221 | dentry->d_name.len); |
| 3222 | goto out; |
| 3223 | } |
| 3224 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3225 | err = btrfs_orphan_add(trans, inode); |
| 3226 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3227 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3228 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3229 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3230 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3231 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3232 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3233 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3234 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3235 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3236 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3237 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3238 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3239 | return err; |
| 3240 | } |
| 3241 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3242 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3243 | * this can truncate away extent items, csum items and directory items. |
| 3244 | * 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] | 3245 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3246 | * |
| 3247 | * csum items that cross the new i_size are truncated to the new size |
| 3248 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3249 | * |
| 3250 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3251 | * 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] | 3252 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3253 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3254 | struct btrfs_root *root, |
| 3255 | struct inode *inode, |
| 3256 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3257 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3258 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3259 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3260 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3261 | struct btrfs_key key; |
| 3262 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3263 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3264 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3265 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3266 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3267 | u64 mask = root->sectorsize - 1; |
| 3268 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3269 | int found_extent; |
| 3270 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3271 | int pending_del_nr = 0; |
| 3272 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3273 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3274 | int ret; |
| 3275 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3276 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3277 | |
| 3278 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3279 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 3280 | path = btrfs_alloc_path(); |
| 3281 | if (!path) |
| 3282 | return -ENOMEM; |
| 3283 | path->reada = -1; |
| 3284 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3285 | /* |
| 3286 | * We want to drop from the next block forward in case this new size is |
| 3287 | * not block aligned since we will be keeping the last block of the |
| 3288 | * extent just the way it is. |
| 3289 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3290 | if (root->ref_cows || root == root->fs_info->tree_root) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3291 | btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3292 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3293 | /* |
| 3294 | * This function is also used to drop the items in the log tree before |
| 3295 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 3296 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 3297 | * items. |
| 3298 | */ |
| 3299 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 3300 | btrfs_kill_delayed_inode_items(inode); |
| 3301 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3302 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3303 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3304 | key.type = (u8)-1; |
| 3305 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3306 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3307 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3308 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3309 | if (ret < 0) { |
| 3310 | err = ret; |
| 3311 | goto out; |
| 3312 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3313 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3314 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3315 | /* there are no items in the tree for us to truncate, we're |
| 3316 | * done |
| 3317 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3318 | if (path->slots[0] == 0) |
| 3319 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3320 | path->slots[0]--; |
| 3321 | } |
| 3322 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3323 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3324 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3325 | leaf = path->nodes[0]; |
| 3326 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3327 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3328 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3329 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3330 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3331 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3332 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3333 | break; |
| 3334 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3335 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3336 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3337 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3338 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3339 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 3340 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3341 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3342 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3343 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3344 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3345 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3346 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3347 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3348 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3349 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3350 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3351 | } else { |
| 3352 | if (item_end < new_size) |
| 3353 | break; |
| 3354 | if (found_key.offset >= new_size) |
| 3355 | del_item = 1; |
| 3356 | else |
| 3357 | del_item = 0; |
| 3358 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3359 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3360 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3361 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3362 | goto delete; |
| 3363 | |
| 3364 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3365 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3366 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 3367 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3368 | u64 orig_num_bytes = |
| 3369 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3370 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3371 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3372 | extent_num_bytes = extent_num_bytes & |
| 3373 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3374 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3375 | extent_num_bytes); |
| 3376 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3377 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3378 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3379 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3380 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3381 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3382 | extent_num_bytes = |
| 3383 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3384 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3385 | extent_offset = found_key.offset - |
| 3386 | btrfs_file_extent_offset(leaf, fi); |
| 3387 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3388 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3389 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3390 | if (extent_start != 0) { |
| 3391 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3392 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3393 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3394 | } |
| 3395 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3396 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3397 | /* |
| 3398 | * we can't truncate inline items that have had |
| 3399 | * special encodings |
| 3400 | */ |
| 3401 | if (!del_item && |
| 3402 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3403 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3404 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3405 | u32 size = new_size - found_key.offset; |
| 3406 | |
| 3407 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3408 | inode_sub_bytes(inode, item_end + 1 - |
| 3409 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3410 | } |
| 3411 | size = |
| 3412 | btrfs_file_extent_calc_inline_size(size); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3413 | btrfs_truncate_item(trans, root, path, |
| 3414 | size, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3415 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3416 | inode_sub_bytes(inode, item_end + 1 - |
| 3417 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3418 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3419 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3420 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3421 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3422 | if (!pending_del_nr) { |
| 3423 | /* no pending yet, add ourselves */ |
| 3424 | pending_del_slot = path->slots[0]; |
| 3425 | pending_del_nr = 1; |
| 3426 | } else if (pending_del_nr && |
| 3427 | path->slots[0] + 1 == pending_del_slot) { |
| 3428 | /* hop on the pending chunk */ |
| 3429 | pending_del_nr++; |
| 3430 | pending_del_slot = path->slots[0]; |
| 3431 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3432 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3433 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3434 | } else { |
| 3435 | break; |
| 3436 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3437 | if (found_extent && (root->ref_cows || |
| 3438 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3439 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3440 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3441 | extent_num_bytes, 0, |
| 3442 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3443 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3444 | BUG_ON(ret); |
| 3445 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3446 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3447 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3448 | break; |
| 3449 | |
| 3450 | if (path->slots[0] == 0 || |
| 3451 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3452 | if (pending_del_nr) { |
| 3453 | ret = btrfs_del_items(trans, root, path, |
| 3454 | pending_del_slot, |
| 3455 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3456 | if (ret) { |
| 3457 | btrfs_abort_transaction(trans, |
| 3458 | root, ret); |
| 3459 | goto error; |
| 3460 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3461 | pending_del_nr = 0; |
| 3462 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3463 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3464 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3465 | } else { |
| 3466 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3467 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3468 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3469 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3470 | if (pending_del_nr) { |
| 3471 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3472 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3473 | if (ret) |
| 3474 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3475 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3476 | error: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3477 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3478 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3479 | } |
| 3480 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3481 | /* |
| 3482 | * taken from block_truncate_page, but does cow as it zeros out |
| 3483 | * any bytes left in the last page in the file. |
| 3484 | */ |
| 3485 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3486 | { |
| 3487 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3488 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3489 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3490 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3491 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3492 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3493 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3494 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3495 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3496 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 3497 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3498 | int ret = 0; |
| 3499 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3500 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3501 | |
| 3502 | if ((offset & (blocksize - 1)) == 0) |
| 3503 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3504 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3505 | if (ret) |
| 3506 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3507 | |
| 3508 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3509 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 3510 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3511 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3512 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3513 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3514 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3515 | |
| 3516 | page_start = page_offset(page); |
| 3517 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3518 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3519 | if (!PageUptodate(page)) { |
| 3520 | ret = btrfs_readpage(NULL, page); |
| 3521 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3522 | if (page->mapping != mapping) { |
| 3523 | unlock_page(page); |
| 3524 | page_cache_release(page); |
| 3525 | goto again; |
| 3526 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3527 | if (!PageUptodate(page)) { |
| 3528 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3529 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3530 | } |
| 3531 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3532 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3533 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 3534 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3535 | set_page_extent_mapped(page); |
| 3536 | |
| 3537 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3538 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3539 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3540 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3541 | unlock_page(page); |
| 3542 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3543 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3544 | btrfs_put_ordered_extent(ordered); |
| 3545 | goto again; |
| 3546 | } |
| 3547 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3548 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3549 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3550 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3551 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3552 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3553 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3554 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3555 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3556 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3557 | goto out_unlock; |
| 3558 | } |
| 3559 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3560 | ret = 0; |
| 3561 | if (offset != PAGE_CACHE_SIZE) { |
| 3562 | kaddr = kmap(page); |
| 3563 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3564 | flush_dcache_page(page); |
| 3565 | kunmap(page); |
| 3566 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3567 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3568 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3569 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3570 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3571 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3572 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3573 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3574 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3575 | unlock_page(page); |
| 3576 | page_cache_release(page); |
| 3577 | out: |
| 3578 | return ret; |
| 3579 | } |
| 3580 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 3581 | /* |
| 3582 | * This function puts in dummy file extents for the area we're creating a hole |
| 3583 | * for. So if we are truncating this file to a larger size we need to insert |
| 3584 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 3585 | * the range between oldsize and size |
| 3586 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3587 | 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] | 3588 | { |
| 3589 | struct btrfs_trans_handle *trans; |
| 3590 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3591 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3592 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3593 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3594 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3595 | u64 mask = root->sectorsize - 1; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3596 | u64 hole_start = (oldsize + mask) & ~mask; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3597 | u64 block_end = (size + mask) & ~mask; |
| 3598 | u64 last_byte; |
| 3599 | u64 cur_offset; |
| 3600 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3601 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3602 | |
| 3603 | if (size <= hole_start) |
| 3604 | return 0; |
| 3605 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3606 | while (1) { |
| 3607 | struct btrfs_ordered_extent *ordered; |
| 3608 | btrfs_wait_ordered_range(inode, hole_start, |
| 3609 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3610 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 3611 | &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3612 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3613 | if (!ordered) |
| 3614 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3615 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3616 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3617 | btrfs_put_ordered_extent(ordered); |
| 3618 | } |
| 3619 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3620 | cur_offset = hole_start; |
| 3621 | while (1) { |
| 3622 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3623 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3624 | if (IS_ERR(em)) { |
| 3625 | err = PTR_ERR(em); |
| 3626 | break; |
| 3627 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3628 | last_byte = min(extent_map_end(em), block_end); |
| 3629 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3630 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3631 | struct extent_map *hole_em; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3632 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3633 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3634 | |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3635 | trans = btrfs_start_transaction(root, 3); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3636 | if (IS_ERR(trans)) { |
| 3637 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3638 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3639 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3640 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3641 | err = btrfs_drop_extents(trans, root, inode, |
| 3642 | cur_offset, |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3643 | cur_offset + hole_size, |
| 3644 | &hint_byte, 1); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3645 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3646 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3647 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3648 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3649 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3650 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3651 | err = btrfs_insert_file_extent(trans, root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3652 | btrfs_ino(inode), cur_offset, 0, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3653 | 0, hole_size, 0, hole_size, |
| 3654 | 0, 0, 0); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3655 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3656 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3657 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3658 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3659 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3660 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3661 | btrfs_drop_extent_cache(inode, cur_offset, |
| 3662 | cur_offset + hole_size - 1, 0); |
| 3663 | hole_em = alloc_extent_map(); |
| 3664 | if (!hole_em) { |
| 3665 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3666 | &BTRFS_I(inode)->runtime_flags); |
| 3667 | goto next; |
| 3668 | } |
| 3669 | hole_em->start = cur_offset; |
| 3670 | hole_em->len = hole_size; |
| 3671 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3672 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3673 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 3674 | hole_em->block_len = 0; |
| 3675 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 3676 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
| 3677 | hole_em->generation = trans->transid; |
| 3678 | |
| 3679 | while (1) { |
| 3680 | write_lock(&em_tree->lock); |
| 3681 | err = add_extent_mapping(em_tree, hole_em); |
| 3682 | if (!err) |
| 3683 | list_move(&hole_em->list, |
| 3684 | &em_tree->modified_extents); |
| 3685 | write_unlock(&em_tree->lock); |
| 3686 | if (err != -EEXIST) |
| 3687 | break; |
| 3688 | btrfs_drop_extent_cache(inode, cur_offset, |
| 3689 | cur_offset + |
| 3690 | hole_size - 1, 0); |
| 3691 | } |
| 3692 | free_extent_map(hole_em); |
| 3693 | next: |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3694 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3695 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3696 | } |
| 3697 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3698 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3699 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3700 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3701 | break; |
| 3702 | } |
| 3703 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3704 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3705 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3706 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3707 | return err; |
| 3708 | } |
| 3709 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3710 | static int btrfs_setsize(struct inode *inode, loff_t newsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3711 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 3712 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3713 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3714 | loff_t oldsize = i_size_read(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3715 | int ret; |
| 3716 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3717 | if (newsize == oldsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3718 | return 0; |
| 3719 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3720 | if (newsize > oldsize) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3721 | truncate_pagecache(inode, oldsize, newsize); |
| 3722 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 3723 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3724 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3725 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 3726 | trans = btrfs_start_transaction(root, 1); |
| 3727 | if (IS_ERR(trans)) |
| 3728 | return PTR_ERR(trans); |
| 3729 | |
| 3730 | i_size_write(inode, newsize); |
| 3731 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 3732 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 3733 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3734 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3735 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3736 | /* |
| 3737 | * We're truncating a file that used to have good data down to |
| 3738 | * zero. Make sure it gets into the ordered flush list so that |
| 3739 | * any new writes get down to disk quickly. |
| 3740 | */ |
| 3741 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3742 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 3743 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3744 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3745 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3746 | truncate_setsize(inode, newsize); |
| 3747 | ret = btrfs_truncate(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3750 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3753 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3754 | { |
| 3755 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3756 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3757 | int err; |
| 3758 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3759 | if (btrfs_root_readonly(root)) |
| 3760 | return -EROFS; |
| 3761 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3762 | err = inode_change_ok(inode, attr); |
| 3763 | if (err) |
| 3764 | return err; |
| 3765 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3766 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3767 | err = btrfs_setsize(inode, attr->ia_size); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3768 | if (err) |
| 3769 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3770 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3771 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3772 | if (attr->ia_valid) { |
| 3773 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3774 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 3775 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3776 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 3777 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3778 | err = btrfs_acl_chmod(inode); |
| 3779 | } |
| 3780 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3781 | return err; |
| 3782 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3783 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3784 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3785 | { |
| 3786 | struct btrfs_trans_handle *trans; |
| 3787 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3788 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 3789 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3790 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3791 | int ret; |
| 3792 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3793 | trace_btrfs_inode_evict(inode); |
| 3794 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3795 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3796 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3797 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3798 | goto no_delete; |
| 3799 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3800 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3801 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3802 | goto no_delete; |
| 3803 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3804 | /* 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] | 3805 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3806 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3807 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 3808 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3809 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3810 | goto no_delete; |
| 3811 | } |
| 3812 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3813 | if (inode->i_nlink > 0) { |
| 3814 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3815 | goto no_delete; |
| 3816 | } |
| 3817 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3818 | rsv = btrfs_alloc_block_rsv(root); |
| 3819 | if (!rsv) { |
| 3820 | btrfs_orphan_del(NULL, inode); |
| 3821 | goto no_delete; |
| 3822 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 3823 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 3824 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3825 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3826 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3827 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3828 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3829 | /* |
| 3830 | * This is a bit simpler than btrfs_truncate since |
| 3831 | * |
| 3832 | * 1) We've already reserved our space for our orphan item in the |
| 3833 | * unlink. |
| 3834 | * 2) We're going to delete the inode item, so we don't need to update |
| 3835 | * it at all. |
| 3836 | * |
| 3837 | * So we just need to reserve some slack space in case we add bytes when |
| 3838 | * doing the truncate. |
| 3839 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3840 | while (1) { |
Miao Xie | aa38a71 | 2011-11-18 17:43:00 +0800 | [diff] [blame] | 3841 | ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3842 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3843 | /* |
| 3844 | * Try and steal from the global reserve since we will |
| 3845 | * likely not use this space anyway, we want to try as |
| 3846 | * hard as possible to get this to work. |
| 3847 | */ |
| 3848 | if (ret) |
| 3849 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 3850 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3851 | if (ret) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3852 | printk(KERN_WARNING "Could not get space for a " |
Josef Bacik | 482e6dc | 2011-08-19 10:31:56 -0400 | [diff] [blame] | 3853 | "delete, will truncate on mount %d\n", ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3854 | btrfs_orphan_del(NULL, inode); |
| 3855 | btrfs_free_block_rsv(root, rsv); |
| 3856 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3857 | } |
| 3858 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3859 | trans = btrfs_start_transaction(root, 0); |
| 3860 | if (IS_ERR(trans)) { |
| 3861 | btrfs_orphan_del(NULL, inode); |
| 3862 | btrfs_free_block_rsv(root, rsv); |
| 3863 | goto no_delete; |
| 3864 | } |
| 3865 | |
| 3866 | trans->block_rsv = rsv; |
| 3867 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3868 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 3869 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3870 | break; |
| 3871 | |
| 3872 | nr = trans->blocks_used; |
| 3873 | btrfs_end_transaction(trans, root); |
| 3874 | trans = NULL; |
| 3875 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3876 | } |
| 3877 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3878 | btrfs_free_block_rsv(root, rsv); |
| 3879 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3880 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3881 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3882 | ret = btrfs_orphan_del(trans, inode); |
| 3883 | BUG_ON(ret); |
| 3884 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3885 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3886 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3887 | if (!(root == root->fs_info->tree_root || |
| 3888 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3889 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3890 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3891 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3892 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3893 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3894 | no_delete: |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 3895 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3896 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3897 | } |
| 3898 | |
| 3899 | /* |
| 3900 | * this returns the key found in the dir entry in the location pointer. |
| 3901 | * If no dir entries were found, location->objectid is 0. |
| 3902 | */ |
| 3903 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3904 | struct btrfs_key *location) |
| 3905 | { |
| 3906 | const char *name = dentry->d_name.name; |
| 3907 | int namelen = dentry->d_name.len; |
| 3908 | struct btrfs_dir_item *di; |
| 3909 | struct btrfs_path *path; |
| 3910 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3911 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3912 | |
| 3913 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 3914 | if (!path) |
| 3915 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3916 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3917 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3918 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3919 | if (IS_ERR(di)) |
| 3920 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3921 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3922 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3923 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3924 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3925 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3926 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3927 | btrfs_free_path(path); |
| 3928 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3929 | out_err: |
| 3930 | location->objectid = 0; |
| 3931 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3932 | } |
| 3933 | |
| 3934 | /* |
| 3935 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3936 | * needs to be changed to reflect the root directory of the tree root. This |
| 3937 | * is kind of like crossing a mount point. |
| 3938 | */ |
| 3939 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3940 | struct inode *dir, |
| 3941 | struct dentry *dentry, |
| 3942 | struct btrfs_key *location, |
| 3943 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3944 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3945 | struct btrfs_path *path; |
| 3946 | struct btrfs_root *new_root; |
| 3947 | struct btrfs_root_ref *ref; |
| 3948 | struct extent_buffer *leaf; |
| 3949 | int ret; |
| 3950 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3951 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3952 | path = btrfs_alloc_path(); |
| 3953 | if (!path) { |
| 3954 | err = -ENOMEM; |
| 3955 | goto out; |
| 3956 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3957 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3958 | err = -ENOENT; |
| 3959 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3960 | BTRFS_I(dir)->root->root_key.objectid, |
| 3961 | location->objectid); |
| 3962 | if (ret) { |
| 3963 | if (ret < 0) |
| 3964 | err = ret; |
| 3965 | goto out; |
| 3966 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3967 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3968 | leaf = path->nodes[0]; |
| 3969 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3970 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3971 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3972 | goto out; |
| 3973 | |
| 3974 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3975 | (unsigned long)(ref + 1), |
| 3976 | dentry->d_name.len); |
| 3977 | if (ret) |
| 3978 | goto out; |
| 3979 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3980 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3981 | |
| 3982 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3983 | if (IS_ERR(new_root)) { |
| 3984 | err = PTR_ERR(new_root); |
| 3985 | goto out; |
| 3986 | } |
| 3987 | |
| 3988 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3989 | err = -ENOENT; |
| 3990 | goto out; |
| 3991 | } |
| 3992 | |
| 3993 | *sub_root = new_root; |
| 3994 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3995 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3996 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3997 | err = 0; |
| 3998 | out: |
| 3999 | btrfs_free_path(path); |
| 4000 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4001 | } |
| 4002 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4003 | static void inode_tree_add(struct inode *inode) |
| 4004 | { |
| 4005 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4006 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4007 | struct rb_node **p; |
| 4008 | struct rb_node *parent; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4009 | u64 ino = btrfs_ino(inode); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4010 | again: |
| 4011 | p = &root->inode_tree.rb_node; |
| 4012 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4013 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 4014 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4015 | return; |
| 4016 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4017 | spin_lock(&root->inode_lock); |
| 4018 | while (*p) { |
| 4019 | parent = *p; |
| 4020 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 4021 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4022 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4023 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4024 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4025 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4026 | else { |
| 4027 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 4028 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4029 | rb_erase(parent, &root->inode_tree); |
| 4030 | RB_CLEAR_NODE(parent); |
| 4031 | spin_unlock(&root->inode_lock); |
| 4032 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4033 | } |
| 4034 | } |
| 4035 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 4036 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 4037 | spin_unlock(&root->inode_lock); |
| 4038 | } |
| 4039 | |
| 4040 | static void inode_tree_del(struct inode *inode) |
| 4041 | { |
| 4042 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4043 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4044 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4045 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4046 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4047 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4048 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4049 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4050 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4051 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4052 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4053 | /* |
| 4054 | * Free space cache has inodes in the tree root, but the tree root has a |
| 4055 | * root_refs of 0, so this could end up dropping the tree root as a |
| 4056 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 4057 | * make sure we don't drop it. |
| 4058 | */ |
| 4059 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 4060 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4061 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 4062 | spin_lock(&root->inode_lock); |
| 4063 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 4064 | spin_unlock(&root->inode_lock); |
| 4065 | if (empty) |
| 4066 | btrfs_add_dead_root(root); |
| 4067 | } |
| 4068 | } |
| 4069 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4070 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4071 | { |
| 4072 | struct rb_node *node; |
| 4073 | struct rb_node *prev; |
| 4074 | struct btrfs_inode *entry; |
| 4075 | struct inode *inode; |
| 4076 | u64 objectid = 0; |
| 4077 | |
| 4078 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4079 | |
| 4080 | spin_lock(&root->inode_lock); |
| 4081 | again: |
| 4082 | node = root->inode_tree.rb_node; |
| 4083 | prev = NULL; |
| 4084 | while (node) { |
| 4085 | prev = node; |
| 4086 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4087 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4088 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4089 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4090 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4091 | node = node->rb_right; |
| 4092 | else |
| 4093 | break; |
| 4094 | } |
| 4095 | if (!node) { |
| 4096 | while (prev) { |
| 4097 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4098 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4099 | node = prev; |
| 4100 | break; |
| 4101 | } |
| 4102 | prev = rb_next(prev); |
| 4103 | } |
| 4104 | } |
| 4105 | while (node) { |
| 4106 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4107 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4108 | inode = igrab(&entry->vfs_inode); |
| 4109 | if (inode) { |
| 4110 | spin_unlock(&root->inode_lock); |
| 4111 | if (atomic_read(&inode->i_count) > 1) |
| 4112 | d_prune_aliases(inode); |
| 4113 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4114 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4115 | * the inode cache when its usage count |
| 4116 | * hits zero. |
| 4117 | */ |
| 4118 | iput(inode); |
| 4119 | cond_resched(); |
| 4120 | spin_lock(&root->inode_lock); |
| 4121 | goto again; |
| 4122 | } |
| 4123 | |
| 4124 | if (cond_resched_lock(&root->inode_lock)) |
| 4125 | goto again; |
| 4126 | |
| 4127 | node = rb_next(node); |
| 4128 | } |
| 4129 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4130 | } |
| 4131 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4132 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4133 | { |
| 4134 | struct btrfs_iget_args *args = p; |
| 4135 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4136 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4137 | return 0; |
| 4138 | } |
| 4139 | |
| 4140 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4141 | { |
| 4142 | struct btrfs_iget_args *args = opaque; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4143 | return args->ino == btrfs_ino(inode) && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4144 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4145 | } |
| 4146 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4147 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4148 | u64 objectid, |
| 4149 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4150 | { |
| 4151 | struct inode *inode; |
| 4152 | struct btrfs_iget_args args; |
| 4153 | args.ino = objectid; |
| 4154 | args.root = root; |
| 4155 | |
| 4156 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 4157 | btrfs_init_locked_inode, |
| 4158 | (void *)&args); |
| 4159 | return inode; |
| 4160 | } |
| 4161 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4162 | /* Get an inode object given its location and corresponding root. |
| 4163 | * Returns in *is_new if the inode was read from disk |
| 4164 | */ |
| 4165 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4166 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4167 | { |
| 4168 | struct inode *inode; |
| 4169 | |
| 4170 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4171 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4172 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4173 | |
| 4174 | if (inode->i_state & I_NEW) { |
| 4175 | BTRFS_I(inode)->root = root; |
| 4176 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4177 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4178 | if (!is_bad_inode(inode)) { |
| 4179 | inode_tree_add(inode); |
| 4180 | unlock_new_inode(inode); |
| 4181 | if (new) |
| 4182 | *new = 1; |
| 4183 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4184 | unlock_new_inode(inode); |
| 4185 | iput(inode); |
| 4186 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4187 | } |
| 4188 | } |
| 4189 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4190 | return inode; |
| 4191 | } |
| 4192 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4193 | static struct inode *new_simple_dir(struct super_block *s, |
| 4194 | struct btrfs_key *key, |
| 4195 | struct btrfs_root *root) |
| 4196 | { |
| 4197 | struct inode *inode = new_inode(s); |
| 4198 | |
| 4199 | if (!inode) |
| 4200 | return ERR_PTR(-ENOMEM); |
| 4201 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4202 | BTRFS_I(inode)->root = root; |
| 4203 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4204 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4205 | |
| 4206 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4207 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4208 | inode->i_fop = &simple_dir_operations; |
| 4209 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 4210 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 4211 | |
| 4212 | return inode; |
| 4213 | } |
| 4214 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4215 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4216 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4217 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4218 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4219 | struct btrfs_root *sub_root = root; |
| 4220 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4221 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4222 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4223 | |
| 4224 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 4225 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4226 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4227 | if (unlikely(d_need_lookup(dentry))) { |
| 4228 | memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key)); |
| 4229 | kfree(dentry->d_fsdata); |
| 4230 | dentry->d_fsdata = NULL; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 4231 | /* This thing is hashed, drop it for now */ |
| 4232 | d_drop(dentry); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4233 | } else { |
| 4234 | ret = btrfs_inode_by_name(dir, dentry, &location); |
| 4235 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4236 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4237 | if (ret < 0) |
| 4238 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4239 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4240 | if (location.objectid == 0) |
| 4241 | return NULL; |
| 4242 | |
| 4243 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4244 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4245 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4246 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4247 | |
| 4248 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4249 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4250 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4251 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4252 | &location, &sub_root); |
| 4253 | if (ret < 0) { |
| 4254 | if (ret != -ENOENT) |
| 4255 | inode = ERR_PTR(ret); |
| 4256 | else |
| 4257 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4258 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4259 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4260 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4261 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4262 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 4263 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4264 | down_read(&root->fs_info->cleanup_work_sem); |
| 4265 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4266 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4267 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4268 | if (ret) |
| 4269 | inode = ERR_PTR(ret); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4272 | return inode; |
| 4273 | } |
| 4274 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 4275 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4276 | { |
| 4277 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4278 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4279 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4280 | if (!inode && !IS_ROOT(dentry)) |
| 4281 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4282 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4283 | if (inode) { |
| 4284 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4285 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4286 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4287 | |
| 4288 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 4289 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4290 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4291 | return 0; |
| 4292 | } |
| 4293 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4294 | static void btrfs_dentry_release(struct dentry *dentry) |
| 4295 | { |
| 4296 | if (dentry->d_fsdata) |
| 4297 | kfree(dentry->d_fsdata); |
| 4298 | } |
| 4299 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4300 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 4301 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4302 | { |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 4303 | struct dentry *ret; |
| 4304 | |
| 4305 | ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); |
| 4306 | if (unlikely(d_need_lookup(dentry))) { |
| 4307 | spin_lock(&dentry->d_lock); |
| 4308 | dentry->d_flags &= ~DCACHE_NEED_LOOKUP; |
| 4309 | spin_unlock(&dentry->d_lock); |
| 4310 | } |
| 4311 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4312 | } |
| 4313 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4314 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4315 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 4316 | }; |
| 4317 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 4318 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 4319 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4320 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4321 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4322 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4323 | struct btrfs_item *item; |
| 4324 | struct btrfs_dir_item *di; |
| 4325 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4326 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4327 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4328 | struct list_head ins_list; |
| 4329 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4330 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4331 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4332 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4333 | unsigned char d_type; |
| 4334 | int over = 0; |
| 4335 | u32 di_cur; |
| 4336 | u32 di_total; |
| 4337 | u32 di_len; |
| 4338 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4339 | char tmp_name[32]; |
| 4340 | char *name_ptr; |
| 4341 | int name_len; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4342 | int is_curr = 0; /* filp->f_pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4343 | |
| 4344 | /* FIXME, use a real flag for deciding about the key type */ |
| 4345 | if (root->fs_info->tree_root == root) |
| 4346 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4347 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4348 | /* special case for "." */ |
| 4349 | if (filp->f_pos == 0) { |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 4350 | over = filldir(dirent, ".", 1, |
| 4351 | filp->f_pos, btrfs_ino(inode), DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4352 | if (over) |
| 4353 | return 0; |
| 4354 | filp->f_pos = 1; |
| 4355 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4356 | /* special case for .., just use the back ref */ |
| 4357 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4358 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4359 | over = filldir(dirent, "..", 2, |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 4360 | filp->f_pos, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4361 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4362 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4363 | filp->f_pos = 2; |
| 4364 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4365 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4366 | if (!path) |
| 4367 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4368 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 4369 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4370 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4371 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4372 | INIT_LIST_HEAD(&ins_list); |
| 4373 | INIT_LIST_HEAD(&del_list); |
| 4374 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 4375 | } |
| 4376 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4377 | btrfs_set_key_type(&key, key_type); |
| 4378 | key.offset = filp->f_pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4379 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4380 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4381 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4382 | if (ret < 0) |
| 4383 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4384 | |
| 4385 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4386 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4387 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4388 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4389 | ret = btrfs_next_leaf(root, path); |
| 4390 | if (ret < 0) |
| 4391 | goto err; |
| 4392 | else if (ret > 0) |
| 4393 | break; |
| 4394 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4395 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4396 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4397 | item = btrfs_item_nr(leaf, slot); |
| 4398 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4399 | |
| 4400 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4401 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4402 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4403 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4404 | if (found_key.offset < filp->f_pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4405 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4406 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 4407 | btrfs_should_delete_dir_index(&del_list, |
| 4408 | found_key.offset)) |
| 4409 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4410 | |
| 4411 | filp->f_pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4412 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4413 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4414 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 4415 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4416 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4417 | |
| 4418 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4419 | struct btrfs_key location; |
| 4420 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 4421 | if (verify_dir_item(root, leaf, di)) |
| 4422 | break; |
| 4423 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4424 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4425 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4426 | name_ptr = tmp_name; |
| 4427 | } else { |
| 4428 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4429 | if (!name_ptr) { |
| 4430 | ret = -ENOMEM; |
| 4431 | goto err; |
| 4432 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4433 | } |
| 4434 | read_extent_buffer(leaf, name_ptr, |
| 4435 | (unsigned long)(di + 1), name_len); |
| 4436 | |
| 4437 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4438 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4439 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4440 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4441 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 4442 | * skip it. |
| 4443 | * |
| 4444 | * In contrast to old kernels, we insert the snapshot's |
| 4445 | * dir item and dir index after it has been created, so |
| 4446 | * we won't find a reference to our own snapshot. We |
| 4447 | * still keep the following code for backward |
| 4448 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4449 | */ |
| 4450 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4451 | location.objectid == root->root_key.objectid) { |
| 4452 | over = 0; |
| 4453 | goto skip; |
| 4454 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4455 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4456 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4457 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4458 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4459 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4460 | if (name_ptr != tmp_name) |
| 4461 | kfree(name_ptr); |
| 4462 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4463 | if (over) |
| 4464 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4465 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4466 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4467 | di_cur += di_len; |
| 4468 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4469 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4470 | next: |
| 4471 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4472 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4473 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4474 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4475 | if (is_curr) |
| 4476 | filp->f_pos++; |
| 4477 | ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir, |
| 4478 | &ins_list); |
| 4479 | if (ret) |
| 4480 | goto nopos; |
| 4481 | } |
| 4482 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4483 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4484 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4485 | /* |
| 4486 | * 32-bit glibc will use getdents64, but then strtol - |
| 4487 | * so the last number we can serve is this. |
| 4488 | */ |
| 4489 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4490 | else |
| 4491 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4492 | nopos: |
| 4493 | ret = 0; |
| 4494 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4495 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 4496 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4497 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4498 | return ret; |
| 4499 | } |
| 4500 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4501 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4502 | { |
| 4503 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4504 | struct btrfs_trans_handle *trans; |
| 4505 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4506 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4507 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4508 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4509 | return 0; |
| 4510 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 4511 | 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] | 4512 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4513 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4514 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4515 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4516 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4517 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4518 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4519 | if (IS_ERR(trans)) |
| 4520 | return PTR_ERR(trans); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4521 | if (nolock) |
| 4522 | ret = btrfs_end_transaction_nolock(trans, root); |
| 4523 | else |
| 4524 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4525 | } |
| 4526 | return ret; |
| 4527 | } |
| 4528 | |
| 4529 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4530 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4531 | * inode changes. But, it is most likely to find the inode in cache. |
| 4532 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4533 | * to keep or drop this code. |
| 4534 | */ |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4535 | int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4536 | { |
| 4537 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4538 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4539 | int ret; |
| 4540 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4541 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4542 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4543 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4544 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4545 | if (IS_ERR(trans)) |
| 4546 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4547 | |
| 4548 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4549 | if (ret && ret == -ENOSPC) { |
| 4550 | /* whoops, lets try again with the full transaction */ |
| 4551 | btrfs_end_transaction(trans, root); |
| 4552 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4553 | if (IS_ERR(trans)) |
| 4554 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4555 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4556 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4557 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4558 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4559 | if (BTRFS_I(inode)->delayed_node) |
| 4560 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4561 | |
| 4562 | return ret; |
| 4563 | } |
| 4564 | |
| 4565 | /* |
| 4566 | * This is a copy of file_update_time. We need this so we can return error on |
| 4567 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 4568 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 4569 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 4570 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4571 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 4572 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4573 | |
| 4574 | if (btrfs_root_readonly(root)) |
| 4575 | return -EROFS; |
| 4576 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 4577 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4578 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 4579 | if (flags & S_CTIME) |
| 4580 | inode->i_ctime = *now; |
| 4581 | if (flags & S_MTIME) |
| 4582 | inode->i_mtime = *now; |
| 4583 | if (flags & S_ATIME) |
| 4584 | inode->i_atime = *now; |
| 4585 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4586 | } |
| 4587 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4588 | /* |
| 4589 | * find the highest existing sequence number in a directory |
| 4590 | * and then set the in-memory index_cnt variable to reflect |
| 4591 | * free sequence numbers |
| 4592 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4593 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4594 | { |
| 4595 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4596 | struct btrfs_key key, found_key; |
| 4597 | struct btrfs_path *path; |
| 4598 | struct extent_buffer *leaf; |
| 4599 | int ret; |
| 4600 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4601 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4602 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4603 | key.offset = (u64)-1; |
| 4604 | |
| 4605 | path = btrfs_alloc_path(); |
| 4606 | if (!path) |
| 4607 | return -ENOMEM; |
| 4608 | |
| 4609 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4610 | if (ret < 0) |
| 4611 | goto out; |
| 4612 | /* FIXME: we should be able to handle this */ |
| 4613 | if (ret == 0) |
| 4614 | goto out; |
| 4615 | ret = 0; |
| 4616 | |
| 4617 | /* |
| 4618 | * MAGIC NUMBER EXPLANATION: |
| 4619 | * since we search a directory based on f_pos we have to start at 2 |
| 4620 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4621 | * else has to start at 2 |
| 4622 | */ |
| 4623 | if (path->slots[0] == 0) { |
| 4624 | BTRFS_I(inode)->index_cnt = 2; |
| 4625 | goto out; |
| 4626 | } |
| 4627 | |
| 4628 | path->slots[0]--; |
| 4629 | |
| 4630 | leaf = path->nodes[0]; |
| 4631 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4632 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4633 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4634 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4635 | BTRFS_I(inode)->index_cnt = 2; |
| 4636 | goto out; |
| 4637 | } |
| 4638 | |
| 4639 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4640 | out: |
| 4641 | btrfs_free_path(path); |
| 4642 | return ret; |
| 4643 | } |
| 4644 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4645 | /* |
| 4646 | * helper to find a free sequence number in a given directory. This current |
| 4647 | * code is very simple, later versions will do smarter things in the btree |
| 4648 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4649 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4650 | { |
| 4651 | int ret = 0; |
| 4652 | |
| 4653 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4654 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 4655 | if (ret) { |
| 4656 | ret = btrfs_set_inode_index_count(dir); |
| 4657 | if (ret) |
| 4658 | return ret; |
| 4659 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4660 | } |
| 4661 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4662 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4663 | BTRFS_I(dir)->index_cnt++; |
| 4664 | |
| 4665 | return ret; |
| 4666 | } |
| 4667 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4668 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4669 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4670 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4671 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 4672 | u64 ref_objectid, u64 objectid, |
| 4673 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4674 | { |
| 4675 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4676 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4677 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4678 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4679 | struct btrfs_inode_ref *ref; |
| 4680 | struct btrfs_key key[2]; |
| 4681 | u32 sizes[2]; |
| 4682 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4683 | int ret; |
| 4684 | int owner; |
| 4685 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4686 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4687 | if (!path) |
| 4688 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4689 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4690 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4691 | if (!inode) { |
| 4692 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4693 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4694 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4695 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4696 | /* |
| 4697 | * we have to initialize this early, so we can reclaim the inode |
| 4698 | * number if we fail afterwards in this function. |
| 4699 | */ |
| 4700 | inode->i_ino = objectid; |
| 4701 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4702 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4703 | trace_btrfs_inode_request(dir); |
| 4704 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4705 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4706 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4707 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4708 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4709 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4710 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4711 | } |
| 4712 | /* |
| 4713 | * index_cnt is ignored for everything but a dir, |
| 4714 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4715 | * number |
| 4716 | */ |
| 4717 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4718 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4719 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 4720 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4721 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4722 | /* |
| 4723 | * We could have gotten an inode number from somebody who was fsynced |
| 4724 | * and then removed in this same transaction, so let's just set full |
| 4725 | * sync since it will be a full sync anyway and this will blow away the |
| 4726 | * old info in the log. |
| 4727 | */ |
| 4728 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 4729 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 4730 | if (S_ISDIR(mode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4731 | owner = 0; |
| 4732 | else |
| 4733 | owner = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4734 | |
| 4735 | key[0].objectid = objectid; |
| 4736 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4737 | key[0].offset = 0; |
| 4738 | |
| 4739 | key[1].objectid = objectid; |
| 4740 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4741 | key[1].offset = ref_objectid; |
| 4742 | |
| 4743 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4744 | sizes[1] = name_len + sizeof(*ref); |
| 4745 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4746 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4747 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4748 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4749 | goto fail; |
| 4750 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 4751 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4752 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4753 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4754 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4755 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 4756 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 4757 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4758 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4759 | |
| 4760 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4761 | struct btrfs_inode_ref); |
| 4762 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4763 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4764 | ptr = (unsigned long)(ref + 1); |
| 4765 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4766 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4767 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4768 | btrfs_free_path(path); |
| 4769 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4770 | location = &BTRFS_I(inode)->location; |
| 4771 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4772 | location->offset = 0; |
| 4773 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4774 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4775 | btrfs_inherit_iflags(inode, dir); |
| 4776 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 4777 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4778 | if (btrfs_test_opt(root, NODATASUM)) |
| 4779 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 4780 | if (btrfs_test_opt(root, NODATACOW) || |
| 4781 | (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4782 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4783 | } |
| 4784 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4785 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4786 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4787 | |
| 4788 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 4789 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4790 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4791 | btrfs_update_root_times(trans, root); |
| 4792 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4793 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4794 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4795 | if (dir) |
| 4796 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4797 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4798 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4799 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4803 | { |
| 4804 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4805 | } |
| 4806 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4807 | /* |
| 4808 | * utility function to add 'inode' into 'parent_inode' with |
| 4809 | * a give name and a given sequence number. |
| 4810 | * if 'add_backref' is true, also insert a backref from the |
| 4811 | * inode to the parent directory. |
| 4812 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4813 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4814 | struct inode *parent_inode, struct inode *inode, |
| 4815 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4816 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4817 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4818 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4819 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4820 | u64 ino = btrfs_ino(inode); |
| 4821 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4822 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4823 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4824 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4825 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4826 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4827 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4828 | key.offset = 0; |
| 4829 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4830 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4831 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4832 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4833 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4834 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4835 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4836 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 4837 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4838 | } |
| 4839 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4840 | /* Nothing to clean up yet */ |
| 4841 | if (ret) |
| 4842 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4843 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4844 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4845 | parent_inode, &key, |
| 4846 | btrfs_inode_type(inode), index); |
| 4847 | if (ret == -EEXIST) |
| 4848 | goto fail_dir_item; |
| 4849 | else if (ret) { |
| 4850 | btrfs_abort_transaction(trans, root, ret); |
| 4851 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4852 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4853 | |
| 4854 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 4855 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4856 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4857 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 4858 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 4859 | if (ret) |
| 4860 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4861 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 4862 | |
| 4863 | fail_dir_item: |
| 4864 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4865 | u64 local_index; |
| 4866 | int err; |
| 4867 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 4868 | key.objectid, root->root_key.objectid, |
| 4869 | parent_ino, &local_index, name, name_len); |
| 4870 | |
| 4871 | } else if (add_backref) { |
| 4872 | u64 local_index; |
| 4873 | int err; |
| 4874 | |
| 4875 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 4876 | ino, parent_ino, &local_index); |
| 4877 | } |
| 4878 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4879 | } |
| 4880 | |
| 4881 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4882 | struct inode *dir, struct dentry *dentry, |
| 4883 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4884 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4885 | int err = btrfs_add_link(trans, dir, inode, |
| 4886 | dentry->d_name.name, dentry->d_name.len, |
| 4887 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4888 | if (err > 0) |
| 4889 | err = -EEXIST; |
| 4890 | return err; |
| 4891 | } |
| 4892 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4893 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 4894 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4895 | { |
| 4896 | struct btrfs_trans_handle *trans; |
| 4897 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4898 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4899 | int err; |
| 4900 | int drop_inode = 0; |
| 4901 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4902 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4903 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4904 | |
| 4905 | if (!new_valid_dev(rdev)) |
| 4906 | return -EINVAL; |
| 4907 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4908 | /* |
| 4909 | * 2 for inode item and ref |
| 4910 | * 2 for dir items |
| 4911 | * 1 for xattr if selinux is on |
| 4912 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4913 | trans = btrfs_start_transaction(root, 5); |
| 4914 | if (IS_ERR(trans)) |
| 4915 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4916 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4917 | err = btrfs_find_free_ino(root, &objectid); |
| 4918 | if (err) |
| 4919 | goto out_unlock; |
| 4920 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4921 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4922 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4923 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4924 | if (IS_ERR(inode)) { |
| 4925 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4926 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4927 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4928 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4929 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4930 | if (err) { |
| 4931 | drop_inode = 1; |
| 4932 | goto out_unlock; |
| 4933 | } |
| 4934 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 4935 | /* |
| 4936 | * If the active LSM wants to access the inode during |
| 4937 | * d_instantiate it needs these. Smack checks to see |
| 4938 | * if the filesystem supports xattrs by looking at the |
| 4939 | * ops vector. |
| 4940 | */ |
| 4941 | |
| 4942 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4943 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4944 | if (err) |
| 4945 | drop_inode = 1; |
| 4946 | else { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4947 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4948 | btrfs_update_inode(trans, root, inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 4949 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4950 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4951 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4952 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4953 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4954 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4955 | if (drop_inode) { |
| 4956 | inode_dec_link_count(inode); |
| 4957 | iput(inode); |
| 4958 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4959 | return err; |
| 4960 | } |
| 4961 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4962 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 4963 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4964 | { |
| 4965 | struct btrfs_trans_handle *trans; |
| 4966 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4967 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4968 | int drop_inode = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4969 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4970 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4971 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4972 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4973 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4974 | /* |
| 4975 | * 2 for inode item and ref |
| 4976 | * 2 for dir items |
| 4977 | * 1 for xattr if selinux is on |
| 4978 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4979 | trans = btrfs_start_transaction(root, 5); |
| 4980 | if (IS_ERR(trans)) |
| 4981 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4982 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4983 | err = btrfs_find_free_ino(root, &objectid); |
| 4984 | if (err) |
| 4985 | goto out_unlock; |
| 4986 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4987 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4988 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4989 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4990 | if (IS_ERR(inode)) { |
| 4991 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4992 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4993 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4994 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4995 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4996 | if (err) { |
| 4997 | drop_inode = 1; |
| 4998 | goto out_unlock; |
| 4999 | } |
| 5000 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5001 | /* |
| 5002 | * If the active LSM wants to access the inode during |
| 5003 | * d_instantiate it needs these. Smack checks to see |
| 5004 | * if the filesystem supports xattrs by looking at the |
| 5005 | * ops vector. |
| 5006 | */ |
| 5007 | inode->i_fop = &btrfs_file_operations; |
| 5008 | inode->i_op = &btrfs_file_inode_operations; |
| 5009 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5010 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5011 | if (err) |
| 5012 | drop_inode = 1; |
| 5013 | else { |
| 5014 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 5015 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5016 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5017 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5018 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5019 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5020 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5021 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5022 | if (drop_inode) { |
| 5023 | inode_dec_link_count(inode); |
| 5024 | iput(inode); |
| 5025 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5026 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5027 | return err; |
| 5028 | } |
| 5029 | |
| 5030 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 5031 | struct dentry *dentry) |
| 5032 | { |
| 5033 | struct btrfs_trans_handle *trans; |
| 5034 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5035 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5036 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5037 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5038 | int err; |
| 5039 | int drop_inode = 0; |
| 5040 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5041 | /* do not allow sys_link's with other subvols of the same device */ |
| 5042 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 5043 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5044 | |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 5045 | if (inode->i_nlink == ~0U) |
| 5046 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5047 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5048 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5049 | if (err) |
| 5050 | goto fail; |
| 5051 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5052 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5053 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5054 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5055 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5056 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5057 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5058 | if (IS_ERR(trans)) { |
| 5059 | err = PTR_ERR(trans); |
| 5060 | goto fail; |
| 5061 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5062 | |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5063 | btrfs_inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5064 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5065 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 5066 | ihold(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5067 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5068 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5069 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5070 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5071 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5072 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 5073 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5074 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5075 | if (err) |
| 5076 | goto fail; |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5077 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5078 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5079 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5080 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5081 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5082 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5083 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5084 | if (drop_inode) { |
| 5085 | inode_dec_link_count(inode); |
| 5086 | iput(inode); |
| 5087 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5088 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5089 | return err; |
| 5090 | } |
| 5091 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 5092 | 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] | 5093 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5094 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5095 | struct btrfs_trans_handle *trans; |
| 5096 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5097 | int err = 0; |
| 5098 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5099 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5100 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5101 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5102 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5103 | /* |
| 5104 | * 2 items for inode and ref |
| 5105 | * 2 items for dir items |
| 5106 | * 1 for xattr if selinux is on |
| 5107 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5108 | trans = btrfs_start_transaction(root, 5); |
| 5109 | if (IS_ERR(trans)) |
| 5110 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5111 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5112 | err = btrfs_find_free_ino(root, &objectid); |
| 5113 | if (err) |
| 5114 | goto out_fail; |
| 5115 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5116 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5117 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5118 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5119 | if (IS_ERR(inode)) { |
| 5120 | err = PTR_ERR(inode); |
| 5121 | goto out_fail; |
| 5122 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5123 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5124 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -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); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5127 | if (err) |
| 5128 | goto out_fail; |
| 5129 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5130 | inode->i_op = &btrfs_dir_inode_operations; |
| 5131 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5132 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5133 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5134 | err = btrfs_update_inode(trans, root, inode); |
| 5135 | if (err) |
| 5136 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5137 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5138 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 5139 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5140 | if (err) |
| 5141 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5142 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5143 | d_instantiate(dentry, inode); |
| 5144 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5145 | |
| 5146 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5147 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5148 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5149 | if (drop_on_err) |
| 5150 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5151 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5152 | return err; |
| 5153 | } |
| 5154 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5155 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 5156 | * and an extent that you want to insert, deal with overlap and insert |
| 5157 | * the new extent into the tree. |
| 5158 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5159 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 5160 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5161 | struct extent_map *em, |
| 5162 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5163 | { |
| 5164 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5165 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5166 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 5167 | start_diff = map_start - em->start; |
| 5168 | em->start = map_start; |
| 5169 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5170 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 5171 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5172 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5173 | em->block_len -= start_diff; |
| 5174 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5175 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5176 | } |
| 5177 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5178 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 5179 | struct inode *inode, struct page *page, |
| 5180 | size_t pg_offset, u64 extent_offset, |
| 5181 | struct btrfs_file_extent_item *item) |
| 5182 | { |
| 5183 | int ret; |
| 5184 | struct extent_buffer *leaf = path->nodes[0]; |
| 5185 | char *tmp; |
| 5186 | size_t max_size; |
| 5187 | unsigned long inline_size; |
| 5188 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5189 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5190 | |
| 5191 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5192 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5193 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 5194 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 5195 | btrfs_item_nr(leaf, path->slots[0])); |
| 5196 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 5197 | if (!tmp) |
| 5198 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5199 | ptr = btrfs_file_extent_inline_start(item); |
| 5200 | |
| 5201 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 5202 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 5203 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5204 | ret = btrfs_decompress(compress_type, tmp, page, |
| 5205 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5206 | if (ret) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 5207 | char *kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5208 | unsigned long copy_size = min_t(u64, |
| 5209 | PAGE_CACHE_SIZE - pg_offset, |
| 5210 | max_size - extent_offset); |
| 5211 | memset(kaddr + pg_offset, 0, copy_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 5212 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5213 | } |
| 5214 | kfree(tmp); |
| 5215 | return 0; |
| 5216 | } |
| 5217 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5218 | /* |
| 5219 | * 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] | 5220 | * the ugly parts come from merging extents from the disk with the in-ram |
| 5221 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5222 | * where the in-ram extents might be locked pending data=ordered completion. |
| 5223 | * |
| 5224 | * This also copies inline extents directly into the page. |
| 5225 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5226 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5227 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5228 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5229 | int create) |
| 5230 | { |
| 5231 | int ret; |
| 5232 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5233 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5234 | u64 extent_start = 0; |
| 5235 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5236 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5237 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5238 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5239 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5240 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5241 | struct extent_buffer *leaf; |
| 5242 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5243 | struct extent_map *em = NULL; |
| 5244 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5245 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5246 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5247 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5248 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5249 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5250 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5251 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5252 | if (em) |
| 5253 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5254 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5255 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5256 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5257 | if (em->start > start || em->start + em->len <= start) |
| 5258 | free_extent_map(em); |
| 5259 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5260 | free_extent_map(em); |
| 5261 | else |
| 5262 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5263 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5264 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5265 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5266 | err = -ENOMEM; |
| 5267 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5268 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5269 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5270 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 5271 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5272 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5273 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5274 | |
| 5275 | if (!path) { |
| 5276 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5277 | if (!path) { |
| 5278 | err = -ENOMEM; |
| 5279 | goto out; |
| 5280 | } |
| 5281 | /* |
| 5282 | * Chances are we'll be called again, so go ahead and do |
| 5283 | * readahead |
| 5284 | */ |
| 5285 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5286 | } |
| 5287 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5288 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 5289 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5290 | if (ret < 0) { |
| 5291 | err = ret; |
| 5292 | goto out; |
| 5293 | } |
| 5294 | |
| 5295 | if (ret != 0) { |
| 5296 | if (path->slots[0] == 0) |
| 5297 | goto not_found; |
| 5298 | path->slots[0]--; |
| 5299 | } |
| 5300 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5301 | leaf = path->nodes[0]; |
| 5302 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5303 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5304 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5305 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5306 | found_type = btrfs_key_type(&found_key); |
| 5307 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5308 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 5309 | goto not_found; |
| 5310 | } |
| 5311 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5312 | found_type = btrfs_file_extent_type(leaf, item); |
| 5313 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5314 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5315 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5316 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5317 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5318 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5319 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5320 | size_t size; |
| 5321 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5322 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 5323 | ~((u64)root->sectorsize - 1); |
| 5324 | } |
| 5325 | |
| 5326 | if (start >= extent_end) { |
| 5327 | path->slots[0]++; |
| 5328 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 5329 | ret = btrfs_next_leaf(root, path); |
| 5330 | if (ret < 0) { |
| 5331 | err = ret; |
| 5332 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5333 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5334 | if (ret > 0) |
| 5335 | goto not_found; |
| 5336 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5337 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5338 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5339 | if (found_key.objectid != objectid || |
| 5340 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 5341 | goto not_found; |
| 5342 | if (start + len <= found_key.offset) |
| 5343 | goto not_found; |
| 5344 | em->start = start; |
| 5345 | em->len = found_key.offset - start; |
| 5346 | goto not_found_em; |
| 5347 | } |
| 5348 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5349 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5350 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5351 | em->start = extent_start; |
| 5352 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5353 | em->orig_start = extent_start - |
| 5354 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5355 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 5356 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5357 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5358 | goto insert; |
| 5359 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5360 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5361 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5362 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5363 | em->block_start = bytenr; |
| 5364 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 5365 | item); |
| 5366 | } else { |
| 5367 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 5368 | em->block_start = bytenr; |
| 5369 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5370 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 5371 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5372 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5373 | goto insert; |
| 5374 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5375 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5376 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5377 | size_t size; |
| 5378 | size_t extent_offset; |
| 5379 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5380 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5381 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5382 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5383 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5384 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5385 | goto out; |
| 5386 | } |
| 5387 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5388 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5389 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5390 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5391 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5392 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5393 | em->len = (copy_size + root->sectorsize - 1) & |
| 5394 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5395 | em->orig_start = EXTENT_MAP_INLINE; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5396 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5397 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5398 | em->compress_type = compress_type; |
| 5399 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5400 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5401 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5402 | if (btrfs_file_extent_compression(leaf, item) != |
| 5403 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5404 | ret = uncompress_inline(path, inode, page, |
| 5405 | pg_offset, |
| 5406 | extent_offset, item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5407 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5408 | } else { |
| 5409 | map = kmap(page); |
| 5410 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 5411 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 5412 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 5413 | memset(map + pg_offset + copy_size, 0, |
| 5414 | PAGE_CACHE_SIZE - pg_offset - |
| 5415 | copy_size); |
| 5416 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5417 | kunmap(page); |
| 5418 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5419 | flush_dcache_page(page); |
| 5420 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 5421 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5422 | if (!trans) { |
| 5423 | kunmap(page); |
| 5424 | free_extent_map(em); |
| 5425 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5426 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5427 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5428 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5429 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5430 | if (IS_ERR(trans)) |
| 5431 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5432 | goto again; |
| 5433 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5434 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5435 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5436 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5437 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5438 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5439 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5440 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 5441 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5442 | goto insert; |
| 5443 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5444 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5445 | WARN_ON(1); |
| 5446 | } |
| 5447 | not_found: |
| 5448 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5449 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5450 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5451 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5452 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5453 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5454 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5455 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5456 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 5457 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 5458 | (unsigned long long)em->len, |
| 5459 | (unsigned long long)start, |
| 5460 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5461 | err = -EIO; |
| 5462 | goto out; |
| 5463 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5464 | |
| 5465 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5466 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5467 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5468 | /* it is possible that someone inserted the extent into the tree |
| 5469 | * while we had the lock dropped. It is also possible that |
| 5470 | * an overlapping map exists in the tree |
| 5471 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5472 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5473 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5474 | |
| 5475 | ret = 0; |
| 5476 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5477 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5478 | if (existing && (existing->start > start || |
| 5479 | existing->start + existing->len <= start)) { |
| 5480 | free_extent_map(existing); |
| 5481 | existing = NULL; |
| 5482 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5483 | if (!existing) { |
| 5484 | existing = lookup_extent_mapping(em_tree, em->start, |
| 5485 | em->len); |
| 5486 | if (existing) { |
| 5487 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5488 | em, start, |
| 5489 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5490 | free_extent_map(existing); |
| 5491 | if (err) { |
| 5492 | free_extent_map(em); |
| 5493 | em = NULL; |
| 5494 | } |
| 5495 | } else { |
| 5496 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5497 | free_extent_map(em); |
| 5498 | em = NULL; |
| 5499 | } |
| 5500 | } else { |
| 5501 | free_extent_map(em); |
| 5502 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5503 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5504 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5505 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5506 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5507 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5508 | |
| 5509 | trace_btrfs_get_extent(root, em); |
| 5510 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5511 | if (path) |
| 5512 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5513 | if (trans) { |
| 5514 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5515 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5516 | err = ret; |
| 5517 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5518 | if (err) { |
| 5519 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5520 | return ERR_PTR(err); |
| 5521 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5522 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5523 | return em; |
| 5524 | } |
| 5525 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5526 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 5527 | size_t pg_offset, u64 start, u64 len, |
| 5528 | int create) |
| 5529 | { |
| 5530 | struct extent_map *em; |
| 5531 | struct extent_map *hole_em = NULL; |
| 5532 | u64 range_start = start; |
| 5533 | u64 end; |
| 5534 | u64 found; |
| 5535 | u64 found_end; |
| 5536 | int err = 0; |
| 5537 | |
| 5538 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 5539 | if (IS_ERR(em)) |
| 5540 | return em; |
| 5541 | if (em) { |
| 5542 | /* |
| 5543 | * if our em maps to a hole, there might |
| 5544 | * actually be delalloc bytes behind it |
| 5545 | */ |
| 5546 | if (em->block_start != EXTENT_MAP_HOLE) |
| 5547 | return em; |
| 5548 | else |
| 5549 | hole_em = em; |
| 5550 | } |
| 5551 | |
| 5552 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 5553 | end = start + len; |
| 5554 | if (end < start) |
| 5555 | end = (u64)-1; |
| 5556 | else |
| 5557 | end -= 1; |
| 5558 | |
| 5559 | em = NULL; |
| 5560 | |
| 5561 | /* ok, we didn't find anything, lets look for delalloc */ |
| 5562 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 5563 | end, len, EXTENT_DELALLOC, 1); |
| 5564 | found_end = range_start + found; |
| 5565 | if (found_end < range_start) |
| 5566 | found_end = (u64)-1; |
| 5567 | |
| 5568 | /* |
| 5569 | * we didn't find anything useful, return |
| 5570 | * the original results from get_extent() |
| 5571 | */ |
| 5572 | if (range_start > end || found_end <= start) { |
| 5573 | em = hole_em; |
| 5574 | hole_em = NULL; |
| 5575 | goto out; |
| 5576 | } |
| 5577 | |
| 5578 | /* adjust the range_start to make sure it doesn't |
| 5579 | * go backwards from the start they passed in |
| 5580 | */ |
| 5581 | range_start = max(start,range_start); |
| 5582 | found = found_end - range_start; |
| 5583 | |
| 5584 | if (found > 0) { |
| 5585 | u64 hole_start = start; |
| 5586 | u64 hole_len = len; |
| 5587 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5588 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5589 | if (!em) { |
| 5590 | err = -ENOMEM; |
| 5591 | goto out; |
| 5592 | } |
| 5593 | /* |
| 5594 | * when btrfs_get_extent can't find anything it |
| 5595 | * returns one huge hole |
| 5596 | * |
| 5597 | * make sure what it found really fits our range, and |
| 5598 | * adjust to make sure it is based on the start from |
| 5599 | * the caller |
| 5600 | */ |
| 5601 | if (hole_em) { |
| 5602 | u64 calc_end = extent_map_end(hole_em); |
| 5603 | |
| 5604 | if (calc_end <= start || (hole_em->start > end)) { |
| 5605 | free_extent_map(hole_em); |
| 5606 | hole_em = NULL; |
| 5607 | } else { |
| 5608 | hole_start = max(hole_em->start, start); |
| 5609 | hole_len = calc_end - hole_start; |
| 5610 | } |
| 5611 | } |
| 5612 | em->bdev = NULL; |
| 5613 | if (hole_em && range_start > hole_start) { |
| 5614 | /* our hole starts before our delalloc, so we |
| 5615 | * have to return just the parts of the hole |
| 5616 | * that go until the delalloc starts |
| 5617 | */ |
| 5618 | em->len = min(hole_len, |
| 5619 | range_start - hole_start); |
| 5620 | em->start = hole_start; |
| 5621 | em->orig_start = hole_start; |
| 5622 | /* |
| 5623 | * don't adjust block start at all, |
| 5624 | * it is fixed at EXTENT_MAP_HOLE |
| 5625 | */ |
| 5626 | em->block_start = hole_em->block_start; |
| 5627 | em->block_len = hole_len; |
| 5628 | } else { |
| 5629 | em->start = range_start; |
| 5630 | em->len = found; |
| 5631 | em->orig_start = range_start; |
| 5632 | em->block_start = EXTENT_MAP_DELALLOC; |
| 5633 | em->block_len = found; |
| 5634 | } |
| 5635 | } else if (hole_em) { |
| 5636 | return hole_em; |
| 5637 | } |
| 5638 | out: |
| 5639 | |
| 5640 | free_extent_map(hole_em); |
| 5641 | if (err) { |
| 5642 | free_extent_map(em); |
| 5643 | return ERR_PTR(err); |
| 5644 | } |
| 5645 | return em; |
| 5646 | } |
| 5647 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5648 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5649 | struct extent_map *em, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5650 | u64 start, u64 len) |
| 5651 | { |
| 5652 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5653 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5654 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 5655 | struct btrfs_key ins; |
| 5656 | u64 alloc_hint; |
| 5657 | int ret; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5658 | bool insert = false; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5659 | |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5660 | /* |
| 5661 | * Ok if the extent map we looked up is a hole and is for the exact |
| 5662 | * range we want, there is no reason to allocate a new one, however if |
| 5663 | * it is not right then we need to free this one and drop the cache for |
| 5664 | * our range. |
| 5665 | */ |
| 5666 | if (em->block_start != EXTENT_MAP_HOLE || em->start != start || |
| 5667 | em->len != len) { |
| 5668 | free_extent_map(em); |
| 5669 | em = NULL; |
| 5670 | insert = true; |
| 5671 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
| 5672 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5673 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5674 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5675 | if (IS_ERR(trans)) |
| 5676 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5677 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 5678 | if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024) |
| 5679 | btrfs_add_inode_defrag(trans, inode); |
| 5680 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5681 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5682 | |
| 5683 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 5684 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 5685 | alloc_hint, &ins, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5686 | if (ret) { |
| 5687 | em = ERR_PTR(ret); |
| 5688 | goto out; |
| 5689 | } |
| 5690 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5691 | if (!em) { |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5692 | em = alloc_extent_map(); |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5693 | if (!em) { |
| 5694 | em = ERR_PTR(-ENOMEM); |
| 5695 | goto out; |
| 5696 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5697 | } |
| 5698 | |
| 5699 | em->start = start; |
| 5700 | em->orig_start = em->start; |
| 5701 | em->len = ins.offset; |
| 5702 | |
| 5703 | em->block_start = ins.objectid; |
| 5704 | em->block_len = ins.offset; |
| 5705 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5706 | |
| 5707 | /* |
| 5708 | * We need to do this because if we're using the original em we searched |
| 5709 | * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that. |
| 5710 | */ |
| 5711 | em->flags = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5712 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5713 | |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5714 | while (insert) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5715 | write_lock(&em_tree->lock); |
| 5716 | ret = add_extent_mapping(em_tree, em); |
| 5717 | write_unlock(&em_tree->lock); |
| 5718 | if (ret != -EEXIST) |
| 5719 | break; |
| 5720 | btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0); |
| 5721 | } |
| 5722 | |
| 5723 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 5724 | ins.offset, ins.offset, 0); |
| 5725 | if (ret) { |
| 5726 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 5727 | em = ERR_PTR(ret); |
| 5728 | } |
| 5729 | out: |
| 5730 | btrfs_end_transaction(trans, root); |
| 5731 | return em; |
| 5732 | } |
| 5733 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5734 | /* |
| 5735 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 5736 | * block must be cow'd |
| 5737 | */ |
| 5738 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 5739 | struct inode *inode, u64 offset, u64 len) |
| 5740 | { |
| 5741 | struct btrfs_path *path; |
| 5742 | int ret; |
| 5743 | struct extent_buffer *leaf; |
| 5744 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5745 | struct btrfs_file_extent_item *fi; |
| 5746 | struct btrfs_key key; |
| 5747 | u64 disk_bytenr; |
| 5748 | u64 backref_offset; |
| 5749 | u64 extent_end; |
| 5750 | u64 num_bytes; |
| 5751 | int slot; |
| 5752 | int found_type; |
| 5753 | |
| 5754 | path = btrfs_alloc_path(); |
| 5755 | if (!path) |
| 5756 | return -ENOMEM; |
| 5757 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5758 | ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5759 | offset, 0); |
| 5760 | if (ret < 0) |
| 5761 | goto out; |
| 5762 | |
| 5763 | slot = path->slots[0]; |
| 5764 | if (ret == 1) { |
| 5765 | if (slot == 0) { |
| 5766 | /* can't find the item, must cow */ |
| 5767 | ret = 0; |
| 5768 | goto out; |
| 5769 | } |
| 5770 | slot--; |
| 5771 | } |
| 5772 | ret = 0; |
| 5773 | leaf = path->nodes[0]; |
| 5774 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5775 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5776 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5777 | /* not our file or wrong item type, must cow */ |
| 5778 | goto out; |
| 5779 | } |
| 5780 | |
| 5781 | if (key.offset > offset) { |
| 5782 | /* Wrong offset, must cow */ |
| 5783 | goto out; |
| 5784 | } |
| 5785 | |
| 5786 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5787 | found_type = btrfs_file_extent_type(leaf, fi); |
| 5788 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 5789 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 5790 | /* not a regular extent, must cow */ |
| 5791 | goto out; |
| 5792 | } |
| 5793 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5794 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 5795 | |
| 5796 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 5797 | if (extent_end < offset + len) { |
| 5798 | /* extent doesn't include our full range, must cow */ |
| 5799 | goto out; |
| 5800 | } |
| 5801 | |
| 5802 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 5803 | goto out; |
| 5804 | |
| 5805 | /* |
| 5806 | * look for other files referencing this extent, if we |
| 5807 | * find any we must cow |
| 5808 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5809 | if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5810 | key.offset - backref_offset, disk_bytenr)) |
| 5811 | goto out; |
| 5812 | |
| 5813 | /* |
| 5814 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 5815 | * in this extent we are about to write. If there |
| 5816 | * are any csums in that range we have to cow in order |
| 5817 | * to keep the csums correct |
| 5818 | */ |
| 5819 | disk_bytenr += backref_offset; |
| 5820 | disk_bytenr += offset - key.offset; |
| 5821 | num_bytes = min(offset + len, extent_end) - offset; |
| 5822 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 5823 | goto out; |
| 5824 | /* |
| 5825 | * all of the above have passed, it is safe to overwrite this extent |
| 5826 | * without cow |
| 5827 | */ |
| 5828 | ret = 1; |
| 5829 | out: |
| 5830 | btrfs_free_path(path); |
| 5831 | return ret; |
| 5832 | } |
| 5833 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5834 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 5835 | struct extent_state **cached_state, int writing) |
| 5836 | { |
| 5837 | struct btrfs_ordered_extent *ordered; |
| 5838 | int ret = 0; |
| 5839 | |
| 5840 | while (1) { |
| 5841 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 5842 | 0, cached_state); |
| 5843 | /* |
| 5844 | * We're concerned with the entire range that we're going to be |
| 5845 | * doing DIO to, so we need to make sure theres no ordered |
| 5846 | * extents in this range. |
| 5847 | */ |
| 5848 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 5849 | lockend - lockstart + 1); |
| 5850 | |
| 5851 | /* |
| 5852 | * We need to make sure there are no buffered pages in this |
| 5853 | * range either, we could have raced between the invalidate in |
| 5854 | * generic_file_direct_write and locking the extent. The |
| 5855 | * invalidate needs to happen so that reads after a write do not |
| 5856 | * get stale data. |
| 5857 | */ |
| 5858 | if (!ordered && (!writing || |
| 5859 | !test_range_bit(&BTRFS_I(inode)->io_tree, |
| 5860 | lockstart, lockend, EXTENT_UPTODATE, 0, |
| 5861 | *cached_state))) |
| 5862 | break; |
| 5863 | |
| 5864 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 5865 | cached_state, GFP_NOFS); |
| 5866 | |
| 5867 | if (ordered) { |
| 5868 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 5869 | btrfs_put_ordered_extent(ordered); |
| 5870 | } else { |
| 5871 | /* Screw you mmap */ |
| 5872 | ret = filemap_write_and_wait_range(inode->i_mapping, |
| 5873 | lockstart, |
| 5874 | lockend); |
| 5875 | if (ret) |
| 5876 | break; |
| 5877 | |
| 5878 | /* |
| 5879 | * If we found a page that couldn't be invalidated just |
| 5880 | * fall back to buffered. |
| 5881 | */ |
| 5882 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 5883 | lockstart >> PAGE_CACHE_SHIFT, |
| 5884 | lockend >> PAGE_CACHE_SHIFT); |
| 5885 | if (ret) |
| 5886 | break; |
| 5887 | } |
| 5888 | |
| 5889 | cond_resched(); |
| 5890 | } |
| 5891 | |
| 5892 | return ret; |
| 5893 | } |
| 5894 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5895 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 5896 | struct buffer_head *bh_result, int create) |
| 5897 | { |
| 5898 | struct extent_map *em; |
| 5899 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5900 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5901 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5902 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5903 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5904 | struct btrfs_trans_handle *trans; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5905 | int unlock_bits = EXTENT_LOCKED; |
| 5906 | int ret; |
| 5907 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5908 | if (create) { |
| 5909 | ret = btrfs_delalloc_reserve_space(inode, len); |
| 5910 | if (ret) |
| 5911 | return ret; |
| 5912 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 5913 | } else { |
| 5914 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5915 | } |
| 5916 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 5917 | lockstart = start; |
| 5918 | lockend = start + len - 1; |
| 5919 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5920 | /* |
| 5921 | * If this errors out it's because we couldn't invalidate pagecache for |
| 5922 | * this range and we need to fallback to buffered. |
| 5923 | */ |
| 5924 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 5925 | return -ENOTBLK; |
| 5926 | |
| 5927 | if (create) { |
| 5928 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 5929 | lockend, EXTENT_DELALLOC, NULL, |
| 5930 | &cached_state, GFP_NOFS); |
| 5931 | if (ret) |
| 5932 | goto unlock_err; |
| 5933 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5934 | |
| 5935 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5936 | if (IS_ERR(em)) { |
| 5937 | ret = PTR_ERR(em); |
| 5938 | goto unlock_err; |
| 5939 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5940 | |
| 5941 | /* |
| 5942 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 5943 | * io. INLINE is special, and we could probably kludge it in here, but |
| 5944 | * it's still buffered so for safety lets just fall back to the generic |
| 5945 | * buffered path. |
| 5946 | * |
| 5947 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 5948 | * decompress it, so there will be buffering required no matter what we |
| 5949 | * do, so go ahead and fallback to buffered. |
| 5950 | * |
| 5951 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 5952 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 5953 | * the generic code. |
| 5954 | */ |
| 5955 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 5956 | em->block_start == EXTENT_MAP_INLINE) { |
| 5957 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5958 | ret = -ENOTBLK; |
| 5959 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5960 | } |
| 5961 | |
| 5962 | /* Just a good old fashioned hole, return */ |
| 5963 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 5964 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5965 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5966 | ret = 0; |
| 5967 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5968 | } |
| 5969 | |
| 5970 | /* |
| 5971 | * We don't allocate a new extent in the following cases |
| 5972 | * |
| 5973 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 5974 | * existing extent. |
| 5975 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 5976 | * just use the extent. |
| 5977 | * |
| 5978 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5979 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 5980 | len = min(len, em->len - (start - em->start)); |
| 5981 | lockstart = start + len; |
| 5982 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5983 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5984 | |
| 5985 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 5986 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 5987 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5988 | int type; |
| 5989 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5990 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5991 | |
| 5992 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5993 | type = BTRFS_ORDERED_PREALLOC; |
| 5994 | else |
| 5995 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5996 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5997 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5998 | |
| 5999 | /* |
| 6000 | * we're not going to log anything, but we do need |
| 6001 | * to make sure the current transaction stays open |
| 6002 | * while we look for nocow cross refs |
| 6003 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6004 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6005 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6006 | goto must_cow; |
| 6007 | |
| 6008 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
| 6009 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 6010 | block_start, len, len, type); |
| 6011 | btrfs_end_transaction(trans, root); |
| 6012 | if (ret) { |
| 6013 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6014 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6015 | } |
| 6016 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6017 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6018 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6019 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6020 | must_cow: |
| 6021 | /* |
| 6022 | * this will cow the extent, reset the len in case we changed |
| 6023 | * it above |
| 6024 | */ |
| 6025 | len = bh_result->b_size; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 6026 | em = btrfs_new_extent_direct(inode, em, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6027 | if (IS_ERR(em)) { |
| 6028 | ret = PTR_ERR(em); |
| 6029 | goto unlock_err; |
| 6030 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6031 | len = min(len, em->len - (start - em->start)); |
| 6032 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6033 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 6034 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6035 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6036 | bh_result->b_bdev = em->bdev; |
| 6037 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6038 | if (create) { |
| 6039 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6040 | set_buffer_new(bh_result); |
| 6041 | |
| 6042 | /* |
| 6043 | * Need to update the i_size under the extent lock so buffered |
| 6044 | * readers will get the updated i_size when we unlock. |
| 6045 | */ |
| 6046 | if (start + len > i_size_read(inode)) |
| 6047 | i_size_write(inode, start + len); |
| 6048 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6049 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6050 | /* |
| 6051 | * In the case of write we need to clear and unlock the entire range, |
| 6052 | * in the case of read we need to unlock only the end area that we |
| 6053 | * aren't using if there is any left over space. |
| 6054 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6055 | if (lockstart < lockend) { |
| 6056 | if (create && len < lockend - lockstart) { |
| 6057 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6058 | lockstart + len - 1, unlock_bits, 1, 0, |
| 6059 | &cached_state, GFP_NOFS); |
| 6060 | /* |
| 6061 | * Beside unlock, we also need to cleanup reserved space |
| 6062 | * for the left range by attaching EXTENT_DO_ACCOUNTING. |
| 6063 | */ |
| 6064 | clear_extent_bit(&BTRFS_I(inode)->io_tree, |
| 6065 | lockstart + len, lockend, |
| 6066 | unlock_bits | EXTENT_DO_ACCOUNTING, |
| 6067 | 1, 0, NULL, GFP_NOFS); |
| 6068 | } else { |
| 6069 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6070 | lockend, unlock_bits, 1, 0, |
| 6071 | &cached_state, GFP_NOFS); |
| 6072 | } |
| 6073 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6074 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6075 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6076 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6077 | free_extent_map(em); |
| 6078 | |
| 6079 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6080 | |
| 6081 | unlock_err: |
| 6082 | if (create) |
| 6083 | unlock_bits |= EXTENT_DO_ACCOUNTING; |
| 6084 | |
| 6085 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6086 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 6087 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6088 | } |
| 6089 | |
| 6090 | struct btrfs_dio_private { |
| 6091 | struct inode *inode; |
| 6092 | u64 logical_offset; |
| 6093 | u64 disk_bytenr; |
| 6094 | u64 bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6095 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6096 | |
| 6097 | /* number of bios pending for this dio */ |
| 6098 | atomic_t pending_bios; |
| 6099 | |
| 6100 | /* IO errors */ |
| 6101 | int errors; |
| 6102 | |
| 6103 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6104 | }; |
| 6105 | |
| 6106 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 6107 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6108 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6109 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 6110 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6111 | struct inode *inode = dip->inode; |
| 6112 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6113 | u64 start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6114 | |
| 6115 | start = dip->logical_offset; |
| 6116 | do { |
| 6117 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 6118 | struct page *page = bvec->bv_page; |
| 6119 | char *kaddr; |
| 6120 | u32 csum = ~(u32)0; |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6121 | u64 private = ~(u32)0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6122 | unsigned long flags; |
| 6123 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6124 | if (get_state_private(&BTRFS_I(inode)->io_tree, |
| 6125 | start, &private)) |
| 6126 | goto failed; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6127 | local_irq_save(flags); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6128 | kaddr = kmap_atomic(page); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6129 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 6130 | csum, bvec->bv_len); |
| 6131 | btrfs_csum_final(csum, (char *)&csum); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6132 | kunmap_atomic(kaddr); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6133 | local_irq_restore(flags); |
| 6134 | |
| 6135 | flush_dcache_page(bvec->bv_page); |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6136 | if (csum != private) { |
| 6137 | failed: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6138 | printk(KERN_ERR "btrfs csum failed ino %llu off" |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6139 | " %llu csum %u private %u\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6140 | (unsigned long long)btrfs_ino(inode), |
| 6141 | (unsigned long long)start, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6142 | csum, (unsigned)private); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6143 | err = -EIO; |
| 6144 | } |
| 6145 | } |
| 6146 | |
| 6147 | start += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6148 | bvec++; |
| 6149 | } while (bvec <= bvec_end); |
| 6150 | |
| 6151 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6152 | dip->logical_offset + dip->bytes - 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6153 | bio->bi_private = dip->private; |
| 6154 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6155 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6156 | |
| 6157 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 6158 | if (err) |
| 6159 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6160 | dio_end_io(bio, err); |
| 6161 | } |
| 6162 | |
| 6163 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 6164 | { |
| 6165 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6166 | struct inode *inode = dip->inode; |
| 6167 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6168 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6169 | u64 ordered_offset = dip->logical_offset; |
| 6170 | u64 ordered_bytes = dip->bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6171 | int ret; |
| 6172 | |
| 6173 | if (err) |
| 6174 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6175 | again: |
| 6176 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 6177 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6178 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6179 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6180 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6181 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6182 | ordered->work.func = finish_ordered_fn; |
| 6183 | ordered->work.flags = 0; |
| 6184 | btrfs_queue_worker(&root->fs_info->endio_write_workers, |
| 6185 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6186 | out_test: |
| 6187 | /* |
| 6188 | * our bio might span multiple ordered extents. If we haven't |
| 6189 | * completed the accounting for the whole dio, go back and try again |
| 6190 | */ |
| 6191 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 6192 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 6193 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6194 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6195 | goto again; |
| 6196 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6197 | out_done: |
| 6198 | bio->bi_private = dip->private; |
| 6199 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6200 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6201 | |
| 6202 | /* If we had an error make sure to clear the uptodate flag */ |
| 6203 | if (err) |
| 6204 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6205 | dio_end_io(bio, err); |
| 6206 | } |
| 6207 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6208 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 6209 | struct bio *bio, int mirror_num, |
| 6210 | unsigned long bio_flags, u64 offset) |
| 6211 | { |
| 6212 | int ret; |
| 6213 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6214 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6215 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6216 | return 0; |
| 6217 | } |
| 6218 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6219 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 6220 | { |
| 6221 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6222 | |
| 6223 | if (err) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6224 | printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6225 | "sector %#Lx len %u err no %d\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6226 | (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw, |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6227 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6228 | dip->errors = 1; |
| 6229 | |
| 6230 | /* |
| 6231 | * before atomic variable goto zero, we must make sure |
| 6232 | * dip->errors is perceived to be set. |
| 6233 | */ |
| 6234 | smp_mb__before_atomic_dec(); |
| 6235 | } |
| 6236 | |
| 6237 | /* if there are more bios still pending for this dio, just exit */ |
| 6238 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 6239 | goto out; |
| 6240 | |
| 6241 | if (dip->errors) |
| 6242 | bio_io_error(dip->orig_bio); |
| 6243 | else { |
| 6244 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 6245 | bio_endio(dip->orig_bio, 0); |
| 6246 | } |
| 6247 | out: |
| 6248 | bio_put(bio); |
| 6249 | } |
| 6250 | |
| 6251 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 6252 | u64 first_sector, gfp_t gfp_flags) |
| 6253 | { |
| 6254 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 6255 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 6256 | } |
| 6257 | |
| 6258 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 6259 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6260 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6261 | { |
| 6262 | int write = rw & REQ_WRITE; |
| 6263 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6264 | int ret; |
| 6265 | |
| 6266 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6267 | |
| 6268 | if (!write) { |
| 6269 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 6270 | if (ret) |
| 6271 | goto err; |
| 6272 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6273 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6274 | if (skip_sum) |
| 6275 | goto map; |
| 6276 | |
| 6277 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6278 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 6279 | inode, rw, bio, 0, 0, |
| 6280 | file_offset, |
| 6281 | __btrfs_submit_bio_start_direct_io, |
| 6282 | __btrfs_submit_bio_done); |
| 6283 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6284 | } else if (write) { |
| 6285 | /* |
| 6286 | * If we aren't doing async submit, calculate the csum of the |
| 6287 | * bio now. |
| 6288 | */ |
| 6289 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 6290 | if (ret) |
| 6291 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 6292 | } else if (!skip_sum) { |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6293 | ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 6294 | if (ret) |
| 6295 | goto err; |
| 6296 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6297 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6298 | map: |
| 6299 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6300 | err: |
| 6301 | bio_put(bio); |
| 6302 | return ret; |
| 6303 | } |
| 6304 | |
| 6305 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 6306 | int skip_sum) |
| 6307 | { |
| 6308 | struct inode *inode = dip->inode; |
| 6309 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6310 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 6311 | struct bio *bio; |
| 6312 | struct bio *orig_bio = dip->orig_bio; |
| 6313 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 6314 | u64 start_sector = orig_bio->bi_sector; |
| 6315 | u64 file_offset = dip->logical_offset; |
| 6316 | u64 submit_len = 0; |
| 6317 | u64 map_length; |
| 6318 | int nr_pages = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6319 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6320 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6321 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6322 | map_length = orig_bio->bi_size; |
| 6323 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 6324 | &map_length, NULL, 0); |
| 6325 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6326 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6327 | return -EIO; |
| 6328 | } |
| 6329 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6330 | if (map_length >= orig_bio->bi_size) { |
| 6331 | bio = orig_bio; |
| 6332 | goto submit; |
| 6333 | } |
| 6334 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6335 | async_submit = 1; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6336 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 6337 | if (!bio) |
| 6338 | return -ENOMEM; |
| 6339 | bio->bi_private = dip; |
| 6340 | bio->bi_end_io = btrfs_end_dio_bio; |
| 6341 | atomic_inc(&dip->pending_bios); |
| 6342 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6343 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 6344 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 6345 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 6346 | bvec->bv_offset) < bvec->bv_len)) { |
| 6347 | /* |
| 6348 | * inc the count before we submit the bio so |
| 6349 | * we know the end IO handler won't happen before |
| 6350 | * we inc the count. Otherwise, the dip might get freed |
| 6351 | * before we're done setting it up |
| 6352 | */ |
| 6353 | atomic_inc(&dip->pending_bios); |
| 6354 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 6355 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6356 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6357 | if (ret) { |
| 6358 | bio_put(bio); |
| 6359 | atomic_dec(&dip->pending_bios); |
| 6360 | goto out_err; |
| 6361 | } |
| 6362 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6363 | start_sector += submit_len >> 9; |
| 6364 | file_offset += submit_len; |
| 6365 | |
| 6366 | submit_len = 0; |
| 6367 | nr_pages = 0; |
| 6368 | |
| 6369 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 6370 | start_sector, GFP_NOFS); |
| 6371 | if (!bio) |
| 6372 | goto out_err; |
| 6373 | bio->bi_private = dip; |
| 6374 | bio->bi_end_io = btrfs_end_dio_bio; |
| 6375 | |
| 6376 | map_length = orig_bio->bi_size; |
| 6377 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 6378 | &map_length, NULL, 0); |
| 6379 | if (ret) { |
| 6380 | bio_put(bio); |
| 6381 | goto out_err; |
| 6382 | } |
| 6383 | } else { |
| 6384 | submit_len += bvec->bv_len; |
| 6385 | nr_pages ++; |
| 6386 | bvec++; |
| 6387 | } |
| 6388 | } |
| 6389 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6390 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6391 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6392 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6393 | if (!ret) |
| 6394 | return 0; |
| 6395 | |
| 6396 | bio_put(bio); |
| 6397 | out_err: |
| 6398 | dip->errors = 1; |
| 6399 | /* |
| 6400 | * before atomic variable goto zero, we must |
| 6401 | * make sure dip->errors is perceived to be set. |
| 6402 | */ |
| 6403 | smp_mb__before_atomic_dec(); |
| 6404 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 6405 | bio_io_error(dip->orig_bio); |
| 6406 | |
| 6407 | /* bio_end_io() will handle error, so we needn't return it */ |
| 6408 | return 0; |
| 6409 | } |
| 6410 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6411 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 6412 | loff_t file_offset) |
| 6413 | { |
| 6414 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6415 | struct btrfs_dio_private *dip; |
| 6416 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6417 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 6418 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6419 | int ret = 0; |
| 6420 | |
| 6421 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 6422 | |
| 6423 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 6424 | if (!dip) { |
| 6425 | ret = -ENOMEM; |
| 6426 | goto free_ordered; |
| 6427 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6428 | |
| 6429 | dip->private = bio->bi_private; |
| 6430 | dip->inode = inode; |
| 6431 | dip->logical_offset = file_offset; |
| 6432 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6433 | dip->bytes = 0; |
| 6434 | do { |
| 6435 | dip->bytes += bvec->bv_len; |
| 6436 | bvec++; |
| 6437 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 6438 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6439 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6440 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6441 | dip->errors = 0; |
| 6442 | dip->orig_bio = bio; |
| 6443 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6444 | |
| 6445 | if (write) |
| 6446 | bio->bi_end_io = btrfs_endio_direct_write; |
| 6447 | else |
| 6448 | bio->bi_end_io = btrfs_endio_direct_read; |
| 6449 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6450 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 6451 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6452 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6453 | free_ordered: |
| 6454 | /* |
| 6455 | * If this is a write, we need to clean up the reserved space and kill |
| 6456 | * the ordered extent. |
| 6457 | */ |
| 6458 | if (write) { |
| 6459 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 6460 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6461 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 6462 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 6463 | btrfs_free_reserved_extent(root, ordered->start, |
| 6464 | ordered->disk_len); |
| 6465 | btrfs_put_ordered_extent(ordered); |
| 6466 | btrfs_put_ordered_extent(ordered); |
| 6467 | } |
| 6468 | bio_endio(bio, ret); |
| 6469 | } |
| 6470 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6471 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 6472 | const struct iovec *iov, loff_t offset, |
| 6473 | unsigned long nr_segs) |
| 6474 | { |
| 6475 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6476 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6477 | size_t size; |
| 6478 | unsigned long addr; |
| 6479 | unsigned blocksize_mask = root->sectorsize - 1; |
| 6480 | ssize_t retval = -EINVAL; |
| 6481 | loff_t end = offset; |
| 6482 | |
| 6483 | if (offset & blocksize_mask) |
| 6484 | goto out; |
| 6485 | |
| 6486 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 6487 | for (seg = 0; seg < nr_segs; seg++) { |
| 6488 | addr = (unsigned long)iov[seg].iov_base; |
| 6489 | size = iov[seg].iov_len; |
| 6490 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6491 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6492 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6493 | |
| 6494 | /* If this is a write we don't need to check anymore */ |
| 6495 | if (rw & WRITE) |
| 6496 | continue; |
| 6497 | |
| 6498 | /* |
| 6499 | * Check to make sure we don't have duplicate iov_base's in this |
| 6500 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 6501 | * when reading back. |
| 6502 | */ |
| 6503 | for (i = seg + 1; i < nr_segs; i++) { |
| 6504 | if (iov[seg].iov_base == iov[i].iov_base) |
| 6505 | goto out; |
| 6506 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6507 | } |
| 6508 | retval = 0; |
| 6509 | out: |
| 6510 | return retval; |
| 6511 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6512 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6513 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 6514 | const struct iovec *iov, loff_t offset, |
| 6515 | unsigned long nr_segs) |
| 6516 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6517 | struct file *file = iocb->ki_filp; |
| 6518 | struct inode *inode = file->f_mapping->host; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6519 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6520 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6521 | offset, nr_segs)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6522 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6523 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6524 | return __blockdev_direct_IO(rw, iocb, inode, |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6525 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 6526 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
| 6527 | btrfs_submit_direct, 0); |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6528 | } |
| 6529 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6530 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 6531 | __u64 start, __u64 len) |
| 6532 | { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6533 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6534 | } |
| 6535 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6536 | int btrfs_readpage(struct file *file, struct page *page) |
| 6537 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6538 | struct extent_io_tree *tree; |
| 6539 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 6540 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6541 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6542 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6543 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 6544 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6545 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6546 | |
| 6547 | |
| 6548 | if (current->flags & PF_MEMALLOC) { |
| 6549 | redirty_page_for_writepage(wbc, page); |
| 6550 | unlock_page(page); |
| 6551 | return 0; |
| 6552 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6553 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6554 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 6555 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6556 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6557 | int btrfs_writepages(struct address_space *mapping, |
| 6558 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6559 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6560 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6561 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6562 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6563 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 6564 | } |
| 6565 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6566 | static int |
| 6567 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 6568 | struct list_head *pages, unsigned nr_pages) |
| 6569 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6570 | struct extent_io_tree *tree; |
| 6571 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6572 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 6573 | btrfs_get_extent); |
| 6574 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6575 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6576 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6577 | struct extent_io_tree *tree; |
| 6578 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6579 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6580 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6581 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 6582 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6583 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6584 | if (ret == 1) { |
| 6585 | ClearPagePrivate(page); |
| 6586 | set_page_private(page, 0); |
| 6587 | page_cache_release(page); |
| 6588 | } |
| 6589 | return ret; |
| 6590 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6591 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6592 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 6593 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 6594 | if (PageWriteback(page) || PageDirty(page)) |
| 6595 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 6596 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6597 | } |
| 6598 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6599 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 6600 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6601 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6602 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6603 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6604 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6605 | u64 page_start = page_offset(page); |
| 6606 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6607 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6608 | /* |
| 6609 | * we have the page locked, so new writeback can't start, |
| 6610 | * and the dirty bit won't be cleared while we are here. |
| 6611 | * |
| 6612 | * Wait for IO on this page so that we can safely clear |
| 6613 | * the PagePrivate2 bit and do ordered accounting |
| 6614 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6615 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6616 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6617 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6618 | if (offset) { |
| 6619 | btrfs_releasepage(page, GFP_NOFS); |
| 6620 | return; |
| 6621 | } |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6622 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6623 | ordered = btrfs_lookup_ordered_extent(inode, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6624 | page_offset(page)); |
| 6625 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6626 | /* |
| 6627 | * IO on this page will never be started, so we need |
| 6628 | * to account for any ordered extents now |
| 6629 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6630 | clear_extent_bit(tree, page_start, page_end, |
| 6631 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6632 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6633 | &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6634 | /* |
| 6635 | * whoever cleared the private bit is responsible |
| 6636 | * for the finish_ordered_io |
| 6637 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6638 | if (TestClearPagePrivate2(page) && |
| 6639 | btrfs_dec_test_ordered_pending(inode, &ordered, page_start, |
| 6640 | PAGE_CACHE_SIZE, 1)) { |
| 6641 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6642 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6643 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6644 | cached_state = NULL; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6645 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6646 | } |
| 6647 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6648 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6649 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6650 | __btrfs_releasepage(page, GFP_NOFS); |
| 6651 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6652 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6653 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6654 | ClearPagePrivate(page); |
| 6655 | set_page_private(page, 0); |
| 6656 | page_cache_release(page); |
| 6657 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6658 | } |
| 6659 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6660 | /* |
| 6661 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 6662 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 6663 | * be careful to check for EOF conditions here. We set the page up correctly |
| 6664 | * for a written page which means we get ENOSPC checking when writing into |
| 6665 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 6666 | * support these features. |
| 6667 | * |
| 6668 | * We are not allowed to take the i_mutex here so we have to play games to |
| 6669 | * protect against truncate races as the page could now be beyond EOF. Because |
| 6670 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 6671 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 6672 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 6673 | * unlock the page. |
| 6674 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6675 | 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] | 6676 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6677 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 6678 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6679 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6680 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 6681 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6682 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6683 | char *kaddr; |
| 6684 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6685 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6686 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6687 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6688 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6689 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6690 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 6691 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6692 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6693 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6694 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6695 | reserved = 1; |
| 6696 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6697 | if (ret) { |
| 6698 | if (ret == -ENOMEM) |
| 6699 | ret = VM_FAULT_OOM; |
| 6700 | else /* -ENOSPC, -EIO, etc */ |
| 6701 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6702 | if (reserved) |
| 6703 | goto out; |
| 6704 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6705 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6706 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6707 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6708 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6709 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6710 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6711 | page_start = page_offset(page); |
| 6712 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6713 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6714 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6715 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6716 | /* page got truncated out from underneath us */ |
| 6717 | goto out_unlock; |
| 6718 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6719 | wait_on_page_writeback(page); |
| 6720 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6721 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6722 | set_page_extent_mapped(page); |
| 6723 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6724 | /* |
| 6725 | * we can't set the delalloc bits if there are pending ordered |
| 6726 | * extents. Drop our locks and wait for them to finish |
| 6727 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6728 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6729 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6730 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6731 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6732 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6733 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6734 | btrfs_put_ordered_extent(ordered); |
| 6735 | goto again; |
| 6736 | } |
| 6737 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6738 | /* |
| 6739 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 6740 | * if it was already dirty, so for space accounting reasons we need to |
| 6741 | * clear any delalloc bits for the range we are fixing to save. There |
| 6742 | * is probably a better way to do this, but for now keep consistent with |
| 6743 | * prepare_pages in the normal write path. |
| 6744 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6745 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6746 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6747 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6748 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6749 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 6750 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6751 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6752 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6753 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6754 | ret = VM_FAULT_SIGBUS; |
| 6755 | goto out_unlock; |
| 6756 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6757 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6758 | |
| 6759 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6760 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6761 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6762 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6763 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6764 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6765 | if (zero_start != PAGE_CACHE_SIZE) { |
| 6766 | kaddr = kmap(page); |
| 6767 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 6768 | flush_dcache_page(page); |
| 6769 | kunmap(page); |
| 6770 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6771 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6772 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6773 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6774 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6775 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 6776 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 6777 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6778 | 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] | 6779 | |
| 6780 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 6781 | if (!ret) { |
| 6782 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6783 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 6784 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6785 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6786 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6787 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6788 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 6789 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6790 | return ret; |
| 6791 | } |
| 6792 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6793 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6794 | { |
| 6795 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6796 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6797 | int ret; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6798 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6799 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6800 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6801 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 6802 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6803 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 6804 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 6805 | if (ret) |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6806 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6807 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6808 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6809 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6810 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6811 | /* |
| 6812 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 6813 | * 3 things going on here |
| 6814 | * |
| 6815 | * 1) We need to reserve space for our orphan item and the space to |
| 6816 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 6817 | * orphan item because we didn't reserve space to remove it. |
| 6818 | * |
| 6819 | * 2) We need to reserve space to update our inode. |
| 6820 | * |
| 6821 | * 3) We need to have something to cache all the space that is going to |
| 6822 | * be free'd up by the truncate operation, but also have some slack |
| 6823 | * space reserved in case it uses space during the truncate (thank you |
| 6824 | * very much snapshotting). |
| 6825 | * |
| 6826 | * And we need these to all be seperate. The fact is we can use alot of |
| 6827 | * space doing the truncate, and we have no earthly idea how much space |
| 6828 | * we will use, so we need the truncate reservation to be seperate so it |
| 6829 | * doesn't end up using space reserved for updating the inode or |
| 6830 | * removing the orphan item. We also need to be able to stop the |
| 6831 | * transaction and start a new one, which means we need to be able to |
| 6832 | * update the inode several times, and we have no idea of knowing how |
| 6833 | * many times that will be, so we can't just reserve 1 item for the |
| 6834 | * entirety of the opration, so that has to be done seperately as well. |
| 6835 | * Then there is the orphan item, which does indeed need to be held on |
| 6836 | * to for the whole operation, and we need nobody to touch this reserved |
| 6837 | * space except the orphan code. |
| 6838 | * |
| 6839 | * So that leaves us with |
| 6840 | * |
| 6841 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 6842 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 6843 | * transaction reservation. |
| 6844 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 6845 | * updating the inode. |
| 6846 | */ |
| 6847 | rsv = btrfs_alloc_block_rsv(root); |
| 6848 | if (!rsv) |
| 6849 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 6850 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 6851 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6852 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6853 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 6854 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6855 | * 1 for the orphan item we're going to add |
| 6856 | * 1 for the orphan item deletion |
| 6857 | * 1 for updating the inode. |
| 6858 | */ |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6859 | trans = btrfs_start_transaction(root, 4); |
| 6860 | if (IS_ERR(trans)) { |
| 6861 | err = PTR_ERR(trans); |
| 6862 | goto out; |
| 6863 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6864 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6865 | /* Migrate the slack space for the truncate to our reserve */ |
| 6866 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 6867 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6868 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6869 | |
| 6870 | ret = btrfs_orphan_add(trans, inode); |
| 6871 | if (ret) { |
| 6872 | btrfs_end_transaction(trans, root); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6873 | goto out; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6874 | } |
| 6875 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6876 | /* |
| 6877 | * setattr is responsible for setting the ordered_data_close flag, |
| 6878 | * but that is only tested during the last file release. That |
| 6879 | * could happen well after the next commit, leaving a great big |
| 6880 | * window where new writes may get lost if someone chooses to write |
| 6881 | * to this file after truncating to zero |
| 6882 | * |
| 6883 | * The inode doesn't have any dirty data here, and so if we commit |
| 6884 | * this is a noop. If someone immediately starts writing to the inode |
| 6885 | * it is very likely we'll catch some of their writes in this |
| 6886 | * transaction, and the commit will find this file on the ordered |
| 6887 | * data list with good things to send down. |
| 6888 | * |
| 6889 | * This is a best effort solution, there is still a window where |
| 6890 | * using truncate to replace the contents of the file will |
| 6891 | * end up with a zero length file after a crash. |
| 6892 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6893 | if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 6894 | &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6895 | btrfs_add_ordered_operation(trans, root, inode); |
| 6896 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6897 | /* |
| 6898 | * So if we truncate and then write and fsync we normally would just |
| 6899 | * write the extents that changed, which is a problem if we need to |
| 6900 | * first truncate that entire inode. So set this flag so we write out |
| 6901 | * all of the extents in the inode to the sync log so we're completely |
| 6902 | * safe. |
| 6903 | */ |
| 6904 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 6905 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6906 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6907 | while (1) { |
| 6908 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 6909 | inode->i_size, |
| 6910 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 6911 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6912 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6913 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6914 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6915 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6916 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6917 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6918 | if (ret) { |
| 6919 | err = ret; |
| 6920 | break; |
| 6921 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 6922 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6923 | nr = trans->blocks_used; |
| 6924 | btrfs_end_transaction(trans, root); |
| 6925 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 6926 | |
| 6927 | trans = btrfs_start_transaction(root, 2); |
| 6928 | if (IS_ERR(trans)) { |
| 6929 | ret = err = PTR_ERR(trans); |
| 6930 | trans = NULL; |
| 6931 | break; |
| 6932 | } |
| 6933 | |
| 6934 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 6935 | rsv, min_size); |
| 6936 | BUG_ON(ret); /* shouldn't happen */ |
| 6937 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6938 | } |
| 6939 | |
| 6940 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6941 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6942 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6943 | if (ret) |
| 6944 | err = ret; |
Josef Bacik | ded5db9 | 2011-03-04 14:09:46 -0500 | [diff] [blame] | 6945 | } else if (ret && inode->i_nlink > 0) { |
| 6946 | /* |
| 6947 | * Failed to do the truncate, remove us from the in memory |
| 6948 | * orphan list. |
| 6949 | */ |
| 6950 | ret = btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6951 | } |
| 6952 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 6953 | if (trans) { |
| 6954 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 6955 | ret = btrfs_update_inode(trans, root, inode); |
| 6956 | if (ret && !err) |
| 6957 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6958 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 6959 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6960 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 6961 | btrfs_btree_balance_dirty(root, nr); |
| 6962 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6963 | |
| 6964 | out: |
| 6965 | btrfs_free_block_rsv(root, rsv); |
| 6966 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6967 | if (ret && !err) |
| 6968 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6969 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6970 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6971 | } |
| 6972 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6973 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6974 | * create a new subvolume directory/inode (helper for the ioctl). |
| 6975 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6976 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6977 | struct btrfs_root *new_root, u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6978 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6979 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6980 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6981 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6982 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 6983 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 6984 | new_dirid, new_dirid, |
| 6985 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 6986 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6987 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 6988 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6989 | inode->i_op = &btrfs_dir_inode_operations; |
| 6990 | inode->i_fop = &btrfs_dir_file_operations; |
| 6991 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 6992 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6993 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6994 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6995 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6996 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6997 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 6998 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6999 | } |
| 7000 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7001 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 7002 | { |
| 7003 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7004 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7005 | |
| 7006 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 7007 | if (!ei) |
| 7008 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7009 | |
| 7010 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7011 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 7012 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7013 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7014 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7015 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7016 | ei->disk_i_size = 0; |
| 7017 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7018 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7019 | ei->index_cnt = (u64)-1; |
| 7020 | ei->last_unlink_trans = 0; |
| 7021 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7022 | spin_lock_init(&ei->lock); |
| 7023 | ei->outstanding_extents = 0; |
| 7024 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7025 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7026 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7027 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7028 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7029 | ei->delayed_node = NULL; |
| 7030 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7031 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 7032 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 7033 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 7034 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 7035 | ei->io_tree.track_uptodate = 1; |
| 7036 | ei->io_failure_tree.track_uptodate = 1; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7037 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 7038 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7039 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7040 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7041 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7042 | RB_CLEAR_NODE(&ei->rb_node); |
| 7043 | |
| 7044 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7045 | } |
| 7046 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7047 | static void btrfs_i_callback(struct rcu_head *head) |
| 7048 | { |
| 7049 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7050 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 7051 | } |
| 7052 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7053 | void btrfs_destroy_inode(struct inode *inode) |
| 7054 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7055 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7056 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7057 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 7058 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7059 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7060 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 7061 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7062 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 7063 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7064 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7065 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7066 | * This can happen where we create an inode, but somebody else also |
| 7067 | * created the same inode and we need to destroy the one we already |
| 7068 | * created. |
| 7069 | */ |
| 7070 | if (!root) |
| 7071 | goto free; |
| 7072 | |
| 7073 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7074 | * Make sure we're properly removed from the ordered operation |
| 7075 | * lists. |
| 7076 | */ |
| 7077 | smp_mb(); |
| 7078 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 7079 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 7080 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 7081 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 7082 | } |
| 7083 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7084 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 7085 | &BTRFS_I(inode)->runtime_flags)) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7086 | printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n", |
| 7087 | (unsigned long long)btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7088 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7089 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7090 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7091 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7092 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 7093 | if (!ordered) |
| 7094 | break; |
| 7095 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7096 | printk(KERN_ERR "btrfs found ordered " |
| 7097 | "extent %llu %llu on inode cleanup\n", |
| 7098 | (unsigned long long)ordered->file_offset, |
| 7099 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7100 | btrfs_remove_ordered_extent(inode, ordered); |
| 7101 | btrfs_put_ordered_extent(ordered); |
| 7102 | btrfs_put_ordered_extent(ordered); |
| 7103 | } |
| 7104 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7105 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7106 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7107 | free: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7108 | btrfs_remove_delayed_node(inode); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7109 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7110 | } |
| 7111 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7112 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7113 | { |
| 7114 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7115 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7116 | if (btrfs_root_refs(&root->root_item) == 0 && |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 7117 | !btrfs_is_free_space_inode(inode)) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7118 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7119 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7120 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7121 | } |
| 7122 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 7123 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7124 | { |
| 7125 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 7126 | |
| 7127 | inode_init_once(&ei->vfs_inode); |
| 7128 | } |
| 7129 | |
| 7130 | void btrfs_destroy_cachep(void) |
| 7131 | { |
| 7132 | if (btrfs_inode_cachep) |
| 7133 | kmem_cache_destroy(btrfs_inode_cachep); |
| 7134 | if (btrfs_trans_handle_cachep) |
| 7135 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 7136 | if (btrfs_transaction_cachep) |
| 7137 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7138 | if (btrfs_path_cachep) |
| 7139 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 7140 | if (btrfs_free_space_cachep) |
| 7141 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7142 | } |
| 7143 | |
| 7144 | int btrfs_init_cachep(void) |
| 7145 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7146 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 7147 | sizeof(struct btrfs_inode), 0, |
| 7148 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7149 | if (!btrfs_inode_cachep) |
| 7150 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7151 | |
| 7152 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 7153 | sizeof(struct btrfs_trans_handle), 0, |
| 7154 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7155 | if (!btrfs_trans_handle_cachep) |
| 7156 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7157 | |
| 7158 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 7159 | sizeof(struct btrfs_transaction), 0, |
| 7160 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7161 | if (!btrfs_transaction_cachep) |
| 7162 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7163 | |
| 7164 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 7165 | sizeof(struct btrfs_path), 0, |
| 7166 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7167 | if (!btrfs_path_cachep) |
| 7168 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7169 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 7170 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache", |
| 7171 | sizeof(struct btrfs_free_space), 0, |
| 7172 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 7173 | if (!btrfs_free_space_cachep) |
| 7174 | goto fail; |
| 7175 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7176 | return 0; |
| 7177 | fail: |
| 7178 | btrfs_destroy_cachep(); |
| 7179 | return -ENOMEM; |
| 7180 | } |
| 7181 | |
| 7182 | static int btrfs_getattr(struct vfsmount *mnt, |
| 7183 | struct dentry *dentry, struct kstat *stat) |
| 7184 | { |
| 7185 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 7186 | u32 blocksize = inode->i_sb->s_blocksize; |
| 7187 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7188 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 7189 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 7190 | stat->blksize = PAGE_CACHE_SIZE; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 7191 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
| 7192 | ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7193 | return 0; |
| 7194 | } |
| 7195 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7196 | /* |
| 7197 | * If a file is moved, it will inherit the cow and compression flags of the new |
| 7198 | * directory. |
| 7199 | */ |
| 7200 | static void fixup_inode_flags(struct inode *dir, struct inode *inode) |
| 7201 | { |
| 7202 | struct btrfs_inode *b_dir = BTRFS_I(dir); |
| 7203 | struct btrfs_inode *b_inode = BTRFS_I(inode); |
| 7204 | |
| 7205 | if (b_dir->flags & BTRFS_INODE_NODATACOW) |
| 7206 | b_inode->flags |= BTRFS_INODE_NODATACOW; |
| 7207 | else |
| 7208 | b_inode->flags &= ~BTRFS_INODE_NODATACOW; |
| 7209 | |
Liu Bo | bc17823 | 2012-06-14 02:23:18 -0600 | [diff] [blame] | 7210 | if (b_dir->flags & BTRFS_INODE_COMPRESS) { |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7211 | b_inode->flags |= BTRFS_INODE_COMPRESS; |
Liu Bo | bc17823 | 2012-06-14 02:23:18 -0600 | [diff] [blame] | 7212 | b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 7213 | } else { |
| 7214 | b_inode->flags &= ~(BTRFS_INODE_COMPRESS | |
| 7215 | BTRFS_INODE_NOCOMPRESS); |
| 7216 | } |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7217 | } |
| 7218 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7219 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 7220 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7221 | { |
| 7222 | struct btrfs_trans_handle *trans; |
| 7223 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7224 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7225 | struct inode *new_inode = new_dentry->d_inode; |
| 7226 | struct inode *old_inode = old_dentry->d_inode; |
| 7227 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7228 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7229 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7230 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7231 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7232 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7233 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7234 | return -EPERM; |
| 7235 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7236 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7237 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7238 | return -EXDEV; |
| 7239 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7240 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 7241 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7242 | return -ENOTEMPTY; |
| 7243 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7244 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7245 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7246 | return -ENOTEMPTY; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7247 | /* |
| 7248 | * we're using rename to replace one file with another. |
| 7249 | * and the replacement file is large. Start IO on it now so |
| 7250 | * we don't add too much work to the end of the transaction |
| 7251 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 7252 | 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] | 7253 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 7254 | filemap_flush(old_inode->i_mapping); |
| 7255 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7256 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7257 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7258 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7259 | /* |
| 7260 | * We want to reserve the absolute worst case amount of items. So if |
| 7261 | * both inodes are subvols and we need to unlink them then that would |
| 7262 | * require 4 item modifications, but if they are both normal inodes it |
| 7263 | * would require 5 item modifications, so we'll assume their normal |
| 7264 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 7265 | * should cover the worst case number of items we'll modify. |
| 7266 | */ |
| 7267 | trans = btrfs_start_transaction(root, 20); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 7268 | if (IS_ERR(trans)) { |
| 7269 | ret = PTR_ERR(trans); |
| 7270 | goto out_notrans; |
| 7271 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7272 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7273 | if (dest != root) |
| 7274 | btrfs_record_root_in_trans(trans, dest); |
| 7275 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7276 | ret = btrfs_set_inode_index(new_dir, &index); |
| 7277 | if (ret) |
| 7278 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7279 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7280 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7281 | /* force full log commit if subvolume involved. */ |
| 7282 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 7283 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7284 | ret = btrfs_insert_inode_ref(trans, dest, |
| 7285 | new_dentry->d_name.name, |
| 7286 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7287 | old_ino, |
| 7288 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7289 | if (ret) |
| 7290 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7291 | /* |
| 7292 | * this is an ugly little race, but the rename is required |
| 7293 | * to make sure that if we crash, the inode is either at the |
| 7294 | * old name or the new one. pinning the log transaction lets |
| 7295 | * us make sure we don't allow a log commit to come in after |
| 7296 | * we unlink the name but before we add the new name back in. |
| 7297 | */ |
| 7298 | btrfs_pin_log_trans(root); |
| 7299 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7300 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7301 | * make sure the inode gets flushed if it is replacing |
| 7302 | * something. |
| 7303 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7304 | 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] | 7305 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7306 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 7307 | inode_inc_iversion(old_dir); |
| 7308 | inode_inc_iversion(new_dir); |
| 7309 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7310 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 7311 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 7312 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7313 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7314 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 7315 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 7316 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7317 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7318 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 7319 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 7320 | old_dentry->d_name.name, |
| 7321 | old_dentry->d_name.len); |
| 7322 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 7323 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 7324 | old_dentry->d_inode, |
| 7325 | old_dentry->d_name.name, |
| 7326 | old_dentry->d_name.len); |
| 7327 | if (!ret) |
| 7328 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7329 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7330 | if (ret) { |
| 7331 | btrfs_abort_transaction(trans, root, ret); |
| 7332 | goto out_fail; |
| 7333 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7334 | |
| 7335 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 7336 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7337 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7338 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7339 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 7340 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 7341 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 7342 | root_objectid, |
| 7343 | new_dentry->d_name.name, |
| 7344 | new_dentry->d_name.len); |
| 7345 | BUG_ON(new_inode->i_nlink == 0); |
| 7346 | } else { |
| 7347 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 7348 | new_dentry->d_inode, |
| 7349 | new_dentry->d_name.name, |
| 7350 | new_dentry->d_name.len); |
| 7351 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7352 | if (!ret && new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7353 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7354 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7355 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7356 | if (ret) { |
| 7357 | btrfs_abort_transaction(trans, root, ret); |
| 7358 | goto out_fail; |
| 7359 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7360 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7361 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7362 | fixup_inode_flags(new_dir, old_inode); |
| 7363 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7364 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 7365 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7366 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7367 | if (ret) { |
| 7368 | btrfs_abort_transaction(trans, root, ret); |
| 7369 | goto out_fail; |
| 7370 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7371 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7372 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 7373 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 7374 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7375 | btrfs_end_log_trans(root); |
| 7376 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7377 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7378 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 7379 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7380 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7381 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7382 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7383 | return ret; |
| 7384 | } |
| 7385 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7386 | /* |
| 7387 | * some fairly slow code that needs optimization. This walks the list |
| 7388 | * of all the inodes with pending delalloc and forces them to disk. |
| 7389 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7390 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7391 | { |
| 7392 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 7393 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7394 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7395 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7396 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 7397 | return -EROFS; |
| 7398 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7399 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7400 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7401 | binode = list_entry(head->next, struct btrfs_inode, |
| 7402 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7403 | inode = igrab(&binode->vfs_inode); |
| 7404 | if (!inode) |
| 7405 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7406 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7407 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7408 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7409 | if (delay_iput) |
| 7410 | btrfs_add_delayed_iput(inode); |
| 7411 | else |
| 7412 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7413 | } |
| 7414 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7415 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7416 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7417 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7418 | |
| 7419 | /* the filemap_flush will queue IO into the worker threads, but |
| 7420 | * we have to make sure the IO is actually started and that |
| 7421 | * ordered extents get created before we return |
| 7422 | */ |
| 7423 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7424 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7425 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7426 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7427 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 7428 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7429 | } |
| 7430 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7431 | return 0; |
| 7432 | } |
| 7433 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7434 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 7435 | const char *symname) |
| 7436 | { |
| 7437 | struct btrfs_trans_handle *trans; |
| 7438 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 7439 | struct btrfs_path *path; |
| 7440 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7441 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7442 | int err; |
| 7443 | int drop_inode = 0; |
| 7444 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7445 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7446 | int name_len; |
| 7447 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7448 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7449 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7450 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7451 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7452 | |
| 7453 | name_len = strlen(symname) + 1; |
| 7454 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 7455 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7456 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7457 | /* |
| 7458 | * 2 items for inode item and ref |
| 7459 | * 2 items for dir items |
| 7460 | * 1 item for xattr if selinux is on |
| 7461 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7462 | trans = btrfs_start_transaction(root, 5); |
| 7463 | if (IS_ERR(trans)) |
| 7464 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7465 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 7466 | err = btrfs_find_free_ino(root, &objectid); |
| 7467 | if (err) |
| 7468 | goto out_unlock; |
| 7469 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7470 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7471 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7472 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7473 | if (IS_ERR(inode)) { |
| 7474 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7475 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7476 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7477 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 7478 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7479 | if (err) { |
| 7480 | drop_inode = 1; |
| 7481 | goto out_unlock; |
| 7482 | } |
| 7483 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 7484 | /* |
| 7485 | * If the active LSM wants to access the inode during |
| 7486 | * d_instantiate it needs these. Smack checks to see |
| 7487 | * if the filesystem supports xattrs by looking at the |
| 7488 | * ops vector. |
| 7489 | */ |
| 7490 | inode->i_fop = &btrfs_file_operations; |
| 7491 | inode->i_op = &btrfs_file_inode_operations; |
| 7492 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 7493 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7494 | if (err) |
| 7495 | drop_inode = 1; |
| 7496 | else { |
| 7497 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7498 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7499 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7500 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7501 | if (drop_inode) |
| 7502 | goto out_unlock; |
| 7503 | |
| 7504 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 7505 | if (!path) { |
| 7506 | err = -ENOMEM; |
| 7507 | drop_inode = 1; |
| 7508 | goto out_unlock; |
| 7509 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7510 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7511 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7512 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 7513 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 7514 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 7515 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7516 | if (err) { |
| 7517 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 7518 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7519 | goto out_unlock; |
| 7520 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7521 | leaf = path->nodes[0]; |
| 7522 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 7523 | struct btrfs_file_extent_item); |
| 7524 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 7525 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7526 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7527 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 7528 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 7529 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 7530 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 7531 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7532 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7533 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 7534 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7535 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7536 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7537 | inode->i_op = &btrfs_symlink_inode_operations; |
| 7538 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7539 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7540 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7541 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7542 | err = btrfs_update_inode(trans, root, inode); |
| 7543 | if (err) |
| 7544 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7545 | |
| 7546 | out_unlock: |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 7547 | if (!err) |
| 7548 | d_instantiate(dentry, inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7549 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7550 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7551 | if (drop_inode) { |
| 7552 | inode_dec_link_count(inode); |
| 7553 | iput(inode); |
| 7554 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7555 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7556 | return err; |
| 7557 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7558 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7559 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7560 | u64 start, u64 num_bytes, u64 min_size, |
| 7561 | loff_t actual_len, u64 *alloc_hint, |
| 7562 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7563 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7564 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 7565 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7566 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7567 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7568 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7569 | u64 i_size; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7570 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7571 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7572 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7573 | if (trans) |
| 7574 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7575 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7576 | if (own_trans) { |
| 7577 | trans = btrfs_start_transaction(root, 3); |
| 7578 | if (IS_ERR(trans)) { |
| 7579 | ret = PTR_ERR(trans); |
| 7580 | break; |
| 7581 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7582 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7583 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7584 | ret = btrfs_reserve_extent(trans, root, num_bytes, min_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 7585 | 0, *alloc_hint, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7586 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7587 | if (own_trans) |
| 7588 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7589 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7590 | } |
| 7591 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7592 | ret = insert_reserved_file_extent(trans, inode, |
| 7593 | cur_offset, ins.objectid, |
| 7594 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 7595 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7596 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7597 | if (ret) { |
| 7598 | btrfs_abort_transaction(trans, root, ret); |
| 7599 | if (own_trans) |
| 7600 | btrfs_end_transaction(trans, root); |
| 7601 | break; |
| 7602 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 7603 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7604 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7605 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7606 | em = alloc_extent_map(); |
| 7607 | if (!em) { |
| 7608 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 7609 | &BTRFS_I(inode)->runtime_flags); |
| 7610 | goto next; |
| 7611 | } |
| 7612 | |
| 7613 | em->start = cur_offset; |
| 7614 | em->orig_start = cur_offset; |
| 7615 | em->len = ins.offset; |
| 7616 | em->block_start = ins.objectid; |
| 7617 | em->block_len = ins.offset; |
| 7618 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 7619 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 7620 | em->generation = trans->transid; |
| 7621 | |
| 7622 | while (1) { |
| 7623 | write_lock(&em_tree->lock); |
| 7624 | ret = add_extent_mapping(em_tree, em); |
| 7625 | if (!ret) |
| 7626 | list_move(&em->list, |
| 7627 | &em_tree->modified_extents); |
| 7628 | write_unlock(&em_tree->lock); |
| 7629 | if (ret != -EEXIST) |
| 7630 | break; |
| 7631 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7632 | cur_offset + ins.offset - 1, |
| 7633 | 0); |
| 7634 | } |
| 7635 | free_extent_map(em); |
| 7636 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7637 | num_bytes -= ins.offset; |
| 7638 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7639 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7640 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 7641 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7642 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7643 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7644 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7645 | (actual_len > inode->i_size) && |
| 7646 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7647 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7648 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7649 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7650 | i_size = cur_offset; |
| 7651 | i_size_write(inode, i_size); |
| 7652 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7653 | } |
| 7654 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7655 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7656 | |
| 7657 | if (ret) { |
| 7658 | btrfs_abort_transaction(trans, root, ret); |
| 7659 | if (own_trans) |
| 7660 | btrfs_end_transaction(trans, root); |
| 7661 | break; |
| 7662 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7663 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7664 | if (own_trans) |
| 7665 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7666 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7667 | return ret; |
| 7668 | } |
| 7669 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7670 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7671 | u64 start, u64 num_bytes, u64 min_size, |
| 7672 | loff_t actual_len, u64 *alloc_hint) |
| 7673 | { |
| 7674 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7675 | min_size, actual_len, alloc_hint, |
| 7676 | NULL); |
| 7677 | } |
| 7678 | |
| 7679 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 7680 | struct btrfs_trans_handle *trans, int mode, |
| 7681 | u64 start, u64 num_bytes, u64 min_size, |
| 7682 | loff_t actual_len, u64 *alloc_hint) |
| 7683 | { |
| 7684 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7685 | min_size, actual_len, alloc_hint, trans); |
| 7686 | } |
| 7687 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7688 | static int btrfs_set_page_dirty(struct page *page) |
| 7689 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7690 | return __set_page_dirty_nobuffers(page); |
| 7691 | } |
| 7692 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 7693 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7694 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7695 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 7696 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7697 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 7698 | if (mask & MAY_WRITE && |
| 7699 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 7700 | if (btrfs_root_readonly(root)) |
| 7701 | return -EROFS; |
| 7702 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 7703 | return -EACCES; |
| 7704 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 7705 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7706 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7707 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7708 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7709 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7710 | .lookup = btrfs_lookup, |
| 7711 | .create = btrfs_create, |
| 7712 | .unlink = btrfs_unlink, |
| 7713 | .link = btrfs_link, |
| 7714 | .mkdir = btrfs_mkdir, |
| 7715 | .rmdir = btrfs_rmdir, |
| 7716 | .rename = btrfs_rename, |
| 7717 | .symlink = btrfs_symlink, |
| 7718 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7719 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7720 | .setxattr = btrfs_setxattr, |
| 7721 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7722 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7723 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7724 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7725 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7726 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7727 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7728 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7729 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7730 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7731 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7732 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 7733 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7734 | .llseek = generic_file_llseek, |
| 7735 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 7736 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7737 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7738 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7739 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7740 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 7741 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7742 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7743 | }; |
| 7744 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7745 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7746 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 7747 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7748 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7749 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7750 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7751 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 7752 | .set_bit_hook = btrfs_set_bit_hook, |
| 7753 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7754 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 7755 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7756 | }; |
| 7757 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 7758 | /* |
| 7759 | * btrfs doesn't support the bmap operation because swapfiles |
| 7760 | * use bmap to make a mapping of extents in the file. They assume |
| 7761 | * these extents won't change over the life of the file and they |
| 7762 | * use the bmap result to do IO directly to the drive. |
| 7763 | * |
| 7764 | * the btrfs bmap call would return logical addresses that aren't |
| 7765 | * suitable for IO and they also will change frequently as COW |
| 7766 | * operations happen. So, swapfile + btrfs == corruption. |
| 7767 | * |
| 7768 | * For now we're avoiding this by dropping bmap. |
| 7769 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7770 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7771 | .readpage = btrfs_readpage, |
| 7772 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7773 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7774 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7775 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7776 | .invalidatepage = btrfs_invalidatepage, |
| 7777 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7778 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 7779 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7780 | }; |
| 7781 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7782 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7783 | .readpage = btrfs_readpage, |
| 7784 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 7785 | .invalidatepage = btrfs_invalidatepage, |
| 7786 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7787 | }; |
| 7788 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7789 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7790 | .getattr = btrfs_getattr, |
| 7791 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7792 | .setxattr = btrfs_setxattr, |
| 7793 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7794 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7795 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7796 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7797 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7798 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 7799 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7800 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7801 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7802 | .getattr = btrfs_getattr, |
| 7803 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7804 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7805 | .setxattr = btrfs_setxattr, |
| 7806 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7807 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7808 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7809 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 7810 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7811 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7812 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7813 | .readlink = generic_readlink, |
| 7814 | .follow_link = page_follow_link_light, |
| 7815 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 7816 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 7817 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7818 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 7819 | .setxattr = btrfs_setxattr, |
| 7820 | .getxattr = btrfs_getxattr, |
| 7821 | .listxattr = btrfs_listxattr, |
| 7822 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7823 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 7824 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7825 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7826 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 7827 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7828 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 7829 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7830 | }; |