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> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 41 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 42 | #include "ctree.h" |
| 43 | #include "disk-io.h" |
| 44 | #include "transaction.h" |
| 45 | #include "btrfs_inode.h" |
| 46 | #include "ioctl.h" |
| 47 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 48 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 49 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 50 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 51 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 52 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 53 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 54 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 55 | #include "inode-map.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 56 | |
| 57 | struct btrfs_iget_args { |
| 58 | u64 ino; |
| 59 | struct btrfs_root *root; |
| 60 | }; |
| 61 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 62 | static const struct inode_operations btrfs_dir_inode_operations; |
| 63 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 64 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 65 | static const struct inode_operations btrfs_special_inode_operations; |
| 66 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 67 | static const struct address_space_operations btrfs_aops; |
| 68 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 69 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 70 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 71 | |
| 72 | static struct kmem_cache *btrfs_inode_cachep; |
| 73 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 74 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 75 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 76 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 77 | |
| 78 | #define S_SHIFT 12 |
| 79 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 80 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 81 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 82 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 83 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 84 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 85 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 86 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 87 | }; |
| 88 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 89 | static int btrfs_setsize(struct inode *inode, loff_t newsize); |
| 90 | static int btrfs_truncate(struct inode *inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 91 | static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 92 | static noinline int cow_file_range(struct inode *inode, |
| 93 | struct page *locked_page, |
| 94 | u64 start, u64 end, int *page_started, |
| 95 | unsigned long *nr_written, int unlock); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 96 | static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 97 | struct btrfs_root *root, struct inode *inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 98 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 99 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 100 | struct inode *inode, struct inode *dir, |
| 101 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 102 | { |
| 103 | int err; |
| 104 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 105 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 106 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 107 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 108 | return err; |
| 109 | } |
| 110 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 111 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 112 | * this does all the hard work for inserting an inline extent into |
| 113 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 114 | * no overlapping inline items exist in the btree |
| 115 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 116 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 117 | struct btrfs_root *root, struct inode *inode, |
| 118 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 119 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 120 | struct page **compressed_pages) |
| 121 | { |
| 122 | struct btrfs_key key; |
| 123 | struct btrfs_path *path; |
| 124 | struct extent_buffer *leaf; |
| 125 | struct page *page = NULL; |
| 126 | char *kaddr; |
| 127 | unsigned long ptr; |
| 128 | struct btrfs_file_extent_item *ei; |
| 129 | int err = 0; |
| 130 | int ret; |
| 131 | size_t cur_size = size; |
| 132 | size_t datasize; |
| 133 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 134 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 135 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 136 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 137 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 138 | path = btrfs_alloc_path(); |
| 139 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 140 | return -ENOMEM; |
| 141 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 142 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 143 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 144 | key.objectid = btrfs_ino(inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 145 | key.offset = start; |
| 146 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 147 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 148 | |
| 149 | inode_add_bytes(inode, size); |
| 150 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 151 | datasize); |
| 152 | BUG_ON(ret); |
| 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 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 175 | kaddr = kmap_atomic(cpage, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 176 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 177 | kunmap_atomic(kaddr, KM_USER0); |
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); |
| 189 | kaddr = kmap_atomic(page, KM_USER0); |
| 190 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 191 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
| 192 | kunmap_atomic(kaddr, KM_USER0); |
| 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; |
| 208 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 209 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 210 | return 0; |
| 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 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 250 | ret = btrfs_drop_extents(trans, inode, start, aligned_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 251 | &hint_byte, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 252 | BUG_ON(ret); |
| 253 | |
| 254 | if (isize > actual_end) |
| 255 | inline_len = min_t(u64, isize, actual_end); |
| 256 | ret = insert_inline_extent(trans, root, inode, start, |
| 257 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 258 | compress_type, compressed_pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 259 | BUG_ON(ret); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 260 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 261 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 265 | struct async_extent { |
| 266 | u64 start; |
| 267 | u64 ram_size; |
| 268 | u64 compressed_size; |
| 269 | struct page **pages; |
| 270 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 271 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 272 | struct list_head list; |
| 273 | }; |
| 274 | |
| 275 | struct async_cow { |
| 276 | struct inode *inode; |
| 277 | struct btrfs_root *root; |
| 278 | struct page *locked_page; |
| 279 | u64 start; |
| 280 | u64 end; |
| 281 | struct list_head extents; |
| 282 | struct btrfs_work work; |
| 283 | }; |
| 284 | |
| 285 | static noinline int add_async_extent(struct async_cow *cow, |
| 286 | u64 start, u64 ram_size, |
| 287 | u64 compressed_size, |
| 288 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 289 | unsigned long nr_pages, |
| 290 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 291 | { |
| 292 | struct async_extent *async_extent; |
| 293 | |
| 294 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Yoshinori Sano | dac97e51 | 2011-02-15 12:01:42 +0000 | [diff] [blame] | 295 | BUG_ON(!async_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 296 | async_extent->start = start; |
| 297 | async_extent->ram_size = ram_size; |
| 298 | async_extent->compressed_size = compressed_size; |
| 299 | async_extent->pages = pages; |
| 300 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 301 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 302 | list_add_tail(&async_extent->list, &cow->extents); |
| 303 | return 0; |
| 304 | } |
| 305 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 306 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 307 | * we create compressed extents in two phases. The first |
| 308 | * phase compresses a range of pages that have already been |
| 309 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 310 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 311 | * This is done inside an ordered work queue, and the compression |
| 312 | * is spread across many cpus. The actual IO submission is step |
| 313 | * two, and the ordered work queue takes care of making sure that |
| 314 | * happens in the same order things were put onto the queue by |
| 315 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 316 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 317 | * If this code finds it can't get good compression, it puts an |
| 318 | * entry onto the work queue to write the uncompressed bytes. This |
| 319 | * makes sure that both compressed inodes and uncompressed inodes |
| 320 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 321 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 322 | static noinline int compress_file_range(struct inode *inode, |
| 323 | struct page *locked_page, |
| 324 | u64 start, u64 end, |
| 325 | struct async_cow *async_cow, |
| 326 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 327 | { |
| 328 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 329 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 330 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 331 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 332 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 333 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 334 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 335 | struct page **pages = NULL; |
| 336 | unsigned long nr_pages; |
| 337 | unsigned long nr_pages_ret = 0; |
| 338 | unsigned long total_compressed = 0; |
| 339 | unsigned long total_in = 0; |
| 340 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 341 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 342 | int i; |
| 343 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 344 | int compress_type = root->fs_info->compress_type; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 345 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 346 | /* if this is a small write inside eof, kick off a defragbot */ |
| 347 | if (end <= BTRFS_I(inode)->disk_i_size && (end - start + 1) < 16 * 1024) |
| 348 | btrfs_add_inode_defrag(NULL, inode); |
| 349 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 350 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 351 | again: |
| 352 | will_compress = 0; |
| 353 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 354 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 355 | |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 356 | /* |
| 357 | * we don't want to send crud past the end of i_size through |
| 358 | * compression, that's just a waste of CPU time. So, if the |
| 359 | * end of the file is before the start of our current |
| 360 | * requested range of bytes, we bail out to the uncompressed |
| 361 | * cleanup code that can deal with all of this. |
| 362 | * |
| 363 | * It isn't really the fastest way to fix things, but this is a |
| 364 | * very uncommon corner. |
| 365 | */ |
| 366 | if (actual_end <= start) |
| 367 | goto cleanup_and_bail_uncompressed; |
| 368 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 369 | total_compressed = actual_end - start; |
| 370 | |
| 371 | /* we want to make sure that amount of ram required to uncompress |
| 372 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 373 | * of a compressed extent to 128k. This is a crucial number |
| 374 | * because it also controls how easily we can spread reads across |
| 375 | * cpus for decompression. |
| 376 | * |
| 377 | * We also want to make sure the amount of IO required to do |
| 378 | * a random read is reasonably small, so we limit the size of |
| 379 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 380 | */ |
| 381 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 382 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 383 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 384 | total_in = 0; |
| 385 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 386 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 387 | /* |
| 388 | * we do compression for mount -o compress and when the |
| 389 | * inode has not been flagged as nocompress. This flag can |
| 390 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 391 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 392 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 393 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 394 | (BTRFS_I(inode)->force_compress) || |
| 395 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 396 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 397 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 398 | if (!pages) { |
| 399 | /* just bail out to the uncompressed code */ |
| 400 | goto cont; |
| 401 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 402 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 403 | if (BTRFS_I(inode)->force_compress) |
| 404 | compress_type = BTRFS_I(inode)->force_compress; |
| 405 | |
| 406 | ret = btrfs_compress_pages(compress_type, |
| 407 | inode->i_mapping, start, |
| 408 | total_compressed, pages, |
| 409 | nr_pages, &nr_pages_ret, |
| 410 | &total_in, |
| 411 | &total_compressed, |
| 412 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 413 | |
| 414 | if (!ret) { |
| 415 | unsigned long offset = total_compressed & |
| 416 | (PAGE_CACHE_SIZE - 1); |
| 417 | struct page *page = pages[nr_pages_ret - 1]; |
| 418 | char *kaddr; |
| 419 | |
| 420 | /* zero the tail end of the last page, we might be |
| 421 | * sending it down to disk |
| 422 | */ |
| 423 | if (offset) { |
| 424 | kaddr = kmap_atomic(page, KM_USER0); |
| 425 | memset(kaddr + offset, 0, |
| 426 | PAGE_CACHE_SIZE - offset); |
| 427 | kunmap_atomic(kaddr, KM_USER0); |
| 428 | } |
| 429 | will_compress = 1; |
| 430 | } |
| 431 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 432 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 433 | if (start == 0) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 434 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 435 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 436 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 437 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 438 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 439 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 440 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 441 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 442 | */ |
| 443 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 444 | start, end, 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 445 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 446 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 447 | ret = cow_file_range_inline(trans, root, inode, |
| 448 | start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 449 | total_compressed, |
| 450 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 451 | } |
| 452 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 453 | /* |
| 454 | * inline extent creation worked, we don't need |
| 455 | * to create any more async work items. Unlock |
| 456 | * and free up our temp pages. |
| 457 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 458 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 459 | &BTRFS_I(inode)->io_tree, |
| 460 | start, end, NULL, |
| 461 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 462 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 463 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 464 | |
| 465 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 466 | goto free_pages_out; |
| 467 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 468 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | if (will_compress) { |
| 472 | /* |
| 473 | * we aren't doing an inline extent round the compressed size |
| 474 | * up to a block size boundary so the allocator does sane |
| 475 | * things |
| 476 | */ |
| 477 | total_compressed = (total_compressed + blocksize - 1) & |
| 478 | ~(blocksize - 1); |
| 479 | |
| 480 | /* |
| 481 | * one last check to make sure the compression is really a |
| 482 | * win, compare the page count read with the blocks on disk |
| 483 | */ |
| 484 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 485 | ~(PAGE_CACHE_SIZE - 1); |
| 486 | if (total_compressed >= total_in) { |
| 487 | will_compress = 0; |
| 488 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 489 | num_bytes = total_in; |
| 490 | } |
| 491 | } |
| 492 | if (!will_compress && pages) { |
| 493 | /* |
| 494 | * the compression code ran but failed to make things smaller, |
| 495 | * free any pages it allocated and our page pointer array |
| 496 | */ |
| 497 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 498 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 499 | page_cache_release(pages[i]); |
| 500 | } |
| 501 | kfree(pages); |
| 502 | pages = NULL; |
| 503 | total_compressed = 0; |
| 504 | nr_pages_ret = 0; |
| 505 | |
| 506 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 507 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 508 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 509 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 510 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 511 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 512 | if (will_compress) { |
| 513 | *num_added += 1; |
| 514 | |
| 515 | /* the async work queues will take care of doing actual |
| 516 | * allocation on disk for these compressed pages, |
| 517 | * and will submit them to the elevator. |
| 518 | */ |
| 519 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 520 | total_compressed, pages, nr_pages_ret, |
| 521 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 522 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 523 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 524 | start += num_bytes; |
| 525 | pages = NULL; |
| 526 | cond_resched(); |
| 527 | goto again; |
| 528 | } |
| 529 | } else { |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 530 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 531 | /* |
| 532 | * No compression, but we still need to write the pages in |
| 533 | * the file we've been given so far. redirty the locked |
| 534 | * page if it corresponds to our extent and set things up |
| 535 | * for the async work queue to run cow_file_range to do |
| 536 | * the normal delalloc dance |
| 537 | */ |
| 538 | if (page_offset(locked_page) >= start && |
| 539 | page_offset(locked_page) <= end) { |
| 540 | __set_page_dirty_nobuffers(locked_page); |
| 541 | /* unlocked later on in the async handlers */ |
| 542 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 543 | add_async_extent(async_cow, start, end - start + 1, |
| 544 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 545 | *num_added += 1; |
| 546 | } |
| 547 | |
| 548 | out: |
| 549 | return 0; |
| 550 | |
| 551 | free_pages_out: |
| 552 | for (i = 0; i < nr_pages_ret; i++) { |
| 553 | WARN_ON(pages[i]->mapping); |
| 554 | page_cache_release(pages[i]); |
| 555 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 556 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 557 | |
| 558 | goto out; |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * phase two of compressed writeback. This is the ordered portion |
| 563 | * of the code, which only gets called in the order the work was |
| 564 | * queued. We walk all the async extents created by compress_file_range |
| 565 | * and send them down to the disk. |
| 566 | */ |
| 567 | static noinline int submit_compressed_extents(struct inode *inode, |
| 568 | struct async_cow *async_cow) |
| 569 | { |
| 570 | struct async_extent *async_extent; |
| 571 | u64 alloc_hint = 0; |
| 572 | struct btrfs_trans_handle *trans; |
| 573 | struct btrfs_key ins; |
| 574 | struct extent_map *em; |
| 575 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 576 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 577 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 578 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 579 | |
| 580 | if (list_empty(&async_cow->extents)) |
| 581 | return 0; |
| 582 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 583 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 584 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 585 | async_extent = list_entry(async_cow->extents.next, |
| 586 | struct async_extent, list); |
| 587 | list_del(&async_extent->list); |
| 588 | |
| 589 | io_tree = &BTRFS_I(inode)->io_tree; |
| 590 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 591 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 592 | /* did the compression code fall back to uncompressed IO? */ |
| 593 | if (!async_extent->pages) { |
| 594 | int page_started = 0; |
| 595 | unsigned long nr_written = 0; |
| 596 | |
| 597 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 598 | async_extent->start + |
| 599 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 600 | |
| 601 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 602 | ret = cow_file_range(inode, async_cow->locked_page, |
| 603 | async_extent->start, |
| 604 | async_extent->start + |
| 605 | async_extent->ram_size - 1, |
| 606 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 607 | |
| 608 | /* |
| 609 | * if page_started, cow_file_range inserted an |
| 610 | * inline extent and took care of all the unlocking |
| 611 | * and IO for us. Otherwise, we need to submit |
| 612 | * all those pages down to the drive. |
| 613 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 614 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 615 | extent_write_locked_range(io_tree, |
| 616 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 617 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 618 | async_extent->ram_size - 1, |
| 619 | btrfs_get_extent, |
| 620 | WB_SYNC_ALL); |
| 621 | kfree(async_extent); |
| 622 | cond_resched(); |
| 623 | continue; |
| 624 | } |
| 625 | |
| 626 | lock_extent(io_tree, async_extent->start, |
| 627 | async_extent->start + async_extent->ram_size - 1, |
| 628 | GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 629 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 630 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 631 | BUG_ON(IS_ERR(trans)); |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 632 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 633 | ret = btrfs_reserve_extent(trans, root, |
| 634 | async_extent->compressed_size, |
| 635 | async_extent->compressed_size, |
| 636 | 0, alloc_hint, |
| 637 | (u64)-1, &ins, 1); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 638 | btrfs_end_transaction(trans, root); |
| 639 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 640 | if (ret) { |
| 641 | int i; |
| 642 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 643 | WARN_ON(async_extent->pages[i]->mapping); |
| 644 | page_cache_release(async_extent->pages[i]); |
| 645 | } |
| 646 | kfree(async_extent->pages); |
| 647 | async_extent->nr_pages = 0; |
| 648 | async_extent->pages = NULL; |
| 649 | unlock_extent(io_tree, async_extent->start, |
| 650 | async_extent->start + |
| 651 | async_extent->ram_size - 1, GFP_NOFS); |
| 652 | goto retry; |
| 653 | } |
| 654 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 655 | /* |
| 656 | * here we're doing allocation and writeback of the |
| 657 | * compressed pages |
| 658 | */ |
| 659 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 660 | async_extent->start + |
| 661 | async_extent->ram_size - 1, 0); |
| 662 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 663 | em = alloc_extent_map(); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 664 | BUG_ON(!em); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 665 | em->start = async_extent->start; |
| 666 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 667 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 668 | |
| 669 | em->block_start = ins.objectid; |
| 670 | em->block_len = ins.offset; |
| 671 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 672 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 673 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 674 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 675 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 676 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 677 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 678 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 679 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 680 | if (ret != -EEXIST) { |
| 681 | free_extent_map(em); |
| 682 | break; |
| 683 | } |
| 684 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 685 | async_extent->start + |
| 686 | async_extent->ram_size - 1, 0); |
| 687 | } |
| 688 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 689 | ret = btrfs_add_ordered_extent_compress(inode, |
| 690 | async_extent->start, |
| 691 | ins.objectid, |
| 692 | async_extent->ram_size, |
| 693 | ins.offset, |
| 694 | BTRFS_ORDERED_COMPRESSED, |
| 695 | async_extent->compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 696 | BUG_ON(ret); |
| 697 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 698 | /* |
| 699 | * clear dirty, set writeback and unlock the pages. |
| 700 | */ |
| 701 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 702 | &BTRFS_I(inode)->io_tree, |
| 703 | async_extent->start, |
| 704 | async_extent->start + |
| 705 | async_extent->ram_size - 1, |
| 706 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 707 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 708 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 709 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 710 | |
| 711 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 712 | async_extent->start, |
| 713 | async_extent->ram_size, |
| 714 | ins.objectid, |
| 715 | ins.offset, async_extent->pages, |
| 716 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 717 | |
| 718 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 719 | alloc_hint = ins.objectid + ins.offset; |
| 720 | kfree(async_extent); |
| 721 | cond_resched(); |
| 722 | } |
| 723 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 724 | return 0; |
| 725 | } |
| 726 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 727 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 728 | u64 num_bytes) |
| 729 | { |
| 730 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 731 | struct extent_map *em; |
| 732 | u64 alloc_hint = 0; |
| 733 | |
| 734 | read_lock(&em_tree->lock); |
| 735 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 736 | if (em) { |
| 737 | /* |
| 738 | * if block start isn't an actual block number then find the |
| 739 | * first block in this inode and use that as a hint. If that |
| 740 | * block is also bogus then just don't worry about it. |
| 741 | */ |
| 742 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 743 | free_extent_map(em); |
| 744 | em = search_extent_mapping(em_tree, 0, 0); |
| 745 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 746 | alloc_hint = em->block_start; |
| 747 | if (em) |
| 748 | free_extent_map(em); |
| 749 | } else { |
| 750 | alloc_hint = em->block_start; |
| 751 | free_extent_map(em); |
| 752 | } |
| 753 | } |
| 754 | read_unlock(&em_tree->lock); |
| 755 | |
| 756 | return alloc_hint; |
| 757 | } |
| 758 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 759 | /* |
| 760 | * when extent_io.c finds a delayed allocation range in the file, |
| 761 | * the call backs end up in this code. The basic idea is to |
| 762 | * allocate extents on disk for the range, and create ordered data structs |
| 763 | * in ram to track those extents. |
| 764 | * |
| 765 | * locked_page is the page that writepage had locked already. We use |
| 766 | * it to make sure we don't do extra locks or unlocks. |
| 767 | * |
| 768 | * *page_started is set to one if we unlock locked_page and do everything |
| 769 | * required to start IO on it. It may be clean and already done with |
| 770 | * IO when we return. |
| 771 | */ |
| 772 | static noinline int cow_file_range(struct inode *inode, |
| 773 | struct page *locked_page, |
| 774 | u64 start, u64 end, int *page_started, |
| 775 | unsigned long *nr_written, |
| 776 | int unlock) |
| 777 | { |
| 778 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 779 | struct btrfs_trans_handle *trans; |
| 780 | u64 alloc_hint = 0; |
| 781 | u64 num_bytes; |
| 782 | unsigned long ram_size; |
| 783 | u64 disk_num_bytes; |
| 784 | u64 cur_alloc_size; |
| 785 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 786 | struct btrfs_key ins; |
| 787 | struct extent_map *em; |
| 788 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 789 | int ret = 0; |
| 790 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 791 | BUG_ON(btrfs_is_free_space_inode(root, inode)); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 792 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 793 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 794 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 795 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 796 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 797 | num_bytes = max(blocksize, num_bytes); |
| 798 | disk_num_bytes = num_bytes; |
| 799 | ret = 0; |
| 800 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 801 | /* if this is a small write inside eof, kick off defrag */ |
| 802 | if (end <= BTRFS_I(inode)->disk_i_size && num_bytes < 64 * 1024) |
| 803 | btrfs_add_inode_defrag(trans, inode); |
| 804 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 805 | if (start == 0) { |
| 806 | /* lets try to make an inline extent */ |
| 807 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 808 | start, end, 0, 0, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 809 | if (ret == 0) { |
| 810 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 811 | &BTRFS_I(inode)->io_tree, |
| 812 | start, end, NULL, |
| 813 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 814 | EXTENT_CLEAR_UNLOCK | |
| 815 | EXTENT_CLEAR_DELALLOC | |
| 816 | EXTENT_CLEAR_DIRTY | |
| 817 | EXTENT_SET_WRITEBACK | |
| 818 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 819 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 820 | *nr_written = *nr_written + |
| 821 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 822 | *page_started = 1; |
| 823 | ret = 0; |
| 824 | goto out; |
| 825 | } |
| 826 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 827 | |
| 828 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 829 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 830 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 831 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 832 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 833 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 834 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 835 | unsigned long op; |
| 836 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 837 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 838 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 839 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 840 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 841 | BUG_ON(ret); |
| 842 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 843 | em = alloc_extent_map(); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 844 | BUG_ON(!em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 845 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 846 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 847 | ram_size = ins.offset; |
| 848 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 849 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 850 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 851 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 852 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 853 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 854 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 855 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 856 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 857 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 858 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 859 | if (ret != -EEXIST) { |
| 860 | free_extent_map(em); |
| 861 | break; |
| 862 | } |
| 863 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 864 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 865 | } |
| 866 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 867 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 868 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 869 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 870 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 871 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 872 | if (root->root_key.objectid == |
| 873 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 874 | ret = btrfs_reloc_clone_csums(inode, start, |
| 875 | cur_alloc_size); |
| 876 | BUG_ON(ret); |
| 877 | } |
| 878 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 879 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 880 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 881 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 882 | /* we're not doing compressed IO, don't unlock the first |
| 883 | * page (which the caller expects to stay locked), don't |
| 884 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 885 | * |
| 886 | * Do set the Private2 bit so we know this page was properly |
| 887 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 888 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 889 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 890 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 891 | EXTENT_SET_PRIVATE2; |
| 892 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 893 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 894 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 895 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 896 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 897 | num_bytes -= cur_alloc_size; |
| 898 | alloc_hint = ins.objectid + ins.offset; |
| 899 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 900 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 901 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 902 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 903 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 904 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 905 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 906 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 907 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 908 | /* |
| 909 | * work queue call back to started compression on a file and pages |
| 910 | */ |
| 911 | static noinline void async_cow_start(struct btrfs_work *work) |
| 912 | { |
| 913 | struct async_cow *async_cow; |
| 914 | int num_added = 0; |
| 915 | async_cow = container_of(work, struct async_cow, work); |
| 916 | |
| 917 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 918 | async_cow->start, async_cow->end, async_cow, |
| 919 | &num_added); |
| 920 | if (num_added == 0) |
| 921 | async_cow->inode = NULL; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * work queue call back to submit previously compressed pages |
| 926 | */ |
| 927 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 928 | { |
| 929 | struct async_cow *async_cow; |
| 930 | struct btrfs_root *root; |
| 931 | unsigned long nr_pages; |
| 932 | |
| 933 | async_cow = container_of(work, struct async_cow, work); |
| 934 | |
| 935 | root = async_cow->root; |
| 936 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 937 | PAGE_CACHE_SHIFT; |
| 938 | |
| 939 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 940 | |
| 941 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 942 | 5 * 1042 * 1024 && |
| 943 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 944 | wake_up(&root->fs_info->async_submit_wait); |
| 945 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 946 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 947 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 948 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 949 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 950 | static noinline void async_cow_free(struct btrfs_work *work) |
| 951 | { |
| 952 | struct async_cow *async_cow; |
| 953 | async_cow = container_of(work, struct async_cow, work); |
| 954 | kfree(async_cow); |
| 955 | } |
| 956 | |
| 957 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 958 | u64 start, u64 end, int *page_started, |
| 959 | unsigned long *nr_written) |
| 960 | { |
| 961 | struct async_cow *async_cow; |
| 962 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 963 | unsigned long nr_pages; |
| 964 | u64 cur_end; |
| 965 | int limit = 10 * 1024 * 1042; |
| 966 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 967 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 968 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 969 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 970 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 971 | BUG_ON(!async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 972 | async_cow->inode = inode; |
| 973 | async_cow->root = root; |
| 974 | async_cow->locked_page = locked_page; |
| 975 | async_cow->start = start; |
| 976 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 977 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 978 | cur_end = end; |
| 979 | else |
| 980 | cur_end = min(end, start + 512 * 1024 - 1); |
| 981 | |
| 982 | async_cow->end = cur_end; |
| 983 | INIT_LIST_HEAD(&async_cow->extents); |
| 984 | |
| 985 | async_cow->work.func = async_cow_start; |
| 986 | async_cow->work.ordered_func = async_cow_submit; |
| 987 | async_cow->work.ordered_free = async_cow_free; |
| 988 | async_cow->work.flags = 0; |
| 989 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 990 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 991 | PAGE_CACHE_SHIFT; |
| 992 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 993 | |
| 994 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 995 | &async_cow->work); |
| 996 | |
| 997 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 998 | wait_event(root->fs_info->async_submit_wait, |
| 999 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1000 | limit)); |
| 1001 | } |
| 1002 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1003 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1004 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1005 | wait_event(root->fs_info->async_submit_wait, |
| 1006 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1007 | 0)); |
| 1008 | } |
| 1009 | |
| 1010 | *nr_written += nr_pages; |
| 1011 | start = cur_end + 1; |
| 1012 | } |
| 1013 | *page_started = 1; |
| 1014 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1015 | } |
| 1016 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1017 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1018 | u64 bytenr, u64 num_bytes) |
| 1019 | { |
| 1020 | int ret; |
| 1021 | struct btrfs_ordered_sum *sums; |
| 1022 | LIST_HEAD(list); |
| 1023 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1024 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1025 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1026 | if (ret == 0 && list_empty(&list)) |
| 1027 | return 0; |
| 1028 | |
| 1029 | while (!list_empty(&list)) { |
| 1030 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1031 | list_del(&sums->list); |
| 1032 | kfree(sums); |
| 1033 | } |
| 1034 | return 1; |
| 1035 | } |
| 1036 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1037 | /* |
| 1038 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1039 | * of the extents that exist in the file, and COWs the file as required. |
| 1040 | * |
| 1041 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1042 | * blocks on disk |
| 1043 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1044 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1045 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1046 | u64 start, u64 end, int *page_started, int force, |
| 1047 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1048 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1049 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1050 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1051 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1052 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1053 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1054 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1055 | u64 cow_start; |
| 1056 | u64 cur_offset; |
| 1057 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1058 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1059 | u64 disk_bytenr; |
| 1060 | u64 num_bytes; |
| 1061 | int extent_type; |
| 1062 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1063 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1064 | int nocow; |
| 1065 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1066 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1067 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1068 | |
| 1069 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1070 | if (!path) |
| 1071 | return -ENOMEM; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1072 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1073 | nolock = btrfs_is_free_space_inode(root, inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1074 | |
| 1075 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1076 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1077 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1078 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1079 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1080 | BUG_ON(IS_ERR(trans)); |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1081 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1082 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1083 | cow_start = (u64)-1; |
| 1084 | cur_offset = start; |
| 1085 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1086 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1087 | cur_offset, 0); |
| 1088 | BUG_ON(ret < 0); |
| 1089 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1090 | leaf = path->nodes[0]; |
| 1091 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1092 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1093 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1094 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1095 | path->slots[0]--; |
| 1096 | } |
| 1097 | check_prev = 0; |
| 1098 | next_slot: |
| 1099 | leaf = path->nodes[0]; |
| 1100 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1101 | ret = btrfs_next_leaf(root, path); |
| 1102 | if (ret < 0) |
| 1103 | BUG_ON(1); |
| 1104 | if (ret > 0) |
| 1105 | break; |
| 1106 | leaf = path->nodes[0]; |
| 1107 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1108 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1109 | nocow = 0; |
| 1110 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1111 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1112 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1113 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1114 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1115 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1116 | found_key.offset > end) |
| 1117 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1118 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1119 | if (found_key.offset > cur_offset) { |
| 1120 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1121 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1122 | goto out_check; |
| 1123 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1124 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1125 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1126 | struct btrfs_file_extent_item); |
| 1127 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1128 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1129 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1130 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1131 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1132 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1133 | extent_end = found_key.offset + |
| 1134 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1135 | if (extent_end <= start) { |
| 1136 | path->slots[0]++; |
| 1137 | goto next_slot; |
| 1138 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1139 | if (disk_bytenr == 0) |
| 1140 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1141 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1142 | btrfs_file_extent_encryption(leaf, fi) || |
| 1143 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1144 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1145 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1146 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1147 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1148 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1149 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1150 | found_key.offset - |
| 1151 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1152 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1153 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1154 | disk_bytenr += cur_offset - found_key.offset; |
| 1155 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1156 | /* |
| 1157 | * force cow if csum exists in the range. |
| 1158 | * this ensure that csum for a given extent are |
| 1159 | * either valid or do not exist. |
| 1160 | */ |
| 1161 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1162 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1163 | nocow = 1; |
| 1164 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1165 | extent_end = found_key.offset + |
| 1166 | btrfs_file_extent_inline_len(leaf, fi); |
| 1167 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1168 | } else { |
| 1169 | BUG_ON(1); |
| 1170 | } |
| 1171 | out_check: |
| 1172 | if (extent_end <= start) { |
| 1173 | path->slots[0]++; |
| 1174 | goto next_slot; |
| 1175 | } |
| 1176 | if (!nocow) { |
| 1177 | if (cow_start == (u64)-1) |
| 1178 | cow_start = cur_offset; |
| 1179 | cur_offset = extent_end; |
| 1180 | if (cur_offset > end) |
| 1181 | break; |
| 1182 | path->slots[0]++; |
| 1183 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1184 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1185 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1186 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1187 | if (cow_start != (u64)-1) { |
| 1188 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1189 | found_key.offset - 1, page_started, |
| 1190 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1191 | BUG_ON(ret); |
| 1192 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1193 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1194 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1195 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1196 | struct extent_map *em; |
| 1197 | struct extent_map_tree *em_tree; |
| 1198 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1199 | em = alloc_extent_map(); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 1200 | BUG_ON(!em); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1201 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1202 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1203 | em->len = num_bytes; |
| 1204 | em->block_len = num_bytes; |
| 1205 | em->block_start = disk_bytenr; |
| 1206 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1207 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1208 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1209 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1210 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1211 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1212 | if (ret != -EEXIST) { |
| 1213 | free_extent_map(em); |
| 1214 | break; |
| 1215 | } |
| 1216 | btrfs_drop_extent_cache(inode, em->start, |
| 1217 | em->start + em->len - 1, 0); |
| 1218 | } |
| 1219 | type = BTRFS_ORDERED_PREALLOC; |
| 1220 | } else { |
| 1221 | type = BTRFS_ORDERED_NOCOW; |
| 1222 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1223 | |
| 1224 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1225 | num_bytes, num_bytes, type); |
| 1226 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1227 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1228 | if (root->root_key.objectid == |
| 1229 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1230 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1231 | num_bytes); |
| 1232 | BUG_ON(ret); |
| 1233 | } |
| 1234 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1235 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1236 | cur_offset, cur_offset + num_bytes - 1, |
| 1237 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1238 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1239 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1240 | cur_offset = extent_end; |
| 1241 | if (cur_offset > end) |
| 1242 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1243 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1244 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1245 | |
| 1246 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1247 | cow_start = cur_offset; |
| 1248 | if (cow_start != (u64)-1) { |
| 1249 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1250 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1251 | BUG_ON(ret); |
| 1252 | } |
| 1253 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1254 | if (nolock) { |
| 1255 | ret = btrfs_end_transaction_nolock(trans, root); |
| 1256 | BUG_ON(ret); |
| 1257 | } else { |
| 1258 | ret = btrfs_end_transaction(trans, root); |
| 1259 | BUG_ON(ret); |
| 1260 | } |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1261 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1262 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1263 | } |
| 1264 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1265 | /* |
| 1266 | * extent_io.c call back to do delayed allocation processing |
| 1267 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1268 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1269 | u64 start, u64 end, int *page_started, |
| 1270 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1271 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1272 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1273 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1274 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1275 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1276 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1277 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1278 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1279 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1280 | page_started, 0, nr_written); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1281 | else if (!btrfs_test_opt(root, COMPRESS) && |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 1282 | !(BTRFS_I(inode)->force_compress) && |
| 1283 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1284 | ret = cow_file_range(inode, locked_page, start, end, |
| 1285 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1286 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1287 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1288 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1289 | return ret; |
| 1290 | } |
| 1291 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1292 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1293 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1294 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1295 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1296 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1297 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1298 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1299 | spin_lock(&BTRFS_I(inode)->lock); |
| 1300 | BTRFS_I(inode)->outstanding_extents++; |
| 1301 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /* |
| 1305 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1306 | * extents so we can keep track of new extents that are just merged onto old |
| 1307 | * extents, such as when we are doing sequential writes, so we can properly |
| 1308 | * account for the metadata space we'll need. |
| 1309 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1310 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1311 | struct extent_state *new, |
| 1312 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1313 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1314 | /* not delalloc, ignore it */ |
| 1315 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1316 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1317 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1318 | spin_lock(&BTRFS_I(inode)->lock); |
| 1319 | BTRFS_I(inode)->outstanding_extents--; |
| 1320 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1321 | } |
| 1322 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1323 | /* |
| 1324 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1325 | * bytes in this file, and to maintain the list of inodes that |
| 1326 | * have pending delalloc work to be done. |
| 1327 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1328 | static void btrfs_set_bit_hook(struct inode *inode, |
| 1329 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1330 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1331 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1332 | /* |
| 1333 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1334 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1335 | * bit, which is only set or cleared with irqs on |
| 1336 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1337 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1338 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1339 | u64 len = state->end + 1 - state->start; |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1340 | bool do_list = !btrfs_is_free_space_inode(root, inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1341 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1342 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1343 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1344 | } else { |
| 1345 | spin_lock(&BTRFS_I(inode)->lock); |
| 1346 | BTRFS_I(inode)->outstanding_extents++; |
| 1347 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1348 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1349 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1350 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1351 | BTRFS_I(inode)->delalloc_bytes += len; |
| 1352 | root->fs_info->delalloc_bytes += len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1353 | if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1354 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1355 | &root->fs_info->delalloc_inodes); |
| 1356 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1357 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1358 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1359 | } |
| 1360 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1361 | /* |
| 1362 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1363 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1364 | static void btrfs_clear_bit_hook(struct inode *inode, |
| 1365 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1366 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1367 | /* |
| 1368 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1369 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1370 | * bit, which is only set or cleared with irqs on |
| 1371 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1372 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1373 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1374 | u64 len = state->end + 1 - state->start; |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1375 | bool do_list = !btrfs_is_free_space_inode(root, inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1376 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1377 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1378 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1379 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1380 | spin_lock(&BTRFS_I(inode)->lock); |
| 1381 | BTRFS_I(inode)->outstanding_extents--; |
| 1382 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1383 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1384 | |
| 1385 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1386 | btrfs_delalloc_release_metadata(inode, len); |
| 1387 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1388 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 1389 | && do_list) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1390 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1391 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1392 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1393 | root->fs_info->delalloc_bytes -= len; |
| 1394 | BTRFS_I(inode)->delalloc_bytes -= len; |
| 1395 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1396 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1397 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1398 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1399 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1400 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1401 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1402 | } |
| 1403 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1404 | /* |
| 1405 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1406 | * we don't create bios that span stripes or chunks |
| 1407 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1408 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1409 | size_t size, struct bio *bio, |
| 1410 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1411 | { |
| 1412 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1413 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1414 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1415 | u64 length = 0; |
| 1416 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1417 | int ret; |
| 1418 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1419 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1420 | return 0; |
| 1421 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1422 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1423 | map_tree = &root->fs_info->mapping_tree; |
| 1424 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1425 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1426 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1427 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1428 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1429 | return 1; |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 1430 | return ret; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1431 | } |
| 1432 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1433 | /* |
| 1434 | * in order to insert checksums into the metadata in large chunks, |
| 1435 | * we wait until bio submission time. All the pages in the bio are |
| 1436 | * checksummed and sums are attached onto the ordered extent record. |
| 1437 | * |
| 1438 | * At IO completion time the cums attached on the ordered extent record |
| 1439 | * are inserted into the btree |
| 1440 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1441 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1442 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1443 | unsigned long bio_flags, |
| 1444 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1445 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1446 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1447 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1448 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1449 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1450 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1451 | return 0; |
| 1452 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1453 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1454 | /* |
| 1455 | * in order to insert checksums into the metadata in large chunks, |
| 1456 | * we wait until bio submission time. All the pages in the bio are |
| 1457 | * checksummed and sums are attached onto the ordered extent record. |
| 1458 | * |
| 1459 | * At IO completion time the cums attached on the ordered extent record |
| 1460 | * are inserted into the btree |
| 1461 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1462 | 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] | 1463 | int mirror_num, unsigned long bio_flags, |
| 1464 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1465 | { |
| 1466 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1467 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1468 | } |
| 1469 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1470 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1471 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1472 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1473 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1474 | 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] | 1475 | int mirror_num, unsigned long bio_flags, |
| 1476 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1477 | { |
| 1478 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1479 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1480 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1481 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1482 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1483 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1484 | if (btrfs_is_free_space_inode(root, inode)) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1485 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2); |
| 1486 | else |
| 1487 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1488 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1489 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1490 | if (!(rw & REQ_WRITE)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1491 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1492 | return btrfs_submit_compressed_read(inode, bio, |
| 1493 | mirror_num, bio_flags); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1494 | } else if (!skip_sum) { |
| 1495 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1496 | if (ret) |
| 1497 | return ret; |
| 1498 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1499 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1500 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1501 | /* csum items have already been cloned */ |
| 1502 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1503 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1504 | /* we're doing a write, do the async checksumming */ |
| 1505 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1506 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1507 | bio_flags, bio_offset, |
| 1508 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1509 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1510 | } |
| 1511 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1512 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1513 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1514 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1515 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1516 | /* |
| 1517 | * given a list of ordered sums record them in the inode. This happens |
| 1518 | * at IO completion time based on sums calculated at bio submission time. |
| 1519 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1520 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1521 | struct inode *inode, u64 file_offset, |
| 1522 | struct list_head *list) |
| 1523 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1524 | struct btrfs_ordered_sum *sum; |
| 1525 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1526 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1527 | btrfs_csum_file_blocks(trans, |
| 1528 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1529 | } |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1533 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1534 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1535 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1536 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1537 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1538 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1539 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1540 | } |
| 1541 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1542 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1543 | struct btrfs_writepage_fixup { |
| 1544 | struct page *page; |
| 1545 | struct btrfs_work work; |
| 1546 | }; |
| 1547 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1548 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1549 | { |
| 1550 | struct btrfs_writepage_fixup *fixup; |
| 1551 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1552 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1553 | struct page *page; |
| 1554 | struct inode *inode; |
| 1555 | u64 page_start; |
| 1556 | u64 page_end; |
| 1557 | |
| 1558 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1559 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1560 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1561 | lock_page(page); |
| 1562 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1563 | ClearPageChecked(page); |
| 1564 | goto out_page; |
| 1565 | } |
| 1566 | |
| 1567 | inode = page->mapping->host; |
| 1568 | page_start = page_offset(page); |
| 1569 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1570 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1571 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
| 1572 | &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1573 | |
| 1574 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1575 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1576 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1577 | |
| 1578 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1579 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1580 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1581 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1582 | unlock_page(page); |
| 1583 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1584 | goto again; |
| 1585 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1586 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1587 | BUG(); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1588 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1589 | ClearPageChecked(page); |
| 1590 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1591 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1592 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1593 | out_page: |
| 1594 | unlock_page(page); |
| 1595 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1596 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * There are a few paths in the higher layers of the kernel that directly |
| 1601 | * set the page dirty bit without asking the filesystem if it is a |
| 1602 | * good idea. This causes problems because we want to make sure COW |
| 1603 | * properly happens and the data=ordered rules are followed. |
| 1604 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1605 | * 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] | 1606 | * hasn't been properly setup for IO. We kick off an async process |
| 1607 | * to fix it up. The async helper will wait for ordered extents, set |
| 1608 | * the delalloc bit and make it safe to write the page. |
| 1609 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1610 | 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] | 1611 | { |
| 1612 | struct inode *inode = page->mapping->host; |
| 1613 | struct btrfs_writepage_fixup *fixup; |
| 1614 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1615 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1616 | /* this page is properly in the ordered list */ |
| 1617 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1618 | return 0; |
| 1619 | |
| 1620 | if (PageChecked(page)) |
| 1621 | return -EAGAIN; |
| 1622 | |
| 1623 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1624 | if (!fixup) |
| 1625 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1626 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1627 | SetPageChecked(page); |
| 1628 | page_cache_get(page); |
| 1629 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1630 | fixup->page = page; |
| 1631 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1632 | return -EAGAIN; |
| 1633 | } |
| 1634 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1635 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1636 | struct inode *inode, u64 file_pos, |
| 1637 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1638 | u64 num_bytes, u64 ram_bytes, |
| 1639 | u8 compression, u8 encryption, |
| 1640 | u16 other_encoding, int extent_type) |
| 1641 | { |
| 1642 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1643 | struct btrfs_file_extent_item *fi; |
| 1644 | struct btrfs_path *path; |
| 1645 | struct extent_buffer *leaf; |
| 1646 | struct btrfs_key ins; |
| 1647 | u64 hint; |
| 1648 | int ret; |
| 1649 | |
| 1650 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1651 | if (!path) |
| 1652 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1653 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1654 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1655 | |
| 1656 | /* |
| 1657 | * we may be replacing one extent in the tree with another. |
| 1658 | * The new extent is pinned in the extent map, and we don't want |
| 1659 | * to drop it from the cache until it is completely in the btree. |
| 1660 | * |
| 1661 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1662 | * the caller is expected to unpin it and allow it to be merged |
| 1663 | * with the others. |
| 1664 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1665 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1666 | &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1667 | BUG_ON(ret); |
| 1668 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1669 | ins.objectid = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1670 | ins.offset = file_pos; |
| 1671 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1672 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1673 | BUG_ON(ret); |
| 1674 | leaf = path->nodes[0]; |
| 1675 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1676 | struct btrfs_file_extent_item); |
| 1677 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1678 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1679 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1680 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1681 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1682 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1683 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1684 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1685 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1686 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1687 | |
| 1688 | btrfs_unlock_up_safe(path, 1); |
| 1689 | btrfs_set_lock_blocking(leaf); |
| 1690 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1691 | btrfs_mark_buffer_dirty(leaf); |
| 1692 | |
| 1693 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1694 | |
| 1695 | ins.objectid = disk_bytenr; |
| 1696 | ins.offset = disk_num_bytes; |
| 1697 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1698 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1699 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1700 | btrfs_ino(inode), file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1701 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1702 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1703 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1704 | return 0; |
| 1705 | } |
| 1706 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1707 | /* |
| 1708 | * helper function for btrfs_finish_ordered_io, this |
| 1709 | * just reads in some of the csum leaves to prime them into ram |
| 1710 | * before we start the transaction. It limits the amount of btree |
| 1711 | * reads required while inside the transaction. |
| 1712 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1713 | /* as ordered data IO finishes, this gets called so we can finish |
| 1714 | * an ordered extent if the range of bytes in the file it covers are |
| 1715 | * fully written. |
| 1716 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1717 | static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1718 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1719 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1720 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1721 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1722 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1723 | struct extent_state *cached_state = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1724 | int compress_type = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1725 | int ret; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1726 | bool nolock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1727 | |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 1728 | ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 1729 | end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1730 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1731 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1732 | BUG_ON(!ordered_extent); |
Josef Bacik | efd049f | 2010-02-02 20:50:10 +0000 | [diff] [blame] | 1733 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1734 | nolock = btrfs_is_free_space_inode(root, inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1735 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1736 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
| 1737 | BUG_ON(!list_empty(&ordered_extent->list)); |
| 1738 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1739 | if (!ret) { |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1740 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1741 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1742 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1743 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1744 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1745 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 1746 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1747 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1748 | } |
| 1749 | goto out; |
| 1750 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1751 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1752 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 1753 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1754 | 0, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1755 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1756 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1757 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1758 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1759 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1760 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1761 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1762 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1763 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1764 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1765 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1766 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1767 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1768 | ordered_extent->file_offset, |
| 1769 | ordered_extent->file_offset + |
| 1770 | ordered_extent->len); |
| 1771 | BUG_ON(ret); |
| 1772 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 1773 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1774 | ret = insert_reserved_file_extent(trans, inode, |
| 1775 | ordered_extent->file_offset, |
| 1776 | ordered_extent->start, |
| 1777 | ordered_extent->disk_len, |
| 1778 | ordered_extent->len, |
| 1779 | ordered_extent->len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1780 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1781 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1782 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1783 | ordered_extent->file_offset, |
| 1784 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1785 | BUG_ON(ret); |
| 1786 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1787 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 1788 | ordered_extent->file_offset + |
| 1789 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
| 1790 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1791 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1792 | &ordered_extent->list); |
| 1793 | |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 1794 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
Miao Xie | a39f752 | 2011-09-11 10:52:25 -0400 | [diff] [blame] | 1795 | if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 1796 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 1797 | BUG_ON(ret); |
| 1798 | } |
| 1799 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1800 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 1801 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1802 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 1803 | if (trans) { |
| 1804 | if (nolock) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1805 | btrfs_end_transaction_nolock(trans, root); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 1806 | else |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1807 | btrfs_end_transaction(trans, root); |
| 1808 | } |
| 1809 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1810 | /* once for us */ |
| 1811 | btrfs_put_ordered_extent(ordered_extent); |
| 1812 | /* once for the tree */ |
| 1813 | btrfs_put_ordered_extent(ordered_extent); |
| 1814 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1815 | return 0; |
| 1816 | } |
| 1817 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1818 | 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] | 1819 | struct extent_state *state, int uptodate) |
| 1820 | { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1821 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 1822 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1823 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1824 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1825 | } |
| 1826 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1827 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1828 | * 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] | 1829 | * if there's a match, we allow the bio to finish. If not, the code in |
| 1830 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1831 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1832 | static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1833 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1834 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1835 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1836 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1837 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1838 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1839 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1840 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1841 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1842 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1843 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1844 | if (PageChecked(page)) { |
| 1845 | ClearPageChecked(page); |
| 1846 | goto good; |
| 1847 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1848 | |
| 1849 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 1850 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1851 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1852 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1853 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1854 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1855 | GFP_NOFS); |
| 1856 | return 0; |
| 1857 | } |
| 1858 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1859 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1860 | private = state->private; |
| 1861 | ret = 0; |
| 1862 | } else { |
| 1863 | ret = get_state_private(io_tree, start, &private); |
| 1864 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1865 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1866 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1867 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1868 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1869 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1870 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1871 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1872 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1873 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1874 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1875 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1876 | return 0; |
| 1877 | |
| 1878 | zeroit: |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 1879 | 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] | 1880 | "private %llu\n", |
| 1881 | (unsigned long long)btrfs_ino(page->mapping->host), |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 1882 | (unsigned long long)start, csum, |
| 1883 | (unsigned long long)private); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1884 | memset(kaddr + offset, 1, end - start + 1); |
| 1885 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1886 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1887 | if (private == 0) |
| 1888 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1889 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1890 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1891 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 1892 | struct delayed_iput { |
| 1893 | struct list_head list; |
| 1894 | struct inode *inode; |
| 1895 | }; |
| 1896 | |
| 1897 | void btrfs_add_delayed_iput(struct inode *inode) |
| 1898 | { |
| 1899 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 1900 | struct delayed_iput *delayed; |
| 1901 | |
| 1902 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 1903 | return; |
| 1904 | |
| 1905 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 1906 | delayed->inode = inode; |
| 1907 | |
| 1908 | spin_lock(&fs_info->delayed_iput_lock); |
| 1909 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 1910 | spin_unlock(&fs_info->delayed_iput_lock); |
| 1911 | } |
| 1912 | |
| 1913 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 1914 | { |
| 1915 | LIST_HEAD(list); |
| 1916 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1917 | struct delayed_iput *delayed; |
| 1918 | int empty; |
| 1919 | |
| 1920 | spin_lock(&fs_info->delayed_iput_lock); |
| 1921 | empty = list_empty(&fs_info->delayed_iputs); |
| 1922 | spin_unlock(&fs_info->delayed_iput_lock); |
| 1923 | if (empty) |
| 1924 | return; |
| 1925 | |
| 1926 | down_read(&root->fs_info->cleanup_work_sem); |
| 1927 | spin_lock(&fs_info->delayed_iput_lock); |
| 1928 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 1929 | spin_unlock(&fs_info->delayed_iput_lock); |
| 1930 | |
| 1931 | while (!list_empty(&list)) { |
| 1932 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 1933 | list_del(&delayed->list); |
| 1934 | iput(delayed->inode); |
| 1935 | kfree(delayed); |
| 1936 | } |
| 1937 | up_read(&root->fs_info->cleanup_work_sem); |
| 1938 | } |
| 1939 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1940 | enum btrfs_orphan_cleanup_state { |
| 1941 | ORPHAN_CLEANUP_STARTED = 1, |
| 1942 | ORPHAN_CLEANUP_DONE = 2, |
| 1943 | }; |
| 1944 | |
| 1945 | /* |
| 1946 | * This is called in transaction commmit time. If there are no orphan |
| 1947 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 1948 | * structure. |
| 1949 | */ |
| 1950 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 1951 | struct btrfs_root *root) |
| 1952 | { |
| 1953 | int ret; |
| 1954 | |
| 1955 | if (!list_empty(&root->orphan_list) || |
| 1956 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 1957 | return; |
| 1958 | |
| 1959 | if (root->orphan_item_inserted && |
| 1960 | btrfs_root_refs(&root->root_item) > 0) { |
| 1961 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 1962 | root->root_key.objectid); |
| 1963 | BUG_ON(ret); |
| 1964 | root->orphan_item_inserted = 0; |
| 1965 | } |
| 1966 | |
| 1967 | if (root->orphan_block_rsv) { |
| 1968 | WARN_ON(root->orphan_block_rsv->size > 0); |
| 1969 | btrfs_free_block_rsv(root, root->orphan_block_rsv); |
| 1970 | root->orphan_block_rsv = NULL; |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1975 | * This creates an orphan entry for the given inode in case something goes |
| 1976 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1977 | * |
| 1978 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 1979 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1980 | */ |
| 1981 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 1982 | { |
| 1983 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1984 | struct btrfs_block_rsv *block_rsv = NULL; |
| 1985 | int reserve = 0; |
| 1986 | int insert = 0; |
| 1987 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1988 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1989 | if (!root->orphan_block_rsv) { |
| 1990 | block_rsv = btrfs_alloc_block_rsv(root); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 1991 | if (!block_rsv) |
| 1992 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1993 | } |
| 1994 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1995 | spin_lock(&root->orphan_lock); |
| 1996 | if (!root->orphan_block_rsv) { |
| 1997 | root->orphan_block_rsv = block_rsv; |
| 1998 | } else if (block_rsv) { |
| 1999 | btrfs_free_block_rsv(root, block_rsv); |
| 2000 | block_rsv = NULL; |
| 2001 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2002 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2003 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2004 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2005 | #if 0 |
| 2006 | /* |
| 2007 | * For proper ENOSPC handling, we should do orphan |
| 2008 | * cleanup when mounting. But this introduces backward |
| 2009 | * compatibility issue. |
| 2010 | */ |
| 2011 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2012 | insert = 2; |
| 2013 | else |
| 2014 | insert = 1; |
| 2015 | #endif |
| 2016 | insert = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2017 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2018 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2019 | if (!BTRFS_I(inode)->orphan_meta_reserved) { |
| 2020 | BTRFS_I(inode)->orphan_meta_reserved = 1; |
| 2021 | reserve = 1; |
| 2022 | } |
| 2023 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2024 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2025 | /* grab metadata reservation from transaction handle */ |
| 2026 | if (reserve) { |
| 2027 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
| 2028 | BUG_ON(ret); |
| 2029 | } |
| 2030 | |
| 2031 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2032 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2033 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2034 | BUG_ON(ret); |
| 2035 | } |
| 2036 | |
| 2037 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2038 | if (insert >= 2) { |
| 2039 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2040 | root->root_key.objectid); |
| 2041 | BUG_ON(ret); |
| 2042 | } |
| 2043 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | /* |
| 2047 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2048 | * item for this particular inode. |
| 2049 | */ |
| 2050 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2051 | { |
| 2052 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2053 | int delete_item = 0; |
| 2054 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2055 | int ret = 0; |
| 2056 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2057 | spin_lock(&root->orphan_lock); |
| 2058 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2059 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2060 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2061 | } |
| 2062 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2063 | if (BTRFS_I(inode)->orphan_meta_reserved) { |
| 2064 | BTRFS_I(inode)->orphan_meta_reserved = 0; |
| 2065 | release_rsv = 1; |
| 2066 | } |
| 2067 | spin_unlock(&root->orphan_lock); |
| 2068 | |
| 2069 | if (trans && delete_item) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2070 | ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode)); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2071 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2072 | } |
| 2073 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2074 | if (release_rsv) |
| 2075 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2076 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2077 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | /* |
| 2081 | * this cleans up any orphans that may be left on the list from the last use |
| 2082 | * of this root. |
| 2083 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2084 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2085 | { |
| 2086 | struct btrfs_path *path; |
| 2087 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2088 | struct btrfs_key key, found_key; |
| 2089 | struct btrfs_trans_handle *trans; |
| 2090 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 2091 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2092 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2093 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2094 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2095 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2096 | |
| 2097 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2098 | if (!path) { |
| 2099 | ret = -ENOMEM; |
| 2100 | goto out; |
| 2101 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2102 | path->reada = -1; |
| 2103 | |
| 2104 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2105 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2106 | key.offset = (u64)-1; |
| 2107 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2108 | while (1) { |
| 2109 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2110 | if (ret < 0) |
| 2111 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2112 | |
| 2113 | /* |
| 2114 | * 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] | 2115 | * 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] | 2116 | * find the key and see if we have stuff that matches |
| 2117 | */ |
| 2118 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2119 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2120 | if (path->slots[0] == 0) |
| 2121 | break; |
| 2122 | path->slots[0]--; |
| 2123 | } |
| 2124 | |
| 2125 | /* pull out the item */ |
| 2126 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2127 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2128 | |
| 2129 | /* make sure the item matches what we want */ |
| 2130 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2131 | break; |
| 2132 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2133 | break; |
| 2134 | |
| 2135 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2136 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2137 | |
| 2138 | /* |
| 2139 | * this is where we are basically btrfs_lookup, without the |
| 2140 | * crossing root thing. we store the inode number in the |
| 2141 | * offset of the orphan item. |
| 2142 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 2143 | |
| 2144 | if (found_key.offset == last_objectid) { |
| 2145 | printk(KERN_ERR "btrfs: Error removing orphan entry, " |
| 2146 | "stopping orphan cleanup\n"); |
| 2147 | ret = -EINVAL; |
| 2148 | goto out; |
| 2149 | } |
| 2150 | |
| 2151 | last_objectid = found_key.offset; |
| 2152 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2153 | found_key.objectid = found_key.offset; |
| 2154 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2155 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2156 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2157 | ret = PTR_RET(inode); |
| 2158 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2159 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2160 | |
| 2161 | /* |
| 2162 | * Inode is already gone but the orphan item is still there, |
| 2163 | * kill the orphan item. |
| 2164 | */ |
| 2165 | if (ret == -ESTALE) { |
| 2166 | trans = btrfs_start_transaction(root, 1); |
| 2167 | if (IS_ERR(trans)) { |
| 2168 | ret = PTR_ERR(trans); |
| 2169 | goto out; |
| 2170 | } |
| 2171 | ret = btrfs_del_orphan_item(trans, root, |
| 2172 | found_key.objectid); |
| 2173 | BUG_ON(ret); |
| 2174 | btrfs_end_transaction(trans, root); |
| 2175 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2176 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2177 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2178 | /* |
| 2179 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2180 | * the proper thing when we hit it |
| 2181 | */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2182 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2183 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2184 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2185 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2186 | /* if we have links, this was a truncate, lets do that */ |
| 2187 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 2188 | if (!S_ISREG(inode->i_mode)) { |
| 2189 | WARN_ON(1); |
| 2190 | iput(inode); |
| 2191 | continue; |
| 2192 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2193 | nr_truncate++; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2194 | ret = btrfs_truncate(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2195 | } else { |
| 2196 | nr_unlink++; |
| 2197 | } |
| 2198 | |
| 2199 | /* this will do delete_inode and everything for us */ |
| 2200 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2201 | if (ret) |
| 2202 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2203 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 2204 | /* release the path since we're done with it */ |
| 2205 | btrfs_release_path(path); |
| 2206 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2207 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 2208 | |
| 2209 | if (root->orphan_block_rsv) |
| 2210 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 2211 | (u64)-1); |
| 2212 | |
| 2213 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2214 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2215 | if (!IS_ERR(trans)) |
| 2216 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2217 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2218 | |
| 2219 | if (nr_unlink) |
| 2220 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2221 | if (nr_truncate) |
| 2222 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2223 | |
| 2224 | out: |
| 2225 | if (ret) |
| 2226 | printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret); |
| 2227 | btrfs_free_path(path); |
| 2228 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2229 | } |
| 2230 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2231 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2232 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2233 | * don't find any xattrs, we know there can't be any acls. |
| 2234 | * |
| 2235 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2236 | */ |
| 2237 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2238 | int slot, u64 objectid) |
| 2239 | { |
| 2240 | u32 nritems = btrfs_header_nritems(leaf); |
| 2241 | struct btrfs_key found_key; |
| 2242 | int scanned = 0; |
| 2243 | |
| 2244 | slot++; |
| 2245 | while (slot < nritems) { |
| 2246 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2247 | |
| 2248 | /* we found a different objectid, there must not be acls */ |
| 2249 | if (found_key.objectid != objectid) |
| 2250 | return 0; |
| 2251 | |
| 2252 | /* we found an xattr, assume we've got an acl */ |
| 2253 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2254 | return 1; |
| 2255 | |
| 2256 | /* |
| 2257 | * we found a key greater than an xattr key, there can't |
| 2258 | * be any acls later on |
| 2259 | */ |
| 2260 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2261 | return 0; |
| 2262 | |
| 2263 | slot++; |
| 2264 | scanned++; |
| 2265 | |
| 2266 | /* |
| 2267 | * it goes inode, inode backrefs, xattrs, extents, |
| 2268 | * so if there are a ton of hard links to an inode there can |
| 2269 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2270 | * this is just an optimization |
| 2271 | */ |
| 2272 | if (scanned >= 8) |
| 2273 | break; |
| 2274 | } |
| 2275 | /* we hit the end of the leaf before we found an xattr or |
| 2276 | * something larger than an xattr. We have to assume the inode |
| 2277 | * has acls |
| 2278 | */ |
| 2279 | return 1; |
| 2280 | } |
| 2281 | |
| 2282 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2283 | * read an inode from the btree into the in-memory inode |
| 2284 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2285 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2286 | { |
| 2287 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2288 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2289 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2290 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2291 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2292 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2293 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2294 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2295 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2296 | bool filled = false; |
| 2297 | |
| 2298 | ret = btrfs_fill_inode(inode, &rdev); |
| 2299 | if (!ret) |
| 2300 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2301 | |
| 2302 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 2303 | if (!path) |
| 2304 | goto make_bad; |
| 2305 | |
Josef Bacik | d90c732 | 2011-05-17 09:50:54 -0400 | [diff] [blame] | 2306 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2307 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2308 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2309 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2310 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2311 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2312 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2313 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2314 | |
| 2315 | if (filled) |
| 2316 | goto cache_acl; |
| 2317 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2318 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2319 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2320 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2321 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2322 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2323 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2324 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2325 | |
| 2326 | tspec = btrfs_inode_atime(inode_item); |
| 2327 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2328 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2329 | |
| 2330 | tspec = btrfs_inode_mtime(inode_item); |
| 2331 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2332 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2333 | |
| 2334 | tspec = btrfs_inode_ctime(inode_item); |
| 2335 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2336 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2337 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2338 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2339 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2340 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2341 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2342 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2343 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2344 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2345 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2346 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2347 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2348 | /* |
| 2349 | * try to precache a NULL acl entry for files that don't have |
| 2350 | * any xattrs or acls |
| 2351 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2352 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
| 2353 | btrfs_ino(inode)); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 2354 | if (!maybe_acls) |
| 2355 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2356 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2357 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2358 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2359 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2360 | case S_IFREG: |
| 2361 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2362 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2363 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2364 | inode->i_fop = &btrfs_file_operations; |
| 2365 | inode->i_op = &btrfs_file_inode_operations; |
| 2366 | break; |
| 2367 | case S_IFDIR: |
| 2368 | inode->i_fop = &btrfs_dir_file_operations; |
| 2369 | if (root == root->fs_info->tree_root) |
| 2370 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2371 | else |
| 2372 | inode->i_op = &btrfs_dir_inode_operations; |
| 2373 | break; |
| 2374 | case S_IFLNK: |
| 2375 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2376 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2377 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2378 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2379 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2380 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2381 | init_special_inode(inode, inode->i_mode, rdev); |
| 2382 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2383 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2384 | |
| 2385 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2386 | return; |
| 2387 | |
| 2388 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2389 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2390 | make_bad_inode(inode); |
| 2391 | } |
| 2392 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2393 | /* |
| 2394 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2395 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2396 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2397 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2398 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2399 | struct inode *inode) |
| 2400 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2401 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2402 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2403 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2404 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2405 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2406 | |
| 2407 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2408 | inode->i_atime.tv_sec); |
| 2409 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2410 | inode->i_atime.tv_nsec); |
| 2411 | |
| 2412 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2413 | inode->i_mtime.tv_sec); |
| 2414 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2415 | inode->i_mtime.tv_nsec); |
| 2416 | |
| 2417 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2418 | inode->i_ctime.tv_sec); |
| 2419 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2420 | inode->i_ctime.tv_nsec); |
| 2421 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2422 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2423 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2424 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2425 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2426 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2427 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 2428 | btrfs_set_inode_block_group(leaf, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2429 | } |
| 2430 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2431 | /* |
| 2432 | * copy everything in the in-memory inode into the btree. |
| 2433 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2434 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2435 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2436 | { |
| 2437 | struct btrfs_inode_item *inode_item; |
| 2438 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2439 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2440 | int ret; |
| 2441 | |
| 2442 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2443 | if (!path) |
| 2444 | return -ENOMEM; |
| 2445 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2446 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2447 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 2448 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2449 | if (ret) { |
| 2450 | if (ret > 0) |
| 2451 | ret = -ENOENT; |
| 2452 | goto failed; |
| 2453 | } |
| 2454 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2455 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2456 | leaf = path->nodes[0]; |
| 2457 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2458 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2459 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2460 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2461 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2462 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2463 | ret = 0; |
| 2464 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2465 | btrfs_free_path(path); |
| 2466 | return ret; |
| 2467 | } |
| 2468 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2469 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2470 | * copy everything in the in-memory inode into the btree. |
| 2471 | */ |
| 2472 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2473 | struct btrfs_root *root, struct inode *inode) |
| 2474 | { |
| 2475 | int ret; |
| 2476 | |
| 2477 | /* |
| 2478 | * If the inode is a free space inode, we can deadlock during commit |
| 2479 | * if we put it into the delayed code. |
| 2480 | * |
| 2481 | * The data relocation inode should also be directly updated |
| 2482 | * without delay |
| 2483 | */ |
| 2484 | if (!btrfs_is_free_space_inode(root, inode) |
| 2485 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 2486 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 2487 | if (!ret) |
| 2488 | btrfs_set_inode_last_trans(trans, inode); |
| 2489 | return ret; |
| 2490 | } |
| 2491 | |
| 2492 | return btrfs_update_inode_item(trans, root, inode); |
| 2493 | } |
| 2494 | |
| 2495 | static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 2496 | struct btrfs_root *root, struct inode *inode) |
| 2497 | { |
| 2498 | int ret; |
| 2499 | |
| 2500 | ret = btrfs_update_inode(trans, root, inode); |
| 2501 | if (ret == -ENOSPC) |
| 2502 | return btrfs_update_inode_item(trans, root, inode); |
| 2503 | return ret; |
| 2504 | } |
| 2505 | |
| 2506 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2507 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2508 | * recovery code. It remove a link in a directory with a given name, and |
| 2509 | * also drops the back refs in the inode to the directory |
| 2510 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2511 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2512 | struct btrfs_root *root, |
| 2513 | struct inode *dir, struct inode *inode, |
| 2514 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2515 | { |
| 2516 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2517 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2518 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2519 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2520 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2521 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2522 | u64 ino = btrfs_ino(inode); |
| 2523 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2524 | |
| 2525 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2526 | if (!path) { |
| 2527 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 2528 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2529 | } |
| 2530 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2531 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2532 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2533 | name, name_len, -1); |
| 2534 | if (IS_ERR(di)) { |
| 2535 | ret = PTR_ERR(di); |
| 2536 | goto err; |
| 2537 | } |
| 2538 | if (!di) { |
| 2539 | ret = -ENOENT; |
| 2540 | goto err; |
| 2541 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2542 | leaf = path->nodes[0]; |
| 2543 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2544 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2545 | if (ret) |
| 2546 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2547 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2548 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2549 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 2550 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2551 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2552 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2553 | "inode %llu parent %llu\n", name_len, name, |
| 2554 | (unsigned long long)ino, (unsigned long long)dir_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2555 | goto err; |
| 2556 | } |
| 2557 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2558 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
| 2559 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2560 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2561 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2562 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2563 | inode, dir_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2564 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2565 | |
| 2566 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2567 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 2568 | if (ret == -ENOENT) |
| 2569 | ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2570 | err: |
| 2571 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2572 | if (ret) |
| 2573 | goto out; |
| 2574 | |
| 2575 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2576 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2577 | btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2578 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2579 | return ret; |
| 2580 | } |
| 2581 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2582 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2583 | struct btrfs_root *root, |
| 2584 | struct inode *dir, struct inode *inode, |
| 2585 | const char *name, int name_len) |
| 2586 | { |
| 2587 | int ret; |
| 2588 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 2589 | if (!ret) { |
| 2590 | btrfs_drop_nlink(inode); |
| 2591 | ret = btrfs_update_inode(trans, root, inode); |
| 2592 | } |
| 2593 | return ret; |
| 2594 | } |
| 2595 | |
| 2596 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2597 | /* helper to check if there is any shared block in the path */ |
| 2598 | static int check_path_shared(struct btrfs_root *root, |
| 2599 | struct btrfs_path *path) |
| 2600 | { |
| 2601 | struct extent_buffer *eb; |
| 2602 | int level; |
Dan Carpenter | 0e4dcbef | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 2603 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2604 | |
| 2605 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2606 | int ret; |
| 2607 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2608 | if (!path->nodes[level]) |
| 2609 | break; |
| 2610 | eb = path->nodes[level]; |
| 2611 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2612 | continue; |
| 2613 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2614 | &refs, NULL); |
| 2615 | if (refs > 1) |
| 2616 | return 1; |
| 2617 | } |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2618 | return 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | /* |
| 2622 | * helper to start transaction for unlink and rmdir. |
| 2623 | * |
| 2624 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2625 | * so in enospc case, we should make sure they will free space before |
| 2626 | * allowing them to use the global metadata reservation. |
| 2627 | */ |
| 2628 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2629 | struct dentry *dentry) |
| 2630 | { |
| 2631 | struct btrfs_trans_handle *trans; |
| 2632 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2633 | struct btrfs_path *path; |
| 2634 | struct btrfs_inode_ref *ref; |
| 2635 | struct btrfs_dir_item *di; |
| 2636 | struct inode *inode = dentry->d_inode; |
| 2637 | u64 index; |
| 2638 | int check_link = 1; |
| 2639 | int err = -ENOSPC; |
| 2640 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2641 | u64 ino = btrfs_ino(inode); |
| 2642 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2643 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 2644 | /* |
| 2645 | * 1 for the possible orphan item |
| 2646 | * 1 for the dir item |
| 2647 | * 1 for the dir index |
| 2648 | * 1 for the inode ref |
| 2649 | * 1 for the inode ref in the tree log |
| 2650 | * 2 for the dir entries in the log |
| 2651 | * 1 for the inode |
| 2652 | */ |
| 2653 | trans = btrfs_start_transaction(root, 8); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2654 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 2655 | return trans; |
| 2656 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2657 | if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2658 | return ERR_PTR(-ENOSPC); |
| 2659 | |
| 2660 | /* check if there is someone else holds reference */ |
| 2661 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 2662 | return ERR_PTR(-ENOSPC); |
| 2663 | |
| 2664 | if (atomic_read(&inode->i_count) > 2) |
| 2665 | return ERR_PTR(-ENOSPC); |
| 2666 | |
| 2667 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 2668 | return ERR_PTR(-ENOSPC); |
| 2669 | |
| 2670 | path = btrfs_alloc_path(); |
| 2671 | if (!path) { |
| 2672 | root->fs_info->enospc_unlink = 0; |
| 2673 | return ERR_PTR(-ENOMEM); |
| 2674 | } |
| 2675 | |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 2676 | /* 1 for the orphan item */ |
| 2677 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2678 | if (IS_ERR(trans)) { |
| 2679 | btrfs_free_path(path); |
| 2680 | root->fs_info->enospc_unlink = 0; |
| 2681 | return trans; |
| 2682 | } |
| 2683 | |
| 2684 | path->skip_locking = 1; |
| 2685 | path->search_commit_root = 1; |
| 2686 | |
| 2687 | ret = btrfs_lookup_inode(trans, root, path, |
| 2688 | &BTRFS_I(dir)->location, 0); |
| 2689 | if (ret < 0) { |
| 2690 | err = ret; |
| 2691 | goto out; |
| 2692 | } |
| 2693 | if (ret == 0) { |
| 2694 | if (check_path_shared(root, path)) |
| 2695 | goto out; |
| 2696 | } else { |
| 2697 | check_link = 0; |
| 2698 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2699 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2700 | |
| 2701 | ret = btrfs_lookup_inode(trans, root, path, |
| 2702 | &BTRFS_I(inode)->location, 0); |
| 2703 | if (ret < 0) { |
| 2704 | err = ret; |
| 2705 | goto out; |
| 2706 | } |
| 2707 | if (ret == 0) { |
| 2708 | if (check_path_shared(root, path)) |
| 2709 | goto out; |
| 2710 | } else { |
| 2711 | check_link = 0; |
| 2712 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2713 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2714 | |
| 2715 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 2716 | ret = btrfs_lookup_file_extent(trans, root, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2717 | ino, (u64)-1, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2718 | if (ret < 0) { |
| 2719 | err = ret; |
| 2720 | goto out; |
| 2721 | } |
| 2722 | BUG_ON(ret == 0); |
| 2723 | if (check_path_shared(root, path)) |
| 2724 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2725 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | if (!check_link) { |
| 2729 | err = 0; |
| 2730 | goto out; |
| 2731 | } |
| 2732 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2733 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2734 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2735 | if (IS_ERR(di)) { |
| 2736 | err = PTR_ERR(di); |
| 2737 | goto out; |
| 2738 | } |
| 2739 | if (di) { |
| 2740 | if (check_path_shared(root, path)) |
| 2741 | goto out; |
| 2742 | } else { |
| 2743 | err = 0; |
| 2744 | goto out; |
| 2745 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2746 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2747 | |
| 2748 | ref = btrfs_lookup_inode_ref(trans, root, path, |
| 2749 | dentry->d_name.name, dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2750 | ino, dir_ino, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2751 | if (IS_ERR(ref)) { |
| 2752 | err = PTR_ERR(ref); |
| 2753 | goto out; |
| 2754 | } |
| 2755 | BUG_ON(!ref); |
| 2756 | if (check_path_shared(root, path)) |
| 2757 | goto out; |
| 2758 | index = btrfs_inode_ref_index(path->nodes[0], ref); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2759 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2760 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2761 | /* |
| 2762 | * This is a commit root search, if we can lookup inode item and other |
| 2763 | * relative items in the commit root, it means the transaction of |
| 2764 | * dir/file creation has been committed, and the dir index item that we |
| 2765 | * delay to insert has also been inserted into the commit root. So |
| 2766 | * we needn't worry about the delayed insertion of the dir index item |
| 2767 | * here. |
| 2768 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2769 | di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2770 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2771 | if (IS_ERR(di)) { |
| 2772 | err = PTR_ERR(di); |
| 2773 | goto out; |
| 2774 | } |
| 2775 | BUG_ON(ret == -ENOENT); |
| 2776 | if (check_path_shared(root, path)) |
| 2777 | goto out; |
| 2778 | |
| 2779 | err = 0; |
| 2780 | out: |
| 2781 | btrfs_free_path(path); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 2782 | /* Migrate the orphan reservation over */ |
| 2783 | if (!err) |
| 2784 | err = btrfs_block_rsv_migrate(trans->block_rsv, |
| 2785 | &root->fs_info->global_block_rsv, |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 2786 | trans->bytes_reserved); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 2787 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2788 | if (err) { |
| 2789 | btrfs_end_transaction(trans, root); |
| 2790 | root->fs_info->enospc_unlink = 0; |
| 2791 | return ERR_PTR(err); |
| 2792 | } |
| 2793 | |
| 2794 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 2795 | return trans; |
| 2796 | } |
| 2797 | |
| 2798 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 2799 | struct btrfs_root *root) |
| 2800 | { |
| 2801 | if (trans->block_rsv == &root->fs_info->global_block_rsv) { |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 2802 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 2803 | trans->bytes_reserved); |
| 2804 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2805 | BUG_ON(!root->fs_info->enospc_unlink); |
| 2806 | root->fs_info->enospc_unlink = 0; |
| 2807 | } |
| 2808 | btrfs_end_transaction_throttle(trans, root); |
| 2809 | } |
| 2810 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2811 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2812 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2813 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2814 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2815 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2816 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2817 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2818 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2819 | trans = __unlink_start_trans(dir, dentry); |
| 2820 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2821 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2822 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2823 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2824 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2825 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2826 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2827 | if (ret) |
| 2828 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2829 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2830 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2831 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2832 | if (ret) |
| 2833 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2834 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2835 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2836 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2837 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2838 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2839 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2840 | return ret; |
| 2841 | } |
| 2842 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2843 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2844 | struct btrfs_root *root, |
| 2845 | struct inode *dir, u64 objectid, |
| 2846 | const char *name, int name_len) |
| 2847 | { |
| 2848 | struct btrfs_path *path; |
| 2849 | struct extent_buffer *leaf; |
| 2850 | struct btrfs_dir_item *di; |
| 2851 | struct btrfs_key key; |
| 2852 | u64 index; |
| 2853 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2854 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2855 | |
| 2856 | path = btrfs_alloc_path(); |
| 2857 | if (!path) |
| 2858 | return -ENOMEM; |
| 2859 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2860 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2861 | name, name_len, -1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2862 | BUG_ON(IS_ERR_OR_NULL(di)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2863 | |
| 2864 | leaf = path->nodes[0]; |
| 2865 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2866 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2867 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2868 | BUG_ON(ret); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2869 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2870 | |
| 2871 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2872 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2873 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2874 | if (ret < 0) { |
| 2875 | BUG_ON(ret != -ENOENT); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2876 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2877 | name, name_len); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2878 | BUG_ON(IS_ERR_OR_NULL(di)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2879 | |
| 2880 | leaf = path->nodes[0]; |
| 2881 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2882 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2883 | index = key.offset; |
| 2884 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2885 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2886 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2887 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
| 2888 | BUG_ON(ret); |
| 2889 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2890 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2891 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2892 | ret = btrfs_update_inode(trans, root, dir); |
| 2893 | BUG_ON(ret); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2894 | |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 2895 | btrfs_free_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2896 | return 0; |
| 2897 | } |
| 2898 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2899 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 2900 | { |
| 2901 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2902 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2903 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2904 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2905 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2906 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 2907 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2908 | btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 2909 | return -ENOTEMPTY; |
| 2910 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2911 | trans = __unlink_start_trans(dir, dentry); |
| 2912 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2913 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2914 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2915 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2916 | err = btrfs_unlink_subvol(trans, root, dir, |
| 2917 | BTRFS_I(inode)->location.objectid, |
| 2918 | dentry->d_name.name, |
| 2919 | dentry->d_name.len); |
| 2920 | goto out; |
| 2921 | } |
| 2922 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2923 | err = btrfs_orphan_add(trans, inode); |
| 2924 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2925 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2926 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2927 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2928 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2929 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2930 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2931 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2932 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2933 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2934 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2935 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2936 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2937 | return err; |
| 2938 | } |
| 2939 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2940 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2941 | * this can truncate away extent items, csum items and directory items. |
| 2942 | * 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] | 2943 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2944 | * |
| 2945 | * csum items that cross the new i_size are truncated to the new size |
| 2946 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2947 | * |
| 2948 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 2949 | * 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] | 2950 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2951 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 2952 | struct btrfs_root *root, |
| 2953 | struct inode *inode, |
| 2954 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2955 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2956 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2957 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2958 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2959 | struct btrfs_key key; |
| 2960 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2961 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2962 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2963 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2964 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2965 | u64 mask = root->sectorsize - 1; |
| 2966 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2967 | int found_extent; |
| 2968 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2969 | int pending_del_nr = 0; |
| 2970 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2971 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2972 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2973 | int ret; |
| 2974 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2975 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2976 | |
| 2977 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2978 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 2979 | path = btrfs_alloc_path(); |
| 2980 | if (!path) |
| 2981 | return -ENOMEM; |
| 2982 | path->reada = -1; |
| 2983 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2984 | if (root->ref_cows || root == root->fs_info->tree_root) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2985 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2986 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2987 | /* |
| 2988 | * This function is also used to drop the items in the log tree before |
| 2989 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 2990 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 2991 | * items. |
| 2992 | */ |
| 2993 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 2994 | btrfs_kill_delayed_inode_items(inode); |
| 2995 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2996 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2997 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2998 | key.type = (u8)-1; |
| 2999 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3000 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3001 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3002 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3003 | if (ret < 0) { |
| 3004 | err = ret; |
| 3005 | goto out; |
| 3006 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3007 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3008 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3009 | /* there are no items in the tree for us to truncate, we're |
| 3010 | * done |
| 3011 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3012 | if (path->slots[0] == 0) |
| 3013 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3014 | path->slots[0]--; |
| 3015 | } |
| 3016 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3017 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3018 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3019 | leaf = path->nodes[0]; |
| 3020 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3021 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3022 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3023 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3024 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3025 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3026 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3027 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3028 | break; |
| 3029 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3030 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3031 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3032 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3033 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3034 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3035 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 3036 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 3037 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 3038 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3039 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3040 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3041 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3042 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3043 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3044 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3045 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3046 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3047 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3048 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3049 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3050 | } else { |
| 3051 | if (item_end < new_size) |
| 3052 | break; |
| 3053 | if (found_key.offset >= new_size) |
| 3054 | del_item = 1; |
| 3055 | else |
| 3056 | del_item = 0; |
| 3057 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3058 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3059 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3060 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3061 | goto delete; |
| 3062 | |
| 3063 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3064 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3065 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3066 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3067 | u64 orig_num_bytes = |
| 3068 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3069 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3070 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3071 | extent_num_bytes = extent_num_bytes & |
| 3072 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3073 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3074 | extent_num_bytes); |
| 3075 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3076 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3077 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3078 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3079 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3080 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3081 | extent_num_bytes = |
| 3082 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3083 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3084 | extent_offset = found_key.offset - |
| 3085 | btrfs_file_extent_offset(leaf, fi); |
| 3086 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3087 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3088 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3089 | if (extent_start != 0) { |
| 3090 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3091 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3092 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3093 | } |
| 3094 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3095 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3096 | /* |
| 3097 | * we can't truncate inline items that have had |
| 3098 | * special encodings |
| 3099 | */ |
| 3100 | if (!del_item && |
| 3101 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3102 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3103 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3104 | u32 size = new_size - found_key.offset; |
| 3105 | |
| 3106 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3107 | inode_sub_bytes(inode, item_end + 1 - |
| 3108 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3109 | } |
| 3110 | size = |
| 3111 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3112 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3113 | size, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3114 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3115 | inode_sub_bytes(inode, item_end + 1 - |
| 3116 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3117 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3118 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3119 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3120 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3121 | if (!pending_del_nr) { |
| 3122 | /* no pending yet, add ourselves */ |
| 3123 | pending_del_slot = path->slots[0]; |
| 3124 | pending_del_nr = 1; |
| 3125 | } else if (pending_del_nr && |
| 3126 | path->slots[0] + 1 == pending_del_slot) { |
| 3127 | /* hop on the pending chunk */ |
| 3128 | pending_del_nr++; |
| 3129 | pending_del_slot = path->slots[0]; |
| 3130 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3131 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3132 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3133 | } else { |
| 3134 | break; |
| 3135 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3136 | if (found_extent && (root->ref_cows || |
| 3137 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3138 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3139 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3140 | extent_num_bytes, 0, |
| 3141 | btrfs_header_owner(leaf), |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3142 | ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3143 | BUG_ON(ret); |
| 3144 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3145 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3146 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3147 | break; |
| 3148 | |
| 3149 | if (path->slots[0] == 0 || |
| 3150 | path->slots[0] != pending_del_slot) { |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 3151 | if (root->ref_cows && |
| 3152 | BTRFS_I(inode)->location.objectid != |
| 3153 | BTRFS_FREE_INO_OBJECTID) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3154 | err = -EAGAIN; |
| 3155 | goto out; |
| 3156 | } |
| 3157 | if (pending_del_nr) { |
| 3158 | ret = btrfs_del_items(trans, root, path, |
| 3159 | pending_del_slot, |
| 3160 | pending_del_nr); |
| 3161 | BUG_ON(ret); |
| 3162 | pending_del_nr = 0; |
| 3163 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3164 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3165 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3166 | } else { |
| 3167 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3168 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3169 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3170 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3171 | if (pending_del_nr) { |
| 3172 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3173 | pending_del_nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3174 | BUG_ON(ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3175 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3176 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3177 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3178 | } |
| 3179 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3180 | /* |
| 3181 | * taken from block_truncate_page, but does cow as it zeros out |
| 3182 | * any bytes left in the last page in the file. |
| 3183 | */ |
| 3184 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3185 | { |
| 3186 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3187 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3188 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3189 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3190 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3191 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3192 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3193 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3194 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3195 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 3196 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3197 | int ret = 0; |
| 3198 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3199 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3200 | |
| 3201 | if ((offset & (blocksize - 1)) == 0) |
| 3202 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3203 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3204 | if (ret) |
| 3205 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3206 | |
| 3207 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3208 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 3209 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3210 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3211 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3212 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3213 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3214 | |
| 3215 | page_start = page_offset(page); |
| 3216 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3217 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3218 | if (!PageUptodate(page)) { |
| 3219 | ret = btrfs_readpage(NULL, page); |
| 3220 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3221 | if (page->mapping != mapping) { |
| 3222 | unlock_page(page); |
| 3223 | page_cache_release(page); |
| 3224 | goto again; |
| 3225 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3226 | if (!PageUptodate(page)) { |
| 3227 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3228 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3229 | } |
| 3230 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3231 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3232 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3233 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 3234 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3235 | set_page_extent_mapped(page); |
| 3236 | |
| 3237 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3238 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3239 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3240 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3241 | unlock_page(page); |
| 3242 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3243 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3244 | btrfs_put_ordered_extent(ordered); |
| 3245 | goto again; |
| 3246 | } |
| 3247 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3248 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3249 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3250 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3251 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3252 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3253 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3254 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3255 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3256 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3257 | goto out_unlock; |
| 3258 | } |
| 3259 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3260 | ret = 0; |
| 3261 | if (offset != PAGE_CACHE_SIZE) { |
| 3262 | kaddr = kmap(page); |
| 3263 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3264 | flush_dcache_page(page); |
| 3265 | kunmap(page); |
| 3266 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3267 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3268 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3269 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3270 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3271 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3272 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3273 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3274 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3275 | unlock_page(page); |
| 3276 | page_cache_release(page); |
| 3277 | out: |
| 3278 | return ret; |
| 3279 | } |
| 3280 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 3281 | /* |
| 3282 | * This function puts in dummy file extents for the area we're creating a hole |
| 3283 | * for. So if we are truncating this file to a larger size we need to insert |
| 3284 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 3285 | * the range between oldsize and size |
| 3286 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3287 | 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] | 3288 | { |
| 3289 | struct btrfs_trans_handle *trans; |
| 3290 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3291 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3292 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3293 | struct extent_state *cached_state = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3294 | u64 mask = root->sectorsize - 1; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3295 | u64 hole_start = (oldsize + mask) & ~mask; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3296 | u64 block_end = (size + mask) & ~mask; |
| 3297 | u64 last_byte; |
| 3298 | u64 cur_offset; |
| 3299 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3300 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3301 | |
| 3302 | if (size <= hole_start) |
| 3303 | return 0; |
| 3304 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3305 | while (1) { |
| 3306 | struct btrfs_ordered_extent *ordered; |
| 3307 | btrfs_wait_ordered_range(inode, hole_start, |
| 3308 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3309 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
| 3310 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3311 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3312 | if (!ordered) |
| 3313 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3314 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3315 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3316 | btrfs_put_ordered_extent(ordered); |
| 3317 | } |
| 3318 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3319 | cur_offset = hole_start; |
| 3320 | while (1) { |
| 3321 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3322 | block_end - cur_offset, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3323 | BUG_ON(IS_ERR_OR_NULL(em)); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3324 | last_byte = min(extent_map_end(em), block_end); |
| 3325 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3326 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3327 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3328 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3329 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3330 | trans = btrfs_start_transaction(root, 2); |
| 3331 | if (IS_ERR(trans)) { |
| 3332 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3333 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3334 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3335 | |
| 3336 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3337 | cur_offset + hole_size, |
| 3338 | &hint_byte, 1); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3339 | if (err) { |
| 3340 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3341 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3342 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3343 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3344 | err = btrfs_insert_file_extent(trans, root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3345 | btrfs_ino(inode), cur_offset, 0, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3346 | 0, hole_size, 0, hole_size, |
| 3347 | 0, 0, 0); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3348 | if (err) { |
| 3349 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3350 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3351 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3352 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3353 | btrfs_drop_extent_cache(inode, hole_start, |
| 3354 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3355 | |
| 3356 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3357 | } |
| 3358 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3359 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3360 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3361 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3362 | break; |
| 3363 | } |
| 3364 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3365 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3366 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3367 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3368 | return err; |
| 3369 | } |
| 3370 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3371 | static int btrfs_setsize(struct inode *inode, loff_t newsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3372 | { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3373 | loff_t oldsize = i_size_read(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3374 | int ret; |
| 3375 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3376 | if (newsize == oldsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3377 | return 0; |
| 3378 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3379 | if (newsize > oldsize) { |
| 3380 | i_size_write(inode, newsize); |
| 3381 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 3382 | truncate_pagecache(inode, oldsize, newsize); |
| 3383 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3384 | if (ret) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3385 | btrfs_setsize(inode, oldsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3386 | return ret; |
| 3387 | } |
| 3388 | |
Josef Bacik | 930f028 | 2011-03-04 14:41:41 -0500 | [diff] [blame] | 3389 | mark_inode_dirty(inode); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3390 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3391 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3392 | /* |
| 3393 | * We're truncating a file that used to have good data down to |
| 3394 | * zero. Make sure it gets into the ordered flush list so that |
| 3395 | * any new writes get down to disk quickly. |
| 3396 | */ |
| 3397 | if (newsize == 0) |
| 3398 | BTRFS_I(inode)->ordered_data_close = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3399 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3400 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3401 | truncate_setsize(inode, newsize); |
| 3402 | ret = btrfs_truncate(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3405 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3408 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3409 | { |
| 3410 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3411 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3412 | int err; |
| 3413 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3414 | if (btrfs_root_readonly(root)) |
| 3415 | return -EROFS; |
| 3416 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3417 | err = inode_change_ok(inode, attr); |
| 3418 | if (err) |
| 3419 | return err; |
| 3420 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3421 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3422 | err = btrfs_setsize(inode, attr->ia_size); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3423 | if (err) |
| 3424 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3425 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3426 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3427 | if (attr->ia_valid) { |
| 3428 | setattr_copy(inode, attr); |
| 3429 | mark_inode_dirty(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3430 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3431 | if (attr->ia_valid & ATTR_MODE) |
| 3432 | err = btrfs_acl_chmod(inode); |
| 3433 | } |
| 3434 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3435 | return err; |
| 3436 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3437 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3438 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3439 | { |
| 3440 | struct btrfs_trans_handle *trans; |
| 3441 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3442 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 3443 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3444 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3445 | int ret; |
| 3446 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3447 | trace_btrfs_inode_evict(inode); |
| 3448 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3449 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3450 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 3451 | btrfs_is_free_space_inode(root, inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3452 | goto no_delete; |
| 3453 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3454 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3455 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3456 | goto no_delete; |
| 3457 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3458 | /* 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] | 3459 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3460 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3461 | if (root->fs_info->log_root_recovering) { |
| 3462 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3463 | goto no_delete; |
| 3464 | } |
| 3465 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3466 | if (inode->i_nlink > 0) { |
| 3467 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3468 | goto no_delete; |
| 3469 | } |
| 3470 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3471 | rsv = btrfs_alloc_block_rsv(root); |
| 3472 | if (!rsv) { |
| 3473 | btrfs_orphan_del(NULL, inode); |
| 3474 | goto no_delete; |
| 3475 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 3476 | rsv->size = min_size; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3477 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3478 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3479 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3480 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3481 | /* |
| 3482 | * This is a bit simpler than btrfs_truncate since |
| 3483 | * |
| 3484 | * 1) We've already reserved our space for our orphan item in the |
| 3485 | * unlink. |
| 3486 | * 2) We're going to delete the inode item, so we don't need to update |
| 3487 | * it at all. |
| 3488 | * |
| 3489 | * So we just need to reserve some slack space in case we add bytes when |
| 3490 | * doing the truncate. |
| 3491 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3492 | while (1) { |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 3493 | ret = btrfs_block_rsv_refill(root, rsv, min_size); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3494 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3495 | /* |
| 3496 | * Try and steal from the global reserve since we will |
| 3497 | * likely not use this space anyway, we want to try as |
| 3498 | * hard as possible to get this to work. |
| 3499 | */ |
| 3500 | if (ret) |
| 3501 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 3502 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3503 | if (ret) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3504 | printk(KERN_WARNING "Could not get space for a " |
Josef Bacik | 482e6dc | 2011-08-19 10:31:56 -0400 | [diff] [blame] | 3505 | "delete, will truncate on mount %d\n", ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3506 | btrfs_orphan_del(NULL, inode); |
| 3507 | btrfs_free_block_rsv(root, rsv); |
| 3508 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3509 | } |
| 3510 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3511 | trans = btrfs_start_transaction(root, 0); |
| 3512 | if (IS_ERR(trans)) { |
| 3513 | btrfs_orphan_del(NULL, inode); |
| 3514 | btrfs_free_block_rsv(root, rsv); |
| 3515 | goto no_delete; |
| 3516 | } |
| 3517 | |
| 3518 | trans->block_rsv = rsv; |
| 3519 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3520 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3521 | if (ret != -EAGAIN) |
| 3522 | break; |
| 3523 | |
| 3524 | nr = trans->blocks_used; |
| 3525 | btrfs_end_transaction(trans, root); |
| 3526 | trans = NULL; |
| 3527 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3528 | } |
| 3529 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3530 | btrfs_free_block_rsv(root, rsv); |
| 3531 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3532 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3533 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3534 | ret = btrfs_orphan_del(trans, inode); |
| 3535 | BUG_ON(ret); |
| 3536 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3537 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3538 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3539 | if (!(root == root->fs_info->tree_root || |
| 3540 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3541 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3542 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3543 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3544 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3545 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3546 | no_delete: |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3547 | end_writeback(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3548 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | /* |
| 3552 | * this returns the key found in the dir entry in the location pointer. |
| 3553 | * If no dir entries were found, location->objectid is 0. |
| 3554 | */ |
| 3555 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3556 | struct btrfs_key *location) |
| 3557 | { |
| 3558 | const char *name = dentry->d_name.name; |
| 3559 | int namelen = dentry->d_name.len; |
| 3560 | struct btrfs_dir_item *di; |
| 3561 | struct btrfs_path *path; |
| 3562 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3563 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3564 | |
| 3565 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 3566 | if (!path) |
| 3567 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3568 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3569 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3570 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3571 | if (IS_ERR(di)) |
| 3572 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3573 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3574 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3575 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3576 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3577 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3578 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3579 | btrfs_free_path(path); |
| 3580 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3581 | out_err: |
| 3582 | location->objectid = 0; |
| 3583 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | /* |
| 3587 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3588 | * needs to be changed to reflect the root directory of the tree root. This |
| 3589 | * is kind of like crossing a mount point. |
| 3590 | */ |
| 3591 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3592 | struct inode *dir, |
| 3593 | struct dentry *dentry, |
| 3594 | struct btrfs_key *location, |
| 3595 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3596 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3597 | struct btrfs_path *path; |
| 3598 | struct btrfs_root *new_root; |
| 3599 | struct btrfs_root_ref *ref; |
| 3600 | struct extent_buffer *leaf; |
| 3601 | int ret; |
| 3602 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3603 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3604 | path = btrfs_alloc_path(); |
| 3605 | if (!path) { |
| 3606 | err = -ENOMEM; |
| 3607 | goto out; |
| 3608 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3609 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3610 | err = -ENOENT; |
| 3611 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3612 | BTRFS_I(dir)->root->root_key.objectid, |
| 3613 | location->objectid); |
| 3614 | if (ret) { |
| 3615 | if (ret < 0) |
| 3616 | err = ret; |
| 3617 | goto out; |
| 3618 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3619 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3620 | leaf = path->nodes[0]; |
| 3621 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3622 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3623 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3624 | goto out; |
| 3625 | |
| 3626 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3627 | (unsigned long)(ref + 1), |
| 3628 | dentry->d_name.len); |
| 3629 | if (ret) |
| 3630 | goto out; |
| 3631 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3632 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3633 | |
| 3634 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3635 | if (IS_ERR(new_root)) { |
| 3636 | err = PTR_ERR(new_root); |
| 3637 | goto out; |
| 3638 | } |
| 3639 | |
| 3640 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3641 | err = -ENOENT; |
| 3642 | goto out; |
| 3643 | } |
| 3644 | |
| 3645 | *sub_root = new_root; |
| 3646 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3647 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3648 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3649 | err = 0; |
| 3650 | out: |
| 3651 | btrfs_free_path(path); |
| 3652 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3653 | } |
| 3654 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3655 | static void inode_tree_add(struct inode *inode) |
| 3656 | { |
| 3657 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3658 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3659 | struct rb_node **p; |
| 3660 | struct rb_node *parent; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3661 | u64 ino = btrfs_ino(inode); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3662 | again: |
| 3663 | p = &root->inode_tree.rb_node; |
| 3664 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3665 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 3666 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3667 | return; |
| 3668 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3669 | spin_lock(&root->inode_lock); |
| 3670 | while (*p) { |
| 3671 | parent = *p; |
| 3672 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3673 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3674 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3675 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3676 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3677 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3678 | else { |
| 3679 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 3680 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3681 | rb_erase(parent, &root->inode_tree); |
| 3682 | RB_CLEAR_NODE(parent); |
| 3683 | spin_unlock(&root->inode_lock); |
| 3684 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3685 | } |
| 3686 | } |
| 3687 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3688 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3689 | spin_unlock(&root->inode_lock); |
| 3690 | } |
| 3691 | |
| 3692 | static void inode_tree_del(struct inode *inode) |
| 3693 | { |
| 3694 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3695 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3696 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3697 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3698 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3699 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3700 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3701 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3702 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3703 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3704 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3705 | /* |
| 3706 | * Free space cache has inodes in the tree root, but the tree root has a |
| 3707 | * root_refs of 0, so this could end up dropping the tree root as a |
| 3708 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 3709 | * make sure we don't drop it. |
| 3710 | */ |
| 3711 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 3712 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3713 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3714 | spin_lock(&root->inode_lock); |
| 3715 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3716 | spin_unlock(&root->inode_lock); |
| 3717 | if (empty) |
| 3718 | btrfs_add_dead_root(root); |
| 3719 | } |
| 3720 | } |
| 3721 | |
| 3722 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 3723 | { |
| 3724 | struct rb_node *node; |
| 3725 | struct rb_node *prev; |
| 3726 | struct btrfs_inode *entry; |
| 3727 | struct inode *inode; |
| 3728 | u64 objectid = 0; |
| 3729 | |
| 3730 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3731 | |
| 3732 | spin_lock(&root->inode_lock); |
| 3733 | again: |
| 3734 | node = root->inode_tree.rb_node; |
| 3735 | prev = NULL; |
| 3736 | while (node) { |
| 3737 | prev = node; |
| 3738 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3739 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3740 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3741 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3742 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3743 | node = node->rb_right; |
| 3744 | else |
| 3745 | break; |
| 3746 | } |
| 3747 | if (!node) { |
| 3748 | while (prev) { |
| 3749 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3750 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3751 | node = prev; |
| 3752 | break; |
| 3753 | } |
| 3754 | prev = rb_next(prev); |
| 3755 | } |
| 3756 | } |
| 3757 | while (node) { |
| 3758 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3759 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3760 | inode = igrab(&entry->vfs_inode); |
| 3761 | if (inode) { |
| 3762 | spin_unlock(&root->inode_lock); |
| 3763 | if (atomic_read(&inode->i_count) > 1) |
| 3764 | d_prune_aliases(inode); |
| 3765 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 3766 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3767 | * the inode cache when its usage count |
| 3768 | * hits zero. |
| 3769 | */ |
| 3770 | iput(inode); |
| 3771 | cond_resched(); |
| 3772 | spin_lock(&root->inode_lock); |
| 3773 | goto again; |
| 3774 | } |
| 3775 | |
| 3776 | if (cond_resched_lock(&root->inode_lock)) |
| 3777 | goto again; |
| 3778 | |
| 3779 | node = rb_next(node); |
| 3780 | } |
| 3781 | spin_unlock(&root->inode_lock); |
| 3782 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3783 | } |
| 3784 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3785 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 3786 | { |
| 3787 | struct btrfs_iget_args *args = p; |
| 3788 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3789 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3790 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3791 | return 0; |
| 3792 | } |
| 3793 | |
| 3794 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 3795 | { |
| 3796 | struct btrfs_iget_args *args = opaque; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3797 | return args->ino == btrfs_ino(inode) && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3798 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3799 | } |
| 3800 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3801 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 3802 | u64 objectid, |
| 3803 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3804 | { |
| 3805 | struct inode *inode; |
| 3806 | struct btrfs_iget_args args; |
| 3807 | args.ino = objectid; |
| 3808 | args.root = root; |
| 3809 | |
| 3810 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 3811 | btrfs_init_locked_inode, |
| 3812 | (void *)&args); |
| 3813 | return inode; |
| 3814 | } |
| 3815 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3816 | /* Get an inode object given its location and corresponding root. |
| 3817 | * Returns in *is_new if the inode was read from disk |
| 3818 | */ |
| 3819 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3820 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3821 | { |
| 3822 | struct inode *inode; |
| 3823 | |
| 3824 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 3825 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3826 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3827 | |
| 3828 | if (inode->i_state & I_NEW) { |
| 3829 | BTRFS_I(inode)->root = root; |
| 3830 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 3831 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3832 | if (!is_bad_inode(inode)) { |
| 3833 | inode_tree_add(inode); |
| 3834 | unlock_new_inode(inode); |
| 3835 | if (new) |
| 3836 | *new = 1; |
| 3837 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3838 | unlock_new_inode(inode); |
| 3839 | iput(inode); |
| 3840 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3841 | } |
| 3842 | } |
| 3843 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3844 | return inode; |
| 3845 | } |
| 3846 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3847 | static struct inode *new_simple_dir(struct super_block *s, |
| 3848 | struct btrfs_key *key, |
| 3849 | struct btrfs_root *root) |
| 3850 | { |
| 3851 | struct inode *inode = new_inode(s); |
| 3852 | |
| 3853 | if (!inode) |
| 3854 | return ERR_PTR(-ENOMEM); |
| 3855 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3856 | BTRFS_I(inode)->root = root; |
| 3857 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 3858 | BTRFS_I(inode)->dummy_inode = 1; |
| 3859 | |
| 3860 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 3861 | inode->i_op = &simple_dir_inode_operations; |
| 3862 | inode->i_fop = &simple_dir_operations; |
| 3863 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 3864 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 3865 | |
| 3866 | return inode; |
| 3867 | } |
| 3868 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3869 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3870 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3871 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3872 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3873 | struct btrfs_root *sub_root = root; |
| 3874 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3875 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 3876 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3877 | |
| 3878 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 3879 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3880 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 3881 | if (unlikely(d_need_lookup(dentry))) { |
| 3882 | memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key)); |
| 3883 | kfree(dentry->d_fsdata); |
| 3884 | dentry->d_fsdata = NULL; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 3885 | /* This thing is hashed, drop it for now */ |
| 3886 | d_drop(dentry); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 3887 | } else { |
| 3888 | ret = btrfs_inode_by_name(dir, dentry, &location); |
| 3889 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3890 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3891 | if (ret < 0) |
| 3892 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3893 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3894 | if (location.objectid == 0) |
| 3895 | return NULL; |
| 3896 | |
| 3897 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3898 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3899 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3900 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3901 | |
| 3902 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 3903 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3904 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3905 | ret = fixup_tree_root_location(root, dir, dentry, |
| 3906 | &location, &sub_root); |
| 3907 | if (ret < 0) { |
| 3908 | if (ret != -ENOENT) |
| 3909 | inode = ERR_PTR(ret); |
| 3910 | else |
| 3911 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 3912 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3913 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3914 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3915 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 3916 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 3917 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3918 | down_read(&root->fs_info->cleanup_work_sem); |
| 3919 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3920 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3921 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3922 | if (ret) |
| 3923 | inode = ERR_PTR(ret); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3926 | return inode; |
| 3927 | } |
| 3928 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 3929 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3930 | { |
| 3931 | struct btrfs_root *root; |
| 3932 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 3933 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 3934 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3935 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 3936 | if (dentry->d_inode) { |
| 3937 | root = BTRFS_I(dentry->d_inode)->root; |
| 3938 | if (btrfs_root_refs(&root->root_item) == 0) |
| 3939 | return 1; |
| 3940 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3941 | return 0; |
| 3942 | } |
| 3943 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 3944 | static void btrfs_dentry_release(struct dentry *dentry) |
| 3945 | { |
| 3946 | if (dentry->d_fsdata) |
| 3947 | kfree(dentry->d_fsdata); |
| 3948 | } |
| 3949 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3950 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 3951 | struct nameidata *nd) |
| 3952 | { |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 3953 | struct dentry *ret; |
| 3954 | |
| 3955 | ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); |
| 3956 | if (unlikely(d_need_lookup(dentry))) { |
| 3957 | spin_lock(&dentry->d_lock); |
| 3958 | dentry->d_flags &= ~DCACHE_NEED_LOOKUP; |
| 3959 | spin_unlock(&dentry->d_lock); |
| 3960 | } |
| 3961 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3962 | } |
| 3963 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3964 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3965 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 3966 | }; |
| 3967 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 3968 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 3969 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3970 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 3971 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3972 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3973 | struct btrfs_item *item; |
| 3974 | struct btrfs_dir_item *di; |
| 3975 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3976 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3977 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3978 | struct list_head ins_list; |
| 3979 | struct list_head del_list; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 3980 | struct qstr q; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3981 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3982 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3983 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3984 | unsigned char d_type; |
| 3985 | int over = 0; |
| 3986 | u32 di_cur; |
| 3987 | u32 di_total; |
| 3988 | u32 di_len; |
| 3989 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3990 | char tmp_name[32]; |
| 3991 | char *name_ptr; |
| 3992 | int name_len; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3993 | int is_curr = 0; /* filp->f_pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3994 | |
| 3995 | /* FIXME, use a real flag for deciding about the key type */ |
| 3996 | if (root->fs_info->tree_root == root) |
| 3997 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3998 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3999 | /* special case for "." */ |
| 4000 | if (filp->f_pos == 0) { |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 4001 | over = filldir(dirent, ".", 1, |
| 4002 | filp->f_pos, btrfs_ino(inode), DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4003 | if (over) |
| 4004 | return 0; |
| 4005 | filp->f_pos = 1; |
| 4006 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4007 | /* special case for .., just use the back ref */ |
| 4008 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4009 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4010 | over = filldir(dirent, "..", 2, |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 4011 | filp->f_pos, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4012 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4013 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4014 | filp->f_pos = 2; |
| 4015 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4016 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4017 | if (!path) |
| 4018 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4019 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 4020 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4021 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4022 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4023 | INIT_LIST_HEAD(&ins_list); |
| 4024 | INIT_LIST_HEAD(&del_list); |
| 4025 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 4026 | } |
| 4027 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4028 | btrfs_set_key_type(&key, key_type); |
| 4029 | key.offset = filp->f_pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4030 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4031 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4032 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4033 | if (ret < 0) |
| 4034 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4035 | |
| 4036 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4037 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4038 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4039 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4040 | ret = btrfs_next_leaf(root, path); |
| 4041 | if (ret < 0) |
| 4042 | goto err; |
| 4043 | else if (ret > 0) |
| 4044 | break; |
| 4045 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4046 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4047 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4048 | item = btrfs_item_nr(leaf, slot); |
| 4049 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4050 | |
| 4051 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4052 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4053 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4054 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4055 | if (found_key.offset < filp->f_pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4056 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4057 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 4058 | btrfs_should_delete_dir_index(&del_list, |
| 4059 | found_key.offset)) |
| 4060 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4061 | |
| 4062 | filp->f_pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4063 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4064 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4065 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 4066 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4067 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4068 | |
| 4069 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4070 | struct btrfs_key location; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4071 | struct dentry *tmp; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4072 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 4073 | if (verify_dir_item(root, leaf, di)) |
| 4074 | break; |
| 4075 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4076 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4077 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4078 | name_ptr = tmp_name; |
| 4079 | } else { |
| 4080 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4081 | if (!name_ptr) { |
| 4082 | ret = -ENOMEM; |
| 4083 | goto err; |
| 4084 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4085 | } |
| 4086 | read_extent_buffer(leaf, name_ptr, |
| 4087 | (unsigned long)(di + 1), name_len); |
| 4088 | |
| 4089 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4090 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4091 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4092 | q.name = name_ptr; |
| 4093 | q.len = name_len; |
| 4094 | q.hash = full_name_hash(q.name, q.len); |
| 4095 | tmp = d_lookup(filp->f_dentry, &q); |
| 4096 | if (!tmp) { |
| 4097 | struct btrfs_key *newkey; |
| 4098 | |
| 4099 | newkey = kzalloc(sizeof(struct btrfs_key), |
| 4100 | GFP_NOFS); |
| 4101 | if (!newkey) |
| 4102 | goto no_dentry; |
| 4103 | tmp = d_alloc(filp->f_dentry, &q); |
| 4104 | if (!tmp) { |
| 4105 | kfree(newkey); |
| 4106 | dput(tmp); |
| 4107 | goto no_dentry; |
| 4108 | } |
| 4109 | memcpy(newkey, &location, |
| 4110 | sizeof(struct btrfs_key)); |
| 4111 | tmp->d_fsdata = newkey; |
| 4112 | tmp->d_flags |= DCACHE_NEED_LOOKUP; |
| 4113 | d_rehash(tmp); |
| 4114 | dput(tmp); |
| 4115 | } else { |
| 4116 | dput(tmp); |
| 4117 | } |
| 4118 | no_dentry: |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4119 | /* is this a reference to our own snapshot? If so |
| 4120 | * skip it |
| 4121 | */ |
| 4122 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4123 | location.objectid == root->root_key.objectid) { |
| 4124 | over = 0; |
| 4125 | goto skip; |
| 4126 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4127 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4128 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4129 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4130 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4131 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4132 | if (name_ptr != tmp_name) |
| 4133 | kfree(name_ptr); |
| 4134 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4135 | if (over) |
| 4136 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4137 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4138 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4139 | di_cur += di_len; |
| 4140 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4141 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4142 | next: |
| 4143 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4144 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4145 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4146 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4147 | if (is_curr) |
| 4148 | filp->f_pos++; |
| 4149 | ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir, |
| 4150 | &ins_list); |
| 4151 | if (ret) |
| 4152 | goto nopos; |
| 4153 | } |
| 4154 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4155 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4156 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4157 | /* |
| 4158 | * 32-bit glibc will use getdents64, but then strtol - |
| 4159 | * so the last number we can serve is this. |
| 4160 | */ |
| 4161 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4162 | else |
| 4163 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4164 | nopos: |
| 4165 | ret = 0; |
| 4166 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4167 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 4168 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4169 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4170 | return ret; |
| 4171 | } |
| 4172 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4173 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4174 | { |
| 4175 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4176 | struct btrfs_trans_handle *trans; |
| 4177 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4178 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4179 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4180 | if (BTRFS_I(inode)->dummy_inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4181 | return 0; |
| 4182 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 4183 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(root, inode)) |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 4184 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4185 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4186 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4187 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4188 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4189 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4190 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4191 | if (IS_ERR(trans)) |
| 4192 | return PTR_ERR(trans); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4193 | if (nolock) |
| 4194 | ret = btrfs_end_transaction_nolock(trans, root); |
| 4195 | else |
| 4196 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4197 | } |
| 4198 | return ret; |
| 4199 | } |
| 4200 | |
| 4201 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4202 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4203 | * inode changes. But, it is most likely to find the inode in cache. |
| 4204 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4205 | * to keep or drop this code. |
| 4206 | */ |
Christoph Hellwig | aa38572 | 2011-05-27 06:53:02 -0400 | [diff] [blame] | 4207 | void btrfs_dirty_inode(struct inode *inode, int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4208 | { |
| 4209 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4210 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4211 | int ret; |
| 4212 | |
| 4213 | if (BTRFS_I(inode)->dummy_inode) |
| 4214 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4215 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4216 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4217 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4218 | |
| 4219 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4220 | if (ret && ret == -ENOSPC) { |
| 4221 | /* whoops, lets try again with the full transaction */ |
| 4222 | btrfs_end_transaction(trans, root); |
| 4223 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4224 | if (IS_ERR(trans)) { |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 4225 | printk_ratelimited(KERN_ERR "btrfs: fail to " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4226 | "dirty inode %llu error %ld\n", |
| 4227 | (unsigned long long)btrfs_ino(inode), |
| 4228 | PTR_ERR(trans)); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4229 | return; |
| 4230 | } |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4231 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4232 | ret = btrfs_update_inode(trans, root, inode); |
| 4233 | if (ret) { |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 4234 | printk_ratelimited(KERN_ERR "btrfs: fail to " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4235 | "dirty inode %llu error %d\n", |
| 4236 | (unsigned long long)btrfs_ino(inode), |
| 4237 | ret); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4238 | } |
| 4239 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4240 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4241 | if (BTRFS_I(inode)->delayed_node) |
| 4242 | btrfs_balance_delayed_items(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4243 | } |
| 4244 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4245 | /* |
| 4246 | * find the highest existing sequence number in a directory |
| 4247 | * and then set the in-memory index_cnt variable to reflect |
| 4248 | * free sequence numbers |
| 4249 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4250 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4251 | { |
| 4252 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4253 | struct btrfs_key key, found_key; |
| 4254 | struct btrfs_path *path; |
| 4255 | struct extent_buffer *leaf; |
| 4256 | int ret; |
| 4257 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4258 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4259 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4260 | key.offset = (u64)-1; |
| 4261 | |
| 4262 | path = btrfs_alloc_path(); |
| 4263 | if (!path) |
| 4264 | return -ENOMEM; |
| 4265 | |
| 4266 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4267 | if (ret < 0) |
| 4268 | goto out; |
| 4269 | /* FIXME: we should be able to handle this */ |
| 4270 | if (ret == 0) |
| 4271 | goto out; |
| 4272 | ret = 0; |
| 4273 | |
| 4274 | /* |
| 4275 | * MAGIC NUMBER EXPLANATION: |
| 4276 | * since we search a directory based on f_pos we have to start at 2 |
| 4277 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4278 | * else has to start at 2 |
| 4279 | */ |
| 4280 | if (path->slots[0] == 0) { |
| 4281 | BTRFS_I(inode)->index_cnt = 2; |
| 4282 | goto out; |
| 4283 | } |
| 4284 | |
| 4285 | path->slots[0]--; |
| 4286 | |
| 4287 | leaf = path->nodes[0]; |
| 4288 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4289 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4290 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4291 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4292 | BTRFS_I(inode)->index_cnt = 2; |
| 4293 | goto out; |
| 4294 | } |
| 4295 | |
| 4296 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4297 | out: |
| 4298 | btrfs_free_path(path); |
| 4299 | return ret; |
| 4300 | } |
| 4301 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4302 | /* |
| 4303 | * helper to find a free sequence number in a given directory. This current |
| 4304 | * code is very simple, later versions will do smarter things in the btree |
| 4305 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4306 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4307 | { |
| 4308 | int ret = 0; |
| 4309 | |
| 4310 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4311 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 4312 | if (ret) { |
| 4313 | ret = btrfs_set_inode_index_count(dir); |
| 4314 | if (ret) |
| 4315 | return ret; |
| 4316 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4317 | } |
| 4318 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4319 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4320 | BTRFS_I(dir)->index_cnt++; |
| 4321 | |
| 4322 | return ret; |
| 4323 | } |
| 4324 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4325 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4326 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4327 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4328 | const char *name, int name_len, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4329 | u64 ref_objectid, u64 objectid, int mode, |
| 4330 | u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4331 | { |
| 4332 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4333 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4334 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4335 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4336 | struct btrfs_inode_ref *ref; |
| 4337 | struct btrfs_key key[2]; |
| 4338 | u32 sizes[2]; |
| 4339 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4340 | int ret; |
| 4341 | int owner; |
| 4342 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4343 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4344 | if (!path) |
| 4345 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4346 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4347 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4348 | if (!inode) { |
| 4349 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4350 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4351 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4352 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4353 | /* |
| 4354 | * we have to initialize this early, so we can reclaim the inode |
| 4355 | * number if we fail afterwards in this function. |
| 4356 | */ |
| 4357 | inode->i_ino = objectid; |
| 4358 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4359 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4360 | trace_btrfs_inode_request(dir); |
| 4361 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4362 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4363 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4364 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4365 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4366 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4367 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4368 | } |
| 4369 | /* |
| 4370 | * index_cnt is ignored for everything but a dir, |
| 4371 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4372 | * number |
| 4373 | */ |
| 4374 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4375 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4376 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 4377 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4378 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4379 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 4380 | if (S_ISDIR(mode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4381 | owner = 0; |
| 4382 | else |
| 4383 | owner = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4384 | |
| 4385 | key[0].objectid = objectid; |
| 4386 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4387 | key[0].offset = 0; |
| 4388 | |
| 4389 | key[1].objectid = objectid; |
| 4390 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4391 | key[1].offset = ref_objectid; |
| 4392 | |
| 4393 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4394 | sizes[1] = name_len + sizeof(*ref); |
| 4395 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4396 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4397 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4398 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4399 | goto fail; |
| 4400 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 4401 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4402 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4403 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4404 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4405 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4406 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4407 | |
| 4408 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4409 | struct btrfs_inode_ref); |
| 4410 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4411 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4412 | ptr = (unsigned long)(ref + 1); |
| 4413 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4414 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4415 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4416 | btrfs_free_path(path); |
| 4417 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4418 | location = &BTRFS_I(inode)->location; |
| 4419 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4420 | location->offset = 0; |
| 4421 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4422 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4423 | btrfs_inherit_iflags(inode, dir); |
| 4424 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 4425 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4426 | if (btrfs_test_opt(root, NODATASUM)) |
| 4427 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 4428 | if (btrfs_test_opt(root, NODATACOW) || |
| 4429 | (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4430 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4431 | } |
| 4432 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4433 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4434 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4435 | |
| 4436 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 4437 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4438 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4439 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4440 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4441 | if (dir) |
| 4442 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4443 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4444 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4445 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4446 | } |
| 4447 | |
| 4448 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4449 | { |
| 4450 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4451 | } |
| 4452 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4453 | /* |
| 4454 | * utility function to add 'inode' into 'parent_inode' with |
| 4455 | * a give name and a given sequence number. |
| 4456 | * if 'add_backref' is true, also insert a backref from the |
| 4457 | * inode to the parent directory. |
| 4458 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4459 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4460 | struct inode *parent_inode, struct inode *inode, |
| 4461 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4462 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4463 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4464 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4465 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4466 | u64 ino = btrfs_ino(inode); |
| 4467 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4468 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4469 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4470 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4471 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4472 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4473 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4474 | key.offset = 0; |
| 4475 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4476 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4477 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4478 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4479 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4480 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4481 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4482 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 4483 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4484 | } |
| 4485 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4486 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4487 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4488 | parent_inode, &key, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4489 | btrfs_inode_type(inode), index); |
| 4490 | BUG_ON(ret); |
| 4491 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4492 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4493 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4494 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4495 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4496 | } |
| 4497 | return ret; |
| 4498 | } |
| 4499 | |
| 4500 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4501 | struct inode *dir, struct dentry *dentry, |
| 4502 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4503 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4504 | int err = btrfs_add_link(trans, dir, inode, |
| 4505 | dentry->d_name.name, dentry->d_name.len, |
| 4506 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4507 | if (!err) { |
| 4508 | d_instantiate(dentry, inode); |
| 4509 | return 0; |
| 4510 | } |
| 4511 | if (err > 0) |
| 4512 | err = -EEXIST; |
| 4513 | return err; |
| 4514 | } |
| 4515 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4516 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4517 | int mode, dev_t rdev) |
| 4518 | { |
| 4519 | struct btrfs_trans_handle *trans; |
| 4520 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4521 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4522 | int err; |
| 4523 | int drop_inode = 0; |
| 4524 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4525 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4526 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4527 | |
| 4528 | if (!new_valid_dev(rdev)) |
| 4529 | return -EINVAL; |
| 4530 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4531 | /* |
| 4532 | * 2 for inode item and ref |
| 4533 | * 2 for dir items |
| 4534 | * 1 for xattr if selinux is on |
| 4535 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4536 | trans = btrfs_start_transaction(root, 5); |
| 4537 | if (IS_ERR(trans)) |
| 4538 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4539 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4540 | err = btrfs_find_free_ino(root, &objectid); |
| 4541 | if (err) |
| 4542 | goto out_unlock; |
| 4543 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4544 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4545 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4546 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4547 | if (IS_ERR(inode)) { |
| 4548 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4549 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4550 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4551 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4552 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4553 | if (err) { |
| 4554 | drop_inode = 1; |
| 4555 | goto out_unlock; |
| 4556 | } |
| 4557 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4558 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4559 | if (err) |
| 4560 | drop_inode = 1; |
| 4561 | else { |
| 4562 | inode->i_op = &btrfs_special_inode_operations; |
| 4563 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4564 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4565 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4566 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4567 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4568 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4569 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4570 | if (drop_inode) { |
| 4571 | inode_dec_link_count(inode); |
| 4572 | iput(inode); |
| 4573 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4574 | return err; |
| 4575 | } |
| 4576 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4577 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4578 | int mode, struct nameidata *nd) |
| 4579 | { |
| 4580 | struct btrfs_trans_handle *trans; |
| 4581 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4582 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4583 | int drop_inode = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4584 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4585 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4586 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4587 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4588 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4589 | /* |
| 4590 | * 2 for inode item and ref |
| 4591 | * 2 for dir items |
| 4592 | * 1 for xattr if selinux is on |
| 4593 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4594 | trans = btrfs_start_transaction(root, 5); |
| 4595 | if (IS_ERR(trans)) |
| 4596 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4597 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4598 | err = btrfs_find_free_ino(root, &objectid); |
| 4599 | if (err) |
| 4600 | goto out_unlock; |
| 4601 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4602 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4603 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4604 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4605 | if (IS_ERR(inode)) { |
| 4606 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4607 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4608 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4609 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4610 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4611 | if (err) { |
| 4612 | drop_inode = 1; |
| 4613 | goto out_unlock; |
| 4614 | } |
| 4615 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4616 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4617 | if (err) |
| 4618 | drop_inode = 1; |
| 4619 | else { |
| 4620 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4621 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4622 | inode->i_fop = &btrfs_file_operations; |
| 4623 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4624 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4625 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4626 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4627 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4628 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4629 | if (drop_inode) { |
| 4630 | inode_dec_link_count(inode); |
| 4631 | iput(inode); |
| 4632 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4633 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4634 | return err; |
| 4635 | } |
| 4636 | |
| 4637 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4638 | struct dentry *dentry) |
| 4639 | { |
| 4640 | struct btrfs_trans_handle *trans; |
| 4641 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4642 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4643 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4644 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4645 | int err; |
| 4646 | int drop_inode = 0; |
| 4647 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4648 | /* do not allow sys_link's with other subvols of the same device */ |
| 4649 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 4650 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4651 | |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 4652 | if (inode->i_nlink == ~0U) |
| 4653 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4654 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4655 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4656 | if (err) |
| 4657 | goto fail; |
| 4658 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4659 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4660 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4661 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4662 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4663 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4664 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4665 | if (IS_ERR(trans)) { |
| 4666 | err = PTR_ERR(trans); |
| 4667 | goto fail; |
| 4668 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4669 | |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 4670 | btrfs_inc_nlink(inode); |
| 4671 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 4672 | ihold(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4673 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4674 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4675 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4676 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4677 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4678 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 4679 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4680 | err = btrfs_update_inode(trans, root, inode); |
| 4681 | BUG_ON(err); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4682 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4683 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4684 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4685 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4686 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4687 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4688 | if (drop_inode) { |
| 4689 | inode_dec_link_count(inode); |
| 4690 | iput(inode); |
| 4691 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4692 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4693 | return err; |
| 4694 | } |
| 4695 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4696 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4697 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4698 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4699 | struct btrfs_trans_handle *trans; |
| 4700 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4701 | int err = 0; |
| 4702 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4703 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4704 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4705 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4706 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4707 | /* |
| 4708 | * 2 items for inode and ref |
| 4709 | * 2 items for dir items |
| 4710 | * 1 for xattr if selinux is on |
| 4711 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4712 | trans = btrfs_start_transaction(root, 5); |
| 4713 | if (IS_ERR(trans)) |
| 4714 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4715 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4716 | err = btrfs_find_free_ino(root, &objectid); |
| 4717 | if (err) |
| 4718 | goto out_fail; |
| 4719 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4720 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4721 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4722 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4723 | if (IS_ERR(inode)) { |
| 4724 | err = PTR_ERR(inode); |
| 4725 | goto out_fail; |
| 4726 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4727 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4728 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4729 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4730 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4731 | if (err) |
| 4732 | goto out_fail; |
| 4733 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4734 | inode->i_op = &btrfs_dir_inode_operations; |
| 4735 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4736 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4737 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4738 | err = btrfs_update_inode(trans, root, inode); |
| 4739 | if (err) |
| 4740 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4741 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4742 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 4743 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4744 | if (err) |
| 4745 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4746 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4747 | d_instantiate(dentry, inode); |
| 4748 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4749 | |
| 4750 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4751 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4752 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4753 | if (drop_on_err) |
| 4754 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4755 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4756 | return err; |
| 4757 | } |
| 4758 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4759 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4760 | * and an extent that you want to insert, deal with overlap and insert |
| 4761 | * the new extent into the tree. |
| 4762 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4763 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4764 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4765 | struct extent_map *em, |
| 4766 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4767 | { |
| 4768 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4769 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4770 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4771 | start_diff = map_start - em->start; |
| 4772 | em->start = map_start; |
| 4773 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4774 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4775 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4776 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4777 | em->block_len -= start_diff; |
| 4778 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4779 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4780 | } |
| 4781 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4782 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4783 | struct inode *inode, struct page *page, |
| 4784 | size_t pg_offset, u64 extent_offset, |
| 4785 | struct btrfs_file_extent_item *item) |
| 4786 | { |
| 4787 | int ret; |
| 4788 | struct extent_buffer *leaf = path->nodes[0]; |
| 4789 | char *tmp; |
| 4790 | size_t max_size; |
| 4791 | unsigned long inline_size; |
| 4792 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4793 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4794 | |
| 4795 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4796 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4797 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4798 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4799 | btrfs_item_nr(leaf, path->slots[0])); |
| 4800 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 4801 | if (!tmp) |
| 4802 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4803 | ptr = btrfs_file_extent_inline_start(item); |
| 4804 | |
| 4805 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4806 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 4807 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4808 | ret = btrfs_decompress(compress_type, tmp, page, |
| 4809 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4810 | if (ret) { |
| 4811 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 4812 | unsigned long copy_size = min_t(u64, |
| 4813 | PAGE_CACHE_SIZE - pg_offset, |
| 4814 | max_size - extent_offset); |
| 4815 | memset(kaddr + pg_offset, 0, copy_size); |
| 4816 | kunmap_atomic(kaddr, KM_USER0); |
| 4817 | } |
| 4818 | kfree(tmp); |
| 4819 | return 0; |
| 4820 | } |
| 4821 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4822 | /* |
| 4823 | * 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] | 4824 | * the ugly parts come from merging extents from the disk with the in-ram |
| 4825 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4826 | * where the in-ram extents might be locked pending data=ordered completion. |
| 4827 | * |
| 4828 | * This also copies inline extents directly into the page. |
| 4829 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4830 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4831 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4832 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4833 | int create) |
| 4834 | { |
| 4835 | int ret; |
| 4836 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4837 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4838 | u64 extent_start = 0; |
| 4839 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4840 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4841 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4842 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4843 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4844 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4845 | struct extent_buffer *leaf; |
| 4846 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4847 | struct extent_map *em = NULL; |
| 4848 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4849 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4850 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4851 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4852 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4853 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4854 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4855 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4856 | if (em) |
| 4857 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4858 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4859 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4860 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4861 | if (em->start > start || em->start + em->len <= start) |
| 4862 | free_extent_map(em); |
| 4863 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4864 | free_extent_map(em); |
| 4865 | else |
| 4866 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4867 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4868 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4869 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4870 | err = -ENOMEM; |
| 4871 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4872 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4873 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4874 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 4875 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4876 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4877 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4878 | |
| 4879 | if (!path) { |
| 4880 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 4881 | if (!path) { |
| 4882 | err = -ENOMEM; |
| 4883 | goto out; |
| 4884 | } |
| 4885 | /* |
| 4886 | * Chances are we'll be called again, so go ahead and do |
| 4887 | * readahead |
| 4888 | */ |
| 4889 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4890 | } |
| 4891 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4892 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 4893 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4894 | if (ret < 0) { |
| 4895 | err = ret; |
| 4896 | goto out; |
| 4897 | } |
| 4898 | |
| 4899 | if (ret != 0) { |
| 4900 | if (path->slots[0] == 0) |
| 4901 | goto not_found; |
| 4902 | path->slots[0]--; |
| 4903 | } |
| 4904 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4905 | leaf = path->nodes[0]; |
| 4906 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4907 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4908 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4909 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4910 | found_type = btrfs_key_type(&found_key); |
| 4911 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4912 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 4913 | goto not_found; |
| 4914 | } |
| 4915 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4916 | found_type = btrfs_file_extent_type(leaf, item); |
| 4917 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4918 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4919 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4920 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4921 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4922 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4923 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 4924 | size_t size; |
| 4925 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4926 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 4927 | ~((u64)root->sectorsize - 1); |
| 4928 | } |
| 4929 | |
| 4930 | if (start >= extent_end) { |
| 4931 | path->slots[0]++; |
| 4932 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 4933 | ret = btrfs_next_leaf(root, path); |
| 4934 | if (ret < 0) { |
| 4935 | err = ret; |
| 4936 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4937 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4938 | if (ret > 0) |
| 4939 | goto not_found; |
| 4940 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4941 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4942 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4943 | if (found_key.objectid != objectid || |
| 4944 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 4945 | goto not_found; |
| 4946 | if (start + len <= found_key.offset) |
| 4947 | goto not_found; |
| 4948 | em->start = start; |
| 4949 | em->len = found_key.offset - start; |
| 4950 | goto not_found_em; |
| 4951 | } |
| 4952 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4953 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4954 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4955 | em->start = extent_start; |
| 4956 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4957 | em->orig_start = extent_start - |
| 4958 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4959 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 4960 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4961 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4962 | goto insert; |
| 4963 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4964 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4965 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4966 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4967 | em->block_start = bytenr; |
| 4968 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 4969 | item); |
| 4970 | } else { |
| 4971 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 4972 | em->block_start = bytenr; |
| 4973 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4974 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 4975 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4976 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4977 | goto insert; |
| 4978 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4979 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4980 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4981 | size_t size; |
| 4982 | size_t extent_offset; |
| 4983 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4984 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4985 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4986 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4987 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4988 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4989 | goto out; |
| 4990 | } |
| 4991 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4992 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4993 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4994 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4995 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4996 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4997 | em->len = (copy_size + root->sectorsize - 1) & |
| 4998 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4999 | em->orig_start = EXTENT_MAP_INLINE; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5000 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5001 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5002 | em->compress_type = compress_type; |
| 5003 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5004 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5005 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5006 | if (btrfs_file_extent_compression(leaf, item) != |
| 5007 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5008 | ret = uncompress_inline(path, inode, page, |
| 5009 | pg_offset, |
| 5010 | extent_offset, item); |
| 5011 | BUG_ON(ret); |
| 5012 | } else { |
| 5013 | map = kmap(page); |
| 5014 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 5015 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 5016 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 5017 | memset(map + pg_offset + copy_size, 0, |
| 5018 | PAGE_CACHE_SIZE - pg_offset - |
| 5019 | copy_size); |
| 5020 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5021 | kunmap(page); |
| 5022 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5023 | flush_dcache_page(page); |
| 5024 | } else if (create && PageUptodate(page)) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5025 | WARN_ON(1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5026 | if (!trans) { |
| 5027 | kunmap(page); |
| 5028 | free_extent_map(em); |
| 5029 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5030 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5031 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5032 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5033 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5034 | if (IS_ERR(trans)) |
| 5035 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5036 | goto again; |
| 5037 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5038 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5039 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5040 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5041 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5042 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5043 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5044 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 5045 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5046 | goto insert; |
| 5047 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5048 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5049 | WARN_ON(1); |
| 5050 | } |
| 5051 | not_found: |
| 5052 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5053 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5054 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5055 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5056 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5057 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5058 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5059 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5060 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 5061 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 5062 | (unsigned long long)em->len, |
| 5063 | (unsigned long long)start, |
| 5064 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5065 | err = -EIO; |
| 5066 | goto out; |
| 5067 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5068 | |
| 5069 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5070 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5071 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5072 | /* it is possible that someone inserted the extent into the tree |
| 5073 | * while we had the lock dropped. It is also possible that |
| 5074 | * an overlapping map exists in the tree |
| 5075 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5076 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5077 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5078 | |
| 5079 | ret = 0; |
| 5080 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5081 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5082 | if (existing && (existing->start > start || |
| 5083 | existing->start + existing->len <= start)) { |
| 5084 | free_extent_map(existing); |
| 5085 | existing = NULL; |
| 5086 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5087 | if (!existing) { |
| 5088 | existing = lookup_extent_mapping(em_tree, em->start, |
| 5089 | em->len); |
| 5090 | if (existing) { |
| 5091 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5092 | em, start, |
| 5093 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5094 | free_extent_map(existing); |
| 5095 | if (err) { |
| 5096 | free_extent_map(em); |
| 5097 | em = NULL; |
| 5098 | } |
| 5099 | } else { |
| 5100 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5101 | free_extent_map(em); |
| 5102 | em = NULL; |
| 5103 | } |
| 5104 | } else { |
| 5105 | free_extent_map(em); |
| 5106 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5107 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5108 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5109 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5110 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5111 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5112 | |
| 5113 | trace_btrfs_get_extent(root, em); |
| 5114 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5115 | if (path) |
| 5116 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5117 | if (trans) { |
| 5118 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5119 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5120 | err = ret; |
| 5121 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5122 | if (err) { |
| 5123 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5124 | return ERR_PTR(err); |
| 5125 | } |
| 5126 | return em; |
| 5127 | } |
| 5128 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5129 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 5130 | size_t pg_offset, u64 start, u64 len, |
| 5131 | int create) |
| 5132 | { |
| 5133 | struct extent_map *em; |
| 5134 | struct extent_map *hole_em = NULL; |
| 5135 | u64 range_start = start; |
| 5136 | u64 end; |
| 5137 | u64 found; |
| 5138 | u64 found_end; |
| 5139 | int err = 0; |
| 5140 | |
| 5141 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 5142 | if (IS_ERR(em)) |
| 5143 | return em; |
| 5144 | if (em) { |
| 5145 | /* |
| 5146 | * if our em maps to a hole, there might |
| 5147 | * actually be delalloc bytes behind it |
| 5148 | */ |
| 5149 | if (em->block_start != EXTENT_MAP_HOLE) |
| 5150 | return em; |
| 5151 | else |
| 5152 | hole_em = em; |
| 5153 | } |
| 5154 | |
| 5155 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 5156 | end = start + len; |
| 5157 | if (end < start) |
| 5158 | end = (u64)-1; |
| 5159 | else |
| 5160 | end -= 1; |
| 5161 | |
| 5162 | em = NULL; |
| 5163 | |
| 5164 | /* ok, we didn't find anything, lets look for delalloc */ |
| 5165 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 5166 | end, len, EXTENT_DELALLOC, 1); |
| 5167 | found_end = range_start + found; |
| 5168 | if (found_end < range_start) |
| 5169 | found_end = (u64)-1; |
| 5170 | |
| 5171 | /* |
| 5172 | * we didn't find anything useful, return |
| 5173 | * the original results from get_extent() |
| 5174 | */ |
| 5175 | if (range_start > end || found_end <= start) { |
| 5176 | em = hole_em; |
| 5177 | hole_em = NULL; |
| 5178 | goto out; |
| 5179 | } |
| 5180 | |
| 5181 | /* adjust the range_start to make sure it doesn't |
| 5182 | * go backwards from the start they passed in |
| 5183 | */ |
| 5184 | range_start = max(start,range_start); |
| 5185 | found = found_end - range_start; |
| 5186 | |
| 5187 | if (found > 0) { |
| 5188 | u64 hole_start = start; |
| 5189 | u64 hole_len = len; |
| 5190 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5191 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5192 | if (!em) { |
| 5193 | err = -ENOMEM; |
| 5194 | goto out; |
| 5195 | } |
| 5196 | /* |
| 5197 | * when btrfs_get_extent can't find anything it |
| 5198 | * returns one huge hole |
| 5199 | * |
| 5200 | * make sure what it found really fits our range, and |
| 5201 | * adjust to make sure it is based on the start from |
| 5202 | * the caller |
| 5203 | */ |
| 5204 | if (hole_em) { |
| 5205 | u64 calc_end = extent_map_end(hole_em); |
| 5206 | |
| 5207 | if (calc_end <= start || (hole_em->start > end)) { |
| 5208 | free_extent_map(hole_em); |
| 5209 | hole_em = NULL; |
| 5210 | } else { |
| 5211 | hole_start = max(hole_em->start, start); |
| 5212 | hole_len = calc_end - hole_start; |
| 5213 | } |
| 5214 | } |
| 5215 | em->bdev = NULL; |
| 5216 | if (hole_em && range_start > hole_start) { |
| 5217 | /* our hole starts before our delalloc, so we |
| 5218 | * have to return just the parts of the hole |
| 5219 | * that go until the delalloc starts |
| 5220 | */ |
| 5221 | em->len = min(hole_len, |
| 5222 | range_start - hole_start); |
| 5223 | em->start = hole_start; |
| 5224 | em->orig_start = hole_start; |
| 5225 | /* |
| 5226 | * don't adjust block start at all, |
| 5227 | * it is fixed at EXTENT_MAP_HOLE |
| 5228 | */ |
| 5229 | em->block_start = hole_em->block_start; |
| 5230 | em->block_len = hole_len; |
| 5231 | } else { |
| 5232 | em->start = range_start; |
| 5233 | em->len = found; |
| 5234 | em->orig_start = range_start; |
| 5235 | em->block_start = EXTENT_MAP_DELALLOC; |
| 5236 | em->block_len = found; |
| 5237 | } |
| 5238 | } else if (hole_em) { |
| 5239 | return hole_em; |
| 5240 | } |
| 5241 | out: |
| 5242 | |
| 5243 | free_extent_map(hole_em); |
| 5244 | if (err) { |
| 5245 | free_extent_map(em); |
| 5246 | return ERR_PTR(err); |
| 5247 | } |
| 5248 | return em; |
| 5249 | } |
| 5250 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5251 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5252 | struct extent_map *em, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5253 | u64 start, u64 len) |
| 5254 | { |
| 5255 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5256 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5257 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 5258 | struct btrfs_key ins; |
| 5259 | u64 alloc_hint; |
| 5260 | int ret; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5261 | bool insert = false; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5262 | |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5263 | /* |
| 5264 | * Ok if the extent map we looked up is a hole and is for the exact |
| 5265 | * range we want, there is no reason to allocate a new one, however if |
| 5266 | * it is not right then we need to free this one and drop the cache for |
| 5267 | * our range. |
| 5268 | */ |
| 5269 | if (em->block_start != EXTENT_MAP_HOLE || em->start != start || |
| 5270 | em->len != len) { |
| 5271 | free_extent_map(em); |
| 5272 | em = NULL; |
| 5273 | insert = true; |
| 5274 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
| 5275 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5276 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5277 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5278 | if (IS_ERR(trans)) |
| 5279 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5280 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 5281 | if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024) |
| 5282 | btrfs_add_inode_defrag(trans, inode); |
| 5283 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5284 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5285 | |
| 5286 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 5287 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
| 5288 | alloc_hint, (u64)-1, &ins, 1); |
| 5289 | if (ret) { |
| 5290 | em = ERR_PTR(ret); |
| 5291 | goto out; |
| 5292 | } |
| 5293 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5294 | if (!em) { |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5295 | em = alloc_extent_map(); |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5296 | if (!em) { |
| 5297 | em = ERR_PTR(-ENOMEM); |
| 5298 | goto out; |
| 5299 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | em->start = start; |
| 5303 | em->orig_start = em->start; |
| 5304 | em->len = ins.offset; |
| 5305 | |
| 5306 | em->block_start = ins.objectid; |
| 5307 | em->block_len = ins.offset; |
| 5308 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5309 | |
| 5310 | /* |
| 5311 | * We need to do this because if we're using the original em we searched |
| 5312 | * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that. |
| 5313 | */ |
| 5314 | em->flags = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5315 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5316 | |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5317 | while (insert) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5318 | write_lock(&em_tree->lock); |
| 5319 | ret = add_extent_mapping(em_tree, em); |
| 5320 | write_unlock(&em_tree->lock); |
| 5321 | if (ret != -EEXIST) |
| 5322 | break; |
| 5323 | btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0); |
| 5324 | } |
| 5325 | |
| 5326 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 5327 | ins.offset, ins.offset, 0); |
| 5328 | if (ret) { |
| 5329 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 5330 | em = ERR_PTR(ret); |
| 5331 | } |
| 5332 | out: |
| 5333 | btrfs_end_transaction(trans, root); |
| 5334 | return em; |
| 5335 | } |
| 5336 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5337 | /* |
| 5338 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 5339 | * block must be cow'd |
| 5340 | */ |
| 5341 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 5342 | struct inode *inode, u64 offset, u64 len) |
| 5343 | { |
| 5344 | struct btrfs_path *path; |
| 5345 | int ret; |
| 5346 | struct extent_buffer *leaf; |
| 5347 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5348 | struct btrfs_file_extent_item *fi; |
| 5349 | struct btrfs_key key; |
| 5350 | u64 disk_bytenr; |
| 5351 | u64 backref_offset; |
| 5352 | u64 extent_end; |
| 5353 | u64 num_bytes; |
| 5354 | int slot; |
| 5355 | int found_type; |
| 5356 | |
| 5357 | path = btrfs_alloc_path(); |
| 5358 | if (!path) |
| 5359 | return -ENOMEM; |
| 5360 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5361 | ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5362 | offset, 0); |
| 5363 | if (ret < 0) |
| 5364 | goto out; |
| 5365 | |
| 5366 | slot = path->slots[0]; |
| 5367 | if (ret == 1) { |
| 5368 | if (slot == 0) { |
| 5369 | /* can't find the item, must cow */ |
| 5370 | ret = 0; |
| 5371 | goto out; |
| 5372 | } |
| 5373 | slot--; |
| 5374 | } |
| 5375 | ret = 0; |
| 5376 | leaf = path->nodes[0]; |
| 5377 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5378 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5379 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5380 | /* not our file or wrong item type, must cow */ |
| 5381 | goto out; |
| 5382 | } |
| 5383 | |
| 5384 | if (key.offset > offset) { |
| 5385 | /* Wrong offset, must cow */ |
| 5386 | goto out; |
| 5387 | } |
| 5388 | |
| 5389 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5390 | found_type = btrfs_file_extent_type(leaf, fi); |
| 5391 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 5392 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 5393 | /* not a regular extent, must cow */ |
| 5394 | goto out; |
| 5395 | } |
| 5396 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5397 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 5398 | |
| 5399 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 5400 | if (extent_end < offset + len) { |
| 5401 | /* extent doesn't include our full range, must cow */ |
| 5402 | goto out; |
| 5403 | } |
| 5404 | |
| 5405 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 5406 | goto out; |
| 5407 | |
| 5408 | /* |
| 5409 | * look for other files referencing this extent, if we |
| 5410 | * find any we must cow |
| 5411 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5412 | if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5413 | key.offset - backref_offset, disk_bytenr)) |
| 5414 | goto out; |
| 5415 | |
| 5416 | /* |
| 5417 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 5418 | * in this extent we are about to write. If there |
| 5419 | * are any csums in that range we have to cow in order |
| 5420 | * to keep the csums correct |
| 5421 | */ |
| 5422 | disk_bytenr += backref_offset; |
| 5423 | disk_bytenr += offset - key.offset; |
| 5424 | num_bytes = min(offset + len, extent_end) - offset; |
| 5425 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 5426 | goto out; |
| 5427 | /* |
| 5428 | * all of the above have passed, it is safe to overwrite this extent |
| 5429 | * without cow |
| 5430 | */ |
| 5431 | ret = 1; |
| 5432 | out: |
| 5433 | btrfs_free_path(path); |
| 5434 | return ret; |
| 5435 | } |
| 5436 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5437 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 5438 | struct buffer_head *bh_result, int create) |
| 5439 | { |
| 5440 | struct extent_map *em; |
| 5441 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5442 | u64 start = iblock << inode->i_blkbits; |
| 5443 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5444 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5445 | |
| 5446 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
| 5447 | if (IS_ERR(em)) |
| 5448 | return PTR_ERR(em); |
| 5449 | |
| 5450 | /* |
| 5451 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 5452 | * io. INLINE is special, and we could probably kludge it in here, but |
| 5453 | * it's still buffered so for safety lets just fall back to the generic |
| 5454 | * buffered path. |
| 5455 | * |
| 5456 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 5457 | * decompress it, so there will be buffering required no matter what we |
| 5458 | * do, so go ahead and fallback to buffered. |
| 5459 | * |
| 5460 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 5461 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 5462 | * the generic code. |
| 5463 | */ |
| 5464 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 5465 | em->block_start == EXTENT_MAP_INLINE) { |
| 5466 | free_extent_map(em); |
| 5467 | return -ENOTBLK; |
| 5468 | } |
| 5469 | |
| 5470 | /* Just a good old fashioned hole, return */ |
| 5471 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 5472 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5473 | free_extent_map(em); |
| 5474 | /* DIO will do one hole at a time, so just unlock a sector */ |
| 5475 | unlock_extent(&BTRFS_I(inode)->io_tree, start, |
| 5476 | start + root->sectorsize - 1, GFP_NOFS); |
| 5477 | return 0; |
| 5478 | } |
| 5479 | |
| 5480 | /* |
| 5481 | * We don't allocate a new extent in the following cases |
| 5482 | * |
| 5483 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 5484 | * existing extent. |
| 5485 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 5486 | * just use the extent. |
| 5487 | * |
| 5488 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5489 | if (!create) { |
| 5490 | len = em->len - (start - em->start); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5491 | goto map; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5492 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5493 | |
| 5494 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 5495 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 5496 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5497 | int type; |
| 5498 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5499 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5500 | |
| 5501 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5502 | type = BTRFS_ORDERED_PREALLOC; |
| 5503 | else |
| 5504 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5505 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5506 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5507 | |
| 5508 | /* |
| 5509 | * we're not going to log anything, but we do need |
| 5510 | * to make sure the current transaction stays open |
| 5511 | * while we look for nocow cross refs |
| 5512 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5513 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5514 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5515 | goto must_cow; |
| 5516 | |
| 5517 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
| 5518 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 5519 | block_start, len, len, type); |
| 5520 | btrfs_end_transaction(trans, root); |
| 5521 | if (ret) { |
| 5522 | free_extent_map(em); |
| 5523 | return ret; |
| 5524 | } |
| 5525 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5526 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5527 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5528 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5529 | must_cow: |
| 5530 | /* |
| 5531 | * this will cow the extent, reset the len in case we changed |
| 5532 | * it above |
| 5533 | */ |
| 5534 | len = bh_result->b_size; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5535 | em = btrfs_new_extent_direct(inode, em, start, len); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5536 | if (IS_ERR(em)) |
| 5537 | return PTR_ERR(em); |
| 5538 | len = min(len, em->len - (start - em->start)); |
| 5539 | unlock: |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5540 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
| 5541 | EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1, |
| 5542 | 0, NULL, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5543 | map: |
| 5544 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 5545 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5546 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5547 | bh_result->b_bdev = em->bdev; |
| 5548 | set_buffer_mapped(bh_result); |
| 5549 | if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5550 | set_buffer_new(bh_result); |
| 5551 | |
| 5552 | free_extent_map(em); |
| 5553 | |
| 5554 | return 0; |
| 5555 | } |
| 5556 | |
| 5557 | struct btrfs_dio_private { |
| 5558 | struct inode *inode; |
| 5559 | u64 logical_offset; |
| 5560 | u64 disk_bytenr; |
| 5561 | u64 bytes; |
| 5562 | u32 *csums; |
| 5563 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5564 | |
| 5565 | /* number of bios pending for this dio */ |
| 5566 | atomic_t pending_bios; |
| 5567 | |
| 5568 | /* IO errors */ |
| 5569 | int errors; |
| 5570 | |
| 5571 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5572 | }; |
| 5573 | |
| 5574 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 5575 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5576 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5577 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 5578 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5579 | struct inode *inode = dip->inode; |
| 5580 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5581 | u64 start; |
| 5582 | u32 *private = dip->csums; |
| 5583 | |
| 5584 | start = dip->logical_offset; |
| 5585 | do { |
| 5586 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 5587 | struct page *page = bvec->bv_page; |
| 5588 | char *kaddr; |
| 5589 | u32 csum = ~(u32)0; |
| 5590 | unsigned long flags; |
| 5591 | |
| 5592 | local_irq_save(flags); |
| 5593 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 5594 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 5595 | csum, bvec->bv_len); |
| 5596 | btrfs_csum_final(csum, (char *)&csum); |
| 5597 | kunmap_atomic(kaddr, KM_IRQ0); |
| 5598 | local_irq_restore(flags); |
| 5599 | |
| 5600 | flush_dcache_page(bvec->bv_page); |
| 5601 | if (csum != *private) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5602 | printk(KERN_ERR "btrfs csum failed ino %llu off" |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5603 | " %llu csum %u private %u\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5604 | (unsigned long long)btrfs_ino(inode), |
| 5605 | (unsigned long long)start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5606 | csum, *private); |
| 5607 | err = -EIO; |
| 5608 | } |
| 5609 | } |
| 5610 | |
| 5611 | start += bvec->bv_len; |
| 5612 | private++; |
| 5613 | bvec++; |
| 5614 | } while (bvec <= bvec_end); |
| 5615 | |
| 5616 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
| 5617 | dip->logical_offset + dip->bytes - 1, GFP_NOFS); |
| 5618 | bio->bi_private = dip->private; |
| 5619 | |
| 5620 | kfree(dip->csums); |
| 5621 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 5622 | |
| 5623 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 5624 | if (err) |
| 5625 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5626 | dio_end_io(bio, err); |
| 5627 | } |
| 5628 | |
| 5629 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 5630 | { |
| 5631 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5632 | struct inode *inode = dip->inode; |
| 5633 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5634 | struct btrfs_trans_handle *trans; |
| 5635 | struct btrfs_ordered_extent *ordered = NULL; |
| 5636 | struct extent_state *cached_state = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5637 | u64 ordered_offset = dip->logical_offset; |
| 5638 | u64 ordered_bytes = dip->bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5639 | int ret; |
| 5640 | |
| 5641 | if (err) |
| 5642 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5643 | again: |
| 5644 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 5645 | &ordered_offset, |
| 5646 | ordered_bytes); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5647 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5648 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5649 | |
| 5650 | BUG_ON(!ordered); |
| 5651 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5652 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5653 | if (IS_ERR(trans)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5654 | err = -ENOMEM; |
| 5655 | goto out; |
| 5656 | } |
| 5657 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5658 | |
| 5659 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) { |
| 5660 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5661 | if (!ret) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 5662 | err = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5663 | goto out; |
| 5664 | } |
| 5665 | |
| 5666 | lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5667 | ordered->file_offset + ordered->len - 1, 0, |
| 5668 | &cached_state, GFP_NOFS); |
| 5669 | |
| 5670 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) { |
| 5671 | ret = btrfs_mark_extent_written(trans, inode, |
| 5672 | ordered->file_offset, |
| 5673 | ordered->file_offset + |
| 5674 | ordered->len); |
| 5675 | if (ret) { |
| 5676 | err = ret; |
| 5677 | goto out_unlock; |
| 5678 | } |
| 5679 | } else { |
| 5680 | ret = insert_reserved_file_extent(trans, inode, |
| 5681 | ordered->file_offset, |
| 5682 | ordered->start, |
| 5683 | ordered->disk_len, |
| 5684 | ordered->len, |
| 5685 | ordered->len, |
| 5686 | 0, 0, 0, |
| 5687 | BTRFS_FILE_EXTENT_REG); |
| 5688 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 5689 | ordered->file_offset, ordered->len); |
| 5690 | if (ret) { |
| 5691 | err = ret; |
| 5692 | WARN_ON(1); |
| 5693 | goto out_unlock; |
| 5694 | } |
| 5695 | } |
| 5696 | |
| 5697 | add_pending_csums(trans, inode, ordered->file_offset, &ordered->list); |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 5698 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
Miao Xie | a39f752 | 2011-09-11 10:52:25 -0400 | [diff] [blame] | 5699 | if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 5700 | btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 5701 | ret = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5702 | out_unlock: |
| 5703 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5704 | ordered->file_offset + ordered->len - 1, |
| 5705 | &cached_state, GFP_NOFS); |
| 5706 | out: |
| 5707 | btrfs_delalloc_release_metadata(inode, ordered->len); |
| 5708 | btrfs_end_transaction(trans, root); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5709 | ordered_offset = ordered->file_offset + ordered->len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5710 | btrfs_put_ordered_extent(ordered); |
| 5711 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5712 | |
| 5713 | out_test: |
| 5714 | /* |
| 5715 | * our bio might span multiple ordered extents. If we haven't |
| 5716 | * completed the accounting for the whole dio, go back and try again |
| 5717 | */ |
| 5718 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 5719 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 5720 | ordered_offset; |
| 5721 | goto again; |
| 5722 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5723 | out_done: |
| 5724 | bio->bi_private = dip->private; |
| 5725 | |
| 5726 | kfree(dip->csums); |
| 5727 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 5728 | |
| 5729 | /* If we had an error make sure to clear the uptodate flag */ |
| 5730 | if (err) |
| 5731 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5732 | dio_end_io(bio, err); |
| 5733 | } |
| 5734 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5735 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 5736 | struct bio *bio, int mirror_num, |
| 5737 | unsigned long bio_flags, u64 offset) |
| 5738 | { |
| 5739 | int ret; |
| 5740 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5741 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
| 5742 | BUG_ON(ret); |
| 5743 | return 0; |
| 5744 | } |
| 5745 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5746 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 5747 | { |
| 5748 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5749 | |
| 5750 | if (err) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5751 | printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 5752 | "sector %#Lx len %u err no %d\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5753 | (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw, |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 5754 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5755 | dip->errors = 1; |
| 5756 | |
| 5757 | /* |
| 5758 | * before atomic variable goto zero, we must make sure |
| 5759 | * dip->errors is perceived to be set. |
| 5760 | */ |
| 5761 | smp_mb__before_atomic_dec(); |
| 5762 | } |
| 5763 | |
| 5764 | /* if there are more bios still pending for this dio, just exit */ |
| 5765 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 5766 | goto out; |
| 5767 | |
| 5768 | if (dip->errors) |
| 5769 | bio_io_error(dip->orig_bio); |
| 5770 | else { |
| 5771 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 5772 | bio_endio(dip->orig_bio, 0); |
| 5773 | } |
| 5774 | out: |
| 5775 | bio_put(bio); |
| 5776 | } |
| 5777 | |
| 5778 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 5779 | u64 first_sector, gfp_t gfp_flags) |
| 5780 | { |
| 5781 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 5782 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 5783 | } |
| 5784 | |
| 5785 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 5786 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5787 | u32 *csums, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5788 | { |
| 5789 | int write = rw & REQ_WRITE; |
| 5790 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5791 | int ret; |
| 5792 | |
| 5793 | bio_get(bio); |
| 5794 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 5795 | if (ret) |
| 5796 | goto err; |
| 5797 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5798 | if (skip_sum) |
| 5799 | goto map; |
| 5800 | |
| 5801 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5802 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 5803 | inode, rw, bio, 0, 0, |
| 5804 | file_offset, |
| 5805 | __btrfs_submit_bio_start_direct_io, |
| 5806 | __btrfs_submit_bio_done); |
| 5807 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5808 | } else if (write) { |
| 5809 | /* |
| 5810 | * If we aren't doing async submit, calculate the csum of the |
| 5811 | * bio now. |
| 5812 | */ |
| 5813 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 5814 | if (ret) |
| 5815 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 5816 | } else if (!skip_sum) { |
| 5817 | ret = btrfs_lookup_bio_sums_dio(root, inode, bio, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5818 | file_offset, csums); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 5819 | if (ret) |
| 5820 | goto err; |
| 5821 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5822 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5823 | map: |
| 5824 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5825 | err: |
| 5826 | bio_put(bio); |
| 5827 | return ret; |
| 5828 | } |
| 5829 | |
| 5830 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 5831 | int skip_sum) |
| 5832 | { |
| 5833 | struct inode *inode = dip->inode; |
| 5834 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5835 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5836 | struct bio *bio; |
| 5837 | struct bio *orig_bio = dip->orig_bio; |
| 5838 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 5839 | u64 start_sector = orig_bio->bi_sector; |
| 5840 | u64 file_offset = dip->logical_offset; |
| 5841 | u64 submit_len = 0; |
| 5842 | u64 map_length; |
| 5843 | int nr_pages = 0; |
| 5844 | u32 *csums = dip->csums; |
| 5845 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5846 | int async_submit = 0; |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 5847 | int write = rw & REQ_WRITE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5848 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5849 | map_length = orig_bio->bi_size; |
| 5850 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 5851 | &map_length, NULL, 0); |
| 5852 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 5853 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5854 | return -EIO; |
| 5855 | } |
| 5856 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 5857 | if (map_length >= orig_bio->bi_size) { |
| 5858 | bio = orig_bio; |
| 5859 | goto submit; |
| 5860 | } |
| 5861 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5862 | async_submit = 1; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 5863 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 5864 | if (!bio) |
| 5865 | return -ENOMEM; |
| 5866 | bio->bi_private = dip; |
| 5867 | bio->bi_end_io = btrfs_end_dio_bio; |
| 5868 | atomic_inc(&dip->pending_bios); |
| 5869 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5870 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 5871 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 5872 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5873 | bvec->bv_offset) < bvec->bv_len)) { |
| 5874 | /* |
| 5875 | * inc the count before we submit the bio so |
| 5876 | * we know the end IO handler won't happen before |
| 5877 | * we inc the count. Otherwise, the dip might get freed |
| 5878 | * before we're done setting it up |
| 5879 | */ |
| 5880 | atomic_inc(&dip->pending_bios); |
| 5881 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 5882 | file_offset, skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5883 | csums, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5884 | if (ret) { |
| 5885 | bio_put(bio); |
| 5886 | atomic_dec(&dip->pending_bios); |
| 5887 | goto out_err; |
| 5888 | } |
| 5889 | |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 5890 | /* Write's use the ordered csums */ |
| 5891 | if (!write && !skip_sum) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5892 | csums = csums + nr_pages; |
| 5893 | start_sector += submit_len >> 9; |
| 5894 | file_offset += submit_len; |
| 5895 | |
| 5896 | submit_len = 0; |
| 5897 | nr_pages = 0; |
| 5898 | |
| 5899 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 5900 | start_sector, GFP_NOFS); |
| 5901 | if (!bio) |
| 5902 | goto out_err; |
| 5903 | bio->bi_private = dip; |
| 5904 | bio->bi_end_io = btrfs_end_dio_bio; |
| 5905 | |
| 5906 | map_length = orig_bio->bi_size; |
| 5907 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 5908 | &map_length, NULL, 0); |
| 5909 | if (ret) { |
| 5910 | bio_put(bio); |
| 5911 | goto out_err; |
| 5912 | } |
| 5913 | } else { |
| 5914 | submit_len += bvec->bv_len; |
| 5915 | nr_pages ++; |
| 5916 | bvec++; |
| 5917 | } |
| 5918 | } |
| 5919 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 5920 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5921 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5922 | csums, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5923 | if (!ret) |
| 5924 | return 0; |
| 5925 | |
| 5926 | bio_put(bio); |
| 5927 | out_err: |
| 5928 | dip->errors = 1; |
| 5929 | /* |
| 5930 | * before atomic variable goto zero, we must |
| 5931 | * make sure dip->errors is perceived to be set. |
| 5932 | */ |
| 5933 | smp_mb__before_atomic_dec(); |
| 5934 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 5935 | bio_io_error(dip->orig_bio); |
| 5936 | |
| 5937 | /* bio_end_io() will handle error, so we needn't return it */ |
| 5938 | return 0; |
| 5939 | } |
| 5940 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5941 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 5942 | loff_t file_offset) |
| 5943 | { |
| 5944 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5945 | struct btrfs_dio_private *dip; |
| 5946 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5947 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5948 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5949 | int ret = 0; |
| 5950 | |
| 5951 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 5952 | |
| 5953 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 5954 | if (!dip) { |
| 5955 | ret = -ENOMEM; |
| 5956 | goto free_ordered; |
| 5957 | } |
| 5958 | dip->csums = NULL; |
| 5959 | |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 5960 | /* Write's use the ordered csum stuff, so we don't need dip->csums */ |
| 5961 | if (!write && !skip_sum) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5962 | dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); |
| 5963 | if (!dip->csums) { |
Daniel J Blueman | b4966b7 | 2011-03-09 16:46:42 +0000 | [diff] [blame] | 5964 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5965 | ret = -ENOMEM; |
| 5966 | goto free_ordered; |
| 5967 | } |
| 5968 | } |
| 5969 | |
| 5970 | dip->private = bio->bi_private; |
| 5971 | dip->inode = inode; |
| 5972 | dip->logical_offset = file_offset; |
| 5973 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5974 | dip->bytes = 0; |
| 5975 | do { |
| 5976 | dip->bytes += bvec->bv_len; |
| 5977 | bvec++; |
| 5978 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 5979 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5980 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5981 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5982 | dip->errors = 0; |
| 5983 | dip->orig_bio = bio; |
| 5984 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5985 | |
| 5986 | if (write) |
| 5987 | bio->bi_end_io = btrfs_endio_direct_write; |
| 5988 | else |
| 5989 | bio->bi_end_io = btrfs_endio_direct_read; |
| 5990 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5991 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 5992 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5993 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5994 | free_ordered: |
| 5995 | /* |
| 5996 | * If this is a write, we need to clean up the reserved space and kill |
| 5997 | * the ordered extent. |
| 5998 | */ |
| 5999 | if (write) { |
| 6000 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 6001 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6002 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 6003 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 6004 | btrfs_free_reserved_extent(root, ordered->start, |
| 6005 | ordered->disk_len); |
| 6006 | btrfs_put_ordered_extent(ordered); |
| 6007 | btrfs_put_ordered_extent(ordered); |
| 6008 | } |
| 6009 | bio_endio(bio, ret); |
| 6010 | } |
| 6011 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6012 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 6013 | const struct iovec *iov, loff_t offset, |
| 6014 | unsigned long nr_segs) |
| 6015 | { |
| 6016 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6017 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6018 | size_t size; |
| 6019 | unsigned long addr; |
| 6020 | unsigned blocksize_mask = root->sectorsize - 1; |
| 6021 | ssize_t retval = -EINVAL; |
| 6022 | loff_t end = offset; |
| 6023 | |
| 6024 | if (offset & blocksize_mask) |
| 6025 | goto out; |
| 6026 | |
| 6027 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 6028 | for (seg = 0; seg < nr_segs; seg++) { |
| 6029 | addr = (unsigned long)iov[seg].iov_base; |
| 6030 | size = iov[seg].iov_len; |
| 6031 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6032 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6033 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6034 | |
| 6035 | /* If this is a write we don't need to check anymore */ |
| 6036 | if (rw & WRITE) |
| 6037 | continue; |
| 6038 | |
| 6039 | /* |
| 6040 | * Check to make sure we don't have duplicate iov_base's in this |
| 6041 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 6042 | * when reading back. |
| 6043 | */ |
| 6044 | for (i = seg + 1; i < nr_segs; i++) { |
| 6045 | if (iov[seg].iov_base == iov[i].iov_base) |
| 6046 | goto out; |
| 6047 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6048 | } |
| 6049 | retval = 0; |
| 6050 | out: |
| 6051 | return retval; |
| 6052 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6053 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 6054 | const struct iovec *iov, loff_t offset, |
| 6055 | unsigned long nr_segs) |
| 6056 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6057 | struct file *file = iocb->ki_filp; |
| 6058 | struct inode *inode = file->f_mapping->host; |
| 6059 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6060 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6061 | u64 lockstart, lockend; |
| 6062 | ssize_t ret; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6063 | int writing = rw & WRITE; |
| 6064 | int write_bits = 0; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6065 | size_t count = iov_length(iov, nr_segs); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6066 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6067 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
| 6068 | offset, nr_segs)) { |
| 6069 | return 0; |
| 6070 | } |
| 6071 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6072 | lockstart = offset; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6073 | lockend = offset + count - 1; |
| 6074 | |
| 6075 | if (writing) { |
| 6076 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 6077 | if (ret) |
| 6078 | goto out; |
| 6079 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6080 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6081 | while (1) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6082 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6083 | 0, &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6084 | /* |
| 6085 | * We're concerned with the entire range that we're going to be |
| 6086 | * doing DIO to, so we need to make sure theres no ordered |
| 6087 | * extents in this range. |
| 6088 | */ |
| 6089 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6090 | lockend - lockstart + 1); |
| 6091 | if (!ordered) |
| 6092 | break; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6093 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6094 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6095 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6096 | btrfs_put_ordered_extent(ordered); |
| 6097 | cond_resched(); |
| 6098 | } |
| 6099 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6100 | /* |
| 6101 | * we don't use btrfs_set_extent_delalloc because we don't want |
| 6102 | * the dirty or uptodate bits |
| 6103 | */ |
| 6104 | if (writing) { |
| 6105 | write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING; |
| 6106 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6107 | EXTENT_DELALLOC, 0, NULL, &cached_state, |
| 6108 | GFP_NOFS); |
| 6109 | if (ret) { |
| 6110 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6111 | lockend, EXTENT_LOCKED | write_bits, |
| 6112 | 1, 0, &cached_state, GFP_NOFS); |
| 6113 | goto out; |
| 6114 | } |
| 6115 | } |
| 6116 | |
| 6117 | free_extent_state(cached_state); |
| 6118 | cached_state = NULL; |
| 6119 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6120 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 6121 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 6122 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
| 6123 | btrfs_submit_direct, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6124 | |
| 6125 | if (ret < 0 && ret != -EIOCBQUEUED) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6126 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset, |
| 6127 | offset + iov_length(iov, nr_segs) - 1, |
| 6128 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6129 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6130 | } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) { |
| 6131 | /* |
| 6132 | * We're falling back to buffered, unlock the section we didn't |
| 6133 | * do IO on. |
| 6134 | */ |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6135 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret, |
| 6136 | offset + iov_length(iov, nr_segs) - 1, |
| 6137 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6138 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6139 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6140 | out: |
| 6141 | free_extent_state(cached_state); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6142 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6143 | } |
| 6144 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6145 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 6146 | __u64 start, __u64 len) |
| 6147 | { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6148 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6149 | } |
| 6150 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6151 | int btrfs_readpage(struct file *file, struct page *page) |
| 6152 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6153 | struct extent_io_tree *tree; |
| 6154 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 6155 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6156 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6157 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6158 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 6159 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6160 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6161 | |
| 6162 | |
| 6163 | if (current->flags & PF_MEMALLOC) { |
| 6164 | redirty_page_for_writepage(wbc, page); |
| 6165 | unlock_page(page); |
| 6166 | return 0; |
| 6167 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6168 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6169 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 6170 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6171 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6172 | int btrfs_writepages(struct address_space *mapping, |
| 6173 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6174 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6175 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6176 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6177 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6178 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 6179 | } |
| 6180 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6181 | static int |
| 6182 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 6183 | struct list_head *pages, unsigned nr_pages) |
| 6184 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6185 | struct extent_io_tree *tree; |
| 6186 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6187 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 6188 | btrfs_get_extent); |
| 6189 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6190 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6191 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6192 | struct extent_io_tree *tree; |
| 6193 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6194 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6195 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6196 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 6197 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6198 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6199 | if (ret == 1) { |
| 6200 | ClearPagePrivate(page); |
| 6201 | set_page_private(page, 0); |
| 6202 | page_cache_release(page); |
| 6203 | } |
| 6204 | return ret; |
| 6205 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6206 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6207 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 6208 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 6209 | if (PageWriteback(page) || PageDirty(page)) |
| 6210 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 6211 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6212 | } |
| 6213 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6214 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 6215 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6216 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6217 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6218 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6219 | u64 page_start = page_offset(page); |
| 6220 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6221 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6222 | |
| 6223 | /* |
| 6224 | * we have the page locked, so new writeback can't start, |
| 6225 | * and the dirty bit won't be cleared while we are here. |
| 6226 | * |
| 6227 | * Wait for IO on this page so that we can safely clear |
| 6228 | * the PagePrivate2 bit and do ordered accounting |
| 6229 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6230 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6231 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6232 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6233 | if (offset) { |
| 6234 | btrfs_releasepage(page, GFP_NOFS); |
| 6235 | return; |
| 6236 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6237 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6238 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6239 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 6240 | page_offset(page)); |
| 6241 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6242 | /* |
| 6243 | * IO on this page will never be started, so we need |
| 6244 | * to account for any ordered extents now |
| 6245 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6246 | clear_extent_bit(tree, page_start, page_end, |
| 6247 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6248 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6249 | &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6250 | /* |
| 6251 | * whoever cleared the private bit is responsible |
| 6252 | * for the finish_ordered_io |
| 6253 | */ |
| 6254 | if (TestClearPagePrivate2(page)) { |
| 6255 | btrfs_finish_ordered_io(page->mapping->host, |
| 6256 | page_start, page_end); |
| 6257 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6258 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6259 | cached_state = NULL; |
| 6260 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6261 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6262 | } |
| 6263 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6264 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6265 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6266 | __btrfs_releasepage(page, GFP_NOFS); |
| 6267 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6268 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6269 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6270 | ClearPagePrivate(page); |
| 6271 | set_page_private(page, 0); |
| 6272 | page_cache_release(page); |
| 6273 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6274 | } |
| 6275 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6276 | /* |
| 6277 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 6278 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 6279 | * be careful to check for EOF conditions here. We set the page up correctly |
| 6280 | * for a written page which means we get ENOSPC checking when writing into |
| 6281 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 6282 | * support these features. |
| 6283 | * |
| 6284 | * We are not allowed to take the i_mutex here so we have to play games to |
| 6285 | * protect against truncate races as the page could now be beyond EOF. Because |
| 6286 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 6287 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 6288 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 6289 | * unlock the page. |
| 6290 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6291 | 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] | 6292 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6293 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 6294 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6295 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6296 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 6297 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6298 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6299 | char *kaddr; |
| 6300 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6301 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6302 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6303 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6304 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6305 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6306 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6307 | if (ret) { |
| 6308 | if (ret == -ENOMEM) |
| 6309 | ret = VM_FAULT_OOM; |
| 6310 | else /* -ENOSPC, -EIO, etc */ |
| 6311 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6312 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6313 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6314 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6315 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6316 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6317 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6318 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6319 | page_start = page_offset(page); |
| 6320 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6321 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6322 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6323 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6324 | /* page got truncated out from underneath us */ |
| 6325 | goto out_unlock; |
| 6326 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6327 | wait_on_page_writeback(page); |
| 6328 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6329 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 6330 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6331 | set_page_extent_mapped(page); |
| 6332 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6333 | /* |
| 6334 | * we can't set the delalloc bits if there are pending ordered |
| 6335 | * extents. Drop our locks and wait for them to finish |
| 6336 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6337 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6338 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6339 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6340 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6341 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6342 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6343 | btrfs_put_ordered_extent(ordered); |
| 6344 | goto again; |
| 6345 | } |
| 6346 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6347 | /* |
| 6348 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 6349 | * if it was already dirty, so for space accounting reasons we need to |
| 6350 | * clear any delalloc bits for the range we are fixing to save. There |
| 6351 | * is probably a better way to do this, but for now keep consistent with |
| 6352 | * prepare_pages in the normal write path. |
| 6353 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6354 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6355 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6356 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6357 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6358 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 6359 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6360 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6361 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6362 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6363 | ret = VM_FAULT_SIGBUS; |
| 6364 | goto out_unlock; |
| 6365 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6366 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6367 | |
| 6368 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6369 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6370 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6371 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6372 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6373 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6374 | if (zero_start != PAGE_CACHE_SIZE) { |
| 6375 | kaddr = kmap(page); |
| 6376 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 6377 | flush_dcache_page(page); |
| 6378 | kunmap(page); |
| 6379 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6380 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6381 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6382 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6383 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6384 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 6385 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 6386 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6387 | 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] | 6388 | |
| 6389 | out_unlock: |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6390 | if (!ret) |
| 6391 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6392 | unlock_page(page); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6393 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6394 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6395 | return ret; |
| 6396 | } |
| 6397 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6398 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6399 | { |
| 6400 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6401 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6402 | int ret; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6403 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6404 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6405 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6406 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 6407 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6408 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 6409 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 6410 | if (ret) |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6411 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6412 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6413 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6414 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6415 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6416 | /* |
| 6417 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 6418 | * 3 things going on here |
| 6419 | * |
| 6420 | * 1) We need to reserve space for our orphan item and the space to |
| 6421 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 6422 | * orphan item because we didn't reserve space to remove it. |
| 6423 | * |
| 6424 | * 2) We need to reserve space to update our inode. |
| 6425 | * |
| 6426 | * 3) We need to have something to cache all the space that is going to |
| 6427 | * be free'd up by the truncate operation, but also have some slack |
| 6428 | * space reserved in case it uses space during the truncate (thank you |
| 6429 | * very much snapshotting). |
| 6430 | * |
| 6431 | * And we need these to all be seperate. The fact is we can use alot of |
| 6432 | * space doing the truncate, and we have no earthly idea how much space |
| 6433 | * we will use, so we need the truncate reservation to be seperate so it |
| 6434 | * doesn't end up using space reserved for updating the inode or |
| 6435 | * removing the orphan item. We also need to be able to stop the |
| 6436 | * transaction and start a new one, which means we need to be able to |
| 6437 | * update the inode several times, and we have no idea of knowing how |
| 6438 | * many times that will be, so we can't just reserve 1 item for the |
| 6439 | * entirety of the opration, so that has to be done seperately as well. |
| 6440 | * Then there is the orphan item, which does indeed need to be held on |
| 6441 | * to for the whole operation, and we need nobody to touch this reserved |
| 6442 | * space except the orphan code. |
| 6443 | * |
| 6444 | * So that leaves us with |
| 6445 | * |
| 6446 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 6447 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 6448 | * transaction reservation. |
| 6449 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 6450 | * updating the inode. |
| 6451 | */ |
| 6452 | rsv = btrfs_alloc_block_rsv(root); |
| 6453 | if (!rsv) |
| 6454 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 6455 | rsv->size = min_size; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6456 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6457 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 6458 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6459 | * 1 for the orphan item we're going to add |
| 6460 | * 1 for the orphan item deletion |
| 6461 | * 1 for updating the inode. |
| 6462 | */ |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6463 | trans = btrfs_start_transaction(root, 4); |
| 6464 | if (IS_ERR(trans)) { |
| 6465 | err = PTR_ERR(trans); |
| 6466 | goto out; |
| 6467 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6468 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6469 | /* Migrate the slack space for the truncate to our reserve */ |
| 6470 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 6471 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6472 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6473 | |
| 6474 | ret = btrfs_orphan_add(trans, inode); |
| 6475 | if (ret) { |
| 6476 | btrfs_end_transaction(trans, root); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6477 | goto out; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6478 | } |
| 6479 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6480 | /* |
| 6481 | * setattr is responsible for setting the ordered_data_close flag, |
| 6482 | * but that is only tested during the last file release. That |
| 6483 | * could happen well after the next commit, leaving a great big |
| 6484 | * window where new writes may get lost if someone chooses to write |
| 6485 | * to this file after truncating to zero |
| 6486 | * |
| 6487 | * The inode doesn't have any dirty data here, and so if we commit |
| 6488 | * this is a noop. If someone immediately starts writing to the inode |
| 6489 | * it is very likely we'll catch some of their writes in this |
| 6490 | * transaction, and the commit will find this file on the ordered |
| 6491 | * data list with good things to send down. |
| 6492 | * |
| 6493 | * This is a best effort solution, there is still a window where |
| 6494 | * using truncate to replace the contents of the file will |
| 6495 | * end up with a zero length file after a crash. |
| 6496 | */ |
| 6497 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 6498 | btrfs_add_ordered_operation(trans, root, inode); |
| 6499 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6500 | while (1) { |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 6501 | ret = btrfs_block_rsv_refill(root, rsv, min_size); |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6502 | if (ret) { |
| 6503 | /* |
| 6504 | * This can only happen with the original transaction we |
| 6505 | * started above, every other time we shouldn't have a |
| 6506 | * transaction started yet. |
| 6507 | */ |
| 6508 | if (ret == -EAGAIN) |
| 6509 | goto end_trans; |
| 6510 | err = ret; |
| 6511 | break; |
| 6512 | } |
| 6513 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6514 | if (!trans) { |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6515 | /* Just need the 1 for updating the inode */ |
| 6516 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6517 | if (IS_ERR(trans)) { |
| 6518 | err = PTR_ERR(trans); |
| 6519 | goto out; |
| 6520 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6521 | } |
| 6522 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6523 | trans->block_rsv = rsv; |
| 6524 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6525 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 6526 | inode->i_size, |
| 6527 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6528 | if (ret != -EAGAIN) { |
| 6529 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6530 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6531 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6532 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6533 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6534 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6535 | if (ret) { |
| 6536 | err = ret; |
| 6537 | break; |
| 6538 | } |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6539 | end_trans: |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6540 | nr = trans->blocks_used; |
| 6541 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6542 | trans = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6543 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6544 | } |
| 6545 | |
| 6546 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6547 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6548 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6549 | if (ret) |
| 6550 | err = ret; |
Josef Bacik | ded5db9 | 2011-03-04 14:09:46 -0500 | [diff] [blame] | 6551 | } else if (ret && inode->i_nlink > 0) { |
| 6552 | /* |
| 6553 | * Failed to do the truncate, remove us from the in memory |
| 6554 | * orphan list. |
| 6555 | */ |
| 6556 | ret = btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6557 | } |
| 6558 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 6559 | if (trans) { |
| 6560 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 6561 | ret = btrfs_update_inode(trans, root, inode); |
| 6562 | if (ret && !err) |
| 6563 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6564 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 6565 | nr = trans->blocks_used; |
| 6566 | ret = btrfs_end_transaction_throttle(trans, root); |
| 6567 | btrfs_btree_balance_dirty(root, nr); |
| 6568 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6569 | |
| 6570 | out: |
| 6571 | btrfs_free_block_rsv(root, rsv); |
| 6572 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6573 | if (ret && !err) |
| 6574 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6575 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6576 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6577 | } |
| 6578 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6579 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6580 | * create a new subvolume directory/inode (helper for the ioctl). |
| 6581 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6582 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6583 | struct btrfs_root *new_root, u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6584 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6585 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6586 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6587 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6588 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6589 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6590 | new_dirid, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6591 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 6592 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6593 | inode->i_op = &btrfs_dir_inode_operations; |
| 6594 | inode->i_fop = &btrfs_dir_file_operations; |
| 6595 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 6596 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6597 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6598 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6599 | err = btrfs_update_inode(trans, new_root, inode); |
| 6600 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6601 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6602 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6603 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6604 | } |
| 6605 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6606 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 6607 | { |
| 6608 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6609 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6610 | |
| 6611 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 6612 | if (!ei) |
| 6613 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6614 | |
| 6615 | ei->root = NULL; |
| 6616 | ei->space_info = NULL; |
| 6617 | ei->generation = 0; |
| 6618 | ei->sequence = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 6619 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6620 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6621 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6622 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6623 | ei->disk_i_size = 0; |
| 6624 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6625 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6626 | ei->index_cnt = (u64)-1; |
| 6627 | ei->last_unlink_trans = 0; |
| 6628 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 6629 | spin_lock_init(&ei->lock); |
| 6630 | ei->outstanding_extents = 0; |
| 6631 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6632 | |
| 6633 | ei->ordered_data_close = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6634 | ei->orphan_meta_reserved = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6635 | ei->dummy_inode = 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 6636 | ei->in_defrag = 0; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 6637 | ei->delalloc_meta_reserved = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6638 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6639 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6640 | ei->delayed_node = NULL; |
| 6641 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6642 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 6643 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 6644 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 6645 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6646 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6647 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6648 | INIT_LIST_HEAD(&ei->i_orphan); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6649 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6650 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6651 | RB_CLEAR_NODE(&ei->rb_node); |
| 6652 | |
| 6653 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6654 | } |
| 6655 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6656 | static void btrfs_i_callback(struct rcu_head *head) |
| 6657 | { |
| 6658 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 6659 | INIT_LIST_HEAD(&inode->i_dentry); |
| 6660 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 6661 | } |
| 6662 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6663 | void btrfs_destroy_inode(struct inode *inode) |
| 6664 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6665 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6666 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6667 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6668 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 6669 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 6670 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 6671 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6672 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 6673 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6674 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6675 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6676 | * This can happen where we create an inode, but somebody else also |
| 6677 | * created the same inode and we need to destroy the one we already |
| 6678 | * created. |
| 6679 | */ |
| 6680 | if (!root) |
| 6681 | goto free; |
| 6682 | |
| 6683 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6684 | * Make sure we're properly removed from the ordered operation |
| 6685 | * lists. |
| 6686 | */ |
| 6687 | smp_mb(); |
| 6688 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 6689 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 6690 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 6691 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 6692 | } |
| 6693 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6694 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6695 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6696 | printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n", |
| 6697 | (unsigned long long)btrfs_ino(inode)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6698 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6699 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6700 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6701 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6702 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6703 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 6704 | if (!ordered) |
| 6705 | break; |
| 6706 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6707 | printk(KERN_ERR "btrfs found ordered " |
| 6708 | "extent %llu %llu on inode cleanup\n", |
| 6709 | (unsigned long long)ordered->file_offset, |
| 6710 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6711 | btrfs_remove_ordered_extent(inode, ordered); |
| 6712 | btrfs_put_ordered_extent(ordered); |
| 6713 | btrfs_put_ordered_extent(ordered); |
| 6714 | } |
| 6715 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6716 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6717 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6718 | free: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6719 | btrfs_remove_delayed_node(inode); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6720 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6721 | } |
| 6722 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6723 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6724 | { |
| 6725 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6726 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6727 | if (btrfs_root_refs(&root->root_item) == 0 && |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 6728 | !btrfs_is_free_space_inode(root, inode)) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6729 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6730 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6731 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6732 | } |
| 6733 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 6734 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6735 | { |
| 6736 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 6737 | |
| 6738 | inode_init_once(&ei->vfs_inode); |
| 6739 | } |
| 6740 | |
| 6741 | void btrfs_destroy_cachep(void) |
| 6742 | { |
| 6743 | if (btrfs_inode_cachep) |
| 6744 | kmem_cache_destroy(btrfs_inode_cachep); |
| 6745 | if (btrfs_trans_handle_cachep) |
| 6746 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 6747 | if (btrfs_transaction_cachep) |
| 6748 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6749 | if (btrfs_path_cachep) |
| 6750 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 6751 | if (btrfs_free_space_cachep) |
| 6752 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6753 | } |
| 6754 | |
| 6755 | int btrfs_init_cachep(void) |
| 6756 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6757 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 6758 | sizeof(struct btrfs_inode), 0, |
| 6759 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6760 | if (!btrfs_inode_cachep) |
| 6761 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6762 | |
| 6763 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 6764 | sizeof(struct btrfs_trans_handle), 0, |
| 6765 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6766 | if (!btrfs_trans_handle_cachep) |
| 6767 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6768 | |
| 6769 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 6770 | sizeof(struct btrfs_transaction), 0, |
| 6771 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6772 | if (!btrfs_transaction_cachep) |
| 6773 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6774 | |
| 6775 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 6776 | sizeof(struct btrfs_path), 0, |
| 6777 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6778 | if (!btrfs_path_cachep) |
| 6779 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6780 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 6781 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache", |
| 6782 | sizeof(struct btrfs_free_space), 0, |
| 6783 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 6784 | if (!btrfs_free_space_cachep) |
| 6785 | goto fail; |
| 6786 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6787 | return 0; |
| 6788 | fail: |
| 6789 | btrfs_destroy_cachep(); |
| 6790 | return -ENOMEM; |
| 6791 | } |
| 6792 | |
| 6793 | static int btrfs_getattr(struct vfsmount *mnt, |
| 6794 | struct dentry *dentry, struct kstat *stat) |
| 6795 | { |
| 6796 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 6797 | u32 blocksize = inode->i_sb->s_blocksize; |
| 6798 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6799 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 6800 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 6801 | stat->blksize = PAGE_CACHE_SIZE; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 6802 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
| 6803 | ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6804 | return 0; |
| 6805 | } |
| 6806 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 6807 | /* |
| 6808 | * If a file is moved, it will inherit the cow and compression flags of the new |
| 6809 | * directory. |
| 6810 | */ |
| 6811 | static void fixup_inode_flags(struct inode *dir, struct inode *inode) |
| 6812 | { |
| 6813 | struct btrfs_inode *b_dir = BTRFS_I(dir); |
| 6814 | struct btrfs_inode *b_inode = BTRFS_I(inode); |
| 6815 | |
| 6816 | if (b_dir->flags & BTRFS_INODE_NODATACOW) |
| 6817 | b_inode->flags |= BTRFS_INODE_NODATACOW; |
| 6818 | else |
| 6819 | b_inode->flags &= ~BTRFS_INODE_NODATACOW; |
| 6820 | |
| 6821 | if (b_dir->flags & BTRFS_INODE_COMPRESS) |
| 6822 | b_inode->flags |= BTRFS_INODE_COMPRESS; |
| 6823 | else |
| 6824 | b_inode->flags &= ~BTRFS_INODE_COMPRESS; |
| 6825 | } |
| 6826 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6827 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 6828 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6829 | { |
| 6830 | struct btrfs_trans_handle *trans; |
| 6831 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6832 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6833 | struct inode *new_inode = new_dentry->d_inode; |
| 6834 | struct inode *old_inode = old_dentry->d_inode; |
| 6835 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6836 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6837 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6838 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6839 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6840 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6841 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6842 | return -EPERM; |
| 6843 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6844 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6845 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6846 | return -EXDEV; |
| 6847 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6848 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 6849 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6850 | return -ENOTEMPTY; |
| 6851 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6852 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6853 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6854 | return -ENOTEMPTY; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6855 | /* |
| 6856 | * we're using rename to replace one file with another. |
| 6857 | * and the replacement file is large. Start IO on it now so |
| 6858 | * we don't add too much work to the end of the transaction |
| 6859 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 6860 | 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] | 6861 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 6862 | filemap_flush(old_inode->i_mapping); |
| 6863 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6864 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6865 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6866 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6867 | /* |
| 6868 | * We want to reserve the absolute worst case amount of items. So if |
| 6869 | * both inodes are subvols and we need to unlink them then that would |
| 6870 | * require 4 item modifications, but if they are both normal inodes it |
| 6871 | * would require 5 item modifications, so we'll assume their normal |
| 6872 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 6873 | * should cover the worst case number of items we'll modify. |
| 6874 | */ |
| 6875 | trans = btrfs_start_transaction(root, 20); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 6876 | if (IS_ERR(trans)) { |
| 6877 | ret = PTR_ERR(trans); |
| 6878 | goto out_notrans; |
| 6879 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6880 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6881 | if (dest != root) |
| 6882 | btrfs_record_root_in_trans(trans, dest); |
| 6883 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6884 | ret = btrfs_set_inode_index(new_dir, &index); |
| 6885 | if (ret) |
| 6886 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6887 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6888 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6889 | /* force full log commit if subvolume involved. */ |
| 6890 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 6891 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6892 | ret = btrfs_insert_inode_ref(trans, dest, |
| 6893 | new_dentry->d_name.name, |
| 6894 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6895 | old_ino, |
| 6896 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6897 | if (ret) |
| 6898 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6899 | /* |
| 6900 | * this is an ugly little race, but the rename is required |
| 6901 | * to make sure that if we crash, the inode is either at the |
| 6902 | * old name or the new one. pinning the log transaction lets |
| 6903 | * us make sure we don't allow a log commit to come in after |
| 6904 | * we unlink the name but before we add the new name back in. |
| 6905 | */ |
| 6906 | btrfs_pin_log_trans(root); |
| 6907 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6908 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6909 | * make sure the inode gets flushed if it is replacing |
| 6910 | * something. |
| 6911 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6912 | 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] | 6913 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6914 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6915 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 6916 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 6917 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6918 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6919 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 6920 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 6921 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6922 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6923 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 6924 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 6925 | old_dentry->d_name.name, |
| 6926 | old_dentry->d_name.len); |
| 6927 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 6928 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 6929 | old_dentry->d_inode, |
| 6930 | old_dentry->d_name.name, |
| 6931 | old_dentry->d_name.len); |
| 6932 | if (!ret) |
| 6933 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6934 | } |
| 6935 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6936 | |
| 6937 | if (new_inode) { |
| 6938 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6939 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6940 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 6941 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 6942 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 6943 | root_objectid, |
| 6944 | new_dentry->d_name.name, |
| 6945 | new_dentry->d_name.len); |
| 6946 | BUG_ON(new_inode->i_nlink == 0); |
| 6947 | } else { |
| 6948 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 6949 | new_dentry->d_inode, |
| 6950 | new_dentry->d_name.name, |
| 6951 | new_dentry->d_name.len); |
| 6952 | } |
| 6953 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6954 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6955 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6956 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6957 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6958 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6959 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 6960 | fixup_inode_flags(new_dir, old_inode); |
| 6961 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6962 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 6963 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6964 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6965 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6966 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6967 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6968 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6969 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6970 | btrfs_end_log_trans(root); |
| 6971 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6972 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 6973 | btrfs_end_transaction_throttle(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 6974 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6975 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6976 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6977 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6978 | return ret; |
| 6979 | } |
| 6980 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6981 | /* |
| 6982 | * some fairly slow code that needs optimization. This walks the list |
| 6983 | * of all the inodes with pending delalloc and forces them to disk. |
| 6984 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 6985 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6986 | { |
| 6987 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 6988 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6989 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6990 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 6991 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 6992 | return -EROFS; |
| 6993 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6994 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6995 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6996 | binode = list_entry(head->next, struct btrfs_inode, |
| 6997 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6998 | inode = igrab(&binode->vfs_inode); |
| 6999 | if (!inode) |
| 7000 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7001 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7002 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7003 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7004 | if (delay_iput) |
| 7005 | btrfs_add_delayed_iput(inode); |
| 7006 | else |
| 7007 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7008 | } |
| 7009 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7010 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7011 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7012 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7013 | |
| 7014 | /* the filemap_flush will queue IO into the worker threads, but |
| 7015 | * we have to make sure the IO is actually started and that |
| 7016 | * ordered extents get created before we return |
| 7017 | */ |
| 7018 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7019 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7020 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7021 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7022 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 7023 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7024 | } |
| 7025 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7026 | return 0; |
| 7027 | } |
| 7028 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7029 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 7030 | const char *symname) |
| 7031 | { |
| 7032 | struct btrfs_trans_handle *trans; |
| 7033 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 7034 | struct btrfs_path *path; |
| 7035 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7036 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7037 | int err; |
| 7038 | int drop_inode = 0; |
| 7039 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7040 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7041 | int name_len; |
| 7042 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7043 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7044 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7045 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7046 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7047 | |
| 7048 | name_len = strlen(symname) + 1; |
| 7049 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 7050 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7051 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7052 | /* |
| 7053 | * 2 items for inode item and ref |
| 7054 | * 2 items for dir items |
| 7055 | * 1 item for xattr if selinux is on |
| 7056 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7057 | trans = btrfs_start_transaction(root, 5); |
| 7058 | if (IS_ERR(trans)) |
| 7059 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7060 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 7061 | err = btrfs_find_free_ino(root, &objectid); |
| 7062 | if (err) |
| 7063 | goto out_unlock; |
| 7064 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7065 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7066 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7067 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7068 | if (IS_ERR(inode)) { |
| 7069 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7070 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7071 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7072 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 7073 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7074 | if (err) { |
| 7075 | drop_inode = 1; |
| 7076 | goto out_unlock; |
| 7077 | } |
| 7078 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 7079 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7080 | if (err) |
| 7081 | drop_inode = 1; |
| 7082 | else { |
| 7083 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7084 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7085 | inode->i_fop = &btrfs_file_operations; |
| 7086 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7087 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7088 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7089 | if (drop_inode) |
| 7090 | goto out_unlock; |
| 7091 | |
| 7092 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 7093 | if (!path) { |
| 7094 | err = -ENOMEM; |
| 7095 | drop_inode = 1; |
| 7096 | goto out_unlock; |
| 7097 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7098 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7099 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7100 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 7101 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 7102 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 7103 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7104 | if (err) { |
| 7105 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 7106 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7107 | goto out_unlock; |
| 7108 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7109 | leaf = path->nodes[0]; |
| 7110 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 7111 | struct btrfs_file_extent_item); |
| 7112 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 7113 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7114 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7115 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 7116 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 7117 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 7118 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 7119 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7120 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7121 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 7122 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7123 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7124 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7125 | inode->i_op = &btrfs_symlink_inode_operations; |
| 7126 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7127 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7128 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7129 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7130 | err = btrfs_update_inode(trans, root, inode); |
| 7131 | if (err) |
| 7132 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7133 | |
| 7134 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7135 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 7136 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7137 | if (drop_inode) { |
| 7138 | inode_dec_link_count(inode); |
| 7139 | iput(inode); |
| 7140 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7141 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7142 | return err; |
| 7143 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7144 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7145 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7146 | u64 start, u64 num_bytes, u64 min_size, |
| 7147 | loff_t actual_len, u64 *alloc_hint, |
| 7148 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7149 | { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7150 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7151 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7152 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7153 | u64 i_size; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7154 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7155 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7156 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7157 | if (trans) |
| 7158 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7159 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7160 | if (own_trans) { |
| 7161 | trans = btrfs_start_transaction(root, 3); |
| 7162 | if (IS_ERR(trans)) { |
| 7163 | ret = PTR_ERR(trans); |
| 7164 | break; |
| 7165 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7166 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7167 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7168 | ret = btrfs_reserve_extent(trans, root, num_bytes, min_size, |
| 7169 | 0, *alloc_hint, (u64)-1, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7170 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7171 | if (own_trans) |
| 7172 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7173 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7174 | } |
| 7175 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7176 | ret = insert_reserved_file_extent(trans, inode, |
| 7177 | cur_offset, ins.objectid, |
| 7178 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 7179 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7180 | BTRFS_FILE_EXTENT_PREALLOC); |
| 7181 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 7182 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7183 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7184 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7185 | num_bytes -= ins.offset; |
| 7186 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7187 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7188 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7189 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7190 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7191 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7192 | (actual_len > inode->i_size) && |
| 7193 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7194 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7195 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7196 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7197 | i_size = cur_offset; |
| 7198 | i_size_write(inode, i_size); |
| 7199 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7200 | } |
| 7201 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7202 | ret = btrfs_update_inode(trans, root, inode); |
| 7203 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7204 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7205 | if (own_trans) |
| 7206 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7207 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7208 | return ret; |
| 7209 | } |
| 7210 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7211 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7212 | u64 start, u64 num_bytes, u64 min_size, |
| 7213 | loff_t actual_len, u64 *alloc_hint) |
| 7214 | { |
| 7215 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7216 | min_size, actual_len, alloc_hint, |
| 7217 | NULL); |
| 7218 | } |
| 7219 | |
| 7220 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 7221 | struct btrfs_trans_handle *trans, int mode, |
| 7222 | u64 start, u64 num_bytes, u64 min_size, |
| 7223 | loff_t actual_len, u64 *alloc_hint) |
| 7224 | { |
| 7225 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7226 | min_size, actual_len, alloc_hint, trans); |
| 7227 | } |
| 7228 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7229 | static int btrfs_set_page_dirty(struct page *page) |
| 7230 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7231 | return __set_page_dirty_nobuffers(page); |
| 7232 | } |
| 7233 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 7234 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7235 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7236 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 7237 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7238 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 7239 | if (mask & MAY_WRITE && |
| 7240 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 7241 | if (btrfs_root_readonly(root)) |
| 7242 | return -EROFS; |
| 7243 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 7244 | return -EACCES; |
| 7245 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 7246 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7247 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7248 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7249 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7250 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7251 | .lookup = btrfs_lookup, |
| 7252 | .create = btrfs_create, |
| 7253 | .unlink = btrfs_unlink, |
| 7254 | .link = btrfs_link, |
| 7255 | .mkdir = btrfs_mkdir, |
| 7256 | .rmdir = btrfs_rmdir, |
| 7257 | .rename = btrfs_rename, |
| 7258 | .symlink = btrfs_symlink, |
| 7259 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7260 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7261 | .setxattr = btrfs_setxattr, |
| 7262 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7263 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7264 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7265 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7266 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7267 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7268 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7269 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7270 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7271 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7272 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7273 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 7274 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7275 | .llseek = generic_file_llseek, |
| 7276 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 7277 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7278 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7279 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7280 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7281 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 7282 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7283 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7284 | }; |
| 7285 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7286 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7287 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 7288 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7289 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7290 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7291 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7292 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 7293 | .set_bit_hook = btrfs_set_bit_hook, |
| 7294 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7295 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 7296 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7297 | }; |
| 7298 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 7299 | /* |
| 7300 | * btrfs doesn't support the bmap operation because swapfiles |
| 7301 | * use bmap to make a mapping of extents in the file. They assume |
| 7302 | * these extents won't change over the life of the file and they |
| 7303 | * use the bmap result to do IO directly to the drive. |
| 7304 | * |
| 7305 | * the btrfs bmap call would return logical addresses that aren't |
| 7306 | * suitable for IO and they also will change frequently as COW |
| 7307 | * operations happen. So, swapfile + btrfs == corruption. |
| 7308 | * |
| 7309 | * For now we're avoiding this by dropping bmap. |
| 7310 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7311 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7312 | .readpage = btrfs_readpage, |
| 7313 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7314 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7315 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7316 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7317 | .invalidatepage = btrfs_invalidatepage, |
| 7318 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7319 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 7320 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7321 | }; |
| 7322 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7323 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7324 | .readpage = btrfs_readpage, |
| 7325 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 7326 | .invalidatepage = btrfs_invalidatepage, |
| 7327 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7328 | }; |
| 7329 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7330 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7331 | .getattr = btrfs_getattr, |
| 7332 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7333 | .setxattr = btrfs_setxattr, |
| 7334 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7335 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7336 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7337 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7338 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7339 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7340 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7341 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7342 | .getattr = btrfs_getattr, |
| 7343 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7344 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7345 | .setxattr = btrfs_setxattr, |
| 7346 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7347 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7348 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7349 | .get_acl = btrfs_get_acl, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7350 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7351 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7352 | .readlink = generic_readlink, |
| 7353 | .follow_link = page_follow_link_light, |
| 7354 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 7355 | .getattr = btrfs_getattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7356 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 7357 | .setxattr = btrfs_setxattr, |
| 7358 | .getxattr = btrfs_getxattr, |
| 7359 | .listxattr = btrfs_listxattr, |
| 7360 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7361 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7362 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7363 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 7364 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7365 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 7366 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7367 | }; |