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> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 40 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 41 | #include "ctree.h" |
| 42 | #include "disk-io.h" |
| 43 | #include "transaction.h" |
| 44 | #include "btrfs_inode.h" |
| 45 | #include "ioctl.h" |
| 46 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 47 | #include "volumes.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" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 51 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 52 | #include "locking.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 53 | |
| 54 | struct btrfs_iget_args { |
| 55 | u64 ino; |
| 56 | struct btrfs_root *root; |
| 57 | }; |
| 58 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 59 | static const struct inode_operations btrfs_dir_inode_operations; |
| 60 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 61 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 62 | static const struct inode_operations btrfs_special_inode_operations; |
| 63 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 64 | static const struct address_space_operations btrfs_aops; |
| 65 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 66 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 67 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 68 | |
| 69 | static struct kmem_cache *btrfs_inode_cachep; |
| 70 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 71 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 72 | struct kmem_cache *btrfs_path_cachep; |
| 73 | |
| 74 | #define S_SHIFT 12 |
| 75 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 76 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 77 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 78 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 79 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 80 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 81 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 82 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 83 | }; |
| 84 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 85 | static void btrfs_truncate(struct inode *inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 86 | 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] | 87 | static noinline int cow_file_range(struct inode *inode, |
| 88 | struct page *locked_page, |
| 89 | u64 start, u64 end, int *page_started, |
| 90 | unsigned long *nr_written, int unlock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 91 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 92 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
| 93 | struct inode *inode, struct inode *dir) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 94 | { |
| 95 | int err; |
| 96 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 97 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 98 | if (!err) |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 99 | err = btrfs_xattr_security_init(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 100 | return err; |
| 101 | } |
| 102 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 103 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 104 | * this does all the hard work for inserting an inline extent into |
| 105 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 106 | * no overlapping inline items exist in the btree |
| 107 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 108 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 109 | struct btrfs_root *root, struct inode *inode, |
| 110 | u64 start, size_t size, size_t compressed_size, |
| 111 | struct page **compressed_pages) |
| 112 | { |
| 113 | struct btrfs_key key; |
| 114 | struct btrfs_path *path; |
| 115 | struct extent_buffer *leaf; |
| 116 | struct page *page = NULL; |
| 117 | char *kaddr; |
| 118 | unsigned long ptr; |
| 119 | struct btrfs_file_extent_item *ei; |
| 120 | int err = 0; |
| 121 | int ret; |
| 122 | size_t cur_size = size; |
| 123 | size_t datasize; |
| 124 | unsigned long offset; |
| 125 | int use_compress = 0; |
| 126 | |
| 127 | if (compressed_size && compressed_pages) { |
| 128 | use_compress = 1; |
| 129 | cur_size = compressed_size; |
| 130 | } |
| 131 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 132 | path = btrfs_alloc_path(); |
| 133 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 134 | return -ENOMEM; |
| 135 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 136 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 137 | btrfs_set_trans_block_group(trans, inode); |
| 138 | |
| 139 | key.objectid = inode->i_ino; |
| 140 | key.offset = start; |
| 141 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 142 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 143 | |
| 144 | inode_add_bytes(inode, size); |
| 145 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 146 | datasize); |
| 147 | BUG_ON(ret); |
| 148 | if (ret) { |
| 149 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 150 | goto fail; |
| 151 | } |
| 152 | leaf = path->nodes[0]; |
| 153 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 154 | struct btrfs_file_extent_item); |
| 155 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 156 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 157 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 158 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 159 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 160 | ptr = btrfs_file_extent_inline_start(ei); |
| 161 | |
| 162 | if (use_compress) { |
| 163 | struct page *cpage; |
| 164 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 165 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 167 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 168 | PAGE_CACHE_SIZE); |
| 169 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 170 | kaddr = kmap_atomic(cpage, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 172 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 173 | |
| 174 | i++; |
| 175 | ptr += cur_size; |
| 176 | compressed_size -= cur_size; |
| 177 | } |
| 178 | btrfs_set_file_extent_compression(leaf, ei, |
| 179 | BTRFS_COMPRESS_ZLIB); |
| 180 | } else { |
| 181 | page = find_get_page(inode->i_mapping, |
| 182 | start >> PAGE_CACHE_SHIFT); |
| 183 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 184 | kaddr = kmap_atomic(page, KM_USER0); |
| 185 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 186 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
| 187 | kunmap_atomic(kaddr, KM_USER0); |
| 188 | page_cache_release(page); |
| 189 | } |
| 190 | btrfs_mark_buffer_dirty(leaf); |
| 191 | btrfs_free_path(path); |
| 192 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 193 | /* |
| 194 | * we're an inline extent, so nobody can |
| 195 | * extend the file past i_size without locking |
| 196 | * a page we already have locked. |
| 197 | * |
| 198 | * We must do any isize and inode updates |
| 199 | * before we unlock the pages. Otherwise we |
| 200 | * could end up racing with unlink. |
| 201 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 202 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
| 203 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 204 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 205 | return 0; |
| 206 | fail: |
| 207 | btrfs_free_path(path); |
| 208 | return err; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /* |
| 213 | * conditionally insert an inline extent into the file. This |
| 214 | * does the checks required to make sure the data is small enough |
| 215 | * to fit as an inline extent. |
| 216 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 217 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 218 | struct btrfs_root *root, |
| 219 | struct inode *inode, u64 start, u64 end, |
| 220 | size_t compressed_size, |
| 221 | struct page **compressed_pages) |
| 222 | { |
| 223 | u64 isize = i_size_read(inode); |
| 224 | u64 actual_end = min(end + 1, isize); |
| 225 | u64 inline_len = actual_end - start; |
| 226 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 227 | ~((u64)root->sectorsize - 1); |
| 228 | u64 hint_byte; |
| 229 | u64 data_len = inline_len; |
| 230 | int ret; |
| 231 | |
| 232 | if (compressed_size) |
| 233 | data_len = compressed_size; |
| 234 | |
| 235 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 236 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 238 | (!compressed_size && |
| 239 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 240 | end + 1 < isize || |
| 241 | data_len > root->fs_info->max_inline) { |
| 242 | return 1; |
| 243 | } |
| 244 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 245 | ret = btrfs_drop_extents(trans, inode, start, aligned_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 246 | &hint_byte, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 247 | BUG_ON(ret); |
| 248 | |
| 249 | if (isize > actual_end) |
| 250 | inline_len = min_t(u64, isize, actual_end); |
| 251 | ret = insert_inline_extent(trans, root, inode, start, |
| 252 | inline_len, compressed_size, |
| 253 | compressed_pages); |
| 254 | BUG_ON(ret); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 255 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 256 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 260 | struct async_extent { |
| 261 | u64 start; |
| 262 | u64 ram_size; |
| 263 | u64 compressed_size; |
| 264 | struct page **pages; |
| 265 | unsigned long nr_pages; |
| 266 | struct list_head list; |
| 267 | }; |
| 268 | |
| 269 | struct async_cow { |
| 270 | struct inode *inode; |
| 271 | struct btrfs_root *root; |
| 272 | struct page *locked_page; |
| 273 | u64 start; |
| 274 | u64 end; |
| 275 | struct list_head extents; |
| 276 | struct btrfs_work work; |
| 277 | }; |
| 278 | |
| 279 | static noinline int add_async_extent(struct async_cow *cow, |
| 280 | u64 start, u64 ram_size, |
| 281 | u64 compressed_size, |
| 282 | struct page **pages, |
| 283 | unsigned long nr_pages) |
| 284 | { |
| 285 | struct async_extent *async_extent; |
| 286 | |
| 287 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
| 288 | async_extent->start = start; |
| 289 | async_extent->ram_size = ram_size; |
| 290 | async_extent->compressed_size = compressed_size; |
| 291 | async_extent->pages = pages; |
| 292 | async_extent->nr_pages = nr_pages; |
| 293 | list_add_tail(&async_extent->list, &cow->extents); |
| 294 | return 0; |
| 295 | } |
| 296 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 297 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 298 | * we create compressed extents in two phases. The first |
| 299 | * phase compresses a range of pages that have already been |
| 300 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 301 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 302 | * This is done inside an ordered work queue, and the compression |
| 303 | * is spread across many cpus. The actual IO submission is step |
| 304 | * two, and the ordered work queue takes care of making sure that |
| 305 | * happens in the same order things were put onto the queue by |
| 306 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 307 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 308 | * If this code finds it can't get good compression, it puts an |
| 309 | * entry onto the work queue to write the uncompressed bytes. This |
| 310 | * makes sure that both compressed inodes and uncompressed inodes |
| 311 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 312 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 313 | static noinline int compress_file_range(struct inode *inode, |
| 314 | struct page *locked_page, |
| 315 | u64 start, u64 end, |
| 316 | struct async_cow *async_cow, |
| 317 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 318 | { |
| 319 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 320 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 321 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 322 | u64 orig_start; |
| 323 | u64 disk_num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 324 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 325 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 326 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 327 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 328 | struct page **pages = NULL; |
| 329 | unsigned long nr_pages; |
| 330 | unsigned long nr_pages_ret = 0; |
| 331 | unsigned long total_compressed = 0; |
| 332 | unsigned long total_in = 0; |
| 333 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 334 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 335 | int i; |
| 336 | int will_compress; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 337 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 338 | orig_start = start; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 339 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 340 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 341 | again: |
| 342 | will_compress = 0; |
| 343 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 344 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 345 | |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 346 | /* |
| 347 | * we don't want to send crud past the end of i_size through |
| 348 | * compression, that's just a waste of CPU time. So, if the |
| 349 | * end of the file is before the start of our current |
| 350 | * requested range of bytes, we bail out to the uncompressed |
| 351 | * cleanup code that can deal with all of this. |
| 352 | * |
| 353 | * It isn't really the fastest way to fix things, but this is a |
| 354 | * very uncommon corner. |
| 355 | */ |
| 356 | if (actual_end <= start) |
| 357 | goto cleanup_and_bail_uncompressed; |
| 358 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 359 | total_compressed = actual_end - start; |
| 360 | |
| 361 | /* we want to make sure that amount of ram required to uncompress |
| 362 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 363 | * of a compressed extent to 128k. This is a crucial number |
| 364 | * because it also controls how easily we can spread reads across |
| 365 | * cpus for decompression. |
| 366 | * |
| 367 | * We also want to make sure the amount of IO required to do |
| 368 | * a random read is reasonably small, so we limit the size of |
| 369 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 370 | */ |
| 371 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 372 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 373 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 374 | disk_num_bytes = num_bytes; |
| 375 | total_in = 0; |
| 376 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 377 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 378 | /* |
| 379 | * we do compression for mount -o compress and when the |
| 380 | * inode has not been flagged as nocompress. This flag can |
| 381 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 382 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 383 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 384 | (btrfs_test_opt(root, COMPRESS) || |
| 385 | (BTRFS_I(inode)->force_compress))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 386 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 387 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 388 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 389 | ret = btrfs_zlib_compress_pages(inode->i_mapping, start, |
| 390 | total_compressed, pages, |
| 391 | nr_pages, &nr_pages_ret, |
| 392 | &total_in, |
| 393 | &total_compressed, |
| 394 | max_compressed); |
| 395 | |
| 396 | if (!ret) { |
| 397 | unsigned long offset = total_compressed & |
| 398 | (PAGE_CACHE_SIZE - 1); |
| 399 | struct page *page = pages[nr_pages_ret - 1]; |
| 400 | char *kaddr; |
| 401 | |
| 402 | /* zero the tail end of the last page, we might be |
| 403 | * sending it down to disk |
| 404 | */ |
| 405 | if (offset) { |
| 406 | kaddr = kmap_atomic(page, KM_USER0); |
| 407 | memset(kaddr + offset, 0, |
| 408 | PAGE_CACHE_SIZE - offset); |
| 409 | kunmap_atomic(kaddr, KM_USER0); |
| 410 | } |
| 411 | will_compress = 1; |
| 412 | } |
| 413 | } |
| 414 | if (start == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 415 | trans = btrfs_join_transaction(root, 1); |
| 416 | BUG_ON(!trans); |
| 417 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 418 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 419 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 420 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 421 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 422 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 423 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 424 | */ |
| 425 | ret = cow_file_range_inline(trans, root, inode, |
| 426 | start, end, 0, NULL); |
| 427 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 428 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 429 | ret = cow_file_range_inline(trans, root, inode, |
| 430 | start, end, |
| 431 | total_compressed, pages); |
| 432 | } |
| 433 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 434 | /* |
| 435 | * inline extent creation worked, we don't need |
| 436 | * to create any more async work items. Unlock |
| 437 | * and free up our temp pages. |
| 438 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 439 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 440 | &BTRFS_I(inode)->io_tree, |
| 441 | start, end, NULL, |
| 442 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 443 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 444 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 445 | |
| 446 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 447 | goto free_pages_out; |
| 448 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 449 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | if (will_compress) { |
| 453 | /* |
| 454 | * we aren't doing an inline extent round the compressed size |
| 455 | * up to a block size boundary so the allocator does sane |
| 456 | * things |
| 457 | */ |
| 458 | total_compressed = (total_compressed + blocksize - 1) & |
| 459 | ~(blocksize - 1); |
| 460 | |
| 461 | /* |
| 462 | * one last check to make sure the compression is really a |
| 463 | * win, compare the page count read with the blocks on disk |
| 464 | */ |
| 465 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 466 | ~(PAGE_CACHE_SIZE - 1); |
| 467 | if (total_compressed >= total_in) { |
| 468 | will_compress = 0; |
| 469 | } else { |
| 470 | disk_num_bytes = total_compressed; |
| 471 | num_bytes = total_in; |
| 472 | } |
| 473 | } |
| 474 | if (!will_compress && pages) { |
| 475 | /* |
| 476 | * the compression code ran but failed to make things smaller, |
| 477 | * free any pages it allocated and our page pointer array |
| 478 | */ |
| 479 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 480 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 481 | page_cache_release(pages[i]); |
| 482 | } |
| 483 | kfree(pages); |
| 484 | pages = NULL; |
| 485 | total_compressed = 0; |
| 486 | nr_pages_ret = 0; |
| 487 | |
| 488 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 489 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 490 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 491 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 492 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 493 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 494 | if (will_compress) { |
| 495 | *num_added += 1; |
| 496 | |
| 497 | /* the async work queues will take care of doing actual |
| 498 | * allocation on disk for these compressed pages, |
| 499 | * and will submit them to the elevator. |
| 500 | */ |
| 501 | add_async_extent(async_cow, start, num_bytes, |
| 502 | total_compressed, pages, nr_pages_ret); |
| 503 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 504 | if (start + num_bytes < end && start + num_bytes < actual_end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 505 | start += num_bytes; |
| 506 | pages = NULL; |
| 507 | cond_resched(); |
| 508 | goto again; |
| 509 | } |
| 510 | } else { |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 511 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 512 | /* |
| 513 | * No compression, but we still need to write the pages in |
| 514 | * the file we've been given so far. redirty the locked |
| 515 | * page if it corresponds to our extent and set things up |
| 516 | * for the async work queue to run cow_file_range to do |
| 517 | * the normal delalloc dance |
| 518 | */ |
| 519 | if (page_offset(locked_page) >= start && |
| 520 | page_offset(locked_page) <= end) { |
| 521 | __set_page_dirty_nobuffers(locked_page); |
| 522 | /* unlocked later on in the async handlers */ |
| 523 | } |
| 524 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0); |
| 525 | *num_added += 1; |
| 526 | } |
| 527 | |
| 528 | out: |
| 529 | return 0; |
| 530 | |
| 531 | free_pages_out: |
| 532 | for (i = 0; i < nr_pages_ret; i++) { |
| 533 | WARN_ON(pages[i]->mapping); |
| 534 | page_cache_release(pages[i]); |
| 535 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 536 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 537 | |
| 538 | goto out; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * phase two of compressed writeback. This is the ordered portion |
| 543 | * of the code, which only gets called in the order the work was |
| 544 | * queued. We walk all the async extents created by compress_file_range |
| 545 | * and send them down to the disk. |
| 546 | */ |
| 547 | static noinline int submit_compressed_extents(struct inode *inode, |
| 548 | struct async_cow *async_cow) |
| 549 | { |
| 550 | struct async_extent *async_extent; |
| 551 | u64 alloc_hint = 0; |
| 552 | struct btrfs_trans_handle *trans; |
| 553 | struct btrfs_key ins; |
| 554 | struct extent_map *em; |
| 555 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 556 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 557 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 558 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 559 | |
| 560 | if (list_empty(&async_cow->extents)) |
| 561 | return 0; |
| 562 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 563 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 564 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 565 | async_extent = list_entry(async_cow->extents.next, |
| 566 | struct async_extent, list); |
| 567 | list_del(&async_extent->list); |
| 568 | |
| 569 | io_tree = &BTRFS_I(inode)->io_tree; |
| 570 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 571 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 572 | /* did the compression code fall back to uncompressed IO? */ |
| 573 | if (!async_extent->pages) { |
| 574 | int page_started = 0; |
| 575 | unsigned long nr_written = 0; |
| 576 | |
| 577 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 578 | async_extent->start + |
| 579 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 580 | |
| 581 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 582 | ret = cow_file_range(inode, async_cow->locked_page, |
| 583 | async_extent->start, |
| 584 | async_extent->start + |
| 585 | async_extent->ram_size - 1, |
| 586 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * if page_started, cow_file_range inserted an |
| 590 | * inline extent and took care of all the unlocking |
| 591 | * and IO for us. Otherwise, we need to submit |
| 592 | * all those pages down to the drive. |
| 593 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 594 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 595 | extent_write_locked_range(io_tree, |
| 596 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 597 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 598 | async_extent->ram_size - 1, |
| 599 | btrfs_get_extent, |
| 600 | WB_SYNC_ALL); |
| 601 | kfree(async_extent); |
| 602 | cond_resched(); |
| 603 | continue; |
| 604 | } |
| 605 | |
| 606 | lock_extent(io_tree, async_extent->start, |
| 607 | async_extent->start + async_extent->ram_size - 1, |
| 608 | GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 609 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 610 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 611 | ret = btrfs_reserve_extent(trans, root, |
| 612 | async_extent->compressed_size, |
| 613 | async_extent->compressed_size, |
| 614 | 0, alloc_hint, |
| 615 | (u64)-1, &ins, 1); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 616 | btrfs_end_transaction(trans, root); |
| 617 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 618 | if (ret) { |
| 619 | int i; |
| 620 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 621 | WARN_ON(async_extent->pages[i]->mapping); |
| 622 | page_cache_release(async_extent->pages[i]); |
| 623 | } |
| 624 | kfree(async_extent->pages); |
| 625 | async_extent->nr_pages = 0; |
| 626 | async_extent->pages = NULL; |
| 627 | unlock_extent(io_tree, async_extent->start, |
| 628 | async_extent->start + |
| 629 | async_extent->ram_size - 1, GFP_NOFS); |
| 630 | goto retry; |
| 631 | } |
| 632 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 633 | /* |
| 634 | * here we're doing allocation and writeback of the |
| 635 | * compressed pages |
| 636 | */ |
| 637 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 638 | async_extent->start + |
| 639 | async_extent->ram_size - 1, 0); |
| 640 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 641 | em = alloc_extent_map(GFP_NOFS); |
| 642 | em->start = async_extent->start; |
| 643 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 644 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 645 | |
| 646 | em->block_start = ins.objectid; |
| 647 | em->block_len = ins.offset; |
| 648 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 649 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 650 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 651 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 652 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 653 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 654 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 655 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 656 | if (ret != -EEXIST) { |
| 657 | free_extent_map(em); |
| 658 | break; |
| 659 | } |
| 660 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 661 | async_extent->start + |
| 662 | async_extent->ram_size - 1, 0); |
| 663 | } |
| 664 | |
| 665 | ret = btrfs_add_ordered_extent(inode, async_extent->start, |
| 666 | ins.objectid, |
| 667 | async_extent->ram_size, |
| 668 | ins.offset, |
| 669 | BTRFS_ORDERED_COMPRESSED); |
| 670 | BUG_ON(ret); |
| 671 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 672 | /* |
| 673 | * clear dirty, set writeback and unlock the pages. |
| 674 | */ |
| 675 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 676 | &BTRFS_I(inode)->io_tree, |
| 677 | async_extent->start, |
| 678 | async_extent->start + |
| 679 | async_extent->ram_size - 1, |
| 680 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 681 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 682 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 683 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 684 | |
| 685 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 686 | async_extent->start, |
| 687 | async_extent->ram_size, |
| 688 | ins.objectid, |
| 689 | ins.offset, async_extent->pages, |
| 690 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 691 | |
| 692 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 693 | alloc_hint = ins.objectid + ins.offset; |
| 694 | kfree(async_extent); |
| 695 | cond_resched(); |
| 696 | } |
| 697 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 701 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 702 | u64 num_bytes) |
| 703 | { |
| 704 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 705 | struct extent_map *em; |
| 706 | u64 alloc_hint = 0; |
| 707 | |
| 708 | read_lock(&em_tree->lock); |
| 709 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 710 | if (em) { |
| 711 | /* |
| 712 | * if block start isn't an actual block number then find the |
| 713 | * first block in this inode and use that as a hint. If that |
| 714 | * block is also bogus then just don't worry about it. |
| 715 | */ |
| 716 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 717 | free_extent_map(em); |
| 718 | em = search_extent_mapping(em_tree, 0, 0); |
| 719 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 720 | alloc_hint = em->block_start; |
| 721 | if (em) |
| 722 | free_extent_map(em); |
| 723 | } else { |
| 724 | alloc_hint = em->block_start; |
| 725 | free_extent_map(em); |
| 726 | } |
| 727 | } |
| 728 | read_unlock(&em_tree->lock); |
| 729 | |
| 730 | return alloc_hint; |
| 731 | } |
| 732 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 733 | /* |
| 734 | * when extent_io.c finds a delayed allocation range in the file, |
| 735 | * the call backs end up in this code. The basic idea is to |
| 736 | * allocate extents on disk for the range, and create ordered data structs |
| 737 | * in ram to track those extents. |
| 738 | * |
| 739 | * locked_page is the page that writepage had locked already. We use |
| 740 | * it to make sure we don't do extra locks or unlocks. |
| 741 | * |
| 742 | * *page_started is set to one if we unlock locked_page and do everything |
| 743 | * required to start IO on it. It may be clean and already done with |
| 744 | * IO when we return. |
| 745 | */ |
| 746 | static noinline int cow_file_range(struct inode *inode, |
| 747 | struct page *locked_page, |
| 748 | u64 start, u64 end, int *page_started, |
| 749 | unsigned long *nr_written, |
| 750 | int unlock) |
| 751 | { |
| 752 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 753 | struct btrfs_trans_handle *trans; |
| 754 | u64 alloc_hint = 0; |
| 755 | u64 num_bytes; |
| 756 | unsigned long ram_size; |
| 757 | u64 disk_num_bytes; |
| 758 | u64 cur_alloc_size; |
| 759 | u64 blocksize = root->sectorsize; |
| 760 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 761 | u64 isize = i_size_read(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 762 | struct btrfs_key ins; |
| 763 | struct extent_map *em; |
| 764 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 765 | int ret = 0; |
| 766 | |
| 767 | trans = btrfs_join_transaction(root, 1); |
| 768 | BUG_ON(!trans); |
| 769 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 770 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 771 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 772 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 773 | |
| 774 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 775 | num_bytes = max(blocksize, num_bytes); |
| 776 | disk_num_bytes = num_bytes; |
| 777 | ret = 0; |
| 778 | |
| 779 | if (start == 0) { |
| 780 | /* lets try to make an inline extent */ |
| 781 | ret = cow_file_range_inline(trans, root, inode, |
| 782 | start, end, 0, NULL); |
| 783 | if (ret == 0) { |
| 784 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 785 | &BTRFS_I(inode)->io_tree, |
| 786 | start, end, NULL, |
| 787 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 788 | EXTENT_CLEAR_UNLOCK | |
| 789 | EXTENT_CLEAR_DELALLOC | |
| 790 | EXTENT_CLEAR_DIRTY | |
| 791 | EXTENT_SET_WRITEBACK | |
| 792 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 793 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 794 | *nr_written = *nr_written + |
| 795 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 796 | *page_started = 1; |
| 797 | ret = 0; |
| 798 | goto out; |
| 799 | } |
| 800 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 801 | |
| 802 | BUG_ON(disk_num_bytes > |
| 803 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 804 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 805 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 806 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 807 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 808 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 809 | unsigned long op; |
| 810 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 811 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 812 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 813 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 814 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 815 | BUG_ON(ret); |
| 816 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 817 | em = alloc_extent_map(GFP_NOFS); |
| 818 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 819 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 820 | ram_size = ins.offset; |
| 821 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 822 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 823 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 824 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 825 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 826 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 827 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 828 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 829 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 830 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 831 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 832 | if (ret != -EEXIST) { |
| 833 | free_extent_map(em); |
| 834 | break; |
| 835 | } |
| 836 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 837 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 838 | } |
| 839 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 840 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 841 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 842 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 843 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 844 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 845 | if (root->root_key.objectid == |
| 846 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 847 | ret = btrfs_reloc_clone_csums(inode, start, |
| 848 | cur_alloc_size); |
| 849 | BUG_ON(ret); |
| 850 | } |
| 851 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 852 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 853 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 854 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 855 | /* we're not doing compressed IO, don't unlock the first |
| 856 | * page (which the caller expects to stay locked), don't |
| 857 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 858 | * |
| 859 | * Do set the Private2 bit so we know this page was properly |
| 860 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 861 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 862 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 863 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 864 | EXTENT_SET_PRIVATE2; |
| 865 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 866 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 867 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 868 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 869 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 870 | num_bytes -= cur_alloc_size; |
| 871 | alloc_hint = ins.objectid + ins.offset; |
| 872 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 873 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 874 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 875 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 876 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 877 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 878 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 879 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 880 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 881 | /* |
| 882 | * work queue call back to started compression on a file and pages |
| 883 | */ |
| 884 | static noinline void async_cow_start(struct btrfs_work *work) |
| 885 | { |
| 886 | struct async_cow *async_cow; |
| 887 | int num_added = 0; |
| 888 | async_cow = container_of(work, struct async_cow, work); |
| 889 | |
| 890 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 891 | async_cow->start, async_cow->end, async_cow, |
| 892 | &num_added); |
| 893 | if (num_added == 0) |
| 894 | async_cow->inode = NULL; |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | * work queue call back to submit previously compressed pages |
| 899 | */ |
| 900 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 901 | { |
| 902 | struct async_cow *async_cow; |
| 903 | struct btrfs_root *root; |
| 904 | unsigned long nr_pages; |
| 905 | |
| 906 | async_cow = container_of(work, struct async_cow, work); |
| 907 | |
| 908 | root = async_cow->root; |
| 909 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 910 | PAGE_CACHE_SHIFT; |
| 911 | |
| 912 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 913 | |
| 914 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 915 | 5 * 1042 * 1024 && |
| 916 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 917 | wake_up(&root->fs_info->async_submit_wait); |
| 918 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 919 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 920 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 921 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 922 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 923 | static noinline void async_cow_free(struct btrfs_work *work) |
| 924 | { |
| 925 | struct async_cow *async_cow; |
| 926 | async_cow = container_of(work, struct async_cow, work); |
| 927 | kfree(async_cow); |
| 928 | } |
| 929 | |
| 930 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 931 | u64 start, u64 end, int *page_started, |
| 932 | unsigned long *nr_written) |
| 933 | { |
| 934 | struct async_cow *async_cow; |
| 935 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 936 | unsigned long nr_pages; |
| 937 | u64 cur_end; |
| 938 | int limit = 10 * 1024 * 1042; |
| 939 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 940 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 941 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 942 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 943 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
| 944 | async_cow->inode = inode; |
| 945 | async_cow->root = root; |
| 946 | async_cow->locked_page = locked_page; |
| 947 | async_cow->start = start; |
| 948 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 949 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 950 | cur_end = end; |
| 951 | else |
| 952 | cur_end = min(end, start + 512 * 1024 - 1); |
| 953 | |
| 954 | async_cow->end = cur_end; |
| 955 | INIT_LIST_HEAD(&async_cow->extents); |
| 956 | |
| 957 | async_cow->work.func = async_cow_start; |
| 958 | async_cow->work.ordered_func = async_cow_submit; |
| 959 | async_cow->work.ordered_free = async_cow_free; |
| 960 | async_cow->work.flags = 0; |
| 961 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 962 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 963 | PAGE_CACHE_SHIFT; |
| 964 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 965 | |
| 966 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 967 | &async_cow->work); |
| 968 | |
| 969 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 970 | wait_event(root->fs_info->async_submit_wait, |
| 971 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 972 | limit)); |
| 973 | } |
| 974 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 975 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 976 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 977 | wait_event(root->fs_info->async_submit_wait, |
| 978 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 979 | 0)); |
| 980 | } |
| 981 | |
| 982 | *nr_written += nr_pages; |
| 983 | start = cur_end + 1; |
| 984 | } |
| 985 | *page_started = 1; |
| 986 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 987 | } |
| 988 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 989 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 990 | u64 bytenr, u64 num_bytes) |
| 991 | { |
| 992 | int ret; |
| 993 | struct btrfs_ordered_sum *sums; |
| 994 | LIST_HEAD(list); |
| 995 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 996 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
| 997 | bytenr + num_bytes - 1, &list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 998 | if (ret == 0 && list_empty(&list)) |
| 999 | return 0; |
| 1000 | |
| 1001 | while (!list_empty(&list)) { |
| 1002 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1003 | list_del(&sums->list); |
| 1004 | kfree(sums); |
| 1005 | } |
| 1006 | return 1; |
| 1007 | } |
| 1008 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1009 | /* |
| 1010 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1011 | * of the extents that exist in the file, and COWs the file as required. |
| 1012 | * |
| 1013 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1014 | * blocks on disk |
| 1015 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1016 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1017 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1018 | u64 start, u64 end, int *page_started, int force, |
| 1019 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1020 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1021 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1022 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1023 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1024 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1025 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1026 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1027 | u64 cow_start; |
| 1028 | u64 cur_offset; |
| 1029 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1030 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1031 | u64 disk_bytenr; |
| 1032 | u64 num_bytes; |
| 1033 | int extent_type; |
| 1034 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1035 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1036 | int nocow; |
| 1037 | int check_prev = 1; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1038 | |
| 1039 | path = btrfs_alloc_path(); |
| 1040 | BUG_ON(!path); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1041 | trans = btrfs_join_transaction(root, 1); |
| 1042 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1043 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1044 | cow_start = (u64)-1; |
| 1045 | cur_offset = start; |
| 1046 | while (1) { |
| 1047 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 1048 | cur_offset, 0); |
| 1049 | BUG_ON(ret < 0); |
| 1050 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1051 | leaf = path->nodes[0]; |
| 1052 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1053 | path->slots[0] - 1); |
| 1054 | if (found_key.objectid == inode->i_ino && |
| 1055 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1056 | path->slots[0]--; |
| 1057 | } |
| 1058 | check_prev = 0; |
| 1059 | next_slot: |
| 1060 | leaf = path->nodes[0]; |
| 1061 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1062 | ret = btrfs_next_leaf(root, path); |
| 1063 | if (ret < 0) |
| 1064 | BUG_ON(1); |
| 1065 | if (ret > 0) |
| 1066 | break; |
| 1067 | leaf = path->nodes[0]; |
| 1068 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1069 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1070 | nocow = 0; |
| 1071 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1072 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1073 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1074 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1075 | if (found_key.objectid > inode->i_ino || |
| 1076 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1077 | found_key.offset > end) |
| 1078 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1079 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1080 | if (found_key.offset > cur_offset) { |
| 1081 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1082 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1083 | goto out_check; |
| 1084 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1085 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1086 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1087 | struct btrfs_file_extent_item); |
| 1088 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1089 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1090 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1091 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1092 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1093 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1094 | extent_end = found_key.offset + |
| 1095 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1096 | if (extent_end <= start) { |
| 1097 | path->slots[0]++; |
| 1098 | goto next_slot; |
| 1099 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1100 | if (disk_bytenr == 0) |
| 1101 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1102 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1103 | btrfs_file_extent_encryption(leaf, fi) || |
| 1104 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1105 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1106 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1107 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1108 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1109 | goto out_check; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1110 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1111 | found_key.offset - |
| 1112 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1113 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1114 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1115 | disk_bytenr += cur_offset - found_key.offset; |
| 1116 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1117 | /* |
| 1118 | * force cow if csum exists in the range. |
| 1119 | * this ensure that csum for a given extent are |
| 1120 | * either valid or do not exist. |
| 1121 | */ |
| 1122 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1123 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1124 | nocow = 1; |
| 1125 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1126 | extent_end = found_key.offset + |
| 1127 | btrfs_file_extent_inline_len(leaf, fi); |
| 1128 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1129 | } else { |
| 1130 | BUG_ON(1); |
| 1131 | } |
| 1132 | out_check: |
| 1133 | if (extent_end <= start) { |
| 1134 | path->slots[0]++; |
| 1135 | goto next_slot; |
| 1136 | } |
| 1137 | if (!nocow) { |
| 1138 | if (cow_start == (u64)-1) |
| 1139 | cow_start = cur_offset; |
| 1140 | cur_offset = extent_end; |
| 1141 | if (cur_offset > end) |
| 1142 | break; |
| 1143 | path->slots[0]++; |
| 1144 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1145 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1146 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1147 | btrfs_release_path(root, path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1148 | if (cow_start != (u64)-1) { |
| 1149 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1150 | found_key.offset - 1, page_started, |
| 1151 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1152 | BUG_ON(ret); |
| 1153 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1154 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1155 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1156 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1157 | struct extent_map *em; |
| 1158 | struct extent_map_tree *em_tree; |
| 1159 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 1160 | em = alloc_extent_map(GFP_NOFS); |
| 1161 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1162 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1163 | em->len = num_bytes; |
| 1164 | em->block_len = num_bytes; |
| 1165 | em->block_start = disk_bytenr; |
| 1166 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1167 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1168 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1169 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1170 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1171 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1172 | if (ret != -EEXIST) { |
| 1173 | free_extent_map(em); |
| 1174 | break; |
| 1175 | } |
| 1176 | btrfs_drop_extent_cache(inode, em->start, |
| 1177 | em->start + em->len - 1, 0); |
| 1178 | } |
| 1179 | type = BTRFS_ORDERED_PREALLOC; |
| 1180 | } else { |
| 1181 | type = BTRFS_ORDERED_NOCOW; |
| 1182 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1183 | |
| 1184 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1185 | num_bytes, num_bytes, type); |
| 1186 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1187 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1188 | if (root->root_key.objectid == |
| 1189 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1190 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1191 | num_bytes); |
| 1192 | BUG_ON(ret); |
| 1193 | } |
| 1194 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1195 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1196 | cur_offset, cur_offset + num_bytes - 1, |
| 1197 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1198 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1199 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1200 | cur_offset = extent_end; |
| 1201 | if (cur_offset > end) |
| 1202 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1203 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1204 | btrfs_release_path(root, path); |
| 1205 | |
| 1206 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1207 | cow_start = cur_offset; |
| 1208 | if (cow_start != (u64)-1) { |
| 1209 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1210 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1211 | BUG_ON(ret); |
| 1212 | } |
| 1213 | |
| 1214 | ret = btrfs_end_transaction(trans, root); |
| 1215 | BUG_ON(ret); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1216 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1217 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1218 | } |
| 1219 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1220 | /* |
| 1221 | * extent_io.c call back to do delayed allocation processing |
| 1222 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1223 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1224 | u64 start, u64 end, int *page_started, |
| 1225 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1226 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1227 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1228 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1229 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1230 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1231 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1232 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1233 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1234 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1235 | page_started, 0, nr_written); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1236 | else if (!btrfs_test_opt(root, COMPRESS) && |
| 1237 | !(BTRFS_I(inode)->force_compress)) |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1238 | ret = cow_file_range(inode, locked_page, start, end, |
| 1239 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1240 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1241 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1242 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1243 | return ret; |
| 1244 | } |
| 1245 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1246 | static int btrfs_split_extent_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1247 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1248 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1249 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1250 | if (!(orig->state & EXTENT_DELALLOC)) |
| 1251 | return 0; |
| 1252 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1253 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | /* |
| 1258 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1259 | * extents so we can keep track of new extents that are just merged onto old |
| 1260 | * extents, such as when we are doing sequential writes, so we can properly |
| 1261 | * account for the metadata space we'll need. |
| 1262 | */ |
| 1263 | static int btrfs_merge_extent_hook(struct inode *inode, |
| 1264 | struct extent_state *new, |
| 1265 | struct extent_state *other) |
| 1266 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1267 | /* not delalloc, ignore it */ |
| 1268 | if (!(other->state & EXTENT_DELALLOC)) |
| 1269 | return 0; |
| 1270 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1271 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1272 | return 0; |
| 1273 | } |
| 1274 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1275 | /* |
| 1276 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1277 | * bytes in this file, and to maintain the list of inodes that |
| 1278 | * have pending delalloc work to be done. |
| 1279 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1280 | static int btrfs_set_bit_hook(struct inode *inode, |
| 1281 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1282 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1283 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1284 | /* |
| 1285 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1286 | * but in this case, we are only testeing for the DELALLOC |
| 1287 | * bit, which is only set or cleared with irqs on |
| 1288 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1289 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1290 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1291 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1292 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1293 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1294 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1295 | else |
| 1296 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1297 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1298 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1299 | BTRFS_I(inode)->delalloc_bytes += len; |
| 1300 | root->fs_info->delalloc_bytes += len; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1301 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1302 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1303 | &root->fs_info->delalloc_inodes); |
| 1304 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1305 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1306 | } |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1310 | /* |
| 1311 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1312 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1313 | static int btrfs_clear_bit_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1314 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1315 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1316 | /* |
| 1317 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1318 | * but in this case, we are only testeing for the DELALLOC |
| 1319 | * bit, which is only set or cleared with irqs on |
| 1320 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1321 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1322 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1323 | u64 len = state->end + 1 - state->start; |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1324 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1325 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1326 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1327 | else if (!(*bits & EXTENT_DO_ACCOUNTING)) |
| 1328 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
| 1329 | |
| 1330 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1331 | btrfs_delalloc_release_metadata(inode, len); |
| 1332 | |
| 1333 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1334 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1335 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1336 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1337 | root->fs_info->delalloc_bytes -= len; |
| 1338 | BTRFS_I(inode)->delalloc_bytes -= len; |
| 1339 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1340 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
| 1341 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1342 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1343 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1344 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1345 | } |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1349 | /* |
| 1350 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1351 | * we don't create bios that span stripes or chunks |
| 1352 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1353 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1354 | size_t size, struct bio *bio, |
| 1355 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1356 | { |
| 1357 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1358 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1359 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1360 | u64 length = 0; |
| 1361 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1362 | int ret; |
| 1363 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1364 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1365 | return 0; |
| 1366 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1367 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1368 | map_tree = &root->fs_info->mapping_tree; |
| 1369 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1370 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1371 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1372 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1373 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1374 | return 1; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1375 | return 0; |
| 1376 | } |
| 1377 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1378 | /* |
| 1379 | * in order to insert checksums into the metadata in large chunks, |
| 1380 | * we wait until bio submission time. All the pages in the bio are |
| 1381 | * checksummed and sums are attached onto the ordered extent record. |
| 1382 | * |
| 1383 | * At IO completion time the cums attached on the ordered extent record |
| 1384 | * are inserted into the btree |
| 1385 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1386 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1387 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1388 | unsigned long bio_flags, |
| 1389 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1390 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1391 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1392 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1393 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1394 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1395 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1398 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1399 | /* |
| 1400 | * in order to insert checksums into the metadata in large chunks, |
| 1401 | * we wait until bio submission time. All the pages in the bio are |
| 1402 | * checksummed and sums are attached onto the ordered extent record. |
| 1403 | * |
| 1404 | * At IO completion time the cums attached on the ordered extent record |
| 1405 | * are inserted into the btree |
| 1406 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1407 | 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] | 1408 | int mirror_num, unsigned long bio_flags, |
| 1409 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1410 | { |
| 1411 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1412 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1413 | } |
| 1414 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1415 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1416 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1417 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1418 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1419 | 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] | 1420 | int mirror_num, unsigned long bio_flags, |
| 1421 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1422 | { |
| 1423 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1424 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1425 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1426 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1427 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1428 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1429 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 1430 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1431 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1432 | if (!(rw & REQ_WRITE)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1433 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1434 | return btrfs_submit_compressed_read(inode, bio, |
| 1435 | mirror_num, bio_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1436 | } else if (!skip_sum) |
| 1437 | btrfs_lookup_bio_sums(root, inode, bio, NULL); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1438 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1439 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1440 | /* csum items have already been cloned */ |
| 1441 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1442 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1443 | /* we're doing a write, do the async checksumming */ |
| 1444 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1445 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1446 | bio_flags, bio_offset, |
| 1447 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1448 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1449 | } |
| 1450 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1451 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1452 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1453 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1454 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1455 | /* |
| 1456 | * given a list of ordered sums record them in the inode. This happens |
| 1457 | * at IO completion time based on sums calculated at bio submission time. |
| 1458 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1459 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1460 | struct inode *inode, u64 file_offset, |
| 1461 | struct list_head *list) |
| 1462 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1463 | struct btrfs_ordered_sum *sum; |
| 1464 | |
| 1465 | btrfs_set_trans_block_group(trans, inode); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1466 | |
| 1467 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1468 | btrfs_csum_file_blocks(trans, |
| 1469 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1470 | } |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1474 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1475 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1476 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1477 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1478 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1479 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1480 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1481 | } |
| 1482 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1483 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1484 | struct btrfs_writepage_fixup { |
| 1485 | struct page *page; |
| 1486 | struct btrfs_work work; |
| 1487 | }; |
| 1488 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1489 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1490 | { |
| 1491 | struct btrfs_writepage_fixup *fixup; |
| 1492 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1493 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1494 | struct page *page; |
| 1495 | struct inode *inode; |
| 1496 | u64 page_start; |
| 1497 | u64 page_end; |
| 1498 | |
| 1499 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1500 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1501 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1502 | lock_page(page); |
| 1503 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1504 | ClearPageChecked(page); |
| 1505 | goto out_page; |
| 1506 | } |
| 1507 | |
| 1508 | inode = page->mapping->host; |
| 1509 | page_start = page_offset(page); |
| 1510 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1511 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1512 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
| 1513 | &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1514 | |
| 1515 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1516 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1517 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1518 | |
| 1519 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1520 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1521 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1522 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1523 | unlock_page(page); |
| 1524 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1525 | goto again; |
| 1526 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1527 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1528 | BUG(); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1529 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1530 | ClearPageChecked(page); |
| 1531 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1532 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1533 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1534 | out_page: |
| 1535 | unlock_page(page); |
| 1536 | page_cache_release(page); |
| 1537 | } |
| 1538 | |
| 1539 | /* |
| 1540 | * There are a few paths in the higher layers of the kernel that directly |
| 1541 | * set the page dirty bit without asking the filesystem if it is a |
| 1542 | * good idea. This causes problems because we want to make sure COW |
| 1543 | * properly happens and the data=ordered rules are followed. |
| 1544 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1545 | * 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] | 1546 | * hasn't been properly setup for IO. We kick off an async process |
| 1547 | * to fix it up. The async helper will wait for ordered extents, set |
| 1548 | * the delalloc bit and make it safe to write the page. |
| 1549 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1550 | 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] | 1551 | { |
| 1552 | struct inode *inode = page->mapping->host; |
| 1553 | struct btrfs_writepage_fixup *fixup; |
| 1554 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1555 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1556 | /* this page is properly in the ordered list */ |
| 1557 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1558 | return 0; |
| 1559 | |
| 1560 | if (PageChecked(page)) |
| 1561 | return -EAGAIN; |
| 1562 | |
| 1563 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1564 | if (!fixup) |
| 1565 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1566 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1567 | SetPageChecked(page); |
| 1568 | page_cache_get(page); |
| 1569 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1570 | fixup->page = page; |
| 1571 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1572 | return -EAGAIN; |
| 1573 | } |
| 1574 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1575 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1576 | struct inode *inode, u64 file_pos, |
| 1577 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1578 | u64 num_bytes, u64 ram_bytes, |
| 1579 | u8 compression, u8 encryption, |
| 1580 | u16 other_encoding, int extent_type) |
| 1581 | { |
| 1582 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1583 | struct btrfs_file_extent_item *fi; |
| 1584 | struct btrfs_path *path; |
| 1585 | struct extent_buffer *leaf; |
| 1586 | struct btrfs_key ins; |
| 1587 | u64 hint; |
| 1588 | int ret; |
| 1589 | |
| 1590 | path = btrfs_alloc_path(); |
| 1591 | BUG_ON(!path); |
| 1592 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1593 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1594 | |
| 1595 | /* |
| 1596 | * we may be replacing one extent in the tree with another. |
| 1597 | * The new extent is pinned in the extent map, and we don't want |
| 1598 | * to drop it from the cache until it is completely in the btree. |
| 1599 | * |
| 1600 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1601 | * the caller is expected to unpin it and allow it to be merged |
| 1602 | * with the others. |
| 1603 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1604 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1605 | &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1606 | BUG_ON(ret); |
| 1607 | |
| 1608 | ins.objectid = inode->i_ino; |
| 1609 | ins.offset = file_pos; |
| 1610 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1611 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1612 | BUG_ON(ret); |
| 1613 | leaf = path->nodes[0]; |
| 1614 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1615 | struct btrfs_file_extent_item); |
| 1616 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1617 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1618 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1619 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1620 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1621 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1622 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1623 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1624 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1625 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1626 | |
| 1627 | btrfs_unlock_up_safe(path, 1); |
| 1628 | btrfs_set_lock_blocking(leaf); |
| 1629 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1630 | btrfs_mark_buffer_dirty(leaf); |
| 1631 | |
| 1632 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1633 | |
| 1634 | ins.objectid = disk_bytenr; |
| 1635 | ins.offset = disk_num_bytes; |
| 1636 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1637 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1638 | root->root_key.objectid, |
| 1639 | inode->i_ino, file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1640 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1641 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1642 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1643 | return 0; |
| 1644 | } |
| 1645 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1646 | /* |
| 1647 | * helper function for btrfs_finish_ordered_io, this |
| 1648 | * just reads in some of the csum leaves to prime them into ram |
| 1649 | * before we start the transaction. It limits the amount of btree |
| 1650 | * reads required while inside the transaction. |
| 1651 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1652 | /* as ordered data IO finishes, this gets called so we can finish |
| 1653 | * an ordered extent if the range of bytes in the file it covers are |
| 1654 | * fully written. |
| 1655 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1656 | 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] | 1657 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1658 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1659 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1660 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1661 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1662 | struct extent_state *cached_state = NULL; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1663 | int compressed = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1664 | int ret; |
| 1665 | |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 1666 | ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 1667 | end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1668 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1669 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1670 | BUG_ON(!ordered_extent); |
Josef Bacik | efd049f | 2010-02-02 20:50:10 +0000 | [diff] [blame] | 1671 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1672 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
| 1673 | BUG_ON(!list_empty(&ordered_extent->list)); |
| 1674 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1675 | if (!ret) { |
| 1676 | trans = btrfs_join_transaction(root, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1677 | btrfs_set_trans_block_group(trans, inode); |
| 1678 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1679 | ret = btrfs_update_inode(trans, root, inode); |
| 1680 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1681 | } |
| 1682 | goto out; |
| 1683 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1684 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1685 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 1686 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1687 | 0, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1688 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1689 | trans = btrfs_join_transaction(root, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1690 | btrfs_set_trans_block_group(trans, inode); |
| 1691 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1692 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1693 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1694 | compressed = 1; |
| 1695 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 1696 | BUG_ON(compressed); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1697 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1698 | ordered_extent->file_offset, |
| 1699 | ordered_extent->file_offset + |
| 1700 | ordered_extent->len); |
| 1701 | BUG_ON(ret); |
| 1702 | } else { |
| 1703 | ret = insert_reserved_file_extent(trans, inode, |
| 1704 | ordered_extent->file_offset, |
| 1705 | ordered_extent->start, |
| 1706 | ordered_extent->disk_len, |
| 1707 | ordered_extent->len, |
| 1708 | ordered_extent->len, |
| 1709 | compressed, 0, 0, |
| 1710 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1711 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1712 | ordered_extent->file_offset, |
| 1713 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1714 | BUG_ON(ret); |
| 1715 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1716 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 1717 | ordered_extent->file_offset + |
| 1718 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
| 1719 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1720 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1721 | &ordered_extent->list); |
| 1722 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1723 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1724 | ret = btrfs_update_inode(trans, root, inode); |
| 1725 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1726 | out: |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1727 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
| 1728 | if (trans) |
| 1729 | btrfs_end_transaction(trans, root); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1730 | /* once for us */ |
| 1731 | btrfs_put_ordered_extent(ordered_extent); |
| 1732 | /* once for the tree */ |
| 1733 | btrfs_put_ordered_extent(ordered_extent); |
| 1734 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1735 | return 0; |
| 1736 | } |
| 1737 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1738 | 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] | 1739 | struct extent_state *state, int uptodate) |
| 1740 | { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1741 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1742 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1743 | } |
| 1744 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1745 | /* |
| 1746 | * When IO fails, either with EIO or csum verification fails, we |
| 1747 | * try other mirrors that might have a good copy of the data. This |
| 1748 | * io_failure_record is used to record state as we go through all the |
| 1749 | * mirrors. If another mirror has good data, the page is set up to date |
| 1750 | * and things continue. If a good mirror can't be found, the original |
| 1751 | * bio end_io callback is called to indicate things have failed. |
| 1752 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1753 | struct io_failure_record { |
| 1754 | struct page *page; |
| 1755 | u64 start; |
| 1756 | u64 len; |
| 1757 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1758 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1759 | int last_mirror; |
| 1760 | }; |
| 1761 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1762 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1763 | struct page *page, u64 start, u64 end, |
| 1764 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1765 | { |
| 1766 | struct io_failure_record *failrec = NULL; |
| 1767 | u64 private; |
| 1768 | struct extent_map *em; |
| 1769 | struct inode *inode = page->mapping->host; |
| 1770 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1771 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1772 | struct bio *bio; |
| 1773 | int num_copies; |
| 1774 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1775 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1776 | u64 logical; |
| 1777 | |
| 1778 | ret = get_state_private(failure_tree, start, &private); |
| 1779 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1780 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1781 | if (!failrec) |
| 1782 | return -ENOMEM; |
| 1783 | failrec->start = start; |
| 1784 | failrec->len = end - start + 1; |
| 1785 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1786 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1787 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1788 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1789 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1790 | if (em->start > start || em->start + em->len < start) { |
| 1791 | free_extent_map(em); |
| 1792 | em = NULL; |
| 1793 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1794 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1795 | |
| 1796 | if (!em || IS_ERR(em)) { |
| 1797 | kfree(failrec); |
| 1798 | return -EIO; |
| 1799 | } |
| 1800 | logical = start - em->start; |
| 1801 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1802 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1803 | logical = em->block_start; |
| 1804 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 1805 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1806 | failrec->logical = logical; |
| 1807 | free_extent_map(em); |
| 1808 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1809 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1810 | set_state_private(failure_tree, start, |
| 1811 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1812 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1813 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1814 | } |
| 1815 | num_copies = btrfs_num_copies( |
| 1816 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1817 | failrec->logical, failrec->len); |
| 1818 | failrec->last_mirror++; |
| 1819 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1820 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1821 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1822 | failrec->start, |
| 1823 | EXTENT_LOCKED); |
| 1824 | if (state && state->start != failrec->start) |
| 1825 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1826 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1827 | } |
| 1828 | if (!state || failrec->last_mirror > num_copies) { |
| 1829 | set_state_private(failure_tree, failrec->start, 0); |
| 1830 | clear_extent_bits(failure_tree, failrec->start, |
| 1831 | failrec->start + failrec->len - 1, |
| 1832 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1833 | kfree(failrec); |
| 1834 | return -EIO; |
| 1835 | } |
| 1836 | bio = bio_alloc(GFP_NOFS, 1); |
| 1837 | bio->bi_private = state; |
| 1838 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1839 | bio->bi_sector = failrec->logical >> 9; |
| 1840 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1841 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1842 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1843 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1844 | if (failed_bio->bi_rw & REQ_WRITE) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1845 | rw = WRITE; |
| 1846 | else |
| 1847 | rw = READ; |
| 1848 | |
| 1849 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1850 | failrec->last_mirror, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1851 | failrec->bio_flags, 0); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1852 | return 0; |
| 1853 | } |
| 1854 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1855 | /* |
| 1856 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1857 | * to see if we need to process or clean up an io_failure_record |
| 1858 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1859 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1860 | { |
| 1861 | u64 private; |
| 1862 | u64 private_failure; |
| 1863 | struct io_failure_record *failure; |
| 1864 | int ret; |
| 1865 | |
| 1866 | private = 0; |
| 1867 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 1868 | (u64)-1, 1, EXTENT_DIRTY)) { |
| 1869 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1870 | start, &private_failure); |
| 1871 | if (ret == 0) { |
| 1872 | failure = (struct io_failure_record *)(unsigned long) |
| 1873 | private_failure; |
| 1874 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1875 | failure->start, 0); |
| 1876 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1877 | failure->start, |
| 1878 | failure->start + failure->len - 1, |
| 1879 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1880 | GFP_NOFS); |
| 1881 | kfree(failure); |
| 1882 | } |
| 1883 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1884 | return 0; |
| 1885 | } |
| 1886 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1887 | /* |
| 1888 | * when reads are done, we need to check csums to verify the data is correct |
| 1889 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1890 | * the io_failure_record routines to find good copies |
| 1891 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1892 | 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] | 1893 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1894 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1895 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1896 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1897 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1898 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1899 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1900 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1901 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1902 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1903 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1904 | if (PageChecked(page)) { |
| 1905 | ClearPageChecked(page); |
| 1906 | goto good; |
| 1907 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1908 | |
| 1909 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1910 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1911 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1912 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1913 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1914 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1915 | GFP_NOFS); |
| 1916 | return 0; |
| 1917 | } |
| 1918 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1919 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1920 | private = state->private; |
| 1921 | ret = 0; |
| 1922 | } else { |
| 1923 | ret = get_state_private(io_tree, start, &private); |
| 1924 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1925 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1926 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1927 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1928 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1929 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1930 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1931 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1932 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1933 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1934 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1935 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1936 | /* if the io failure tree for this inode is non-empty, |
| 1937 | * check to see if we've recovered from a failed IO |
| 1938 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1939 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1940 | return 0; |
| 1941 | |
| 1942 | zeroit: |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 1943 | if (printk_ratelimit()) { |
| 1944 | printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " |
| 1945 | "private %llu\n", page->mapping->host->i_ino, |
| 1946 | (unsigned long long)start, csum, |
| 1947 | (unsigned long long)private); |
| 1948 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1949 | memset(kaddr + offset, 1, end - start + 1); |
| 1950 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1951 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1952 | if (private == 0) |
| 1953 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1954 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1955 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1956 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 1957 | struct delayed_iput { |
| 1958 | struct list_head list; |
| 1959 | struct inode *inode; |
| 1960 | }; |
| 1961 | |
| 1962 | void btrfs_add_delayed_iput(struct inode *inode) |
| 1963 | { |
| 1964 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 1965 | struct delayed_iput *delayed; |
| 1966 | |
| 1967 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 1968 | return; |
| 1969 | |
| 1970 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 1971 | delayed->inode = inode; |
| 1972 | |
| 1973 | spin_lock(&fs_info->delayed_iput_lock); |
| 1974 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 1975 | spin_unlock(&fs_info->delayed_iput_lock); |
| 1976 | } |
| 1977 | |
| 1978 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 1979 | { |
| 1980 | LIST_HEAD(list); |
| 1981 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1982 | struct delayed_iput *delayed; |
| 1983 | int empty; |
| 1984 | |
| 1985 | spin_lock(&fs_info->delayed_iput_lock); |
| 1986 | empty = list_empty(&fs_info->delayed_iputs); |
| 1987 | spin_unlock(&fs_info->delayed_iput_lock); |
| 1988 | if (empty) |
| 1989 | return; |
| 1990 | |
| 1991 | down_read(&root->fs_info->cleanup_work_sem); |
| 1992 | spin_lock(&fs_info->delayed_iput_lock); |
| 1993 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 1994 | spin_unlock(&fs_info->delayed_iput_lock); |
| 1995 | |
| 1996 | while (!list_empty(&list)) { |
| 1997 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 1998 | list_del(&delayed->list); |
| 1999 | iput(delayed->inode); |
| 2000 | kfree(delayed); |
| 2001 | } |
| 2002 | up_read(&root->fs_info->cleanup_work_sem); |
| 2003 | } |
| 2004 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2005 | /* |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2006 | * calculate extra metadata reservation when snapshotting a subvolume |
| 2007 | * contains orphan files. |
| 2008 | */ |
| 2009 | void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans, |
| 2010 | struct btrfs_pending_snapshot *pending, |
| 2011 | u64 *bytes_to_reserve) |
| 2012 | { |
| 2013 | struct btrfs_root *root; |
| 2014 | struct btrfs_block_rsv *block_rsv; |
| 2015 | u64 num_bytes; |
| 2016 | int index; |
| 2017 | |
| 2018 | root = pending->root; |
| 2019 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2020 | return; |
| 2021 | |
| 2022 | block_rsv = root->orphan_block_rsv; |
| 2023 | |
| 2024 | /* orphan block reservation for the snapshot */ |
| 2025 | num_bytes = block_rsv->size; |
| 2026 | |
| 2027 | /* |
| 2028 | * after the snapshot is created, COWing tree blocks may use more |
| 2029 | * space than it frees. So we should make sure there is enough |
| 2030 | * reserved space. |
| 2031 | */ |
| 2032 | index = trans->transid & 0x1; |
| 2033 | if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) { |
| 2034 | num_bytes += block_rsv->size - |
| 2035 | (block_rsv->reserved + block_rsv->freed[index]); |
| 2036 | } |
| 2037 | |
| 2038 | *bytes_to_reserve += num_bytes; |
| 2039 | } |
| 2040 | |
| 2041 | void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans, |
| 2042 | struct btrfs_pending_snapshot *pending) |
| 2043 | { |
| 2044 | struct btrfs_root *root = pending->root; |
| 2045 | struct btrfs_root *snap = pending->snap; |
| 2046 | struct btrfs_block_rsv *block_rsv; |
| 2047 | u64 num_bytes; |
| 2048 | int index; |
| 2049 | int ret; |
| 2050 | |
| 2051 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2052 | return; |
| 2053 | |
| 2054 | /* refill source subvolume's orphan block reservation */ |
| 2055 | block_rsv = root->orphan_block_rsv; |
| 2056 | index = trans->transid & 0x1; |
| 2057 | if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) { |
| 2058 | num_bytes = block_rsv->size - |
| 2059 | (block_rsv->reserved + block_rsv->freed[index]); |
| 2060 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2061 | root->orphan_block_rsv, |
| 2062 | num_bytes); |
| 2063 | BUG_ON(ret); |
| 2064 | } |
| 2065 | |
| 2066 | /* setup orphan block reservation for the snapshot */ |
| 2067 | block_rsv = btrfs_alloc_block_rsv(snap); |
| 2068 | BUG_ON(!block_rsv); |
| 2069 | |
| 2070 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2071 | snap->orphan_block_rsv = block_rsv; |
| 2072 | |
| 2073 | num_bytes = root->orphan_block_rsv->size; |
| 2074 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2075 | block_rsv, num_bytes); |
| 2076 | BUG_ON(ret); |
| 2077 | |
| 2078 | #if 0 |
| 2079 | /* insert orphan item for the snapshot */ |
| 2080 | WARN_ON(!root->orphan_item_inserted); |
| 2081 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2082 | snap->root_key.objectid); |
| 2083 | BUG_ON(ret); |
| 2084 | snap->orphan_item_inserted = 1; |
| 2085 | #endif |
| 2086 | } |
| 2087 | |
| 2088 | enum btrfs_orphan_cleanup_state { |
| 2089 | ORPHAN_CLEANUP_STARTED = 1, |
| 2090 | ORPHAN_CLEANUP_DONE = 2, |
| 2091 | }; |
| 2092 | |
| 2093 | /* |
| 2094 | * This is called in transaction commmit time. If there are no orphan |
| 2095 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2096 | * structure. |
| 2097 | */ |
| 2098 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2099 | struct btrfs_root *root) |
| 2100 | { |
| 2101 | int ret; |
| 2102 | |
| 2103 | if (!list_empty(&root->orphan_list) || |
| 2104 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2105 | return; |
| 2106 | |
| 2107 | if (root->orphan_item_inserted && |
| 2108 | btrfs_root_refs(&root->root_item) > 0) { |
| 2109 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2110 | root->root_key.objectid); |
| 2111 | BUG_ON(ret); |
| 2112 | root->orphan_item_inserted = 0; |
| 2113 | } |
| 2114 | |
| 2115 | if (root->orphan_block_rsv) { |
| 2116 | WARN_ON(root->orphan_block_rsv->size > 0); |
| 2117 | btrfs_free_block_rsv(root, root->orphan_block_rsv); |
| 2118 | root->orphan_block_rsv = NULL; |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2123 | * This creates an orphan entry for the given inode in case something goes |
| 2124 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2125 | * |
| 2126 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2127 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2128 | */ |
| 2129 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2130 | { |
| 2131 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2132 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2133 | int reserve = 0; |
| 2134 | int insert = 0; |
| 2135 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2136 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2137 | if (!root->orphan_block_rsv) { |
| 2138 | block_rsv = btrfs_alloc_block_rsv(root); |
| 2139 | BUG_ON(!block_rsv); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2140 | } |
| 2141 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2142 | spin_lock(&root->orphan_lock); |
| 2143 | if (!root->orphan_block_rsv) { |
| 2144 | root->orphan_block_rsv = block_rsv; |
| 2145 | } else if (block_rsv) { |
| 2146 | btrfs_free_block_rsv(root, block_rsv); |
| 2147 | block_rsv = NULL; |
| 2148 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2149 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2150 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2151 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2152 | #if 0 |
| 2153 | /* |
| 2154 | * For proper ENOSPC handling, we should do orphan |
| 2155 | * cleanup when mounting. But this introduces backward |
| 2156 | * compatibility issue. |
| 2157 | */ |
| 2158 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2159 | insert = 2; |
| 2160 | else |
| 2161 | insert = 1; |
| 2162 | #endif |
| 2163 | insert = 1; |
| 2164 | } else { |
| 2165 | WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved); |
| 2166 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2167 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2168 | if (!BTRFS_I(inode)->orphan_meta_reserved) { |
| 2169 | BTRFS_I(inode)->orphan_meta_reserved = 1; |
| 2170 | reserve = 1; |
| 2171 | } |
| 2172 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2173 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2174 | if (block_rsv) |
| 2175 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2176 | |
| 2177 | /* grab metadata reservation from transaction handle */ |
| 2178 | if (reserve) { |
| 2179 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
| 2180 | BUG_ON(ret); |
| 2181 | } |
| 2182 | |
| 2183 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2184 | if (insert >= 1) { |
| 2185 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 2186 | BUG_ON(ret); |
| 2187 | } |
| 2188 | |
| 2189 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2190 | if (insert >= 2) { |
| 2191 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2192 | root->root_key.objectid); |
| 2193 | BUG_ON(ret); |
| 2194 | } |
| 2195 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | /* |
| 2199 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2200 | * item for this particular inode. |
| 2201 | */ |
| 2202 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2203 | { |
| 2204 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2205 | int delete_item = 0; |
| 2206 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2207 | int ret = 0; |
| 2208 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2209 | spin_lock(&root->orphan_lock); |
| 2210 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2211 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2212 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2213 | } |
| 2214 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2215 | if (BTRFS_I(inode)->orphan_meta_reserved) { |
| 2216 | BTRFS_I(inode)->orphan_meta_reserved = 0; |
| 2217 | release_rsv = 1; |
| 2218 | } |
| 2219 | spin_unlock(&root->orphan_lock); |
| 2220 | |
| 2221 | if (trans && delete_item) { |
| 2222 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 2223 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2224 | } |
| 2225 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2226 | if (release_rsv) |
| 2227 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2228 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2229 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | * this cleans up any orphans that may be left on the list from the last use |
| 2234 | * of this root. |
| 2235 | */ |
| 2236 | void btrfs_orphan_cleanup(struct btrfs_root *root) |
| 2237 | { |
| 2238 | struct btrfs_path *path; |
| 2239 | struct extent_buffer *leaf; |
| 2240 | struct btrfs_item *item; |
| 2241 | struct btrfs_key key, found_key; |
| 2242 | struct btrfs_trans_handle *trans; |
| 2243 | struct inode *inode; |
| 2244 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2245 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2246 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2247 | return; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2248 | |
| 2249 | path = btrfs_alloc_path(); |
| 2250 | BUG_ON(!path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2251 | path->reada = -1; |
| 2252 | |
| 2253 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2254 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2255 | key.offset = (u64)-1; |
| 2256 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2257 | while (1) { |
| 2258 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2259 | if (ret < 0) { |
| 2260 | printk(KERN_ERR "Error searching slot for orphan: %d" |
| 2261 | "\n", ret); |
| 2262 | break; |
| 2263 | } |
| 2264 | |
| 2265 | /* |
| 2266 | * if ret == 0 means we found what we were searching for, which |
| 2267 | * is weird, but possible, so only screw with path if we didnt |
| 2268 | * find the key and see if we have stuff that matches |
| 2269 | */ |
| 2270 | if (ret > 0) { |
| 2271 | if (path->slots[0] == 0) |
| 2272 | break; |
| 2273 | path->slots[0]--; |
| 2274 | } |
| 2275 | |
| 2276 | /* pull out the item */ |
| 2277 | leaf = path->nodes[0]; |
| 2278 | item = btrfs_item_nr(leaf, path->slots[0]); |
| 2279 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2280 | |
| 2281 | /* make sure the item matches what we want */ |
| 2282 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2283 | break; |
| 2284 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2285 | break; |
| 2286 | |
| 2287 | /* release the path since we're done with it */ |
| 2288 | btrfs_release_path(root, path); |
| 2289 | |
| 2290 | /* |
| 2291 | * this is where we are basically btrfs_lookup, without the |
| 2292 | * crossing root thing. we store the inode number in the |
| 2293 | * offset of the orphan item. |
| 2294 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2295 | found_key.objectid = found_key.offset; |
| 2296 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2297 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2298 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2299 | BUG_ON(IS_ERR(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2300 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2301 | /* |
| 2302 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2303 | * the proper thing when we hit it |
| 2304 | */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2305 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2306 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2307 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2308 | |
| 2309 | /* |
| 2310 | * if this is a bad inode, means we actually succeeded in |
| 2311 | * removing the inode, but not the orphan record, which means |
| 2312 | * we need to manually delete the orphan since iput will just |
| 2313 | * do a destroy_inode |
| 2314 | */ |
| 2315 | if (is_bad_inode(inode)) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2316 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2317 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2318 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2319 | iput(inode); |
| 2320 | continue; |
| 2321 | } |
| 2322 | |
| 2323 | /* if we have links, this was a truncate, lets do that */ |
| 2324 | if (inode->i_nlink) { |
| 2325 | nr_truncate++; |
| 2326 | btrfs_truncate(inode); |
| 2327 | } else { |
| 2328 | nr_unlink++; |
| 2329 | } |
| 2330 | |
| 2331 | /* this will do delete_inode and everything for us */ |
| 2332 | iput(inode); |
| 2333 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2334 | btrfs_free_path(path); |
| 2335 | |
| 2336 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 2337 | |
| 2338 | if (root->orphan_block_rsv) |
| 2339 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 2340 | (u64)-1); |
| 2341 | |
| 2342 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
| 2343 | trans = btrfs_join_transaction(root, 1); |
| 2344 | btrfs_end_transaction(trans, root); |
| 2345 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2346 | |
| 2347 | if (nr_unlink) |
| 2348 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2349 | if (nr_truncate) |
| 2350 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2351 | } |
| 2352 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2353 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2354 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2355 | * don't find any xattrs, we know there can't be any acls. |
| 2356 | * |
| 2357 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2358 | */ |
| 2359 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2360 | int slot, u64 objectid) |
| 2361 | { |
| 2362 | u32 nritems = btrfs_header_nritems(leaf); |
| 2363 | struct btrfs_key found_key; |
| 2364 | int scanned = 0; |
| 2365 | |
| 2366 | slot++; |
| 2367 | while (slot < nritems) { |
| 2368 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2369 | |
| 2370 | /* we found a different objectid, there must not be acls */ |
| 2371 | if (found_key.objectid != objectid) |
| 2372 | return 0; |
| 2373 | |
| 2374 | /* we found an xattr, assume we've got an acl */ |
| 2375 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2376 | return 1; |
| 2377 | |
| 2378 | /* |
| 2379 | * we found a key greater than an xattr key, there can't |
| 2380 | * be any acls later on |
| 2381 | */ |
| 2382 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2383 | return 0; |
| 2384 | |
| 2385 | slot++; |
| 2386 | scanned++; |
| 2387 | |
| 2388 | /* |
| 2389 | * it goes inode, inode backrefs, xattrs, extents, |
| 2390 | * so if there are a ton of hard links to an inode there can |
| 2391 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2392 | * this is just an optimization |
| 2393 | */ |
| 2394 | if (scanned >= 8) |
| 2395 | break; |
| 2396 | } |
| 2397 | /* we hit the end of the leaf before we found an xattr or |
| 2398 | * something larger than an xattr. We have to assume the inode |
| 2399 | * has acls |
| 2400 | */ |
| 2401 | return 1; |
| 2402 | } |
| 2403 | |
| 2404 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2405 | * read an inode from the btree into the in-memory inode |
| 2406 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2407 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2408 | { |
| 2409 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2410 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2411 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2412 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2413 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2414 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2415 | int maybe_acls; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2416 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2417 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2418 | int ret; |
| 2419 | |
| 2420 | path = btrfs_alloc_path(); |
| 2421 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2422 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2423 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2424 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2425 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2426 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2427 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2428 | leaf = path->nodes[0]; |
| 2429 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2430 | struct btrfs_inode_item); |
| 2431 | |
| 2432 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2433 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2434 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2435 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2436 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2437 | |
| 2438 | tspec = btrfs_inode_atime(inode_item); |
| 2439 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2440 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2441 | |
| 2442 | tspec = btrfs_inode_mtime(inode_item); |
| 2443 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2444 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2445 | |
| 2446 | tspec = btrfs_inode_ctime(inode_item); |
| 2447 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2448 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2449 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2450 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2451 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2452 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2453 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2454 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2455 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2456 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2457 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2458 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2459 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2460 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2461 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2462 | /* |
| 2463 | * try to precache a NULL acl entry for files that don't have |
| 2464 | * any xattrs or acls |
| 2465 | */ |
| 2466 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 2467 | if (!maybe_acls) |
| 2468 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2469 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2470 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2471 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2472 | btrfs_free_path(path); |
| 2473 | inode_item = NULL; |
| 2474 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2475 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2476 | case S_IFREG: |
| 2477 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2478 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2479 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2480 | inode->i_fop = &btrfs_file_operations; |
| 2481 | inode->i_op = &btrfs_file_inode_operations; |
| 2482 | break; |
| 2483 | case S_IFDIR: |
| 2484 | inode->i_fop = &btrfs_dir_file_operations; |
| 2485 | if (root == root->fs_info->tree_root) |
| 2486 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2487 | else |
| 2488 | inode->i_op = &btrfs_dir_inode_operations; |
| 2489 | break; |
| 2490 | case S_IFLNK: |
| 2491 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2492 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2493 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2494 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2495 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2496 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2497 | init_special_inode(inode, inode->i_mode, rdev); |
| 2498 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2499 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2500 | |
| 2501 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2502 | return; |
| 2503 | |
| 2504 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2505 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2506 | make_bad_inode(inode); |
| 2507 | } |
| 2508 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2509 | /* |
| 2510 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2511 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2512 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2513 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2514 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2515 | struct inode *inode) |
| 2516 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2517 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2518 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2519 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2520 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2521 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2522 | |
| 2523 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2524 | inode->i_atime.tv_sec); |
| 2525 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2526 | inode->i_atime.tv_nsec); |
| 2527 | |
| 2528 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2529 | inode->i_mtime.tv_sec); |
| 2530 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2531 | inode->i_mtime.tv_nsec); |
| 2532 | |
| 2533 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2534 | inode->i_ctime.tv_sec); |
| 2535 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2536 | inode->i_ctime.tv_nsec); |
| 2537 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2538 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2539 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2540 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2541 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2542 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2543 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2544 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2545 | } |
| 2546 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2547 | /* |
| 2548 | * copy everything in the in-memory inode into the btree. |
| 2549 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2550 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2551 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2552 | { |
| 2553 | struct btrfs_inode_item *inode_item; |
| 2554 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2555 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2556 | int ret; |
| 2557 | |
| 2558 | path = btrfs_alloc_path(); |
| 2559 | BUG_ON(!path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2560 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2561 | ret = btrfs_lookup_inode(trans, root, path, |
| 2562 | &BTRFS_I(inode)->location, 1); |
| 2563 | if (ret) { |
| 2564 | if (ret > 0) |
| 2565 | ret = -ENOENT; |
| 2566 | goto failed; |
| 2567 | } |
| 2568 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2569 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2570 | leaf = path->nodes[0]; |
| 2571 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2572 | struct btrfs_inode_item); |
| 2573 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2574 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2575 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2576 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2577 | ret = 0; |
| 2578 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2579 | btrfs_free_path(path); |
| 2580 | return ret; |
| 2581 | } |
| 2582 | |
| 2583 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2584 | /* |
| 2585 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2586 | * recovery code. It remove a link in a directory with a given name, and |
| 2587 | * also drops the back refs in the inode to the directory |
| 2588 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2589 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2590 | struct btrfs_root *root, |
| 2591 | struct inode *dir, struct inode *inode, |
| 2592 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2593 | { |
| 2594 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2595 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2596 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2597 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2598 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2599 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2600 | |
| 2601 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2602 | if (!path) { |
| 2603 | ret = -ENOMEM; |
| 2604 | goto err; |
| 2605 | } |
| 2606 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2607 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2608 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2609 | name, name_len, -1); |
| 2610 | if (IS_ERR(di)) { |
| 2611 | ret = PTR_ERR(di); |
| 2612 | goto err; |
| 2613 | } |
| 2614 | if (!di) { |
| 2615 | ret = -ENOENT; |
| 2616 | goto err; |
| 2617 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2618 | leaf = path->nodes[0]; |
| 2619 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2620 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2621 | if (ret) |
| 2622 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2623 | btrfs_release_path(root, path); |
| 2624 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2625 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2626 | inode->i_ino, |
| 2627 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2628 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2629 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2630 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2631 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2632 | goto err; |
| 2633 | } |
| 2634 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2635 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2636 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2637 | if (IS_ERR(di)) { |
| 2638 | ret = PTR_ERR(di); |
| 2639 | goto err; |
| 2640 | } |
| 2641 | if (!di) { |
| 2642 | ret = -ENOENT; |
| 2643 | goto err; |
| 2644 | } |
| 2645 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2646 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2647 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2648 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 2649 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2650 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2651 | |
| 2652 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2653 | dir, index); |
| 2654 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2655 | err: |
| 2656 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2657 | if (ret) |
| 2658 | goto out; |
| 2659 | |
| 2660 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2661 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2662 | btrfs_update_inode(trans, root, dir); |
| 2663 | btrfs_drop_nlink(inode); |
| 2664 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2665 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2666 | return ret; |
| 2667 | } |
| 2668 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2669 | /* helper to check if there is any shared block in the path */ |
| 2670 | static int check_path_shared(struct btrfs_root *root, |
| 2671 | struct btrfs_path *path) |
| 2672 | { |
| 2673 | struct extent_buffer *eb; |
| 2674 | int level; |
| 2675 | int ret; |
Dan Carpenter | 0e4dcbe | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 2676 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2677 | |
| 2678 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
| 2679 | if (!path->nodes[level]) |
| 2680 | break; |
| 2681 | eb = path->nodes[level]; |
| 2682 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2683 | continue; |
| 2684 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2685 | &refs, NULL); |
| 2686 | if (refs > 1) |
| 2687 | return 1; |
| 2688 | } |
| 2689 | return 0; |
| 2690 | } |
| 2691 | |
| 2692 | /* |
| 2693 | * helper to start transaction for unlink and rmdir. |
| 2694 | * |
| 2695 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2696 | * so in enospc case, we should make sure they will free space before |
| 2697 | * allowing them to use the global metadata reservation. |
| 2698 | */ |
| 2699 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2700 | struct dentry *dentry) |
| 2701 | { |
| 2702 | struct btrfs_trans_handle *trans; |
| 2703 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2704 | struct btrfs_path *path; |
| 2705 | struct btrfs_inode_ref *ref; |
| 2706 | struct btrfs_dir_item *di; |
| 2707 | struct inode *inode = dentry->d_inode; |
| 2708 | u64 index; |
| 2709 | int check_link = 1; |
| 2710 | int err = -ENOSPC; |
| 2711 | int ret; |
| 2712 | |
| 2713 | trans = btrfs_start_transaction(root, 10); |
| 2714 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 2715 | return trans; |
| 2716 | |
| 2717 | if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 2718 | return ERR_PTR(-ENOSPC); |
| 2719 | |
| 2720 | /* check if there is someone else holds reference */ |
| 2721 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 2722 | return ERR_PTR(-ENOSPC); |
| 2723 | |
| 2724 | if (atomic_read(&inode->i_count) > 2) |
| 2725 | return ERR_PTR(-ENOSPC); |
| 2726 | |
| 2727 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 2728 | return ERR_PTR(-ENOSPC); |
| 2729 | |
| 2730 | path = btrfs_alloc_path(); |
| 2731 | if (!path) { |
| 2732 | root->fs_info->enospc_unlink = 0; |
| 2733 | return ERR_PTR(-ENOMEM); |
| 2734 | } |
| 2735 | |
| 2736 | trans = btrfs_start_transaction(root, 0); |
| 2737 | if (IS_ERR(trans)) { |
| 2738 | btrfs_free_path(path); |
| 2739 | root->fs_info->enospc_unlink = 0; |
| 2740 | return trans; |
| 2741 | } |
| 2742 | |
| 2743 | path->skip_locking = 1; |
| 2744 | path->search_commit_root = 1; |
| 2745 | |
| 2746 | ret = btrfs_lookup_inode(trans, root, path, |
| 2747 | &BTRFS_I(dir)->location, 0); |
| 2748 | if (ret < 0) { |
| 2749 | err = ret; |
| 2750 | goto out; |
| 2751 | } |
| 2752 | if (ret == 0) { |
| 2753 | if (check_path_shared(root, path)) |
| 2754 | goto out; |
| 2755 | } else { |
| 2756 | check_link = 0; |
| 2757 | } |
| 2758 | btrfs_release_path(root, path); |
| 2759 | |
| 2760 | ret = btrfs_lookup_inode(trans, root, path, |
| 2761 | &BTRFS_I(inode)->location, 0); |
| 2762 | if (ret < 0) { |
| 2763 | err = ret; |
| 2764 | goto out; |
| 2765 | } |
| 2766 | if (ret == 0) { |
| 2767 | if (check_path_shared(root, path)) |
| 2768 | goto out; |
| 2769 | } else { |
| 2770 | check_link = 0; |
| 2771 | } |
| 2772 | btrfs_release_path(root, path); |
| 2773 | |
| 2774 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 2775 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 2776 | inode->i_ino, (u64)-1, 0); |
| 2777 | if (ret < 0) { |
| 2778 | err = ret; |
| 2779 | goto out; |
| 2780 | } |
| 2781 | BUG_ON(ret == 0); |
| 2782 | if (check_path_shared(root, path)) |
| 2783 | goto out; |
| 2784 | btrfs_release_path(root, path); |
| 2785 | } |
| 2786 | |
| 2787 | if (!check_link) { |
| 2788 | err = 0; |
| 2789 | goto out; |
| 2790 | } |
| 2791 | |
| 2792 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2793 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2794 | if (IS_ERR(di)) { |
| 2795 | err = PTR_ERR(di); |
| 2796 | goto out; |
| 2797 | } |
| 2798 | if (di) { |
| 2799 | if (check_path_shared(root, path)) |
| 2800 | goto out; |
| 2801 | } else { |
| 2802 | err = 0; |
| 2803 | goto out; |
| 2804 | } |
| 2805 | btrfs_release_path(root, path); |
| 2806 | |
| 2807 | ref = btrfs_lookup_inode_ref(trans, root, path, |
| 2808 | dentry->d_name.name, dentry->d_name.len, |
| 2809 | inode->i_ino, dir->i_ino, 0); |
| 2810 | if (IS_ERR(ref)) { |
| 2811 | err = PTR_ERR(ref); |
| 2812 | goto out; |
| 2813 | } |
| 2814 | BUG_ON(!ref); |
| 2815 | if (check_path_shared(root, path)) |
| 2816 | goto out; |
| 2817 | index = btrfs_inode_ref_index(path->nodes[0], ref); |
| 2818 | btrfs_release_path(root, path); |
| 2819 | |
| 2820 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index, |
| 2821 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2822 | if (IS_ERR(di)) { |
| 2823 | err = PTR_ERR(di); |
| 2824 | goto out; |
| 2825 | } |
| 2826 | BUG_ON(ret == -ENOENT); |
| 2827 | if (check_path_shared(root, path)) |
| 2828 | goto out; |
| 2829 | |
| 2830 | err = 0; |
| 2831 | out: |
| 2832 | btrfs_free_path(path); |
| 2833 | if (err) { |
| 2834 | btrfs_end_transaction(trans, root); |
| 2835 | root->fs_info->enospc_unlink = 0; |
| 2836 | return ERR_PTR(err); |
| 2837 | } |
| 2838 | |
| 2839 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 2840 | return trans; |
| 2841 | } |
| 2842 | |
| 2843 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 2844 | struct btrfs_root *root) |
| 2845 | { |
| 2846 | if (trans->block_rsv == &root->fs_info->global_block_rsv) { |
| 2847 | BUG_ON(!root->fs_info->enospc_unlink); |
| 2848 | root->fs_info->enospc_unlink = 0; |
| 2849 | } |
| 2850 | btrfs_end_transaction_throttle(trans, root); |
| 2851 | } |
| 2852 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2853 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2854 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2855 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2856 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2857 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2858 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2859 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2860 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2861 | trans = __unlink_start_trans(dir, dentry); |
| 2862 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2863 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2864 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2865 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2866 | |
| 2867 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2868 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2869 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2870 | dentry->d_name.name, dentry->d_name.len); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2871 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2872 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2873 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2874 | ret = btrfs_orphan_add(trans, inode); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2875 | BUG_ON(ret); |
| 2876 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2877 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2878 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2879 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2880 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2881 | return ret; |
| 2882 | } |
| 2883 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2884 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2885 | struct btrfs_root *root, |
| 2886 | struct inode *dir, u64 objectid, |
| 2887 | const char *name, int name_len) |
| 2888 | { |
| 2889 | struct btrfs_path *path; |
| 2890 | struct extent_buffer *leaf; |
| 2891 | struct btrfs_dir_item *di; |
| 2892 | struct btrfs_key key; |
| 2893 | u64 index; |
| 2894 | int ret; |
| 2895 | |
| 2896 | path = btrfs_alloc_path(); |
| 2897 | if (!path) |
| 2898 | return -ENOMEM; |
| 2899 | |
| 2900 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2901 | name, name_len, -1); |
| 2902 | BUG_ON(!di || IS_ERR(di)); |
| 2903 | |
| 2904 | leaf = path->nodes[0]; |
| 2905 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2906 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2907 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2908 | BUG_ON(ret); |
| 2909 | btrfs_release_path(root, path); |
| 2910 | |
| 2911 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2912 | objectid, root->root_key.objectid, |
| 2913 | dir->i_ino, &index, name, name_len); |
| 2914 | if (ret < 0) { |
| 2915 | BUG_ON(ret != -ENOENT); |
| 2916 | di = btrfs_search_dir_index_item(root, path, dir->i_ino, |
| 2917 | name, name_len); |
| 2918 | BUG_ON(!di || IS_ERR(di)); |
| 2919 | |
| 2920 | leaf = path->nodes[0]; |
| 2921 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2922 | btrfs_release_path(root, path); |
| 2923 | index = key.offset; |
| 2924 | } |
| 2925 | |
| 2926 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 2927 | index, name, name_len, -1); |
| 2928 | BUG_ON(!di || IS_ERR(di)); |
| 2929 | |
| 2930 | leaf = path->nodes[0]; |
| 2931 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2932 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2933 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2934 | BUG_ON(ret); |
| 2935 | btrfs_release_path(root, path); |
| 2936 | |
| 2937 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2938 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2939 | ret = btrfs_update_inode(trans, root, dir); |
| 2940 | BUG_ON(ret); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2941 | |
| 2942 | btrfs_free_path(path); |
| 2943 | return 0; |
| 2944 | } |
| 2945 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2946 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 2947 | { |
| 2948 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2949 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2950 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2951 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2952 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2953 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 2954 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2955 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 2956 | return -ENOTEMPTY; |
| 2957 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2958 | trans = __unlink_start_trans(dir, dentry); |
| 2959 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2960 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2961 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2962 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2963 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2964 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 2965 | err = btrfs_unlink_subvol(trans, root, dir, |
| 2966 | BTRFS_I(inode)->location.objectid, |
| 2967 | dentry->d_name.name, |
| 2968 | dentry->d_name.len); |
| 2969 | goto out; |
| 2970 | } |
| 2971 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2972 | err = btrfs_orphan_add(trans, inode); |
| 2973 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2974 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2975 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2976 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2977 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2978 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2979 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2980 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2981 | out: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2982 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2983 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2984 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2985 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2986 | return err; |
| 2987 | } |
| 2988 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2989 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2990 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2991 | * when truncating bytes in a file, it is possible to avoid reading |
| 2992 | * the leaves that contain only checksum items. This can be the |
| 2993 | * majority of the IO required to delete a large file, but it must |
| 2994 | * be done carefully. |
| 2995 | * |
| 2996 | * The keys in the level just above the leaves are checked to make sure |
| 2997 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 2998 | * after the new size. |
| 2999 | * |
| 3000 | * Then the key for the next leaf is checked to make sure it also has |
| 3001 | * a checksum item for the same file. If it does, we know our target leaf |
| 3002 | * contains only checksum items, and it can be safely freed without reading |
| 3003 | * it. |
| 3004 | * |
| 3005 | * This is just an optimization targeted at large files. It may do |
| 3006 | * nothing. It will return 0 unless things went badly. |
| 3007 | */ |
| 3008 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 3009 | struct btrfs_root *root, |
| 3010 | struct btrfs_path *path, |
| 3011 | struct inode *inode, u64 new_size) |
| 3012 | { |
| 3013 | struct btrfs_key key; |
| 3014 | int ret; |
| 3015 | int nritems; |
| 3016 | struct btrfs_key found_key; |
| 3017 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3018 | struct btrfs_leaf_ref *ref; |
| 3019 | u64 leaf_gen; |
| 3020 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3021 | |
| 3022 | path->lowest_level = 1; |
| 3023 | key.objectid = inode->i_ino; |
| 3024 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 3025 | key.offset = new_size; |
| 3026 | again: |
| 3027 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3028 | if (ret < 0) |
| 3029 | goto out; |
| 3030 | |
| 3031 | if (path->nodes[1] == NULL) { |
| 3032 | ret = 0; |
| 3033 | goto out; |
| 3034 | } |
| 3035 | ret = 0; |
| 3036 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 3037 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 3038 | |
| 3039 | if (!nritems) |
| 3040 | goto out; |
| 3041 | |
| 3042 | if (path->slots[1] >= nritems) |
| 3043 | goto next_node; |
| 3044 | |
| 3045 | /* did we find a key greater than anything we want to delete? */ |
| 3046 | if (found_key.objectid > inode->i_ino || |
| 3047 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 3048 | goto out; |
| 3049 | |
| 3050 | /* we check the next key in the node to make sure the leave contains |
| 3051 | * only checksum items. This comparison doesn't work if our |
| 3052 | * leaf is the last one in the node |
| 3053 | */ |
| 3054 | if (path->slots[1] + 1 >= nritems) { |
| 3055 | next_node: |
| 3056 | /* search forward from the last key in the node, this |
| 3057 | * will bring us into the next node in the tree |
| 3058 | */ |
| 3059 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 3060 | |
| 3061 | /* unlikely, but we inc below, so check to be safe */ |
| 3062 | if (found_key.offset == (u64)-1) |
| 3063 | goto out; |
| 3064 | |
| 3065 | /* search_forward needs a path with locks held, do the |
| 3066 | * search again for the original key. It is possible |
| 3067 | * this will race with a balance and return a path that |
| 3068 | * we could modify, but this drop is just an optimization |
| 3069 | * and is allowed to miss some leaves. |
| 3070 | */ |
| 3071 | btrfs_release_path(root, path); |
| 3072 | found_key.offset++; |
| 3073 | |
| 3074 | /* setup a max key for search_forward */ |
| 3075 | other_key.offset = (u64)-1; |
| 3076 | other_key.type = key.type; |
| 3077 | other_key.objectid = key.objectid; |
| 3078 | |
| 3079 | path->keep_locks = 1; |
| 3080 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 3081 | path, 0, 0); |
| 3082 | path->keep_locks = 0; |
| 3083 | if (ret || found_key.objectid != key.objectid || |
| 3084 | found_key.type != key.type) { |
| 3085 | ret = 0; |
| 3086 | goto out; |
| 3087 | } |
| 3088 | |
| 3089 | key.offset = found_key.offset; |
| 3090 | btrfs_release_path(root, path); |
| 3091 | cond_resched(); |
| 3092 | goto again; |
| 3093 | } |
| 3094 | |
| 3095 | /* we know there's one more slot after us in the tree, |
| 3096 | * read that key so we can verify it is also a checksum item |
| 3097 | */ |
| 3098 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 3099 | |
| 3100 | if (found_key.objectid < inode->i_ino) |
| 3101 | goto next_key; |
| 3102 | |
| 3103 | if (found_key.type != key.type || found_key.offset < new_size) |
| 3104 | goto next_key; |
| 3105 | |
| 3106 | /* |
| 3107 | * if the key for the next leaf isn't a csum key from this objectid, |
| 3108 | * we can't be sure there aren't good items inside this leaf. |
| 3109 | * Bail out |
| 3110 | */ |
| 3111 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 3112 | goto out; |
| 3113 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3114 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 3115 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3116 | /* |
| 3117 | * it is safe to delete this leaf, it contains only |
| 3118 | * csum items from this inode at an offset >= new_size |
| 3119 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3120 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3121 | BUG_ON(ret); |
| 3122 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3123 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 3124 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 3125 | if (ref) { |
| 3126 | ref->root_gen = root->root_key.offset; |
| 3127 | ref->bytenr = leaf_start; |
| 3128 | ref->owner = 0; |
| 3129 | ref->generation = leaf_gen; |
| 3130 | ref->nritems = 0; |
| 3131 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3132 | btrfs_sort_leaf_ref(ref); |
| 3133 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3134 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 3135 | WARN_ON(ret); |
| 3136 | btrfs_free_leaf_ref(root, ref); |
| 3137 | } else { |
| 3138 | WARN_ON(1); |
| 3139 | } |
| 3140 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3141 | next_key: |
| 3142 | btrfs_release_path(root, path); |
| 3143 | |
| 3144 | if (other_key.objectid == inode->i_ino && |
| 3145 | other_key.type == key.type && other_key.offset > key.offset) { |
| 3146 | key.offset = other_key.offset; |
| 3147 | cond_resched(); |
| 3148 | goto again; |
| 3149 | } |
| 3150 | ret = 0; |
| 3151 | out: |
| 3152 | /* fixup any changes we've made to the path */ |
| 3153 | path->lowest_level = 0; |
| 3154 | path->keep_locks = 0; |
| 3155 | btrfs_release_path(root, path); |
| 3156 | return ret; |
| 3157 | } |
| 3158 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3159 | #endif |
| 3160 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3161 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3162 | * this can truncate away extent items, csum items and directory items. |
| 3163 | * 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] | 3164 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3165 | * |
| 3166 | * csum items that cross the new i_size are truncated to the new size |
| 3167 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3168 | * |
| 3169 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3170 | * 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] | 3171 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3172 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3173 | struct btrfs_root *root, |
| 3174 | struct inode *inode, |
| 3175 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3176 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3177 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3178 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3179 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3180 | struct btrfs_key key; |
| 3181 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3182 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3183 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3184 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3185 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3186 | u64 mask = root->sectorsize - 1; |
| 3187 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3188 | int found_extent; |
| 3189 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3190 | int pending_del_nr = 0; |
| 3191 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3192 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3193 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3194 | int ret; |
| 3195 | int err = 0; |
| 3196 | |
| 3197 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3198 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3199 | if (root->ref_cows) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3200 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3201 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3202 | path = btrfs_alloc_path(); |
| 3203 | BUG_ON(!path); |
Julia Lawall | 33c17ad | 2009-07-22 16:49:01 -0400 | [diff] [blame] | 3204 | path->reada = -1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3205 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3206 | key.objectid = inode->i_ino; |
| 3207 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3208 | key.type = (u8)-1; |
| 3209 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3210 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3211 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3212 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3213 | if (ret < 0) { |
| 3214 | err = ret; |
| 3215 | goto out; |
| 3216 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3217 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3218 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3219 | /* there are no items in the tree for us to truncate, we're |
| 3220 | * done |
| 3221 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3222 | if (path->slots[0] == 0) |
| 3223 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3224 | path->slots[0]--; |
| 3225 | } |
| 3226 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3227 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3228 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3229 | leaf = path->nodes[0]; |
| 3230 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3231 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3232 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3233 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3234 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3235 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3236 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3237 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3238 | break; |
| 3239 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3240 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3241 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3242 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3243 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3244 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3245 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 3246 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 3247 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 3248 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3249 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3250 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3251 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3252 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3253 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3254 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3255 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3256 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3257 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3258 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3259 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3260 | } else { |
| 3261 | if (item_end < new_size) |
| 3262 | break; |
| 3263 | if (found_key.offset >= new_size) |
| 3264 | del_item = 1; |
| 3265 | else |
| 3266 | del_item = 0; |
| 3267 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3268 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3269 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3270 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3271 | goto delete; |
| 3272 | |
| 3273 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3274 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3275 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3276 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3277 | u64 orig_num_bytes = |
| 3278 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3279 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3280 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3281 | extent_num_bytes = extent_num_bytes & |
| 3282 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3283 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3284 | extent_num_bytes); |
| 3285 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3286 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3287 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3288 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3289 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3290 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3291 | extent_num_bytes = |
| 3292 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3293 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3294 | extent_offset = found_key.offset - |
| 3295 | btrfs_file_extent_offset(leaf, fi); |
| 3296 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3297 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3298 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3299 | if (extent_start != 0) { |
| 3300 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3301 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3302 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3303 | } |
| 3304 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3305 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3306 | /* |
| 3307 | * we can't truncate inline items that have had |
| 3308 | * special encodings |
| 3309 | */ |
| 3310 | if (!del_item && |
| 3311 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3312 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3313 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3314 | u32 size = new_size - found_key.offset; |
| 3315 | |
| 3316 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3317 | inode_sub_bytes(inode, item_end + 1 - |
| 3318 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3319 | } |
| 3320 | size = |
| 3321 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3322 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3323 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3324 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3325 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3326 | inode_sub_bytes(inode, item_end + 1 - |
| 3327 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3328 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3329 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3330 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3331 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3332 | if (!pending_del_nr) { |
| 3333 | /* no pending yet, add ourselves */ |
| 3334 | pending_del_slot = path->slots[0]; |
| 3335 | pending_del_nr = 1; |
| 3336 | } else if (pending_del_nr && |
| 3337 | path->slots[0] + 1 == pending_del_slot) { |
| 3338 | /* hop on the pending chunk */ |
| 3339 | pending_del_nr++; |
| 3340 | pending_del_slot = path->slots[0]; |
| 3341 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3342 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3343 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3344 | } else { |
| 3345 | break; |
| 3346 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3347 | if (found_extent && root->ref_cows) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3348 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3349 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3350 | extent_num_bytes, 0, |
| 3351 | btrfs_header_owner(leaf), |
| 3352 | inode->i_ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3353 | BUG_ON(ret); |
| 3354 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3355 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3356 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3357 | break; |
| 3358 | |
| 3359 | if (path->slots[0] == 0 || |
| 3360 | path->slots[0] != pending_del_slot) { |
| 3361 | if (root->ref_cows) { |
| 3362 | err = -EAGAIN; |
| 3363 | goto out; |
| 3364 | } |
| 3365 | if (pending_del_nr) { |
| 3366 | ret = btrfs_del_items(trans, root, path, |
| 3367 | pending_del_slot, |
| 3368 | pending_del_nr); |
| 3369 | BUG_ON(ret); |
| 3370 | pending_del_nr = 0; |
| 3371 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3372 | btrfs_release_path(root, path); |
| 3373 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3374 | } else { |
| 3375 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3376 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3377 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3378 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3379 | if (pending_del_nr) { |
| 3380 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3381 | pending_del_nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3382 | BUG_ON(ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3383 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3384 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3385 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3386 | } |
| 3387 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3388 | /* |
| 3389 | * taken from block_truncate_page, but does cow as it zeros out |
| 3390 | * any bytes left in the last page in the file. |
| 3391 | */ |
| 3392 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3393 | { |
| 3394 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3395 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3396 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3397 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3398 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3399 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3400 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3401 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3402 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3403 | struct page *page; |
| 3404 | int ret = 0; |
| 3405 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3406 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3407 | |
| 3408 | if ((offset & (blocksize - 1)) == 0) |
| 3409 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3410 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3411 | if (ret) |
| 3412 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3413 | |
| 3414 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3415 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3416 | page = grab_cache_page(mapping, index); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3417 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3418 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3419 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3420 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3421 | |
| 3422 | page_start = page_offset(page); |
| 3423 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3424 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3425 | if (!PageUptodate(page)) { |
| 3426 | ret = btrfs_readpage(NULL, page); |
| 3427 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3428 | if (page->mapping != mapping) { |
| 3429 | unlock_page(page); |
| 3430 | page_cache_release(page); |
| 3431 | goto again; |
| 3432 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3433 | if (!PageUptodate(page)) { |
| 3434 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3435 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3436 | } |
| 3437 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3438 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3439 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3440 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 3441 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3442 | set_page_extent_mapped(page); |
| 3443 | |
| 3444 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3445 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3446 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3447 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3448 | unlock_page(page); |
| 3449 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3450 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3451 | btrfs_put_ordered_extent(ordered); |
| 3452 | goto again; |
| 3453 | } |
| 3454 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3455 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3456 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3457 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3458 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3459 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3460 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3461 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3462 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3463 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3464 | goto out_unlock; |
| 3465 | } |
| 3466 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3467 | ret = 0; |
| 3468 | if (offset != PAGE_CACHE_SIZE) { |
| 3469 | kaddr = kmap(page); |
| 3470 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3471 | flush_dcache_page(page); |
| 3472 | kunmap(page); |
| 3473 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3474 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3475 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3476 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3477 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3478 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3479 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3480 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3481 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3482 | unlock_page(page); |
| 3483 | page_cache_release(page); |
| 3484 | out: |
| 3485 | return ret; |
| 3486 | } |
| 3487 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3488 | int btrfs_cont_expand(struct inode *inode, loff_t size) |
| 3489 | { |
| 3490 | struct btrfs_trans_handle *trans; |
| 3491 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3492 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3493 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3494 | struct extent_state *cached_state = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3495 | u64 mask = root->sectorsize - 1; |
| 3496 | u64 hole_start = (inode->i_size + mask) & ~mask; |
| 3497 | u64 block_end = (size + mask) & ~mask; |
| 3498 | u64 last_byte; |
| 3499 | u64 cur_offset; |
| 3500 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3501 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3502 | |
| 3503 | if (size <= hole_start) |
| 3504 | return 0; |
| 3505 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3506 | while (1) { |
| 3507 | struct btrfs_ordered_extent *ordered; |
| 3508 | btrfs_wait_ordered_range(inode, hole_start, |
| 3509 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3510 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
| 3511 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3512 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3513 | if (!ordered) |
| 3514 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3515 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3516 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3517 | btrfs_put_ordered_extent(ordered); |
| 3518 | } |
| 3519 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3520 | cur_offset = hole_start; |
| 3521 | while (1) { |
| 3522 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3523 | block_end - cur_offset, 0); |
| 3524 | BUG_ON(IS_ERR(em) || !em); |
| 3525 | last_byte = min(extent_map_end(em), block_end); |
| 3526 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3527 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3528 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3529 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3530 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3531 | trans = btrfs_start_transaction(root, 2); |
| 3532 | if (IS_ERR(trans)) { |
| 3533 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3534 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3535 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3536 | btrfs_set_trans_block_group(trans, inode); |
| 3537 | |
| 3538 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3539 | cur_offset + hole_size, |
| 3540 | &hint_byte, 1); |
| 3541 | BUG_ON(err); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3542 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3543 | err = btrfs_insert_file_extent(trans, root, |
| 3544 | inode->i_ino, cur_offset, 0, |
| 3545 | 0, hole_size, 0, hole_size, |
| 3546 | 0, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3547 | BUG_ON(err); |
| 3548 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3549 | btrfs_drop_extent_cache(inode, hole_start, |
| 3550 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3551 | |
| 3552 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3553 | } |
| 3554 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3555 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3556 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3557 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3558 | break; |
| 3559 | } |
| 3560 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3561 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3562 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3563 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3564 | return err; |
| 3565 | } |
| 3566 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3567 | static int btrfs_setattr_size(struct inode *inode, struct iattr *attr) |
| 3568 | { |
| 3569 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3570 | struct btrfs_trans_handle *trans; |
| 3571 | unsigned long nr; |
| 3572 | int ret; |
| 3573 | |
| 3574 | if (attr->ia_size == inode->i_size) |
| 3575 | return 0; |
| 3576 | |
| 3577 | if (attr->ia_size > inode->i_size) { |
| 3578 | unsigned long limit; |
| 3579 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
| 3580 | if (attr->ia_size > inode->i_sb->s_maxbytes) |
| 3581 | return -EFBIG; |
| 3582 | if (limit != RLIM_INFINITY && attr->ia_size > limit) { |
| 3583 | send_sig(SIGXFSZ, current, 0); |
| 3584 | return -EFBIG; |
| 3585 | } |
| 3586 | } |
| 3587 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3588 | trans = btrfs_start_transaction(root, 5); |
| 3589 | if (IS_ERR(trans)) |
| 3590 | return PTR_ERR(trans); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3591 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3592 | btrfs_set_trans_block_group(trans, inode); |
| 3593 | |
| 3594 | ret = btrfs_orphan_add(trans, inode); |
| 3595 | BUG_ON(ret); |
| 3596 | |
| 3597 | nr = trans->blocks_used; |
| 3598 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3599 | btrfs_btree_balance_dirty(root, nr); |
| 3600 | |
| 3601 | if (attr->ia_size > inode->i_size) { |
| 3602 | ret = btrfs_cont_expand(inode, attr->ia_size); |
| 3603 | if (ret) { |
| 3604 | btrfs_truncate(inode); |
| 3605 | return ret; |
| 3606 | } |
| 3607 | |
| 3608 | i_size_write(inode, attr->ia_size); |
| 3609 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
| 3610 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3611 | trans = btrfs_start_transaction(root, 0); |
| 3612 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3613 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3614 | trans->block_rsv = root->orphan_block_rsv; |
| 3615 | BUG_ON(!trans->block_rsv); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3616 | |
| 3617 | ret = btrfs_update_inode(trans, root, inode); |
| 3618 | BUG_ON(ret); |
| 3619 | if (inode->i_nlink > 0) { |
| 3620 | ret = btrfs_orphan_del(trans, inode); |
| 3621 | BUG_ON(ret); |
| 3622 | } |
| 3623 | nr = trans->blocks_used; |
| 3624 | btrfs_end_transaction(trans, root); |
| 3625 | btrfs_btree_balance_dirty(root, nr); |
| 3626 | return 0; |
| 3627 | } |
| 3628 | |
| 3629 | /* |
| 3630 | * We're truncating a file that used to have good data down to |
| 3631 | * zero. Make sure it gets into the ordered flush list so that |
| 3632 | * any new writes get down to disk quickly. |
| 3633 | */ |
| 3634 | if (attr->ia_size == 0) |
| 3635 | BTRFS_I(inode)->ordered_data_close = 1; |
| 3636 | |
| 3637 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3638 | ret = vmtruncate(inode, attr->ia_size); |
| 3639 | BUG_ON(ret); |
| 3640 | |
| 3641 | return 0; |
| 3642 | } |
| 3643 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3644 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3645 | { |
| 3646 | struct inode *inode = dentry->d_inode; |
| 3647 | int err; |
| 3648 | |
| 3649 | err = inode_change_ok(inode, attr); |
| 3650 | if (err) |
| 3651 | return err; |
| 3652 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3653 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3654 | err = btrfs_setattr_size(inode, attr); |
| 3655 | if (err) |
| 3656 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3657 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3658 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3659 | if (attr->ia_valid) { |
| 3660 | setattr_copy(inode, attr); |
| 3661 | mark_inode_dirty(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3662 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3663 | if (attr->ia_valid & ATTR_MODE) |
| 3664 | err = btrfs_acl_chmod(inode); |
| 3665 | } |
| 3666 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3667 | return err; |
| 3668 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3669 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3670 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3671 | { |
| 3672 | struct btrfs_trans_handle *trans; |
| 3673 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3674 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3675 | int ret; |
| 3676 | |
| 3677 | truncate_inode_pages(&inode->i_data, 0); |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3678 | if (inode->i_nlink && btrfs_root_refs(&root->root_item) != 0) |
| 3679 | goto no_delete; |
| 3680 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3681 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3682 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3683 | goto no_delete; |
| 3684 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3685 | /* 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] | 3686 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3687 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3688 | if (root->fs_info->log_root_recovering) { |
| 3689 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3690 | goto no_delete; |
| 3691 | } |
| 3692 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3693 | if (inode->i_nlink > 0) { |
| 3694 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3695 | goto no_delete; |
| 3696 | } |
| 3697 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3698 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3699 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3700 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3701 | trans = btrfs_start_transaction(root, 0); |
| 3702 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3703 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3704 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3705 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3706 | ret = btrfs_block_rsv_check(trans, root, |
| 3707 | root->orphan_block_rsv, 0, 5); |
| 3708 | if (ret) { |
| 3709 | BUG_ON(ret != -EAGAIN); |
| 3710 | ret = btrfs_commit_transaction(trans, root); |
| 3711 | BUG_ON(ret); |
| 3712 | continue; |
| 3713 | } |
| 3714 | |
| 3715 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3716 | if (ret != -EAGAIN) |
| 3717 | break; |
| 3718 | |
| 3719 | nr = trans->blocks_used; |
| 3720 | btrfs_end_transaction(trans, root); |
| 3721 | trans = NULL; |
| 3722 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3723 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3724 | } |
| 3725 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3726 | if (ret == 0) { |
| 3727 | ret = btrfs_orphan_del(trans, inode); |
| 3728 | BUG_ON(ret); |
| 3729 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3730 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3731 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3732 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3733 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3734 | no_delete: |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3735 | end_writeback(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3736 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | /* |
| 3740 | * this returns the key found in the dir entry in the location pointer. |
| 3741 | * If no dir entries were found, location->objectid is 0. |
| 3742 | */ |
| 3743 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3744 | struct btrfs_key *location) |
| 3745 | { |
| 3746 | const char *name = dentry->d_name.name; |
| 3747 | int namelen = dentry->d_name.len; |
| 3748 | struct btrfs_dir_item *di; |
| 3749 | struct btrfs_path *path; |
| 3750 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3751 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3752 | |
| 3753 | path = btrfs_alloc_path(); |
| 3754 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3755 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3756 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 3757 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3758 | if (IS_ERR(di)) |
| 3759 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3760 | |
| 3761 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3762 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3763 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3764 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3765 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3766 | btrfs_free_path(path); |
| 3767 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3768 | out_err: |
| 3769 | location->objectid = 0; |
| 3770 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | /* |
| 3774 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3775 | * needs to be changed to reflect the root directory of the tree root. This |
| 3776 | * is kind of like crossing a mount point. |
| 3777 | */ |
| 3778 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3779 | struct inode *dir, |
| 3780 | struct dentry *dentry, |
| 3781 | struct btrfs_key *location, |
| 3782 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3783 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3784 | struct btrfs_path *path; |
| 3785 | struct btrfs_root *new_root; |
| 3786 | struct btrfs_root_ref *ref; |
| 3787 | struct extent_buffer *leaf; |
| 3788 | int ret; |
| 3789 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3790 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3791 | path = btrfs_alloc_path(); |
| 3792 | if (!path) { |
| 3793 | err = -ENOMEM; |
| 3794 | goto out; |
| 3795 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3796 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3797 | err = -ENOENT; |
| 3798 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3799 | BTRFS_I(dir)->root->root_key.objectid, |
| 3800 | location->objectid); |
| 3801 | if (ret) { |
| 3802 | if (ret < 0) |
| 3803 | err = ret; |
| 3804 | goto out; |
| 3805 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3806 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3807 | leaf = path->nodes[0]; |
| 3808 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 3809 | if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || |
| 3810 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3811 | goto out; |
| 3812 | |
| 3813 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3814 | (unsigned long)(ref + 1), |
| 3815 | dentry->d_name.len); |
| 3816 | if (ret) |
| 3817 | goto out; |
| 3818 | |
| 3819 | btrfs_release_path(root->fs_info->tree_root, path); |
| 3820 | |
| 3821 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3822 | if (IS_ERR(new_root)) { |
| 3823 | err = PTR_ERR(new_root); |
| 3824 | goto out; |
| 3825 | } |
| 3826 | |
| 3827 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3828 | err = -ENOENT; |
| 3829 | goto out; |
| 3830 | } |
| 3831 | |
| 3832 | *sub_root = new_root; |
| 3833 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3834 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3835 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3836 | err = 0; |
| 3837 | out: |
| 3838 | btrfs_free_path(path); |
| 3839 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3840 | } |
| 3841 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3842 | static void inode_tree_add(struct inode *inode) |
| 3843 | { |
| 3844 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3845 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3846 | struct rb_node **p; |
| 3847 | struct rb_node *parent; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3848 | again: |
| 3849 | p = &root->inode_tree.rb_node; |
| 3850 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3851 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3852 | if (hlist_unhashed(&inode->i_hash)) |
| 3853 | return; |
| 3854 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3855 | spin_lock(&root->inode_lock); |
| 3856 | while (*p) { |
| 3857 | parent = *p; |
| 3858 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3859 | |
| 3860 | if (inode->i_ino < entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3861 | p = &parent->rb_left; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3862 | else if (inode->i_ino > entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3863 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3864 | else { |
| 3865 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 3866 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3867 | rb_erase(parent, &root->inode_tree); |
| 3868 | RB_CLEAR_NODE(parent); |
| 3869 | spin_unlock(&root->inode_lock); |
| 3870 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3871 | } |
| 3872 | } |
| 3873 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3874 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3875 | spin_unlock(&root->inode_lock); |
| 3876 | } |
| 3877 | |
| 3878 | static void inode_tree_del(struct inode *inode) |
| 3879 | { |
| 3880 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3881 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3882 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3883 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3884 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3885 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3886 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3887 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3888 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3889 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3890 | |
| 3891 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
| 3892 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3893 | spin_lock(&root->inode_lock); |
| 3894 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3895 | spin_unlock(&root->inode_lock); |
| 3896 | if (empty) |
| 3897 | btrfs_add_dead_root(root); |
| 3898 | } |
| 3899 | } |
| 3900 | |
| 3901 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 3902 | { |
| 3903 | struct rb_node *node; |
| 3904 | struct rb_node *prev; |
| 3905 | struct btrfs_inode *entry; |
| 3906 | struct inode *inode; |
| 3907 | u64 objectid = 0; |
| 3908 | |
| 3909 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3910 | |
| 3911 | spin_lock(&root->inode_lock); |
| 3912 | again: |
| 3913 | node = root->inode_tree.rb_node; |
| 3914 | prev = NULL; |
| 3915 | while (node) { |
| 3916 | prev = node; |
| 3917 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3918 | |
| 3919 | if (objectid < entry->vfs_inode.i_ino) |
| 3920 | node = node->rb_left; |
| 3921 | else if (objectid > entry->vfs_inode.i_ino) |
| 3922 | node = node->rb_right; |
| 3923 | else |
| 3924 | break; |
| 3925 | } |
| 3926 | if (!node) { |
| 3927 | while (prev) { |
| 3928 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
| 3929 | if (objectid <= entry->vfs_inode.i_ino) { |
| 3930 | node = prev; |
| 3931 | break; |
| 3932 | } |
| 3933 | prev = rb_next(prev); |
| 3934 | } |
| 3935 | } |
| 3936 | while (node) { |
| 3937 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3938 | objectid = entry->vfs_inode.i_ino + 1; |
| 3939 | inode = igrab(&entry->vfs_inode); |
| 3940 | if (inode) { |
| 3941 | spin_unlock(&root->inode_lock); |
| 3942 | if (atomic_read(&inode->i_count) > 1) |
| 3943 | d_prune_aliases(inode); |
| 3944 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 3945 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3946 | * the inode cache when its usage count |
| 3947 | * hits zero. |
| 3948 | */ |
| 3949 | iput(inode); |
| 3950 | cond_resched(); |
| 3951 | spin_lock(&root->inode_lock); |
| 3952 | goto again; |
| 3953 | } |
| 3954 | |
| 3955 | if (cond_resched_lock(&root->inode_lock)) |
| 3956 | goto again; |
| 3957 | |
| 3958 | node = rb_next(node); |
| 3959 | } |
| 3960 | spin_unlock(&root->inode_lock); |
| 3961 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3962 | } |
| 3963 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3964 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 3965 | { |
| 3966 | struct btrfs_iget_args *args = p; |
| 3967 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3968 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3969 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3970 | return 0; |
| 3971 | } |
| 3972 | |
| 3973 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 3974 | { |
| 3975 | struct btrfs_iget_args *args = opaque; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3976 | return args->ino == inode->i_ino && |
| 3977 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3978 | } |
| 3979 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3980 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 3981 | u64 objectid, |
| 3982 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3983 | { |
| 3984 | struct inode *inode; |
| 3985 | struct btrfs_iget_args args; |
| 3986 | args.ino = objectid; |
| 3987 | args.root = root; |
| 3988 | |
| 3989 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 3990 | btrfs_init_locked_inode, |
| 3991 | (void *)&args); |
| 3992 | return inode; |
| 3993 | } |
| 3994 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3995 | /* Get an inode object given its location and corresponding root. |
| 3996 | * Returns in *is_new if the inode was read from disk |
| 3997 | */ |
| 3998 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3999 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4000 | { |
| 4001 | struct inode *inode; |
| 4002 | |
| 4003 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4004 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4005 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4006 | |
| 4007 | if (inode->i_state & I_NEW) { |
| 4008 | BTRFS_I(inode)->root = root; |
| 4009 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4010 | btrfs_read_locked_inode(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4011 | |
| 4012 | inode_tree_add(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4013 | unlock_new_inode(inode); |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4014 | if (new) |
| 4015 | *new = 1; |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4016 | } |
| 4017 | |
| 4018 | return inode; |
| 4019 | } |
| 4020 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4021 | static struct inode *new_simple_dir(struct super_block *s, |
| 4022 | struct btrfs_key *key, |
| 4023 | struct btrfs_root *root) |
| 4024 | { |
| 4025 | struct inode *inode = new_inode(s); |
| 4026 | |
| 4027 | if (!inode) |
| 4028 | return ERR_PTR(-ENOMEM); |
| 4029 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4030 | BTRFS_I(inode)->root = root; |
| 4031 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 4032 | BTRFS_I(inode)->dummy_inode = 1; |
| 4033 | |
| 4034 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 4035 | inode->i_op = &simple_dir_inode_operations; |
| 4036 | inode->i_fop = &simple_dir_operations; |
| 4037 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 4038 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 4039 | |
| 4040 | return inode; |
| 4041 | } |
| 4042 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4043 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4044 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4045 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4046 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4047 | struct btrfs_root *sub_root = root; |
| 4048 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4049 | int index; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4050 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4051 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4052 | dentry->d_op = &btrfs_dentry_operations; |
| 4053 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4054 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 4055 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4056 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4057 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4058 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4059 | if (ret < 0) |
| 4060 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4061 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4062 | if (location.objectid == 0) |
| 4063 | return NULL; |
| 4064 | |
| 4065 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4066 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4067 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4068 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4069 | |
| 4070 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4071 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4072 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4073 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4074 | &location, &sub_root); |
| 4075 | if (ret < 0) { |
| 4076 | if (ret != -ENOENT) |
| 4077 | inode = ERR_PTR(ret); |
| 4078 | else |
| 4079 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4080 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4081 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4082 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4083 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4084 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4085 | if (root != sub_root) { |
| 4086 | down_read(&root->fs_info->cleanup_work_sem); |
| 4087 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
| 4088 | btrfs_orphan_cleanup(sub_root); |
| 4089 | up_read(&root->fs_info->cleanup_work_sem); |
| 4090 | } |
| 4091 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4092 | return inode; |
| 4093 | } |
| 4094 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4095 | static int btrfs_dentry_delete(struct dentry *dentry) |
| 4096 | { |
| 4097 | struct btrfs_root *root; |
| 4098 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4099 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 4100 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4101 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4102 | if (dentry->d_inode) { |
| 4103 | root = BTRFS_I(dentry->d_inode)->root; |
| 4104 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4105 | return 1; |
| 4106 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4107 | return 0; |
| 4108 | } |
| 4109 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4110 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 4111 | struct nameidata *nd) |
| 4112 | { |
| 4113 | struct inode *inode; |
| 4114 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4115 | inode = btrfs_lookup_dentry(dir, dentry); |
| 4116 | if (IS_ERR(inode)) |
| 4117 | return ERR_CAST(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4118 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4119 | return d_splice_alias(inode, dentry); |
| 4120 | } |
| 4121 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4122 | static unsigned char btrfs_filetype_table[] = { |
| 4123 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 4124 | }; |
| 4125 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 4126 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 4127 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4128 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4129 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4130 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4131 | struct btrfs_item *item; |
| 4132 | struct btrfs_dir_item *di; |
| 4133 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4134 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4135 | struct btrfs_path *path; |
| 4136 | int ret; |
| 4137 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4138 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4139 | int slot; |
| 4140 | int advance; |
| 4141 | unsigned char d_type; |
| 4142 | int over = 0; |
| 4143 | u32 di_cur; |
| 4144 | u32 di_total; |
| 4145 | u32 di_len; |
| 4146 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4147 | char tmp_name[32]; |
| 4148 | char *name_ptr; |
| 4149 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4150 | |
| 4151 | /* FIXME, use a real flag for deciding about the key type */ |
| 4152 | if (root->fs_info->tree_root == root) |
| 4153 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4154 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4155 | /* special case for "." */ |
| 4156 | if (filp->f_pos == 0) { |
| 4157 | over = filldir(dirent, ".", 1, |
| 4158 | 1, inode->i_ino, |
| 4159 | DT_DIR); |
| 4160 | if (over) |
| 4161 | return 0; |
| 4162 | filp->f_pos = 1; |
| 4163 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4164 | /* special case for .., just use the back ref */ |
| 4165 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4166 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4167 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4168 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4169 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4170 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4171 | filp->f_pos = 2; |
| 4172 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4173 | path = btrfs_alloc_path(); |
| 4174 | path->reada = 2; |
| 4175 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4176 | btrfs_set_key_type(&key, key_type); |
| 4177 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4178 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4179 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4180 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4181 | if (ret < 0) |
| 4182 | goto err; |
| 4183 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4184 | |
| 4185 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4186 | leaf = path->nodes[0]; |
| 4187 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4188 | slot = path->slots[0]; |
| 4189 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4190 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4191 | ret = btrfs_next_leaf(root, path); |
| 4192 | if (ret) |
| 4193 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4194 | leaf = path->nodes[0]; |
| 4195 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4196 | slot = path->slots[0]; |
| 4197 | } else { |
| 4198 | slot++; |
| 4199 | path->slots[0]++; |
| 4200 | } |
| 4201 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4202 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4203 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4204 | item = btrfs_item_nr(leaf, slot); |
| 4205 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4206 | |
| 4207 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4208 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4209 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4210 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4211 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4212 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4213 | |
| 4214 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4215 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4216 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 4217 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4218 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4219 | |
| 4220 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4221 | struct btrfs_key location; |
| 4222 | |
| 4223 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4224 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4225 | name_ptr = tmp_name; |
| 4226 | } else { |
| 4227 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4228 | if (!name_ptr) { |
| 4229 | ret = -ENOMEM; |
| 4230 | goto err; |
| 4231 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4232 | } |
| 4233 | read_extent_buffer(leaf, name_ptr, |
| 4234 | (unsigned long)(di + 1), name_len); |
| 4235 | |
| 4236 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4237 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4238 | |
| 4239 | /* is this a reference to our own snapshot? If so |
| 4240 | * skip it |
| 4241 | */ |
| 4242 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4243 | location.objectid == root->root_key.objectid) { |
| 4244 | over = 0; |
| 4245 | goto skip; |
| 4246 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4247 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4248 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4249 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4250 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4251 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4252 | if (name_ptr != tmp_name) |
| 4253 | kfree(name_ptr); |
| 4254 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4255 | if (over) |
| 4256 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4257 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4258 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4259 | di_cur += di_len; |
| 4260 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4261 | } |
| 4262 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4263 | |
| 4264 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4265 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4266 | /* |
| 4267 | * 32-bit glibc will use getdents64, but then strtol - |
| 4268 | * so the last number we can serve is this. |
| 4269 | */ |
| 4270 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4271 | else |
| 4272 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4273 | nopos: |
| 4274 | ret = 0; |
| 4275 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4276 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4277 | return ret; |
| 4278 | } |
| 4279 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4280 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4281 | { |
| 4282 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4283 | struct btrfs_trans_handle *trans; |
| 4284 | int ret = 0; |
| 4285 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4286 | if (BTRFS_I(inode)->dummy_inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4287 | return 0; |
| 4288 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4289 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4290 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4291 | btrfs_set_trans_block_group(trans, inode); |
| 4292 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4293 | } |
| 4294 | return ret; |
| 4295 | } |
| 4296 | |
| 4297 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4298 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4299 | * inode changes. But, it is most likely to find the inode in cache. |
| 4300 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4301 | * to keep or drop this code. |
| 4302 | */ |
| 4303 | void btrfs_dirty_inode(struct inode *inode) |
| 4304 | { |
| 4305 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4306 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4307 | int ret; |
| 4308 | |
| 4309 | if (BTRFS_I(inode)->dummy_inode) |
| 4310 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4311 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4312 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4313 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4314 | |
| 4315 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4316 | if (ret && ret == -ENOSPC) { |
| 4317 | /* whoops, lets try again with the full transaction */ |
| 4318 | btrfs_end_transaction(trans, root); |
| 4319 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4320 | if (IS_ERR(trans)) { |
| 4321 | if (printk_ratelimit()) { |
| 4322 | printk(KERN_ERR "btrfs: fail to " |
| 4323 | "dirty inode %lu error %ld\n", |
| 4324 | inode->i_ino, PTR_ERR(trans)); |
| 4325 | } |
| 4326 | return; |
| 4327 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4328 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4329 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4330 | ret = btrfs_update_inode(trans, root, inode); |
| 4331 | if (ret) { |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4332 | if (printk_ratelimit()) { |
| 4333 | printk(KERN_ERR "btrfs: fail to " |
| 4334 | "dirty inode %lu error %d\n", |
| 4335 | inode->i_ino, ret); |
| 4336 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4337 | } |
| 4338 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4339 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4340 | } |
| 4341 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4342 | /* |
| 4343 | * find the highest existing sequence number in a directory |
| 4344 | * and then set the in-memory index_cnt variable to reflect |
| 4345 | * free sequence numbers |
| 4346 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4347 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4348 | { |
| 4349 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4350 | struct btrfs_key key, found_key; |
| 4351 | struct btrfs_path *path; |
| 4352 | struct extent_buffer *leaf; |
| 4353 | int ret; |
| 4354 | |
| 4355 | key.objectid = inode->i_ino; |
| 4356 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4357 | key.offset = (u64)-1; |
| 4358 | |
| 4359 | path = btrfs_alloc_path(); |
| 4360 | if (!path) |
| 4361 | return -ENOMEM; |
| 4362 | |
| 4363 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4364 | if (ret < 0) |
| 4365 | goto out; |
| 4366 | /* FIXME: we should be able to handle this */ |
| 4367 | if (ret == 0) |
| 4368 | goto out; |
| 4369 | ret = 0; |
| 4370 | |
| 4371 | /* |
| 4372 | * MAGIC NUMBER EXPLANATION: |
| 4373 | * since we search a directory based on f_pos we have to start at 2 |
| 4374 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4375 | * else has to start at 2 |
| 4376 | */ |
| 4377 | if (path->slots[0] == 0) { |
| 4378 | BTRFS_I(inode)->index_cnt = 2; |
| 4379 | goto out; |
| 4380 | } |
| 4381 | |
| 4382 | path->slots[0]--; |
| 4383 | |
| 4384 | leaf = path->nodes[0]; |
| 4385 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4386 | |
| 4387 | if (found_key.objectid != inode->i_ino || |
| 4388 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4389 | BTRFS_I(inode)->index_cnt = 2; |
| 4390 | goto out; |
| 4391 | } |
| 4392 | |
| 4393 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4394 | out: |
| 4395 | btrfs_free_path(path); |
| 4396 | return ret; |
| 4397 | } |
| 4398 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4399 | /* |
| 4400 | * helper to find a free sequence number in a given directory. This current |
| 4401 | * code is very simple, later versions will do smarter things in the btree |
| 4402 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4403 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4404 | { |
| 4405 | int ret = 0; |
| 4406 | |
| 4407 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 4408 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4409 | if (ret) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4410 | return ret; |
| 4411 | } |
| 4412 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4413 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4414 | BTRFS_I(dir)->index_cnt++; |
| 4415 | |
| 4416 | return ret; |
| 4417 | } |
| 4418 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4419 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4420 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4421 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4422 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4423 | u64 ref_objectid, u64 objectid, |
| 4424 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4425 | { |
| 4426 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4427 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4428 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4429 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4430 | struct btrfs_inode_ref *ref; |
| 4431 | struct btrfs_key key[2]; |
| 4432 | u32 sizes[2]; |
| 4433 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4434 | int ret; |
| 4435 | int owner; |
| 4436 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4437 | path = btrfs_alloc_path(); |
| 4438 | BUG_ON(!path); |
| 4439 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4440 | inode = new_inode(root->fs_info->sb); |
| 4441 | if (!inode) |
| 4442 | return ERR_PTR(-ENOMEM); |
| 4443 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4444 | if (dir) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4445 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4446 | if (ret) { |
| 4447 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4448 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4449 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4450 | } |
| 4451 | /* |
| 4452 | * index_cnt is ignored for everything but a dir, |
| 4453 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4454 | * number |
| 4455 | */ |
| 4456 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4457 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4458 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4459 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4460 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4461 | if (mode & S_IFDIR) |
| 4462 | owner = 0; |
| 4463 | else |
| 4464 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4465 | BTRFS_I(inode)->block_group = |
| 4466 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4467 | |
| 4468 | key[0].objectid = objectid; |
| 4469 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4470 | key[0].offset = 0; |
| 4471 | |
| 4472 | key[1].objectid = objectid; |
| 4473 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4474 | key[1].offset = ref_objectid; |
| 4475 | |
| 4476 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4477 | sizes[1] = name_len + sizeof(*ref); |
| 4478 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4479 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4480 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4481 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4482 | goto fail; |
| 4483 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 4484 | inode_init_owner(inode, dir, mode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4485 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4486 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4487 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4488 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4489 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4490 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4491 | |
| 4492 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4493 | struct btrfs_inode_ref); |
| 4494 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4495 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4496 | ptr = (unsigned long)(ref + 1); |
| 4497 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4498 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4499 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4500 | btrfs_free_path(path); |
| 4501 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4502 | location = &BTRFS_I(inode)->location; |
| 4503 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4504 | location->offset = 0; |
| 4505 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4506 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4507 | btrfs_inherit_iflags(inode, dir); |
| 4508 | |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4509 | if ((mode & S_IFREG)) { |
| 4510 | if (btrfs_test_opt(root, NODATASUM)) |
| 4511 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 4512 | if (btrfs_test_opt(root, NODATACOW)) |
| 4513 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4514 | } |
| 4515 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4516 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4517 | inode_tree_add(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4518 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4519 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4520 | if (dir) |
| 4521 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4522 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4523 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4524 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4528 | { |
| 4529 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4530 | } |
| 4531 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4532 | /* |
| 4533 | * utility function to add 'inode' into 'parent_inode' with |
| 4534 | * a give name and a given sequence number. |
| 4535 | * if 'add_backref' is true, also insert a backref from the |
| 4536 | * inode to the parent directory. |
| 4537 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4538 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4539 | struct inode *parent_inode, struct inode *inode, |
| 4540 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4541 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4542 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4543 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4544 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4545 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4546 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4547 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4548 | } else { |
| 4549 | key.objectid = inode->i_ino; |
| 4550 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4551 | key.offset = 0; |
| 4552 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4553 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4554 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4555 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4556 | key.objectid, root->root_key.objectid, |
| 4557 | parent_inode->i_ino, |
| 4558 | index, name, name_len); |
| 4559 | } else if (add_backref) { |
| 4560 | ret = btrfs_insert_inode_ref(trans, root, |
| 4561 | name, name_len, inode->i_ino, |
| 4562 | parent_inode->i_ino, index); |
| 4563 | } |
| 4564 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4565 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4566 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4567 | parent_inode->i_ino, &key, |
| 4568 | btrfs_inode_type(inode), index); |
| 4569 | BUG_ON(ret); |
| 4570 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4571 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4572 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4573 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4574 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4575 | } |
| 4576 | return ret; |
| 4577 | } |
| 4578 | |
| 4579 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4580 | struct dentry *dentry, struct inode *inode, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4581 | int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4582 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4583 | int err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4584 | inode, dentry->d_name.name, |
| 4585 | dentry->d_name.len, backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4586 | if (!err) { |
| 4587 | d_instantiate(dentry, inode); |
| 4588 | return 0; |
| 4589 | } |
| 4590 | if (err > 0) |
| 4591 | err = -EEXIST; |
| 4592 | return err; |
| 4593 | } |
| 4594 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4595 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4596 | int mode, dev_t rdev) |
| 4597 | { |
| 4598 | struct btrfs_trans_handle *trans; |
| 4599 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4600 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4601 | int err; |
| 4602 | int drop_inode = 0; |
| 4603 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4604 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4605 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4606 | |
| 4607 | if (!new_valid_dev(rdev)) |
| 4608 | return -EINVAL; |
| 4609 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4610 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4611 | if (err) |
| 4612 | return err; |
| 4613 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4614 | /* |
| 4615 | * 2 for inode item and ref |
| 4616 | * 2 for dir items |
| 4617 | * 1 for xattr if selinux is on |
| 4618 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4619 | trans = btrfs_start_transaction(root, 5); |
| 4620 | if (IS_ERR(trans)) |
| 4621 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4622 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4623 | btrfs_set_trans_block_group(trans, dir); |
| 4624 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4625 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4626 | dentry->d_name.len, |
| 4627 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4628 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4629 | err = PTR_ERR(inode); |
| 4630 | if (IS_ERR(inode)) |
| 4631 | goto out_unlock; |
| 4632 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4633 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4634 | if (err) { |
| 4635 | drop_inode = 1; |
| 4636 | goto out_unlock; |
| 4637 | } |
| 4638 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4639 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4640 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4641 | if (err) |
| 4642 | drop_inode = 1; |
| 4643 | else { |
| 4644 | inode->i_op = &btrfs_special_inode_operations; |
| 4645 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4646 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4647 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4648 | btrfs_update_inode_block_group(trans, inode); |
| 4649 | btrfs_update_inode_block_group(trans, dir); |
| 4650 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4651 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4652 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4653 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4654 | if (drop_inode) { |
| 4655 | inode_dec_link_count(inode); |
| 4656 | iput(inode); |
| 4657 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4658 | return err; |
| 4659 | } |
| 4660 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4661 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4662 | int mode, struct nameidata *nd) |
| 4663 | { |
| 4664 | struct btrfs_trans_handle *trans; |
| 4665 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4666 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4667 | int drop_inode = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4668 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4669 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4670 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4671 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4672 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4673 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4674 | if (err) |
| 4675 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4676 | /* |
| 4677 | * 2 for inode item and ref |
| 4678 | * 2 for dir items |
| 4679 | * 1 for xattr if selinux is on |
| 4680 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4681 | trans = btrfs_start_transaction(root, 5); |
| 4682 | if (IS_ERR(trans)) |
| 4683 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4684 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4685 | btrfs_set_trans_block_group(trans, dir); |
| 4686 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4687 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4688 | dentry->d_name.len, |
| 4689 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4690 | objectid, BTRFS_I(dir)->block_group, mode, |
| 4691 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4692 | err = PTR_ERR(inode); |
| 4693 | if (IS_ERR(inode)) |
| 4694 | goto out_unlock; |
| 4695 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4696 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4697 | if (err) { |
| 4698 | drop_inode = 1; |
| 4699 | goto out_unlock; |
| 4700 | } |
| 4701 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4702 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4703 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4704 | if (err) |
| 4705 | drop_inode = 1; |
| 4706 | else { |
| 4707 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4708 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4709 | inode->i_fop = &btrfs_file_operations; |
| 4710 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4711 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4712 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4713 | btrfs_update_inode_block_group(trans, inode); |
| 4714 | btrfs_update_inode_block_group(trans, dir); |
| 4715 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4716 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4717 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4718 | if (drop_inode) { |
| 4719 | inode_dec_link_count(inode); |
| 4720 | iput(inode); |
| 4721 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4722 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4723 | return err; |
| 4724 | } |
| 4725 | |
| 4726 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4727 | struct dentry *dentry) |
| 4728 | { |
| 4729 | struct btrfs_trans_handle *trans; |
| 4730 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4731 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4732 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4733 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4734 | int err; |
| 4735 | int drop_inode = 0; |
| 4736 | |
| 4737 | if (inode->i_nlink == 0) |
| 4738 | return -ENOENT; |
| 4739 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4740 | /* do not allow sys_link's with other subvols of the same device */ |
| 4741 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
| 4742 | return -EPERM; |
| 4743 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4744 | btrfs_inc_nlink(inode); |
| 4745 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4746 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4747 | if (err) |
| 4748 | goto fail; |
| 4749 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4750 | /* |
| 4751 | * 1 item for inode ref |
| 4752 | * 2 items for dir items |
| 4753 | */ |
| 4754 | trans = btrfs_start_transaction(root, 3); |
| 4755 | if (IS_ERR(trans)) { |
| 4756 | err = PTR_ERR(trans); |
| 4757 | goto fail; |
| 4758 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4759 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4760 | btrfs_set_trans_block_group(trans, dir); |
| 4761 | atomic_inc(&inode->i_count); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4762 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4763 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4764 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4765 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4766 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4767 | } else { |
| 4768 | btrfs_update_inode_block_group(trans, dir); |
| 4769 | err = btrfs_update_inode(trans, root, inode); |
| 4770 | BUG_ON(err); |
| 4771 | btrfs_log_new_name(trans, inode, NULL, dentry->d_parent); |
| 4772 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4773 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4774 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4775 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4776 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4777 | if (drop_inode) { |
| 4778 | inode_dec_link_count(inode); |
| 4779 | iput(inode); |
| 4780 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4781 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4782 | return err; |
| 4783 | } |
| 4784 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4785 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4786 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4787 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4788 | struct btrfs_trans_handle *trans; |
| 4789 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4790 | int err = 0; |
| 4791 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4792 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4793 | u64 index = 0; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4794 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4795 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4796 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4797 | if (err) |
| 4798 | return err; |
| 4799 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4800 | /* |
| 4801 | * 2 items for inode and ref |
| 4802 | * 2 items for dir items |
| 4803 | * 1 for xattr if selinux is on |
| 4804 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4805 | trans = btrfs_start_transaction(root, 5); |
| 4806 | if (IS_ERR(trans)) |
| 4807 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4808 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4809 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4810 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4811 | dentry->d_name.len, |
| 4812 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4813 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 4814 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4815 | if (IS_ERR(inode)) { |
| 4816 | err = PTR_ERR(inode); |
| 4817 | goto out_fail; |
| 4818 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4819 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4820 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4821 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4822 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4823 | if (err) |
| 4824 | goto out_fail; |
| 4825 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4826 | inode->i_op = &btrfs_dir_inode_operations; |
| 4827 | inode->i_fop = &btrfs_dir_file_operations; |
| 4828 | btrfs_set_trans_block_group(trans, inode); |
| 4829 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4830 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4831 | err = btrfs_update_inode(trans, root, inode); |
| 4832 | if (err) |
| 4833 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4834 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4835 | err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4836 | inode, dentry->d_name.name, |
| 4837 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4838 | if (err) |
| 4839 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4840 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4841 | d_instantiate(dentry, inode); |
| 4842 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4843 | btrfs_update_inode_block_group(trans, inode); |
| 4844 | btrfs_update_inode_block_group(trans, dir); |
| 4845 | |
| 4846 | out_fail: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4847 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4848 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4849 | if (drop_on_err) |
| 4850 | iput(inode); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4851 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4852 | return err; |
| 4853 | } |
| 4854 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4855 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4856 | * and an extent that you want to insert, deal with overlap and insert |
| 4857 | * the new extent into the tree. |
| 4858 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4859 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4860 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4861 | struct extent_map *em, |
| 4862 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4863 | { |
| 4864 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4865 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4866 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4867 | start_diff = map_start - em->start; |
| 4868 | em->start = map_start; |
| 4869 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4870 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4871 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4872 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4873 | em->block_len -= start_diff; |
| 4874 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4875 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4876 | } |
| 4877 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4878 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4879 | struct inode *inode, struct page *page, |
| 4880 | size_t pg_offset, u64 extent_offset, |
| 4881 | struct btrfs_file_extent_item *item) |
| 4882 | { |
| 4883 | int ret; |
| 4884 | struct extent_buffer *leaf = path->nodes[0]; |
| 4885 | char *tmp; |
| 4886 | size_t max_size; |
| 4887 | unsigned long inline_size; |
| 4888 | unsigned long ptr; |
| 4889 | |
| 4890 | WARN_ON(pg_offset != 0); |
| 4891 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4892 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4893 | btrfs_item_nr(leaf, path->slots[0])); |
| 4894 | tmp = kmalloc(inline_size, GFP_NOFS); |
| 4895 | ptr = btrfs_file_extent_inline_start(item); |
| 4896 | |
| 4897 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4898 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 4899 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4900 | ret = btrfs_zlib_decompress(tmp, page, extent_offset, |
| 4901 | inline_size, max_size); |
| 4902 | if (ret) { |
| 4903 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 4904 | unsigned long copy_size = min_t(u64, |
| 4905 | PAGE_CACHE_SIZE - pg_offset, |
| 4906 | max_size - extent_offset); |
| 4907 | memset(kaddr + pg_offset, 0, copy_size); |
| 4908 | kunmap_atomic(kaddr, KM_USER0); |
| 4909 | } |
| 4910 | kfree(tmp); |
| 4911 | return 0; |
| 4912 | } |
| 4913 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4914 | /* |
| 4915 | * 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] | 4916 | * the ugly parts come from merging extents from the disk with the in-ram |
| 4917 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4918 | * where the in-ram extents might be locked pending data=ordered completion. |
| 4919 | * |
| 4920 | * This also copies inline extents directly into the page. |
| 4921 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4922 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4923 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4924 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4925 | int create) |
| 4926 | { |
| 4927 | int ret; |
| 4928 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4929 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4930 | u64 extent_start = 0; |
| 4931 | u64 extent_end = 0; |
| 4932 | u64 objectid = inode->i_ino; |
| 4933 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4934 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4935 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4936 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4937 | struct extent_buffer *leaf; |
| 4938 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4939 | struct extent_map *em = NULL; |
| 4940 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4941 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4942 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4943 | int compressed; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4944 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4945 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4946 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4947 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4948 | if (em) |
| 4949 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4950 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4951 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4952 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4953 | if (em->start > start || em->start + em->len <= start) |
| 4954 | free_extent_map(em); |
| 4955 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4956 | free_extent_map(em); |
| 4957 | else |
| 4958 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4959 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4960 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4961 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4962 | err = -ENOMEM; |
| 4963 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4964 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4965 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4966 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 4967 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4968 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4969 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4970 | |
| 4971 | if (!path) { |
| 4972 | path = btrfs_alloc_path(); |
| 4973 | BUG_ON(!path); |
| 4974 | } |
| 4975 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4976 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 4977 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4978 | if (ret < 0) { |
| 4979 | err = ret; |
| 4980 | goto out; |
| 4981 | } |
| 4982 | |
| 4983 | if (ret != 0) { |
| 4984 | if (path->slots[0] == 0) |
| 4985 | goto not_found; |
| 4986 | path->slots[0]--; |
| 4987 | } |
| 4988 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4989 | leaf = path->nodes[0]; |
| 4990 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4991 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4992 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4993 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4994 | found_type = btrfs_key_type(&found_key); |
| 4995 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4996 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 4997 | goto not_found; |
| 4998 | } |
| 4999 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5000 | found_type = btrfs_file_extent_type(leaf, item); |
| 5001 | extent_start = found_key.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5002 | compressed = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5003 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5004 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5005 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5006 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5007 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5008 | size_t size; |
| 5009 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5010 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 5011 | ~((u64)root->sectorsize - 1); |
| 5012 | } |
| 5013 | |
| 5014 | if (start >= extent_end) { |
| 5015 | path->slots[0]++; |
| 5016 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 5017 | ret = btrfs_next_leaf(root, path); |
| 5018 | if (ret < 0) { |
| 5019 | err = ret; |
| 5020 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5021 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5022 | if (ret > 0) |
| 5023 | goto not_found; |
| 5024 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5025 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5026 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5027 | if (found_key.objectid != objectid || |
| 5028 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 5029 | goto not_found; |
| 5030 | if (start + len <= found_key.offset) |
| 5031 | goto not_found; |
| 5032 | em->start = start; |
| 5033 | em->len = found_key.offset - start; |
| 5034 | goto not_found_em; |
| 5035 | } |
| 5036 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5037 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5038 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5039 | em->start = extent_start; |
| 5040 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5041 | em->orig_start = extent_start - |
| 5042 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5043 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 5044 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5045 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5046 | goto insert; |
| 5047 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5048 | if (compressed) { |
| 5049 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 5050 | em->block_start = bytenr; |
| 5051 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 5052 | item); |
| 5053 | } else { |
| 5054 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 5055 | em->block_start = bytenr; |
| 5056 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5057 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 5058 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5059 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5060 | goto insert; |
| 5061 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5062 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5063 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5064 | size_t size; |
| 5065 | size_t extent_offset; |
| 5066 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5067 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5068 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5069 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5070 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5071 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5072 | goto out; |
| 5073 | } |
| 5074 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5075 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5076 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5077 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5078 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5079 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5080 | em->len = (copy_size + root->sectorsize - 1) & |
| 5081 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5082 | em->orig_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5083 | if (compressed) |
| 5084 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5085 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5086 | if (create == 0 && !PageUptodate(page)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5087 | if (btrfs_file_extent_compression(leaf, item) == |
| 5088 | BTRFS_COMPRESS_ZLIB) { |
| 5089 | ret = uncompress_inline(path, inode, page, |
| 5090 | pg_offset, |
| 5091 | extent_offset, item); |
| 5092 | BUG_ON(ret); |
| 5093 | } else { |
| 5094 | map = kmap(page); |
| 5095 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 5096 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 5097 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 5098 | memset(map + pg_offset + copy_size, 0, |
| 5099 | PAGE_CACHE_SIZE - pg_offset - |
| 5100 | copy_size); |
| 5101 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5102 | kunmap(page); |
| 5103 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5104 | flush_dcache_page(page); |
| 5105 | } else if (create && PageUptodate(page)) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5106 | WARN_ON(1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5107 | if (!trans) { |
| 5108 | kunmap(page); |
| 5109 | free_extent_map(em); |
| 5110 | em = NULL; |
| 5111 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 5112 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5113 | goto again; |
| 5114 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5115 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5116 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5117 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5118 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5119 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5120 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5121 | set_extent_uptodate(io_tree, em->start, |
| 5122 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5123 | goto insert; |
| 5124 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5125 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5126 | WARN_ON(1); |
| 5127 | } |
| 5128 | not_found: |
| 5129 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5130 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5131 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5132 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5133 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5134 | insert: |
| 5135 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5136 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5137 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 5138 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 5139 | (unsigned long long)em->len, |
| 5140 | (unsigned long long)start, |
| 5141 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5142 | err = -EIO; |
| 5143 | goto out; |
| 5144 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5145 | |
| 5146 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5147 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5148 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5149 | /* it is possible that someone inserted the extent into the tree |
| 5150 | * while we had the lock dropped. It is also possible that |
| 5151 | * an overlapping map exists in the tree |
| 5152 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5153 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5154 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5155 | |
| 5156 | ret = 0; |
| 5157 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5158 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5159 | if (existing && (existing->start > start || |
| 5160 | existing->start + existing->len <= start)) { |
| 5161 | free_extent_map(existing); |
| 5162 | existing = NULL; |
| 5163 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5164 | if (!existing) { |
| 5165 | existing = lookup_extent_mapping(em_tree, em->start, |
| 5166 | em->len); |
| 5167 | if (existing) { |
| 5168 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5169 | em, start, |
| 5170 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5171 | free_extent_map(existing); |
| 5172 | if (err) { |
| 5173 | free_extent_map(em); |
| 5174 | em = NULL; |
| 5175 | } |
| 5176 | } else { |
| 5177 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5178 | free_extent_map(em); |
| 5179 | em = NULL; |
| 5180 | } |
| 5181 | } else { |
| 5182 | free_extent_map(em); |
| 5183 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5184 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5185 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5186 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5187 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5188 | out: |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5189 | if (path) |
| 5190 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5191 | if (trans) { |
| 5192 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5193 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5194 | err = ret; |
| 5195 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5196 | if (err) { |
| 5197 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5198 | return ERR_PTR(err); |
| 5199 | } |
| 5200 | return em; |
| 5201 | } |
| 5202 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5203 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 5204 | u64 start, u64 len) |
| 5205 | { |
| 5206 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5207 | struct btrfs_trans_handle *trans; |
| 5208 | struct extent_map *em; |
| 5209 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 5210 | struct btrfs_key ins; |
| 5211 | u64 alloc_hint; |
| 5212 | int ret; |
| 5213 | |
| 5214 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
| 5215 | |
| 5216 | trans = btrfs_join_transaction(root, 0); |
| 5217 | if (!trans) |
| 5218 | return ERR_PTR(-ENOMEM); |
| 5219 | |
| 5220 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5221 | |
| 5222 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 5223 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
| 5224 | alloc_hint, (u64)-1, &ins, 1); |
| 5225 | if (ret) { |
| 5226 | em = ERR_PTR(ret); |
| 5227 | goto out; |
| 5228 | } |
| 5229 | |
| 5230 | em = alloc_extent_map(GFP_NOFS); |
| 5231 | if (!em) { |
| 5232 | em = ERR_PTR(-ENOMEM); |
| 5233 | goto out; |
| 5234 | } |
| 5235 | |
| 5236 | em->start = start; |
| 5237 | em->orig_start = em->start; |
| 5238 | em->len = ins.offset; |
| 5239 | |
| 5240 | em->block_start = ins.objectid; |
| 5241 | em->block_len = ins.offset; |
| 5242 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 5243 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5244 | |
| 5245 | while (1) { |
| 5246 | write_lock(&em_tree->lock); |
| 5247 | ret = add_extent_mapping(em_tree, em); |
| 5248 | write_unlock(&em_tree->lock); |
| 5249 | if (ret != -EEXIST) |
| 5250 | break; |
| 5251 | btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0); |
| 5252 | } |
| 5253 | |
| 5254 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 5255 | ins.offset, ins.offset, 0); |
| 5256 | if (ret) { |
| 5257 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 5258 | em = ERR_PTR(ret); |
| 5259 | } |
| 5260 | out: |
| 5261 | btrfs_end_transaction(trans, root); |
| 5262 | return em; |
| 5263 | } |
| 5264 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5265 | /* |
| 5266 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 5267 | * block must be cow'd |
| 5268 | */ |
| 5269 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 5270 | struct inode *inode, u64 offset, u64 len) |
| 5271 | { |
| 5272 | struct btrfs_path *path; |
| 5273 | int ret; |
| 5274 | struct extent_buffer *leaf; |
| 5275 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5276 | struct btrfs_file_extent_item *fi; |
| 5277 | struct btrfs_key key; |
| 5278 | u64 disk_bytenr; |
| 5279 | u64 backref_offset; |
| 5280 | u64 extent_end; |
| 5281 | u64 num_bytes; |
| 5282 | int slot; |
| 5283 | int found_type; |
| 5284 | |
| 5285 | path = btrfs_alloc_path(); |
| 5286 | if (!path) |
| 5287 | return -ENOMEM; |
| 5288 | |
| 5289 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 5290 | offset, 0); |
| 5291 | if (ret < 0) |
| 5292 | goto out; |
| 5293 | |
| 5294 | slot = path->slots[0]; |
| 5295 | if (ret == 1) { |
| 5296 | if (slot == 0) { |
| 5297 | /* can't find the item, must cow */ |
| 5298 | ret = 0; |
| 5299 | goto out; |
| 5300 | } |
| 5301 | slot--; |
| 5302 | } |
| 5303 | ret = 0; |
| 5304 | leaf = path->nodes[0]; |
| 5305 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5306 | if (key.objectid != inode->i_ino || |
| 5307 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5308 | /* not our file or wrong item type, must cow */ |
| 5309 | goto out; |
| 5310 | } |
| 5311 | |
| 5312 | if (key.offset > offset) { |
| 5313 | /* Wrong offset, must cow */ |
| 5314 | goto out; |
| 5315 | } |
| 5316 | |
| 5317 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5318 | found_type = btrfs_file_extent_type(leaf, fi); |
| 5319 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 5320 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 5321 | /* not a regular extent, must cow */ |
| 5322 | goto out; |
| 5323 | } |
| 5324 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5325 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 5326 | |
| 5327 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 5328 | if (extent_end < offset + len) { |
| 5329 | /* extent doesn't include our full range, must cow */ |
| 5330 | goto out; |
| 5331 | } |
| 5332 | |
| 5333 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 5334 | goto out; |
| 5335 | |
| 5336 | /* |
| 5337 | * look for other files referencing this extent, if we |
| 5338 | * find any we must cow |
| 5339 | */ |
| 5340 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
| 5341 | key.offset - backref_offset, disk_bytenr)) |
| 5342 | goto out; |
| 5343 | |
| 5344 | /* |
| 5345 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 5346 | * in this extent we are about to write. If there |
| 5347 | * are any csums in that range we have to cow in order |
| 5348 | * to keep the csums correct |
| 5349 | */ |
| 5350 | disk_bytenr += backref_offset; |
| 5351 | disk_bytenr += offset - key.offset; |
| 5352 | num_bytes = min(offset + len, extent_end) - offset; |
| 5353 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 5354 | goto out; |
| 5355 | /* |
| 5356 | * all of the above have passed, it is safe to overwrite this extent |
| 5357 | * without cow |
| 5358 | */ |
| 5359 | ret = 1; |
| 5360 | out: |
| 5361 | btrfs_free_path(path); |
| 5362 | return ret; |
| 5363 | } |
| 5364 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5365 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 5366 | struct buffer_head *bh_result, int create) |
| 5367 | { |
| 5368 | struct extent_map *em; |
| 5369 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5370 | u64 start = iblock << inode->i_blkbits; |
| 5371 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5372 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5373 | |
| 5374 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
| 5375 | if (IS_ERR(em)) |
| 5376 | return PTR_ERR(em); |
| 5377 | |
| 5378 | /* |
| 5379 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 5380 | * io. INLINE is special, and we could probably kludge it in here, but |
| 5381 | * it's still buffered so for safety lets just fall back to the generic |
| 5382 | * buffered path. |
| 5383 | * |
| 5384 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 5385 | * decompress it, so there will be buffering required no matter what we |
| 5386 | * do, so go ahead and fallback to buffered. |
| 5387 | * |
| 5388 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 5389 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 5390 | * the generic code. |
| 5391 | */ |
| 5392 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 5393 | em->block_start == EXTENT_MAP_INLINE) { |
| 5394 | free_extent_map(em); |
| 5395 | return -ENOTBLK; |
| 5396 | } |
| 5397 | |
| 5398 | /* Just a good old fashioned hole, return */ |
| 5399 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 5400 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5401 | free_extent_map(em); |
| 5402 | /* DIO will do one hole at a time, so just unlock a sector */ |
| 5403 | unlock_extent(&BTRFS_I(inode)->io_tree, start, |
| 5404 | start + root->sectorsize - 1, GFP_NOFS); |
| 5405 | return 0; |
| 5406 | } |
| 5407 | |
| 5408 | /* |
| 5409 | * We don't allocate a new extent in the following cases |
| 5410 | * |
| 5411 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 5412 | * existing extent. |
| 5413 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 5414 | * just use the extent. |
| 5415 | * |
| 5416 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5417 | if (!create) { |
| 5418 | len = em->len - (start - em->start); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5419 | goto map; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5420 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5421 | |
| 5422 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 5423 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 5424 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5425 | int type; |
| 5426 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5427 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5428 | |
| 5429 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5430 | type = BTRFS_ORDERED_PREALLOC; |
| 5431 | else |
| 5432 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5433 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5434 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5435 | |
| 5436 | /* |
| 5437 | * we're not going to log anything, but we do need |
| 5438 | * to make sure the current transaction stays open |
| 5439 | * while we look for nocow cross refs |
| 5440 | */ |
| 5441 | trans = btrfs_join_transaction(root, 0); |
| 5442 | if (!trans) |
| 5443 | goto must_cow; |
| 5444 | |
| 5445 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
| 5446 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 5447 | block_start, len, len, type); |
| 5448 | btrfs_end_transaction(trans, root); |
| 5449 | if (ret) { |
| 5450 | free_extent_map(em); |
| 5451 | return ret; |
| 5452 | } |
| 5453 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5454 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5455 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5456 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5457 | must_cow: |
| 5458 | /* |
| 5459 | * this will cow the extent, reset the len in case we changed |
| 5460 | * it above |
| 5461 | */ |
| 5462 | len = bh_result->b_size; |
| 5463 | free_extent_map(em); |
| 5464 | em = btrfs_new_extent_direct(inode, start, len); |
| 5465 | if (IS_ERR(em)) |
| 5466 | return PTR_ERR(em); |
| 5467 | len = min(len, em->len - (start - em->start)); |
| 5468 | unlock: |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5469 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
| 5470 | EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1, |
| 5471 | 0, NULL, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5472 | map: |
| 5473 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 5474 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5475 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5476 | bh_result->b_bdev = em->bdev; |
| 5477 | set_buffer_mapped(bh_result); |
| 5478 | if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5479 | set_buffer_new(bh_result); |
| 5480 | |
| 5481 | free_extent_map(em); |
| 5482 | |
| 5483 | return 0; |
| 5484 | } |
| 5485 | |
| 5486 | struct btrfs_dio_private { |
| 5487 | struct inode *inode; |
| 5488 | u64 logical_offset; |
| 5489 | u64 disk_bytenr; |
| 5490 | u64 bytes; |
| 5491 | u32 *csums; |
| 5492 | void *private; |
| 5493 | }; |
| 5494 | |
| 5495 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 5496 | { |
| 5497 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 5498 | struct bio_vec *bvec = bio->bi_io_vec; |
| 5499 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5500 | struct inode *inode = dip->inode; |
| 5501 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5502 | u64 start; |
| 5503 | u32 *private = dip->csums; |
| 5504 | |
| 5505 | start = dip->logical_offset; |
| 5506 | do { |
| 5507 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 5508 | struct page *page = bvec->bv_page; |
| 5509 | char *kaddr; |
| 5510 | u32 csum = ~(u32)0; |
| 5511 | unsigned long flags; |
| 5512 | |
| 5513 | local_irq_save(flags); |
| 5514 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 5515 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 5516 | csum, bvec->bv_len); |
| 5517 | btrfs_csum_final(csum, (char *)&csum); |
| 5518 | kunmap_atomic(kaddr, KM_IRQ0); |
| 5519 | local_irq_restore(flags); |
| 5520 | |
| 5521 | flush_dcache_page(bvec->bv_page); |
| 5522 | if (csum != *private) { |
| 5523 | printk(KERN_ERR "btrfs csum failed ino %lu off" |
| 5524 | " %llu csum %u private %u\n", |
| 5525 | inode->i_ino, (unsigned long long)start, |
| 5526 | csum, *private); |
| 5527 | err = -EIO; |
| 5528 | } |
| 5529 | } |
| 5530 | |
| 5531 | start += bvec->bv_len; |
| 5532 | private++; |
| 5533 | bvec++; |
| 5534 | } while (bvec <= bvec_end); |
| 5535 | |
| 5536 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
| 5537 | dip->logical_offset + dip->bytes - 1, GFP_NOFS); |
| 5538 | bio->bi_private = dip->private; |
| 5539 | |
| 5540 | kfree(dip->csums); |
| 5541 | kfree(dip); |
| 5542 | dio_end_io(bio, err); |
| 5543 | } |
| 5544 | |
| 5545 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 5546 | { |
| 5547 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5548 | struct inode *inode = dip->inode; |
| 5549 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5550 | struct btrfs_trans_handle *trans; |
| 5551 | struct btrfs_ordered_extent *ordered = NULL; |
| 5552 | struct extent_state *cached_state = NULL; |
| 5553 | int ret; |
| 5554 | |
| 5555 | if (err) |
| 5556 | goto out_done; |
| 5557 | |
| 5558 | ret = btrfs_dec_test_ordered_pending(inode, &ordered, |
| 5559 | dip->logical_offset, dip->bytes); |
| 5560 | if (!ret) |
| 5561 | goto out_done; |
| 5562 | |
| 5563 | BUG_ON(!ordered); |
| 5564 | |
| 5565 | trans = btrfs_join_transaction(root, 1); |
| 5566 | if (!trans) { |
| 5567 | err = -ENOMEM; |
| 5568 | goto out; |
| 5569 | } |
| 5570 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5571 | |
| 5572 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) { |
| 5573 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5574 | if (!ret) |
| 5575 | ret = btrfs_update_inode(trans, root, inode); |
| 5576 | err = ret; |
| 5577 | goto out; |
| 5578 | } |
| 5579 | |
| 5580 | lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5581 | ordered->file_offset + ordered->len - 1, 0, |
| 5582 | &cached_state, GFP_NOFS); |
| 5583 | |
| 5584 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) { |
| 5585 | ret = btrfs_mark_extent_written(trans, inode, |
| 5586 | ordered->file_offset, |
| 5587 | ordered->file_offset + |
| 5588 | ordered->len); |
| 5589 | if (ret) { |
| 5590 | err = ret; |
| 5591 | goto out_unlock; |
| 5592 | } |
| 5593 | } else { |
| 5594 | ret = insert_reserved_file_extent(trans, inode, |
| 5595 | ordered->file_offset, |
| 5596 | ordered->start, |
| 5597 | ordered->disk_len, |
| 5598 | ordered->len, |
| 5599 | ordered->len, |
| 5600 | 0, 0, 0, |
| 5601 | BTRFS_FILE_EXTENT_REG); |
| 5602 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 5603 | ordered->file_offset, ordered->len); |
| 5604 | if (ret) { |
| 5605 | err = ret; |
| 5606 | WARN_ON(1); |
| 5607 | goto out_unlock; |
| 5608 | } |
| 5609 | } |
| 5610 | |
| 5611 | add_pending_csums(trans, inode, ordered->file_offset, &ordered->list); |
| 5612 | btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5613 | btrfs_update_inode(trans, root, inode); |
| 5614 | out_unlock: |
| 5615 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5616 | ordered->file_offset + ordered->len - 1, |
| 5617 | &cached_state, GFP_NOFS); |
| 5618 | out: |
| 5619 | btrfs_delalloc_release_metadata(inode, ordered->len); |
| 5620 | btrfs_end_transaction(trans, root); |
| 5621 | btrfs_put_ordered_extent(ordered); |
| 5622 | btrfs_put_ordered_extent(ordered); |
| 5623 | out_done: |
| 5624 | bio->bi_private = dip->private; |
| 5625 | |
| 5626 | kfree(dip->csums); |
| 5627 | kfree(dip); |
| 5628 | dio_end_io(bio, err); |
| 5629 | } |
| 5630 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5631 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 5632 | struct bio *bio, int mirror_num, |
| 5633 | unsigned long bio_flags, u64 offset) |
| 5634 | { |
| 5635 | int ret; |
| 5636 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5637 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
| 5638 | BUG_ON(ret); |
| 5639 | return 0; |
| 5640 | } |
| 5641 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5642 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 5643 | loff_t file_offset) |
| 5644 | { |
| 5645 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5646 | struct btrfs_dio_private *dip; |
| 5647 | struct bio_vec *bvec = bio->bi_io_vec; |
| 5648 | u64 start; |
| 5649 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5650 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5651 | int ret = 0; |
| 5652 | |
| 5653 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 5654 | |
| 5655 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 5656 | if (!dip) { |
| 5657 | ret = -ENOMEM; |
| 5658 | goto free_ordered; |
| 5659 | } |
| 5660 | dip->csums = NULL; |
| 5661 | |
| 5662 | if (!skip_sum) { |
| 5663 | dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); |
| 5664 | if (!dip->csums) { |
| 5665 | ret = -ENOMEM; |
| 5666 | goto free_ordered; |
| 5667 | } |
| 5668 | } |
| 5669 | |
| 5670 | dip->private = bio->bi_private; |
| 5671 | dip->inode = inode; |
| 5672 | dip->logical_offset = file_offset; |
| 5673 | |
| 5674 | start = dip->logical_offset; |
| 5675 | dip->bytes = 0; |
| 5676 | do { |
| 5677 | dip->bytes += bvec->bv_len; |
| 5678 | bvec++; |
| 5679 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 5680 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5681 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5682 | bio->bi_private = dip; |
| 5683 | |
| 5684 | if (write) |
| 5685 | bio->bi_end_io = btrfs_endio_direct_write; |
| 5686 | else |
| 5687 | bio->bi_end_io = btrfs_endio_direct_read; |
| 5688 | |
| 5689 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 5690 | if (ret) |
| 5691 | goto out_err; |
| 5692 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5693 | if (write && !skip_sum) { |
| 5694 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
| 5695 | inode, rw, bio, 0, 0, |
| 5696 | dip->logical_offset, |
| 5697 | __btrfs_submit_bio_start_direct_io, |
| 5698 | __btrfs_submit_bio_done); |
| 5699 | if (ret) |
| 5700 | goto out_err; |
| 5701 | return; |
| 5702 | } else if (!skip_sum) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5703 | btrfs_lookup_bio_sums_dio(root, inode, bio, |
| 5704 | dip->logical_offset, dip->csums); |
| 5705 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5706 | ret = btrfs_map_bio(root, rw, bio, 0, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5707 | if (ret) |
| 5708 | goto out_err; |
| 5709 | return; |
| 5710 | out_err: |
| 5711 | kfree(dip->csums); |
| 5712 | kfree(dip); |
| 5713 | free_ordered: |
| 5714 | /* |
| 5715 | * If this is a write, we need to clean up the reserved space and kill |
| 5716 | * the ordered extent. |
| 5717 | */ |
| 5718 | if (write) { |
| 5719 | struct btrfs_ordered_extent *ordered; |
| 5720 | ordered = btrfs_lookup_ordered_extent(inode, |
| 5721 | dip->logical_offset); |
| 5722 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 5723 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 5724 | btrfs_free_reserved_extent(root, ordered->start, |
| 5725 | ordered->disk_len); |
| 5726 | btrfs_put_ordered_extent(ordered); |
| 5727 | btrfs_put_ordered_extent(ordered); |
| 5728 | } |
| 5729 | bio_endio(bio, ret); |
| 5730 | } |
| 5731 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 5732 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 5733 | const struct iovec *iov, loff_t offset, |
| 5734 | unsigned long nr_segs) |
| 5735 | { |
| 5736 | int seg; |
| 5737 | size_t size; |
| 5738 | unsigned long addr; |
| 5739 | unsigned blocksize_mask = root->sectorsize - 1; |
| 5740 | ssize_t retval = -EINVAL; |
| 5741 | loff_t end = offset; |
| 5742 | |
| 5743 | if (offset & blocksize_mask) |
| 5744 | goto out; |
| 5745 | |
| 5746 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 5747 | for (seg = 0; seg < nr_segs; seg++) { |
| 5748 | addr = (unsigned long)iov[seg].iov_base; |
| 5749 | size = iov[seg].iov_len; |
| 5750 | end += size; |
| 5751 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
| 5752 | goto out; |
| 5753 | } |
| 5754 | retval = 0; |
| 5755 | out: |
| 5756 | return retval; |
| 5757 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 5758 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 5759 | const struct iovec *iov, loff_t offset, |
| 5760 | unsigned long nr_segs) |
| 5761 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5762 | struct file *file = iocb->ki_filp; |
| 5763 | struct inode *inode = file->f_mapping->host; |
| 5764 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5765 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5766 | u64 lockstart, lockend; |
| 5767 | ssize_t ret; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5768 | int writing = rw & WRITE; |
| 5769 | int write_bits = 0; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 5770 | size_t count = iov_length(iov, nr_segs); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5771 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 5772 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
| 5773 | offset, nr_segs)) { |
| 5774 | return 0; |
| 5775 | } |
| 5776 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5777 | lockstart = offset; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 5778 | lockend = offset + count - 1; |
| 5779 | |
| 5780 | if (writing) { |
| 5781 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 5782 | if (ret) |
| 5783 | goto out; |
| 5784 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5785 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5786 | while (1) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5787 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 5788 | 0, &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5789 | /* |
| 5790 | * We're concerned with the entire range that we're going to be |
| 5791 | * doing DIO to, so we need to make sure theres no ordered |
| 5792 | * extents in this range. |
| 5793 | */ |
| 5794 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 5795 | lockend - lockstart + 1); |
| 5796 | if (!ordered) |
| 5797 | break; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5798 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 5799 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5800 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 5801 | btrfs_put_ordered_extent(ordered); |
| 5802 | cond_resched(); |
| 5803 | } |
| 5804 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5805 | /* |
| 5806 | * we don't use btrfs_set_extent_delalloc because we don't want |
| 5807 | * the dirty or uptodate bits |
| 5808 | */ |
| 5809 | if (writing) { |
| 5810 | write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING; |
| 5811 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 5812 | EXTENT_DELALLOC, 0, NULL, &cached_state, |
| 5813 | GFP_NOFS); |
| 5814 | if (ret) { |
| 5815 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 5816 | lockend, EXTENT_LOCKED | write_bits, |
| 5817 | 1, 0, &cached_state, GFP_NOFS); |
| 5818 | goto out; |
| 5819 | } |
| 5820 | } |
| 5821 | |
| 5822 | free_extent_state(cached_state); |
| 5823 | cached_state = NULL; |
| 5824 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 5825 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 5826 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 5827 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
| 5828 | btrfs_submit_direct, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5829 | |
| 5830 | if (ret < 0 && ret != -EIOCBQUEUED) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5831 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset, |
| 5832 | offset + iov_length(iov, nr_segs) - 1, |
| 5833 | EXTENT_LOCKED | write_bits, 1, 0, |
| 5834 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5835 | } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) { |
| 5836 | /* |
| 5837 | * We're falling back to buffered, unlock the section we didn't |
| 5838 | * do IO on. |
| 5839 | */ |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5840 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret, |
| 5841 | offset + iov_length(iov, nr_segs) - 1, |
| 5842 | EXTENT_LOCKED | write_bits, 1, 0, |
| 5843 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5844 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5845 | out: |
| 5846 | free_extent_state(cached_state); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5847 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 5848 | } |
| 5849 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5850 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 5851 | __u64 start, __u64 len) |
| 5852 | { |
| 5853 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent); |
| 5854 | } |
| 5855 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5856 | int btrfs_readpage(struct file *file, struct page *page) |
| 5857 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5858 | struct extent_io_tree *tree; |
| 5859 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5860 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5861 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5862 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5863 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 5864 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5865 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5866 | |
| 5867 | |
| 5868 | if (current->flags & PF_MEMALLOC) { |
| 5869 | redirty_page_for_writepage(wbc, page); |
| 5870 | unlock_page(page); |
| 5871 | return 0; |
| 5872 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5873 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5874 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 5875 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5876 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5877 | int btrfs_writepages(struct address_space *mapping, |
| 5878 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 5879 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5880 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5881 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5882 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 5883 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 5884 | } |
| 5885 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 5886 | static int |
| 5887 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 5888 | struct list_head *pages, unsigned nr_pages) |
| 5889 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5890 | struct extent_io_tree *tree; |
| 5891 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 5892 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 5893 | btrfs_get_extent); |
| 5894 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5895 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5896 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5897 | struct extent_io_tree *tree; |
| 5898 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5899 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5900 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5901 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 5902 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5903 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5904 | if (ret == 1) { |
| 5905 | ClearPagePrivate(page); |
| 5906 | set_page_private(page, 0); |
| 5907 | page_cache_release(page); |
| 5908 | } |
| 5909 | return ret; |
| 5910 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5911 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5912 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 5913 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 5914 | if (PageWriteback(page) || PageDirty(page)) |
| 5915 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 5916 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5917 | } |
| 5918 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5919 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 5920 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5921 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5922 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5923 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5924 | u64 page_start = page_offset(page); |
| 5925 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5926 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 5927 | |
| 5928 | /* |
| 5929 | * we have the page locked, so new writeback can't start, |
| 5930 | * and the dirty bit won't be cleared while we are here. |
| 5931 | * |
| 5932 | * Wait for IO on this page so that we can safely clear |
| 5933 | * the PagePrivate2 bit and do ordered accounting |
| 5934 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5935 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 5936 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5937 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5938 | if (offset) { |
| 5939 | btrfs_releasepage(page, GFP_NOFS); |
| 5940 | return; |
| 5941 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5942 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 5943 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5944 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 5945 | page_offset(page)); |
| 5946 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5947 | /* |
| 5948 | * IO on this page will never be started, so we need |
| 5949 | * to account for any ordered extents now |
| 5950 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5951 | clear_extent_bit(tree, page_start, page_end, |
| 5952 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5953 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5954 | &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 5955 | /* |
| 5956 | * whoever cleared the private bit is responsible |
| 5957 | * for the finish_ordered_io |
| 5958 | */ |
| 5959 | if (TestClearPagePrivate2(page)) { |
| 5960 | btrfs_finish_ordered_io(page->mapping->host, |
| 5961 | page_start, page_end); |
| 5962 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5963 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5964 | cached_state = NULL; |
| 5965 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 5966 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5967 | } |
| 5968 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5969 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5970 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5971 | __btrfs_releasepage(page, GFP_NOFS); |
| 5972 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 5973 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 5974 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 5975 | ClearPagePrivate(page); |
| 5976 | set_page_private(page, 0); |
| 5977 | page_cache_release(page); |
| 5978 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5979 | } |
| 5980 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5981 | /* |
| 5982 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 5983 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 5984 | * be careful to check for EOF conditions here. We set the page up correctly |
| 5985 | * for a written page which means we get ENOSPC checking when writing into |
| 5986 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 5987 | * support these features. |
| 5988 | * |
| 5989 | * We are not allowed to take the i_mutex here so we have to play games to |
| 5990 | * protect against truncate races as the page could now be beyond EOF. Because |
| 5991 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 5992 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 5993 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 5994 | * unlock the page. |
| 5995 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5996 | 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] | 5997 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5998 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 5999 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6000 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6001 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 6002 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6003 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6004 | char *kaddr; |
| 6005 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6006 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6007 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6008 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6009 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6010 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6011 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6012 | if (ret) { |
| 6013 | if (ret == -ENOMEM) |
| 6014 | ret = VM_FAULT_OOM; |
| 6015 | else /* -ENOSPC, -EIO, etc */ |
| 6016 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6017 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6018 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6019 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6020 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6021 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6022 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6023 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6024 | page_start = page_offset(page); |
| 6025 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6026 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6027 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6028 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6029 | /* page got truncated out from underneath us */ |
| 6030 | goto out_unlock; |
| 6031 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6032 | wait_on_page_writeback(page); |
| 6033 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6034 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 6035 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6036 | set_page_extent_mapped(page); |
| 6037 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6038 | /* |
| 6039 | * we can't set the delalloc bits if there are pending ordered |
| 6040 | * extents. Drop our locks and wait for them to finish |
| 6041 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6042 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6043 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6044 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6045 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6046 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6047 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6048 | btrfs_put_ordered_extent(ordered); |
| 6049 | goto again; |
| 6050 | } |
| 6051 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6052 | /* |
| 6053 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 6054 | * if it was already dirty, so for space accounting reasons we need to |
| 6055 | * clear any delalloc bits for the range we are fixing to save. There |
| 6056 | * is probably a better way to do this, but for now keep consistent with |
| 6057 | * prepare_pages in the normal write path. |
| 6058 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6059 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6060 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6061 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6062 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6063 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 6064 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6065 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6066 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6067 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6068 | ret = VM_FAULT_SIGBUS; |
| 6069 | goto out_unlock; |
| 6070 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6071 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6072 | |
| 6073 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6074 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6075 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6076 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6077 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6078 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6079 | if (zero_start != PAGE_CACHE_SIZE) { |
| 6080 | kaddr = kmap(page); |
| 6081 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 6082 | flush_dcache_page(page); |
| 6083 | kunmap(page); |
| 6084 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6085 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6086 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6087 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6088 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6089 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 6090 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 6091 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6092 | 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] | 6093 | |
| 6094 | out_unlock: |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6095 | if (!ret) |
| 6096 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6097 | unlock_page(page); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6098 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6099 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6100 | return ret; |
| 6101 | } |
| 6102 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6103 | static void btrfs_truncate(struct inode *inode) |
| 6104 | { |
| 6105 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6106 | int ret; |
| 6107 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6108 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6109 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6110 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6111 | if (!S_ISREG(inode->i_mode)) { |
| 6112 | WARN_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6113 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6114 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6115 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 6116 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 6117 | if (ret) |
| 6118 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6119 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6120 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6121 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6122 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6123 | trans = btrfs_start_transaction(root, 0); |
| 6124 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6125 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6126 | trans->block_rsv = root->orphan_block_rsv; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6127 | |
| 6128 | /* |
| 6129 | * setattr is responsible for setting the ordered_data_close flag, |
| 6130 | * but that is only tested during the last file release. That |
| 6131 | * could happen well after the next commit, leaving a great big |
| 6132 | * window where new writes may get lost if someone chooses to write |
| 6133 | * to this file after truncating to zero |
| 6134 | * |
| 6135 | * The inode doesn't have any dirty data here, and so if we commit |
| 6136 | * this is a noop. If someone immediately starts writing to the inode |
| 6137 | * it is very likely we'll catch some of their writes in this |
| 6138 | * transaction, and the commit will find this file on the ordered |
| 6139 | * data list with good things to send down. |
| 6140 | * |
| 6141 | * This is a best effort solution, there is still a window where |
| 6142 | * using truncate to replace the contents of the file will |
| 6143 | * end up with a zero length file after a crash. |
| 6144 | */ |
| 6145 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 6146 | btrfs_add_ordered_operation(trans, root, inode); |
| 6147 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6148 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6149 | if (!trans) { |
| 6150 | trans = btrfs_start_transaction(root, 0); |
| 6151 | BUG_ON(IS_ERR(trans)); |
| 6152 | btrfs_set_trans_block_group(trans, inode); |
| 6153 | trans->block_rsv = root->orphan_block_rsv; |
| 6154 | } |
| 6155 | |
| 6156 | ret = btrfs_block_rsv_check(trans, root, |
| 6157 | root->orphan_block_rsv, 0, 5); |
| 6158 | if (ret) { |
| 6159 | BUG_ON(ret != -EAGAIN); |
| 6160 | ret = btrfs_commit_transaction(trans, root); |
| 6161 | BUG_ON(ret); |
| 6162 | trans = NULL; |
| 6163 | continue; |
| 6164 | } |
| 6165 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6166 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 6167 | inode->i_size, |
| 6168 | BTRFS_EXTENT_DATA_KEY); |
| 6169 | if (ret != -EAGAIN) |
| 6170 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6171 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6172 | ret = btrfs_update_inode(trans, root, inode); |
| 6173 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6174 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6175 | nr = trans->blocks_used; |
| 6176 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6177 | trans = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6178 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6179 | } |
| 6180 | |
| 6181 | if (ret == 0 && inode->i_nlink > 0) { |
| 6182 | ret = btrfs_orphan_del(trans, inode); |
| 6183 | BUG_ON(ret); |
| 6184 | } |
| 6185 | |
| 6186 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6187 | BUG_ON(ret); |
| 6188 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6189 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6190 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6191 | BUG_ON(ret); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6192 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6193 | } |
| 6194 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6195 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6196 | * create a new subvolume directory/inode (helper for the ioctl). |
| 6197 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6198 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6199 | struct btrfs_root *new_root, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6200 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6201 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6202 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6203 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6204 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6205 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6206 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6207 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6208 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 6209 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6210 | inode->i_op = &btrfs_dir_inode_operations; |
| 6211 | inode->i_fop = &btrfs_dir_file_operations; |
| 6212 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6213 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6214 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6215 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6216 | err = btrfs_update_inode(trans, new_root, inode); |
| 6217 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6218 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6219 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6220 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6221 | } |
| 6222 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6223 | /* helper function for file defrag and space balancing. This |
| 6224 | * forces readahead on a given range of bytes in an inode |
| 6225 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6226 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6227 | struct file_ra_state *ra, struct file *file, |
| 6228 | pgoff_t offset, pgoff_t last_index) |
| 6229 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6230 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6231 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6232 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 6233 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6234 | } |
| 6235 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6236 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 6237 | { |
| 6238 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6239 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6240 | |
| 6241 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 6242 | if (!ei) |
| 6243 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6244 | |
| 6245 | ei->root = NULL; |
| 6246 | ei->space_info = NULL; |
| 6247 | ei->generation = 0; |
| 6248 | ei->sequence = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 6249 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6250 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6251 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6252 | ei->delalloc_bytes = 0; |
| 6253 | ei->reserved_bytes = 0; |
| 6254 | ei->disk_i_size = 0; |
| 6255 | ei->flags = 0; |
| 6256 | ei->index_cnt = (u64)-1; |
| 6257 | ei->last_unlink_trans = 0; |
| 6258 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6259 | spin_lock_init(&ei->accounting_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6260 | atomic_set(&ei->outstanding_extents, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6261 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6262 | |
| 6263 | ei->ordered_data_close = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6264 | ei->orphan_meta_reserved = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6265 | ei->dummy_inode = 0; |
| 6266 | ei->force_compress = 0; |
| 6267 | |
| 6268 | inode = &ei->vfs_inode; |
| 6269 | extent_map_tree_init(&ei->extent_tree, GFP_NOFS); |
| 6270 | extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS); |
| 6271 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS); |
| 6272 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6273 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6274 | INIT_LIST_HEAD(&ei->i_orphan); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6275 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6276 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6277 | RB_CLEAR_NODE(&ei->rb_node); |
| 6278 | |
| 6279 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6280 | } |
| 6281 | |
| 6282 | void btrfs_destroy_inode(struct inode *inode) |
| 6283 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6284 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6285 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6286 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6287 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 6288 | WARN_ON(inode->i_data.nrpages); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6289 | WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents)); |
| 6290 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6291 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6292 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6293 | * This can happen where we create an inode, but somebody else also |
| 6294 | * created the same inode and we need to destroy the one we already |
| 6295 | * created. |
| 6296 | */ |
| 6297 | if (!root) |
| 6298 | goto free; |
| 6299 | |
| 6300 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6301 | * Make sure we're properly removed from the ordered operation |
| 6302 | * lists. |
| 6303 | */ |
| 6304 | smp_mb(); |
| 6305 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 6306 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 6307 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 6308 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 6309 | } |
| 6310 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6311 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6312 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6313 | printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", |
| 6314 | inode->i_ino); |
| 6315 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6316 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6317 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6318 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6319 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6320 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 6321 | if (!ordered) |
| 6322 | break; |
| 6323 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6324 | printk(KERN_ERR "btrfs found ordered " |
| 6325 | "extent %llu %llu on inode cleanup\n", |
| 6326 | (unsigned long long)ordered->file_offset, |
| 6327 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6328 | btrfs_remove_ordered_extent(inode, ordered); |
| 6329 | btrfs_put_ordered_extent(ordered); |
| 6330 | btrfs_put_ordered_extent(ordered); |
| 6331 | } |
| 6332 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6333 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6334 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6335 | free: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6336 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 6337 | } |
| 6338 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6339 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6340 | { |
| 6341 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6342 | |
| 6343 | if (btrfs_root_refs(&root->root_item) == 0) |
| 6344 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6345 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6346 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6347 | } |
| 6348 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 6349 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6350 | { |
| 6351 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 6352 | |
| 6353 | inode_init_once(&ei->vfs_inode); |
| 6354 | } |
| 6355 | |
| 6356 | void btrfs_destroy_cachep(void) |
| 6357 | { |
| 6358 | if (btrfs_inode_cachep) |
| 6359 | kmem_cache_destroy(btrfs_inode_cachep); |
| 6360 | if (btrfs_trans_handle_cachep) |
| 6361 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 6362 | if (btrfs_transaction_cachep) |
| 6363 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6364 | if (btrfs_path_cachep) |
| 6365 | kmem_cache_destroy(btrfs_path_cachep); |
| 6366 | } |
| 6367 | |
| 6368 | int btrfs_init_cachep(void) |
| 6369 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6370 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 6371 | sizeof(struct btrfs_inode), 0, |
| 6372 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6373 | if (!btrfs_inode_cachep) |
| 6374 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6375 | |
| 6376 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 6377 | sizeof(struct btrfs_trans_handle), 0, |
| 6378 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6379 | if (!btrfs_trans_handle_cachep) |
| 6380 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6381 | |
| 6382 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 6383 | sizeof(struct btrfs_transaction), 0, |
| 6384 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6385 | if (!btrfs_transaction_cachep) |
| 6386 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6387 | |
| 6388 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 6389 | sizeof(struct btrfs_path), 0, |
| 6390 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6391 | if (!btrfs_path_cachep) |
| 6392 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6393 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6394 | return 0; |
| 6395 | fail: |
| 6396 | btrfs_destroy_cachep(); |
| 6397 | return -ENOMEM; |
| 6398 | } |
| 6399 | |
| 6400 | static int btrfs_getattr(struct vfsmount *mnt, |
| 6401 | struct dentry *dentry, struct kstat *stat) |
| 6402 | { |
| 6403 | struct inode *inode = dentry->d_inode; |
| 6404 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6405 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 6406 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6407 | stat->blocks = (inode_get_bytes(inode) + |
| 6408 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6409 | return 0; |
| 6410 | } |
| 6411 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6412 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 6413 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6414 | { |
| 6415 | struct btrfs_trans_handle *trans; |
| 6416 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6417 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6418 | struct inode *new_inode = new_dentry->d_inode; |
| 6419 | struct inode *old_inode = old_dentry->d_inode; |
| 6420 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6421 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6422 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6423 | int ret; |
| 6424 | |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6425 | if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 6426 | return -EPERM; |
| 6427 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6428 | /* we only allow rename subvolume link between subvolumes */ |
| 6429 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6430 | return -EXDEV; |
| 6431 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6432 | if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 6433 | (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) |
| 6434 | return -ENOTEMPTY; |
| 6435 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6436 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6437 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6438 | return -ENOTEMPTY; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6439 | /* |
| 6440 | * we're using rename to replace one file with another. |
| 6441 | * and the replacement file is large. Start IO on it now so |
| 6442 | * we don't add too much work to the end of the transaction |
| 6443 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 6444 | 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] | 6445 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 6446 | filemap_flush(old_inode->i_mapping); |
| 6447 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6448 | /* close the racy window with snapshot create/destroy ioctl */ |
| 6449 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 6450 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6451 | /* |
| 6452 | * We want to reserve the absolute worst case amount of items. So if |
| 6453 | * both inodes are subvols and we need to unlink them then that would |
| 6454 | * require 4 item modifications, but if they are both normal inodes it |
| 6455 | * would require 5 item modifications, so we'll assume their normal |
| 6456 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 6457 | * should cover the worst case number of items we'll modify. |
| 6458 | */ |
| 6459 | trans = btrfs_start_transaction(root, 20); |
| 6460 | if (IS_ERR(trans)) |
| 6461 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6462 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6463 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6464 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6465 | if (dest != root) |
| 6466 | btrfs_record_root_in_trans(trans, dest); |
| 6467 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6468 | ret = btrfs_set_inode_index(new_dir, &index); |
| 6469 | if (ret) |
| 6470 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6471 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6472 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6473 | /* force full log commit if subvolume involved. */ |
| 6474 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 6475 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6476 | ret = btrfs_insert_inode_ref(trans, dest, |
| 6477 | new_dentry->d_name.name, |
| 6478 | new_dentry->d_name.len, |
| 6479 | old_inode->i_ino, |
| 6480 | new_dir->i_ino, index); |
| 6481 | if (ret) |
| 6482 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6483 | /* |
| 6484 | * this is an ugly little race, but the rename is required |
| 6485 | * to make sure that if we crash, the inode is either at the |
| 6486 | * old name or the new one. pinning the log transaction lets |
| 6487 | * us make sure we don't allow a log commit to come in after |
| 6488 | * we unlink the name but before we add the new name back in. |
| 6489 | */ |
| 6490 | btrfs_pin_log_trans(root); |
| 6491 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6492 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6493 | * make sure the inode gets flushed if it is replacing |
| 6494 | * something. |
| 6495 | */ |
| 6496 | if (new_inode && new_inode->i_size && |
| 6497 | old_inode && S_ISREG(old_inode->i_mode)) { |
| 6498 | btrfs_add_ordered_operation(trans, root, old_inode); |
| 6499 | } |
| 6500 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6501 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 6502 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 6503 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6504 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6505 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 6506 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 6507 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6508 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6509 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 6510 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 6511 | old_dentry->d_name.name, |
| 6512 | old_dentry->d_name.len); |
| 6513 | } else { |
| 6514 | btrfs_inc_nlink(old_dentry->d_inode); |
| 6515 | ret = btrfs_unlink_inode(trans, root, old_dir, |
| 6516 | old_dentry->d_inode, |
| 6517 | old_dentry->d_name.name, |
| 6518 | old_dentry->d_name.len); |
| 6519 | } |
| 6520 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6521 | |
| 6522 | if (new_inode) { |
| 6523 | new_inode->i_ctime = CURRENT_TIME; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6524 | if (unlikely(new_inode->i_ino == |
| 6525 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 6526 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 6527 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 6528 | root_objectid, |
| 6529 | new_dentry->d_name.name, |
| 6530 | new_dentry->d_name.len); |
| 6531 | BUG_ON(new_inode->i_nlink == 0); |
| 6532 | } else { |
| 6533 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 6534 | new_dentry->d_inode, |
| 6535 | new_dentry->d_name.name, |
| 6536 | new_dentry->d_name.len); |
| 6537 | } |
| 6538 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6539 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6540 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6541 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6542 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6543 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6544 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6545 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 6546 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6547 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6548 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6549 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6550 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 6551 | btrfs_log_new_name(trans, old_inode, old_dir, |
| 6552 | new_dentry->d_parent); |
| 6553 | btrfs_end_log_trans(root); |
| 6554 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6555 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 6556 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6557 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6558 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 6559 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6560 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6561 | return ret; |
| 6562 | } |
| 6563 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6564 | /* |
| 6565 | * some fairly slow code that needs optimization. This walks the list |
| 6566 | * of all the inodes with pending delalloc and forces them to disk. |
| 6567 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 6568 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6569 | { |
| 6570 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 6571 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6572 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6573 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 6574 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 6575 | return -EROFS; |
| 6576 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6577 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6578 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6579 | binode = list_entry(head->next, struct btrfs_inode, |
| 6580 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6581 | inode = igrab(&binode->vfs_inode); |
| 6582 | if (!inode) |
| 6583 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6584 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6585 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6586 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 6587 | if (delay_iput) |
| 6588 | btrfs_add_delayed_iput(inode); |
| 6589 | else |
| 6590 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6591 | } |
| 6592 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6593 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6594 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6595 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6596 | |
| 6597 | /* the filemap_flush will queue IO into the worker threads, but |
| 6598 | * we have to make sure the IO is actually started and that |
| 6599 | * ordered extents get created before we return |
| 6600 | */ |
| 6601 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6602 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6603 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6604 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6605 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 6606 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6607 | } |
| 6608 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6609 | return 0; |
| 6610 | } |
| 6611 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6612 | int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput) |
| 6613 | { |
| 6614 | struct btrfs_inode *binode; |
| 6615 | struct inode *inode = NULL; |
| 6616 | |
| 6617 | spin_lock(&root->fs_info->delalloc_lock); |
| 6618 | while (!list_empty(&root->fs_info->delalloc_inodes)) { |
| 6619 | binode = list_entry(root->fs_info->delalloc_inodes.next, |
| 6620 | struct btrfs_inode, delalloc_inodes); |
| 6621 | inode = igrab(&binode->vfs_inode); |
| 6622 | if (inode) { |
| 6623 | list_move_tail(&binode->delalloc_inodes, |
| 6624 | &root->fs_info->delalloc_inodes); |
| 6625 | break; |
| 6626 | } |
| 6627 | |
| 6628 | list_del_init(&binode->delalloc_inodes); |
| 6629 | cond_resched_lock(&root->fs_info->delalloc_lock); |
| 6630 | } |
| 6631 | spin_unlock(&root->fs_info->delalloc_lock); |
| 6632 | |
| 6633 | if (inode) { |
| 6634 | write_inode_now(inode, 0); |
| 6635 | if (delay_iput) |
| 6636 | btrfs_add_delayed_iput(inode); |
| 6637 | else |
| 6638 | iput(inode); |
| 6639 | return 1; |
| 6640 | } |
| 6641 | return 0; |
| 6642 | } |
| 6643 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6644 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 6645 | const char *symname) |
| 6646 | { |
| 6647 | struct btrfs_trans_handle *trans; |
| 6648 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6649 | struct btrfs_path *path; |
| 6650 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6651 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6652 | int err; |
| 6653 | int drop_inode = 0; |
| 6654 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6655 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6656 | int name_len; |
| 6657 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6658 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6659 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6660 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6661 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6662 | |
| 6663 | name_len = strlen(symname) + 1; |
| 6664 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 6665 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6666 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6667 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 6668 | if (err) |
| 6669 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6670 | /* |
| 6671 | * 2 items for inode item and ref |
| 6672 | * 2 items for dir items |
| 6673 | * 1 item for xattr if selinux is on |
| 6674 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6675 | trans = btrfs_start_transaction(root, 5); |
| 6676 | if (IS_ERR(trans)) |
| 6677 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6678 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6679 | btrfs_set_trans_block_group(trans, dir); |
| 6680 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6681 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6682 | dentry->d_name.len, |
| 6683 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6684 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 6685 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6686 | err = PTR_ERR(inode); |
| 6687 | if (IS_ERR(inode)) |
| 6688 | goto out_unlock; |
| 6689 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 6690 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6691 | if (err) { |
| 6692 | drop_inode = 1; |
| 6693 | goto out_unlock; |
| 6694 | } |
| 6695 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6696 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6697 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6698 | if (err) |
| 6699 | drop_inode = 1; |
| 6700 | else { |
| 6701 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 6702 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6703 | inode->i_fop = &btrfs_file_operations; |
| 6704 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6705 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6706 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6707 | btrfs_update_inode_block_group(trans, inode); |
| 6708 | btrfs_update_inode_block_group(trans, dir); |
| 6709 | if (drop_inode) |
| 6710 | goto out_unlock; |
| 6711 | |
| 6712 | path = btrfs_alloc_path(); |
| 6713 | BUG_ON(!path); |
| 6714 | key.objectid = inode->i_ino; |
| 6715 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6716 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 6717 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 6718 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 6719 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6720 | if (err) { |
| 6721 | drop_inode = 1; |
| 6722 | goto out_unlock; |
| 6723 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6724 | leaf = path->nodes[0]; |
| 6725 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 6726 | struct btrfs_file_extent_item); |
| 6727 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 6728 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6729 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6730 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 6731 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 6732 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 6733 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 6734 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6735 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6736 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 6737 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6738 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6739 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6740 | inode->i_op = &btrfs_symlink_inode_operations; |
| 6741 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 6742 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6743 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6744 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6745 | err = btrfs_update_inode(trans, root, inode); |
| 6746 | if (err) |
| 6747 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6748 | |
| 6749 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6750 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 6751 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6752 | if (drop_inode) { |
| 6753 | inode_dec_link_count(inode); |
| 6754 | iput(inode); |
| 6755 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6756 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6757 | return err; |
| 6758 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6759 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6760 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 6761 | u64 start, u64 num_bytes, u64 min_size, |
| 6762 | loff_t actual_len, u64 *alloc_hint) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6763 | { |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 6764 | struct btrfs_trans_handle *trans; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6765 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6766 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6767 | u64 cur_offset = start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6768 | int ret = 0; |
| 6769 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6770 | while (num_bytes > 0) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6771 | trans = btrfs_start_transaction(root, 3); |
| 6772 | if (IS_ERR(trans)) { |
| 6773 | ret = PTR_ERR(trans); |
| 6774 | break; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6775 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 6776 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6777 | ret = btrfs_reserve_extent(trans, root, num_bytes, min_size, |
| 6778 | 0, *alloc_hint, (u64)-1, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 6779 | if (ret) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6780 | btrfs_end_transaction(trans, root); |
| 6781 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 6782 | } |
| 6783 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6784 | ret = insert_reserved_file_extent(trans, inode, |
| 6785 | cur_offset, ins.objectid, |
| 6786 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 6787 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6788 | BTRFS_FILE_EXTENT_PREALLOC); |
| 6789 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 6790 | btrfs_drop_extent_cache(inode, cur_offset, |
| 6791 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 6792 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6793 | num_bytes -= ins.offset; |
| 6794 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6795 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 6796 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6797 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6798 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6799 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6800 | (actual_len > inode->i_size) && |
| 6801 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 6802 | if (cur_offset > actual_len) |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6803 | i_size_write(inode, actual_len); |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 6804 | else |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6805 | i_size_write(inode, cur_offset); |
| 6806 | i_size_write(inode, cur_offset); |
| 6807 | btrfs_ordered_update_i_size(inode, cur_offset, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 6808 | } |
| 6809 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6810 | ret = btrfs_update_inode(trans, root, inode); |
| 6811 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6812 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 6813 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 6814 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6815 | return ret; |
| 6816 | } |
| 6817 | |
| 6818 | static long btrfs_fallocate(struct inode *inode, int mode, |
| 6819 | loff_t offset, loff_t len) |
| 6820 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6821 | struct extent_state *cached_state = NULL; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6822 | u64 cur_offset; |
| 6823 | u64 last_byte; |
| 6824 | u64 alloc_start; |
| 6825 | u64 alloc_end; |
| 6826 | u64 alloc_hint = 0; |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 6827 | u64 locked_end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6828 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 6829 | struct extent_map *em; |
| 6830 | int ret; |
| 6831 | |
| 6832 | alloc_start = offset & ~mask; |
| 6833 | alloc_end = (offset + len + mask) & ~mask; |
| 6834 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 6835 | /* |
| 6836 | * wait for ordered IO before we have any locks. We'll loop again |
| 6837 | * below with the locks held. |
| 6838 | */ |
| 6839 | btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start); |
| 6840 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6841 | mutex_lock(&inode->i_mutex); |
| 6842 | if (alloc_start > inode->i_size) { |
| 6843 | ret = btrfs_cont_expand(inode, alloc_start); |
| 6844 | if (ret) |
| 6845 | goto out; |
| 6846 | } |
| 6847 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6848 | ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start); |
Josef Bacik | a970b0a | 2009-06-27 21:07:34 -0400 | [diff] [blame] | 6849 | if (ret) |
| 6850 | goto out; |
| 6851 | |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 6852 | locked_end = alloc_end - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6853 | while (1) { |
| 6854 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 6855 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 6856 | /* the extent lock is ordered inside the running |
| 6857 | * transaction |
| 6858 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6859 | lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start, |
| 6860 | locked_end, 0, &cached_state, GFP_NOFS); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6861 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 6862 | alloc_end - 1); |
| 6863 | if (ordered && |
| 6864 | ordered->file_offset + ordered->len > alloc_start && |
| 6865 | ordered->file_offset < alloc_end) { |
| 6866 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6867 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 6868 | alloc_start, locked_end, |
| 6869 | &cached_state, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 6870 | /* |
| 6871 | * we can't wait on the range with the transaction |
| 6872 | * running or with the extent lock held |
| 6873 | */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6874 | btrfs_wait_ordered_range(inode, alloc_start, |
| 6875 | alloc_end - alloc_start); |
| 6876 | } else { |
| 6877 | if (ordered) |
| 6878 | btrfs_put_ordered_extent(ordered); |
| 6879 | break; |
| 6880 | } |
| 6881 | } |
| 6882 | |
| 6883 | cur_offset = alloc_start; |
| 6884 | while (1) { |
| 6885 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 6886 | alloc_end - cur_offset, 0); |
| 6887 | BUG_ON(IS_ERR(em) || !em); |
| 6888 | last_byte = min(extent_map_end(em), alloc_end); |
| 6889 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 6890 | if (em->block_start == EXTENT_MAP_HOLE || |
| 6891 | (cur_offset >= inode->i_size && |
| 6892 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
Josef Bacik | 8360977 | 2010-06-07 18:26:37 +0000 | [diff] [blame] | 6893 | ret = btrfs_prealloc_file_range(inode, mode, cur_offset, |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6894 | last_byte - cur_offset, |
| 6895 | 1 << inode->i_blkbits, |
| 6896 | offset + len, |
| 6897 | &alloc_hint); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6898 | if (ret < 0) { |
| 6899 | free_extent_map(em); |
| 6900 | break; |
| 6901 | } |
| 6902 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6903 | free_extent_map(em); |
| 6904 | |
| 6905 | cur_offset = last_byte; |
| 6906 | if (cur_offset >= alloc_end) { |
| 6907 | ret = 0; |
| 6908 | break; |
| 6909 | } |
| 6910 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6911 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
| 6912 | &cached_state, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 6913 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6914 | btrfs_free_reserved_data_space(inode, alloc_end - alloc_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6915 | out: |
| 6916 | mutex_unlock(&inode->i_mutex); |
| 6917 | return ret; |
| 6918 | } |
| 6919 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6920 | static int btrfs_set_page_dirty(struct page *page) |
| 6921 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6922 | return __set_page_dirty_nobuffers(page); |
| 6923 | } |
| 6924 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 6925 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6926 | { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6927 | if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6928 | return -EACCES; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6929 | return generic_permission(inode, mask, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6930 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6931 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6932 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6933 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6934 | .lookup = btrfs_lookup, |
| 6935 | .create = btrfs_create, |
| 6936 | .unlink = btrfs_unlink, |
| 6937 | .link = btrfs_link, |
| 6938 | .mkdir = btrfs_mkdir, |
| 6939 | .rmdir = btrfs_rmdir, |
| 6940 | .rename = btrfs_rename, |
| 6941 | .symlink = btrfs_symlink, |
| 6942 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6943 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6944 | .setxattr = btrfs_setxattr, |
| 6945 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 6946 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6947 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6948 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6949 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6950 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6951 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6952 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6953 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6954 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 6955 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6956 | .llseek = generic_file_llseek, |
| 6957 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 6958 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 6959 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6960 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 6961 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6962 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 6963 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6964 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6965 | }; |
| 6966 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6967 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 6968 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 6969 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6970 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 6971 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6972 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6973 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6974 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 6975 | .set_bit_hook = btrfs_set_bit_hook, |
| 6976 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6977 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 6978 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 6979 | }; |
| 6980 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 6981 | /* |
| 6982 | * btrfs doesn't support the bmap operation because swapfiles |
| 6983 | * use bmap to make a mapping of extents in the file. They assume |
| 6984 | * these extents won't change over the life of the file and they |
| 6985 | * use the bmap result to do IO directly to the drive. |
| 6986 | * |
| 6987 | * the btrfs bmap call would return logical addresses that aren't |
| 6988 | * suitable for IO and they also will change frequently as COW |
| 6989 | * operations happen. So, swapfile + btrfs == corruption. |
| 6990 | * |
| 6991 | * For now we're avoiding this by dropping bmap. |
| 6992 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 6993 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6994 | .readpage = btrfs_readpage, |
| 6995 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6996 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6997 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6998 | .sync_page = block_sync_page, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6999 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7000 | .invalidatepage = btrfs_invalidatepage, |
| 7001 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7002 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 7003 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7004 | }; |
| 7005 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7006 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7007 | .readpage = btrfs_readpage, |
| 7008 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 7009 | .invalidatepage = btrfs_invalidatepage, |
| 7010 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7011 | }; |
| 7012 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7013 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7014 | .truncate = btrfs_truncate, |
| 7015 | .getattr = btrfs_getattr, |
| 7016 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7017 | .setxattr = btrfs_setxattr, |
| 7018 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7019 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7020 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7021 | .permission = btrfs_permission, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7022 | .fallocate = btrfs_fallocate, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7023 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7024 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7025 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7026 | .getattr = btrfs_getattr, |
| 7027 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7028 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7029 | .setxattr = btrfs_setxattr, |
| 7030 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7031 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7032 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7033 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7034 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7035 | .readlink = generic_readlink, |
| 7036 | .follow_link = page_follow_link_light, |
| 7037 | .put_link = page_put_link, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7038 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 7039 | .setxattr = btrfs_setxattr, |
| 7040 | .getxattr = btrfs_getxattr, |
| 7041 | .listxattr = btrfs_listxattr, |
| 7042 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7043 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7044 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 7045 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7046 | .d_delete = btrfs_dentry_delete, |
| 7047 | }; |