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 | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 322 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 323 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 324 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 325 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 326 | struct page **pages = NULL; |
| 327 | unsigned long nr_pages; |
| 328 | unsigned long nr_pages_ret = 0; |
| 329 | unsigned long total_compressed = 0; |
| 330 | unsigned long total_in = 0; |
| 331 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 332 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 333 | int i; |
| 334 | int will_compress; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 335 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 336 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 337 | again: |
| 338 | will_compress = 0; |
| 339 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 340 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 341 | |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 342 | /* |
| 343 | * we don't want to send crud past the end of i_size through |
| 344 | * compression, that's just a waste of CPU time. So, if the |
| 345 | * end of the file is before the start of our current |
| 346 | * requested range of bytes, we bail out to the uncompressed |
| 347 | * cleanup code that can deal with all of this. |
| 348 | * |
| 349 | * It isn't really the fastest way to fix things, but this is a |
| 350 | * very uncommon corner. |
| 351 | */ |
| 352 | if (actual_end <= start) |
| 353 | goto cleanup_and_bail_uncompressed; |
| 354 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 355 | total_compressed = actual_end - start; |
| 356 | |
| 357 | /* we want to make sure that amount of ram required to uncompress |
| 358 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 359 | * of a compressed extent to 128k. This is a crucial number |
| 360 | * because it also controls how easily we can spread reads across |
| 361 | * cpus for decompression. |
| 362 | * |
| 363 | * We also want to make sure the amount of IO required to do |
| 364 | * a random read is reasonably small, so we limit the size of |
| 365 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 366 | */ |
| 367 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 368 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 369 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 370 | total_in = 0; |
| 371 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 372 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 373 | /* |
| 374 | * we do compression for mount -o compress and when the |
| 375 | * inode has not been flagged as nocompress. This flag can |
| 376 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 377 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 378 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 379 | (btrfs_test_opt(root, COMPRESS) || |
| 380 | (BTRFS_I(inode)->force_compress))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 381 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 382 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 383 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 384 | ret = btrfs_zlib_compress_pages(inode->i_mapping, start, |
| 385 | total_compressed, pages, |
| 386 | nr_pages, &nr_pages_ret, |
| 387 | &total_in, |
| 388 | &total_compressed, |
| 389 | max_compressed); |
| 390 | |
| 391 | if (!ret) { |
| 392 | unsigned long offset = total_compressed & |
| 393 | (PAGE_CACHE_SIZE - 1); |
| 394 | struct page *page = pages[nr_pages_ret - 1]; |
| 395 | char *kaddr; |
| 396 | |
| 397 | /* zero the tail end of the last page, we might be |
| 398 | * sending it down to disk |
| 399 | */ |
| 400 | if (offset) { |
| 401 | kaddr = kmap_atomic(page, KM_USER0); |
| 402 | memset(kaddr + offset, 0, |
| 403 | PAGE_CACHE_SIZE - offset); |
| 404 | kunmap_atomic(kaddr, KM_USER0); |
| 405 | } |
| 406 | will_compress = 1; |
| 407 | } |
| 408 | } |
| 409 | if (start == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 410 | trans = btrfs_join_transaction(root, 1); |
| 411 | BUG_ON(!trans); |
| 412 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 413 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 414 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 415 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 416 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 417 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 418 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 419 | */ |
| 420 | ret = cow_file_range_inline(trans, root, inode, |
| 421 | start, end, 0, NULL); |
| 422 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 423 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 424 | ret = cow_file_range_inline(trans, root, inode, |
| 425 | start, end, |
| 426 | total_compressed, pages); |
| 427 | } |
| 428 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 429 | /* |
| 430 | * inline extent creation worked, we don't need |
| 431 | * to create any more async work items. Unlock |
| 432 | * and free up our temp pages. |
| 433 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 434 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 435 | &BTRFS_I(inode)->io_tree, |
| 436 | start, end, NULL, |
| 437 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 438 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 439 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 440 | |
| 441 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 442 | goto free_pages_out; |
| 443 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 444 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | if (will_compress) { |
| 448 | /* |
| 449 | * we aren't doing an inline extent round the compressed size |
| 450 | * up to a block size boundary so the allocator does sane |
| 451 | * things |
| 452 | */ |
| 453 | total_compressed = (total_compressed + blocksize - 1) & |
| 454 | ~(blocksize - 1); |
| 455 | |
| 456 | /* |
| 457 | * one last check to make sure the compression is really a |
| 458 | * win, compare the page count read with the blocks on disk |
| 459 | */ |
| 460 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 461 | ~(PAGE_CACHE_SIZE - 1); |
| 462 | if (total_compressed >= total_in) { |
| 463 | will_compress = 0; |
| 464 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 465 | num_bytes = total_in; |
| 466 | } |
| 467 | } |
| 468 | if (!will_compress && pages) { |
| 469 | /* |
| 470 | * the compression code ran but failed to make things smaller, |
| 471 | * free any pages it allocated and our page pointer array |
| 472 | */ |
| 473 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 474 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 475 | page_cache_release(pages[i]); |
| 476 | } |
| 477 | kfree(pages); |
| 478 | pages = NULL; |
| 479 | total_compressed = 0; |
| 480 | nr_pages_ret = 0; |
| 481 | |
| 482 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 483 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 484 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 485 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 486 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 487 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 488 | if (will_compress) { |
| 489 | *num_added += 1; |
| 490 | |
| 491 | /* the async work queues will take care of doing actual |
| 492 | * allocation on disk for these compressed pages, |
| 493 | * and will submit them to the elevator. |
| 494 | */ |
| 495 | add_async_extent(async_cow, start, num_bytes, |
| 496 | total_compressed, pages, nr_pages_ret); |
| 497 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 498 | if (start + num_bytes < end && start + num_bytes < actual_end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 499 | start += num_bytes; |
| 500 | pages = NULL; |
| 501 | cond_resched(); |
| 502 | goto again; |
| 503 | } |
| 504 | } else { |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 505 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 506 | /* |
| 507 | * No compression, but we still need to write the pages in |
| 508 | * the file we've been given so far. redirty the locked |
| 509 | * page if it corresponds to our extent and set things up |
| 510 | * for the async work queue to run cow_file_range to do |
| 511 | * the normal delalloc dance |
| 512 | */ |
| 513 | if (page_offset(locked_page) >= start && |
| 514 | page_offset(locked_page) <= end) { |
| 515 | __set_page_dirty_nobuffers(locked_page); |
| 516 | /* unlocked later on in the async handlers */ |
| 517 | } |
| 518 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0); |
| 519 | *num_added += 1; |
| 520 | } |
| 521 | |
| 522 | out: |
| 523 | return 0; |
| 524 | |
| 525 | free_pages_out: |
| 526 | for (i = 0; i < nr_pages_ret; i++) { |
| 527 | WARN_ON(pages[i]->mapping); |
| 528 | page_cache_release(pages[i]); |
| 529 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 530 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 531 | |
| 532 | goto out; |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * phase two of compressed writeback. This is the ordered portion |
| 537 | * of the code, which only gets called in the order the work was |
| 538 | * queued. We walk all the async extents created by compress_file_range |
| 539 | * and send them down to the disk. |
| 540 | */ |
| 541 | static noinline int submit_compressed_extents(struct inode *inode, |
| 542 | struct async_cow *async_cow) |
| 543 | { |
| 544 | struct async_extent *async_extent; |
| 545 | u64 alloc_hint = 0; |
| 546 | struct btrfs_trans_handle *trans; |
| 547 | struct btrfs_key ins; |
| 548 | struct extent_map *em; |
| 549 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 550 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 551 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 552 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 553 | |
| 554 | if (list_empty(&async_cow->extents)) |
| 555 | return 0; |
| 556 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 557 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 558 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 559 | async_extent = list_entry(async_cow->extents.next, |
| 560 | struct async_extent, list); |
| 561 | list_del(&async_extent->list); |
| 562 | |
| 563 | io_tree = &BTRFS_I(inode)->io_tree; |
| 564 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 565 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 566 | /* did the compression code fall back to uncompressed IO? */ |
| 567 | if (!async_extent->pages) { |
| 568 | int page_started = 0; |
| 569 | unsigned long nr_written = 0; |
| 570 | |
| 571 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 572 | async_extent->start + |
| 573 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 574 | |
| 575 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 576 | ret = cow_file_range(inode, async_cow->locked_page, |
| 577 | async_extent->start, |
| 578 | async_extent->start + |
| 579 | async_extent->ram_size - 1, |
| 580 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 581 | |
| 582 | /* |
| 583 | * if page_started, cow_file_range inserted an |
| 584 | * inline extent and took care of all the unlocking |
| 585 | * and IO for us. Otherwise, we need to submit |
| 586 | * all those pages down to the drive. |
| 587 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 588 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 589 | extent_write_locked_range(io_tree, |
| 590 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 591 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 592 | async_extent->ram_size - 1, |
| 593 | btrfs_get_extent, |
| 594 | WB_SYNC_ALL); |
| 595 | kfree(async_extent); |
| 596 | cond_resched(); |
| 597 | continue; |
| 598 | } |
| 599 | |
| 600 | lock_extent(io_tree, async_extent->start, |
| 601 | async_extent->start + async_extent->ram_size - 1, |
| 602 | GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 603 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 604 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 605 | ret = btrfs_reserve_extent(trans, root, |
| 606 | async_extent->compressed_size, |
| 607 | async_extent->compressed_size, |
| 608 | 0, alloc_hint, |
| 609 | (u64)-1, &ins, 1); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 610 | btrfs_end_transaction(trans, root); |
| 611 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 612 | if (ret) { |
| 613 | int i; |
| 614 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 615 | WARN_ON(async_extent->pages[i]->mapping); |
| 616 | page_cache_release(async_extent->pages[i]); |
| 617 | } |
| 618 | kfree(async_extent->pages); |
| 619 | async_extent->nr_pages = 0; |
| 620 | async_extent->pages = NULL; |
| 621 | unlock_extent(io_tree, async_extent->start, |
| 622 | async_extent->start + |
| 623 | async_extent->ram_size - 1, GFP_NOFS); |
| 624 | goto retry; |
| 625 | } |
| 626 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 627 | /* |
| 628 | * here we're doing allocation and writeback of the |
| 629 | * compressed pages |
| 630 | */ |
| 631 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 632 | async_extent->start + |
| 633 | async_extent->ram_size - 1, 0); |
| 634 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 635 | em = alloc_extent_map(GFP_NOFS); |
| 636 | em->start = async_extent->start; |
| 637 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 638 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 639 | |
| 640 | em->block_start = ins.objectid; |
| 641 | em->block_len = ins.offset; |
| 642 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 643 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 644 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 645 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 646 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 647 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 648 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 649 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 650 | if (ret != -EEXIST) { |
| 651 | free_extent_map(em); |
| 652 | break; |
| 653 | } |
| 654 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 655 | async_extent->start + |
| 656 | async_extent->ram_size - 1, 0); |
| 657 | } |
| 658 | |
| 659 | ret = btrfs_add_ordered_extent(inode, async_extent->start, |
| 660 | ins.objectid, |
| 661 | async_extent->ram_size, |
| 662 | ins.offset, |
| 663 | BTRFS_ORDERED_COMPRESSED); |
| 664 | BUG_ON(ret); |
| 665 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 666 | /* |
| 667 | * clear dirty, set writeback and unlock the pages. |
| 668 | */ |
| 669 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 670 | &BTRFS_I(inode)->io_tree, |
| 671 | async_extent->start, |
| 672 | async_extent->start + |
| 673 | async_extent->ram_size - 1, |
| 674 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 675 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 676 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 677 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 678 | |
| 679 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 680 | async_extent->start, |
| 681 | async_extent->ram_size, |
| 682 | ins.objectid, |
| 683 | ins.offset, async_extent->pages, |
| 684 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 685 | |
| 686 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 687 | alloc_hint = ins.objectid + ins.offset; |
| 688 | kfree(async_extent); |
| 689 | cond_resched(); |
| 690 | } |
| 691 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 692 | return 0; |
| 693 | } |
| 694 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 695 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 696 | u64 num_bytes) |
| 697 | { |
| 698 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 699 | struct extent_map *em; |
| 700 | u64 alloc_hint = 0; |
| 701 | |
| 702 | read_lock(&em_tree->lock); |
| 703 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 704 | if (em) { |
| 705 | /* |
| 706 | * if block start isn't an actual block number then find the |
| 707 | * first block in this inode and use that as a hint. If that |
| 708 | * block is also bogus then just don't worry about it. |
| 709 | */ |
| 710 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 711 | free_extent_map(em); |
| 712 | em = search_extent_mapping(em_tree, 0, 0); |
| 713 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 714 | alloc_hint = em->block_start; |
| 715 | if (em) |
| 716 | free_extent_map(em); |
| 717 | } else { |
| 718 | alloc_hint = em->block_start; |
| 719 | free_extent_map(em); |
| 720 | } |
| 721 | } |
| 722 | read_unlock(&em_tree->lock); |
| 723 | |
| 724 | return alloc_hint; |
| 725 | } |
| 726 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 727 | /* |
| 728 | * when extent_io.c finds a delayed allocation range in the file, |
| 729 | * the call backs end up in this code. The basic idea is to |
| 730 | * allocate extents on disk for the range, and create ordered data structs |
| 731 | * in ram to track those extents. |
| 732 | * |
| 733 | * locked_page is the page that writepage had locked already. We use |
| 734 | * it to make sure we don't do extra locks or unlocks. |
| 735 | * |
| 736 | * *page_started is set to one if we unlock locked_page and do everything |
| 737 | * required to start IO on it. It may be clean and already done with |
| 738 | * IO when we return. |
| 739 | */ |
| 740 | static noinline int cow_file_range(struct inode *inode, |
| 741 | struct page *locked_page, |
| 742 | u64 start, u64 end, int *page_started, |
| 743 | unsigned long *nr_written, |
| 744 | int unlock) |
| 745 | { |
| 746 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 747 | struct btrfs_trans_handle *trans; |
| 748 | u64 alloc_hint = 0; |
| 749 | u64 num_bytes; |
| 750 | unsigned long ram_size; |
| 751 | u64 disk_num_bytes; |
| 752 | u64 cur_alloc_size; |
| 753 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 754 | struct btrfs_key ins; |
| 755 | struct extent_map *em; |
| 756 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 757 | int ret = 0; |
| 758 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 759 | BUG_ON(root == root->fs_info->tree_root); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 760 | trans = btrfs_join_transaction(root, 1); |
| 761 | BUG_ON(!trans); |
| 762 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 763 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 764 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 765 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 766 | num_bytes = max(blocksize, num_bytes); |
| 767 | disk_num_bytes = num_bytes; |
| 768 | ret = 0; |
| 769 | |
| 770 | if (start == 0) { |
| 771 | /* lets try to make an inline extent */ |
| 772 | ret = cow_file_range_inline(trans, root, inode, |
| 773 | start, end, 0, NULL); |
| 774 | if (ret == 0) { |
| 775 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 776 | &BTRFS_I(inode)->io_tree, |
| 777 | start, end, NULL, |
| 778 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 779 | EXTENT_CLEAR_UNLOCK | |
| 780 | EXTENT_CLEAR_DELALLOC | |
| 781 | EXTENT_CLEAR_DIRTY | |
| 782 | EXTENT_SET_WRITEBACK | |
| 783 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 784 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 785 | *nr_written = *nr_written + |
| 786 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 787 | *page_started = 1; |
| 788 | ret = 0; |
| 789 | goto out; |
| 790 | } |
| 791 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 792 | |
| 793 | BUG_ON(disk_num_bytes > |
| 794 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 795 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 796 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 797 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 798 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 799 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 800 | unsigned long op; |
| 801 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 802 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 803 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 804 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 805 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 806 | BUG_ON(ret); |
| 807 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 808 | em = alloc_extent_map(GFP_NOFS); |
| 809 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 810 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 811 | ram_size = ins.offset; |
| 812 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 813 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 814 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 815 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 816 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 817 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 818 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 819 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 820 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 821 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 822 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 823 | if (ret != -EEXIST) { |
| 824 | free_extent_map(em); |
| 825 | break; |
| 826 | } |
| 827 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 828 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 829 | } |
| 830 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 831 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 832 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 833 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 834 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 835 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 836 | if (root->root_key.objectid == |
| 837 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 838 | ret = btrfs_reloc_clone_csums(inode, start, |
| 839 | cur_alloc_size); |
| 840 | BUG_ON(ret); |
| 841 | } |
| 842 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 843 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 844 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 845 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 846 | /* we're not doing compressed IO, don't unlock the first |
| 847 | * page (which the caller expects to stay locked), don't |
| 848 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 849 | * |
| 850 | * Do set the Private2 bit so we know this page was properly |
| 851 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 852 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 853 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 854 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 855 | EXTENT_SET_PRIVATE2; |
| 856 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 857 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 858 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 859 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 860 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 861 | num_bytes -= cur_alloc_size; |
| 862 | alloc_hint = ins.objectid + ins.offset; |
| 863 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 864 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 865 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 866 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 867 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 868 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 869 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 870 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 871 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 872 | /* |
| 873 | * work queue call back to started compression on a file and pages |
| 874 | */ |
| 875 | static noinline void async_cow_start(struct btrfs_work *work) |
| 876 | { |
| 877 | struct async_cow *async_cow; |
| 878 | int num_added = 0; |
| 879 | async_cow = container_of(work, struct async_cow, work); |
| 880 | |
| 881 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 882 | async_cow->start, async_cow->end, async_cow, |
| 883 | &num_added); |
| 884 | if (num_added == 0) |
| 885 | async_cow->inode = NULL; |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * work queue call back to submit previously compressed pages |
| 890 | */ |
| 891 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 892 | { |
| 893 | struct async_cow *async_cow; |
| 894 | struct btrfs_root *root; |
| 895 | unsigned long nr_pages; |
| 896 | |
| 897 | async_cow = container_of(work, struct async_cow, work); |
| 898 | |
| 899 | root = async_cow->root; |
| 900 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 901 | PAGE_CACHE_SHIFT; |
| 902 | |
| 903 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 904 | |
| 905 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 906 | 5 * 1042 * 1024 && |
| 907 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 908 | wake_up(&root->fs_info->async_submit_wait); |
| 909 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 910 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 911 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 912 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 913 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 914 | static noinline void async_cow_free(struct btrfs_work *work) |
| 915 | { |
| 916 | struct async_cow *async_cow; |
| 917 | async_cow = container_of(work, struct async_cow, work); |
| 918 | kfree(async_cow); |
| 919 | } |
| 920 | |
| 921 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 922 | u64 start, u64 end, int *page_started, |
| 923 | unsigned long *nr_written) |
| 924 | { |
| 925 | struct async_cow *async_cow; |
| 926 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 927 | unsigned long nr_pages; |
| 928 | u64 cur_end; |
| 929 | int limit = 10 * 1024 * 1042; |
| 930 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 931 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 932 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 933 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 934 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
| 935 | async_cow->inode = inode; |
| 936 | async_cow->root = root; |
| 937 | async_cow->locked_page = locked_page; |
| 938 | async_cow->start = start; |
| 939 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 940 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 941 | cur_end = end; |
| 942 | else |
| 943 | cur_end = min(end, start + 512 * 1024 - 1); |
| 944 | |
| 945 | async_cow->end = cur_end; |
| 946 | INIT_LIST_HEAD(&async_cow->extents); |
| 947 | |
| 948 | async_cow->work.func = async_cow_start; |
| 949 | async_cow->work.ordered_func = async_cow_submit; |
| 950 | async_cow->work.ordered_free = async_cow_free; |
| 951 | async_cow->work.flags = 0; |
| 952 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 953 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 954 | PAGE_CACHE_SHIFT; |
| 955 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 956 | |
| 957 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 958 | &async_cow->work); |
| 959 | |
| 960 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 961 | wait_event(root->fs_info->async_submit_wait, |
| 962 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 963 | limit)); |
| 964 | } |
| 965 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 966 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 967 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 968 | wait_event(root->fs_info->async_submit_wait, |
| 969 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 970 | 0)); |
| 971 | } |
| 972 | |
| 973 | *nr_written += nr_pages; |
| 974 | start = cur_end + 1; |
| 975 | } |
| 976 | *page_started = 1; |
| 977 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 978 | } |
| 979 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 980 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 981 | u64 bytenr, u64 num_bytes) |
| 982 | { |
| 983 | int ret; |
| 984 | struct btrfs_ordered_sum *sums; |
| 985 | LIST_HEAD(list); |
| 986 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 987 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
| 988 | bytenr + num_bytes - 1, &list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 989 | if (ret == 0 && list_empty(&list)) |
| 990 | return 0; |
| 991 | |
| 992 | while (!list_empty(&list)) { |
| 993 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 994 | list_del(&sums->list); |
| 995 | kfree(sums); |
| 996 | } |
| 997 | return 1; |
| 998 | } |
| 999 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1000 | /* |
| 1001 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1002 | * of the extents that exist in the file, and COWs the file as required. |
| 1003 | * |
| 1004 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1005 | * blocks on disk |
| 1006 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1007 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1008 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1009 | u64 start, u64 end, int *page_started, int force, |
| 1010 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1011 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1012 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1013 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1014 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1015 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1016 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1017 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1018 | u64 cow_start; |
| 1019 | u64 cur_offset; |
| 1020 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1021 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1022 | u64 disk_bytenr; |
| 1023 | u64 num_bytes; |
| 1024 | int extent_type; |
| 1025 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1026 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1027 | int nocow; |
| 1028 | int check_prev = 1; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1029 | bool nolock = false; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1030 | |
| 1031 | path = btrfs_alloc_path(); |
| 1032 | BUG_ON(!path); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1033 | if (root == root->fs_info->tree_root) { |
| 1034 | nolock = true; |
| 1035 | trans = btrfs_join_transaction_nolock(root, 1); |
| 1036 | } else { |
| 1037 | trans = btrfs_join_transaction(root, 1); |
| 1038 | } |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1039 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1040 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1041 | cow_start = (u64)-1; |
| 1042 | cur_offset = start; |
| 1043 | while (1) { |
| 1044 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 1045 | cur_offset, 0); |
| 1046 | BUG_ON(ret < 0); |
| 1047 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1048 | leaf = path->nodes[0]; |
| 1049 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1050 | path->slots[0] - 1); |
| 1051 | if (found_key.objectid == inode->i_ino && |
| 1052 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1053 | path->slots[0]--; |
| 1054 | } |
| 1055 | check_prev = 0; |
| 1056 | next_slot: |
| 1057 | leaf = path->nodes[0]; |
| 1058 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1059 | ret = btrfs_next_leaf(root, path); |
| 1060 | if (ret < 0) |
| 1061 | BUG_ON(1); |
| 1062 | if (ret > 0) |
| 1063 | break; |
| 1064 | leaf = path->nodes[0]; |
| 1065 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1066 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1067 | nocow = 0; |
| 1068 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1069 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1070 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1071 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1072 | if (found_key.objectid > inode->i_ino || |
| 1073 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1074 | found_key.offset > end) |
| 1075 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1076 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1077 | if (found_key.offset > cur_offset) { |
| 1078 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1079 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1080 | goto out_check; |
| 1081 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1082 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1083 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1084 | struct btrfs_file_extent_item); |
| 1085 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1086 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1087 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1088 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1089 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1090 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1091 | extent_end = found_key.offset + |
| 1092 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1093 | if (extent_end <= start) { |
| 1094 | path->slots[0]++; |
| 1095 | goto next_slot; |
| 1096 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1097 | if (disk_bytenr == 0) |
| 1098 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1099 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1100 | btrfs_file_extent_encryption(leaf, fi) || |
| 1101 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1102 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1103 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1104 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1105 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1106 | goto out_check; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1107 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1108 | found_key.offset - |
| 1109 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1110 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1111 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1112 | disk_bytenr += cur_offset - found_key.offset; |
| 1113 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1114 | /* |
| 1115 | * force cow if csum exists in the range. |
| 1116 | * this ensure that csum for a given extent are |
| 1117 | * either valid or do not exist. |
| 1118 | */ |
| 1119 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1120 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1121 | nocow = 1; |
| 1122 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1123 | extent_end = found_key.offset + |
| 1124 | btrfs_file_extent_inline_len(leaf, fi); |
| 1125 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1126 | } else { |
| 1127 | BUG_ON(1); |
| 1128 | } |
| 1129 | out_check: |
| 1130 | if (extent_end <= start) { |
| 1131 | path->slots[0]++; |
| 1132 | goto next_slot; |
| 1133 | } |
| 1134 | if (!nocow) { |
| 1135 | if (cow_start == (u64)-1) |
| 1136 | cow_start = cur_offset; |
| 1137 | cur_offset = extent_end; |
| 1138 | if (cur_offset > end) |
| 1139 | break; |
| 1140 | path->slots[0]++; |
| 1141 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1142 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1143 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1144 | btrfs_release_path(root, path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1145 | if (cow_start != (u64)-1) { |
| 1146 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1147 | found_key.offset - 1, page_started, |
| 1148 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1149 | BUG_ON(ret); |
| 1150 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1151 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1152 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1153 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1154 | struct extent_map *em; |
| 1155 | struct extent_map_tree *em_tree; |
| 1156 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 1157 | em = alloc_extent_map(GFP_NOFS); |
| 1158 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1159 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1160 | em->len = num_bytes; |
| 1161 | em->block_len = num_bytes; |
| 1162 | em->block_start = disk_bytenr; |
| 1163 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1164 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1165 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1166 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1167 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1168 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1169 | if (ret != -EEXIST) { |
| 1170 | free_extent_map(em); |
| 1171 | break; |
| 1172 | } |
| 1173 | btrfs_drop_extent_cache(inode, em->start, |
| 1174 | em->start + em->len - 1, 0); |
| 1175 | } |
| 1176 | type = BTRFS_ORDERED_PREALLOC; |
| 1177 | } else { |
| 1178 | type = BTRFS_ORDERED_NOCOW; |
| 1179 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1180 | |
| 1181 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1182 | num_bytes, num_bytes, type); |
| 1183 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1184 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1185 | if (root->root_key.objectid == |
| 1186 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1187 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1188 | num_bytes); |
| 1189 | BUG_ON(ret); |
| 1190 | } |
| 1191 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1192 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1193 | cur_offset, cur_offset + num_bytes - 1, |
| 1194 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1195 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1196 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1197 | cur_offset = extent_end; |
| 1198 | if (cur_offset > end) |
| 1199 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1200 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1201 | btrfs_release_path(root, path); |
| 1202 | |
| 1203 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1204 | cow_start = cur_offset; |
| 1205 | if (cow_start != (u64)-1) { |
| 1206 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1207 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1208 | BUG_ON(ret); |
| 1209 | } |
| 1210 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1211 | if (nolock) { |
| 1212 | ret = btrfs_end_transaction_nolock(trans, root); |
| 1213 | BUG_ON(ret); |
| 1214 | } else { |
| 1215 | ret = btrfs_end_transaction(trans, root); |
| 1216 | BUG_ON(ret); |
| 1217 | } |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1218 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1219 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1220 | } |
| 1221 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1222 | /* |
| 1223 | * extent_io.c call back to do delayed allocation processing |
| 1224 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1225 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1226 | u64 start, u64 end, int *page_started, |
| 1227 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1228 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1229 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1230 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1231 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1232 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1233 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1234 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1235 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1236 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1237 | page_started, 0, nr_written); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1238 | else if (!btrfs_test_opt(root, COMPRESS) && |
| 1239 | !(BTRFS_I(inode)->force_compress)) |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1240 | ret = cow_file_range(inode, locked_page, start, end, |
| 1241 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1242 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1243 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1244 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1245 | return ret; |
| 1246 | } |
| 1247 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1248 | static int btrfs_split_extent_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1249 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1250 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1251 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1252 | if (!(orig->state & EXTENT_DELALLOC)) |
| 1253 | return 0; |
| 1254 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1255 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1261 | * extents so we can keep track of new extents that are just merged onto old |
| 1262 | * extents, such as when we are doing sequential writes, so we can properly |
| 1263 | * account for the metadata space we'll need. |
| 1264 | */ |
| 1265 | static int btrfs_merge_extent_hook(struct inode *inode, |
| 1266 | struct extent_state *new, |
| 1267 | struct extent_state *other) |
| 1268 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1269 | /* not delalloc, ignore it */ |
| 1270 | if (!(other->state & EXTENT_DELALLOC)) |
| 1271 | return 0; |
| 1272 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1273 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1274 | return 0; |
| 1275 | } |
| 1276 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1277 | /* |
| 1278 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1279 | * bytes in this file, and to maintain the list of inodes that |
| 1280 | * have pending delalloc work to be done. |
| 1281 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1282 | static int btrfs_set_bit_hook(struct inode *inode, |
| 1283 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1284 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1285 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1286 | /* |
| 1287 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1288 | * but in this case, we are only testeing for the DELALLOC |
| 1289 | * bit, which is only set or cleared with irqs on |
| 1290 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1291 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1292 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1293 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1294 | int do_list = (root->root_key.objectid != |
| 1295 | BTRFS_ROOT_TREE_OBJECTID); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1296 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1297 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1298 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1299 | else |
| 1300 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1301 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1302 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1303 | BTRFS_I(inode)->delalloc_bytes += len; |
| 1304 | root->fs_info->delalloc_bytes += len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1305 | if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1306 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1307 | &root->fs_info->delalloc_inodes); |
| 1308 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1309 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1310 | } |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1314 | /* |
| 1315 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1316 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1317 | static int btrfs_clear_bit_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1318 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1319 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1320 | /* |
| 1321 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1322 | * but in this case, we are only testeing for the DELALLOC |
| 1323 | * bit, which is only set or cleared with irqs on |
| 1324 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1325 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1326 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1327 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1328 | int do_list = (root->root_key.objectid != |
| 1329 | BTRFS_ROOT_TREE_OBJECTID); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1330 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1331 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1332 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1333 | else if (!(*bits & EXTENT_DO_ACCOUNTING)) |
| 1334 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
| 1335 | |
| 1336 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1337 | btrfs_delalloc_release_metadata(inode, len); |
| 1338 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1339 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 1340 | && do_list) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1341 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1342 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1343 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1344 | root->fs_info->delalloc_bytes -= len; |
| 1345 | BTRFS_I(inode)->delalloc_bytes -= len; |
| 1346 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1347 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1348 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1349 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1350 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1351 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1352 | } |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1356 | /* |
| 1357 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1358 | * we don't create bios that span stripes or chunks |
| 1359 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1360 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1361 | size_t size, struct bio *bio, |
| 1362 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1363 | { |
| 1364 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1365 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1366 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1367 | u64 length = 0; |
| 1368 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1369 | int ret; |
| 1370 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1371 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1372 | return 0; |
| 1373 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1374 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1375 | map_tree = &root->fs_info->mapping_tree; |
| 1376 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1377 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1378 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1379 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1380 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1381 | return 1; |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 1382 | return ret; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1383 | } |
| 1384 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1385 | /* |
| 1386 | * in order to insert checksums into the metadata in large chunks, |
| 1387 | * we wait until bio submission time. All the pages in the bio are |
| 1388 | * checksummed and sums are attached onto the ordered extent record. |
| 1389 | * |
| 1390 | * At IO completion time the cums attached on the ordered extent record |
| 1391 | * are inserted into the btree |
| 1392 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1393 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1394 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1395 | unsigned long bio_flags, |
| 1396 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1397 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1398 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1399 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1400 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1401 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1402 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1403 | return 0; |
| 1404 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1405 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1406 | /* |
| 1407 | * in order to insert checksums into the metadata in large chunks, |
| 1408 | * we wait until bio submission time. All the pages in the bio are |
| 1409 | * checksummed and sums are attached onto the ordered extent record. |
| 1410 | * |
| 1411 | * At IO completion time the cums attached on the ordered extent record |
| 1412 | * are inserted into the btree |
| 1413 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1414 | 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] | 1415 | int mirror_num, unsigned long bio_flags, |
| 1416 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1417 | { |
| 1418 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1419 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1420 | } |
| 1421 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1422 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1423 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1424 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1425 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1426 | 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] | 1427 | int mirror_num, unsigned long bio_flags, |
| 1428 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1429 | { |
| 1430 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1431 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1432 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1433 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1434 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1435 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1436 | if (root == root->fs_info->tree_root) |
| 1437 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2); |
| 1438 | else |
| 1439 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1440 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1441 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1442 | if (!(rw & REQ_WRITE)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1443 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1444 | return btrfs_submit_compressed_read(inode, bio, |
| 1445 | mirror_num, bio_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1446 | } else if (!skip_sum) |
| 1447 | btrfs_lookup_bio_sums(root, inode, bio, NULL); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1448 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1449 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1450 | /* csum items have already been cloned */ |
| 1451 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1452 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1453 | /* we're doing a write, do the async checksumming */ |
| 1454 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1455 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1456 | bio_flags, bio_offset, |
| 1457 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1458 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1459 | } |
| 1460 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1461 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1462 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1463 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1464 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1465 | /* |
| 1466 | * given a list of ordered sums record them in the inode. This happens |
| 1467 | * at IO completion time based on sums calculated at bio submission time. |
| 1468 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1469 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1470 | struct inode *inode, u64 file_offset, |
| 1471 | struct list_head *list) |
| 1472 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1473 | struct btrfs_ordered_sum *sum; |
| 1474 | |
| 1475 | btrfs_set_trans_block_group(trans, inode); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1476 | |
| 1477 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1478 | btrfs_csum_file_blocks(trans, |
| 1479 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1480 | } |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1484 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1485 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1486 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1487 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1488 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1489 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1490 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1491 | } |
| 1492 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1493 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1494 | struct btrfs_writepage_fixup { |
| 1495 | struct page *page; |
| 1496 | struct btrfs_work work; |
| 1497 | }; |
| 1498 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1499 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1500 | { |
| 1501 | struct btrfs_writepage_fixup *fixup; |
| 1502 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1503 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1504 | struct page *page; |
| 1505 | struct inode *inode; |
| 1506 | u64 page_start; |
| 1507 | u64 page_end; |
| 1508 | |
| 1509 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1510 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1511 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1512 | lock_page(page); |
| 1513 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1514 | ClearPageChecked(page); |
| 1515 | goto out_page; |
| 1516 | } |
| 1517 | |
| 1518 | inode = page->mapping->host; |
| 1519 | page_start = page_offset(page); |
| 1520 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1521 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1522 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
| 1523 | &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1524 | |
| 1525 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1526 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1527 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1528 | |
| 1529 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1530 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1531 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1532 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1533 | unlock_page(page); |
| 1534 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1535 | goto again; |
| 1536 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1537 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1538 | BUG(); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1539 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1540 | ClearPageChecked(page); |
| 1541 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1542 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1543 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1544 | out_page: |
| 1545 | unlock_page(page); |
| 1546 | page_cache_release(page); |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * There are a few paths in the higher layers of the kernel that directly |
| 1551 | * set the page dirty bit without asking the filesystem if it is a |
| 1552 | * good idea. This causes problems because we want to make sure COW |
| 1553 | * properly happens and the data=ordered rules are followed. |
| 1554 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1555 | * 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] | 1556 | * hasn't been properly setup for IO. We kick off an async process |
| 1557 | * to fix it up. The async helper will wait for ordered extents, set |
| 1558 | * the delalloc bit and make it safe to write the page. |
| 1559 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1560 | 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] | 1561 | { |
| 1562 | struct inode *inode = page->mapping->host; |
| 1563 | struct btrfs_writepage_fixup *fixup; |
| 1564 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1565 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1566 | /* this page is properly in the ordered list */ |
| 1567 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1568 | return 0; |
| 1569 | |
| 1570 | if (PageChecked(page)) |
| 1571 | return -EAGAIN; |
| 1572 | |
| 1573 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1574 | if (!fixup) |
| 1575 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1576 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1577 | SetPageChecked(page); |
| 1578 | page_cache_get(page); |
| 1579 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1580 | fixup->page = page; |
| 1581 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1582 | return -EAGAIN; |
| 1583 | } |
| 1584 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1585 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1586 | struct inode *inode, u64 file_pos, |
| 1587 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1588 | u64 num_bytes, u64 ram_bytes, |
| 1589 | u8 compression, u8 encryption, |
| 1590 | u16 other_encoding, int extent_type) |
| 1591 | { |
| 1592 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1593 | struct btrfs_file_extent_item *fi; |
| 1594 | struct btrfs_path *path; |
| 1595 | struct extent_buffer *leaf; |
| 1596 | struct btrfs_key ins; |
| 1597 | u64 hint; |
| 1598 | int ret; |
| 1599 | |
| 1600 | path = btrfs_alloc_path(); |
| 1601 | BUG_ON(!path); |
| 1602 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1603 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1604 | |
| 1605 | /* |
| 1606 | * we may be replacing one extent in the tree with another. |
| 1607 | * The new extent is pinned in the extent map, and we don't want |
| 1608 | * to drop it from the cache until it is completely in the btree. |
| 1609 | * |
| 1610 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1611 | * the caller is expected to unpin it and allow it to be merged |
| 1612 | * with the others. |
| 1613 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1614 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1615 | &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1616 | BUG_ON(ret); |
| 1617 | |
| 1618 | ins.objectid = inode->i_ino; |
| 1619 | ins.offset = file_pos; |
| 1620 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1621 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1622 | BUG_ON(ret); |
| 1623 | leaf = path->nodes[0]; |
| 1624 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1625 | struct btrfs_file_extent_item); |
| 1626 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1627 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1628 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1629 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1630 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1631 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1632 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1633 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1634 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1635 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1636 | |
| 1637 | btrfs_unlock_up_safe(path, 1); |
| 1638 | btrfs_set_lock_blocking(leaf); |
| 1639 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1640 | btrfs_mark_buffer_dirty(leaf); |
| 1641 | |
| 1642 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1643 | |
| 1644 | ins.objectid = disk_bytenr; |
| 1645 | ins.offset = disk_num_bytes; |
| 1646 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1647 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1648 | root->root_key.objectid, |
| 1649 | inode->i_ino, file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1650 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1651 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1652 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1653 | return 0; |
| 1654 | } |
| 1655 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1656 | /* |
| 1657 | * helper function for btrfs_finish_ordered_io, this |
| 1658 | * just reads in some of the csum leaves to prime them into ram |
| 1659 | * before we start the transaction. It limits the amount of btree |
| 1660 | * reads required while inside the transaction. |
| 1661 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1662 | /* as ordered data IO finishes, this gets called so we can finish |
| 1663 | * an ordered extent if the range of bytes in the file it covers are |
| 1664 | * fully written. |
| 1665 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1666 | 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] | 1667 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1668 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1669 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1670 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1671 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1672 | struct extent_state *cached_state = NULL; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1673 | int compressed = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1674 | int ret; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1675 | bool nolock = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1676 | |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 1677 | ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 1678 | end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1679 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1680 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1681 | BUG_ON(!ordered_extent); |
Josef Bacik | efd049f | 2010-02-02 20:50:10 +0000 | [diff] [blame] | 1682 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1683 | nolock = (root == root->fs_info->tree_root); |
| 1684 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1685 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
| 1686 | BUG_ON(!list_empty(&ordered_extent->list)); |
| 1687 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1688 | if (!ret) { |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1689 | if (nolock) |
| 1690 | trans = btrfs_join_transaction_nolock(root, 1); |
| 1691 | else |
| 1692 | trans = btrfs_join_transaction(root, 1); |
| 1693 | BUG_ON(!trans); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1694 | btrfs_set_trans_block_group(trans, inode); |
| 1695 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1696 | ret = btrfs_update_inode(trans, root, inode); |
| 1697 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1698 | } |
| 1699 | goto out; |
| 1700 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1701 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1702 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 1703 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1704 | 0, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1705 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1706 | if (nolock) |
| 1707 | trans = btrfs_join_transaction_nolock(root, 1); |
| 1708 | else |
| 1709 | trans = btrfs_join_transaction(root, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1710 | btrfs_set_trans_block_group(trans, inode); |
| 1711 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1712 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1713 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1714 | compressed = 1; |
| 1715 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 1716 | BUG_ON(compressed); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1717 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1718 | ordered_extent->file_offset, |
| 1719 | ordered_extent->file_offset + |
| 1720 | ordered_extent->len); |
| 1721 | BUG_ON(ret); |
| 1722 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 1723 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1724 | ret = insert_reserved_file_extent(trans, inode, |
| 1725 | ordered_extent->file_offset, |
| 1726 | ordered_extent->start, |
| 1727 | ordered_extent->disk_len, |
| 1728 | ordered_extent->len, |
| 1729 | ordered_extent->len, |
| 1730 | compressed, 0, 0, |
| 1731 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1732 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1733 | ordered_extent->file_offset, |
| 1734 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1735 | BUG_ON(ret); |
| 1736 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1737 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 1738 | ordered_extent->file_offset + |
| 1739 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
| 1740 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1741 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1742 | &ordered_extent->list); |
| 1743 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1744 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1745 | ret = btrfs_update_inode(trans, root, inode); |
| 1746 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1747 | out: |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1748 | if (nolock) { |
| 1749 | if (trans) |
| 1750 | btrfs_end_transaction_nolock(trans, root); |
| 1751 | } else { |
| 1752 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
| 1753 | if (trans) |
| 1754 | btrfs_end_transaction(trans, root); |
| 1755 | } |
| 1756 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1757 | /* once for us */ |
| 1758 | btrfs_put_ordered_extent(ordered_extent); |
| 1759 | /* once for the tree */ |
| 1760 | btrfs_put_ordered_extent(ordered_extent); |
| 1761 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1762 | return 0; |
| 1763 | } |
| 1764 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1765 | 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] | 1766 | struct extent_state *state, int uptodate) |
| 1767 | { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1768 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1769 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1770 | } |
| 1771 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1772 | /* |
| 1773 | * When IO fails, either with EIO or csum verification fails, we |
| 1774 | * try other mirrors that might have a good copy of the data. This |
| 1775 | * io_failure_record is used to record state as we go through all the |
| 1776 | * mirrors. If another mirror has good data, the page is set up to date |
| 1777 | * and things continue. If a good mirror can't be found, the original |
| 1778 | * bio end_io callback is called to indicate things have failed. |
| 1779 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1780 | struct io_failure_record { |
| 1781 | struct page *page; |
| 1782 | u64 start; |
| 1783 | u64 len; |
| 1784 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1785 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1786 | int last_mirror; |
| 1787 | }; |
| 1788 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1789 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1790 | struct page *page, u64 start, u64 end, |
| 1791 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1792 | { |
| 1793 | struct io_failure_record *failrec = NULL; |
| 1794 | u64 private; |
| 1795 | struct extent_map *em; |
| 1796 | struct inode *inode = page->mapping->host; |
| 1797 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1798 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1799 | struct bio *bio; |
| 1800 | int num_copies; |
| 1801 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1802 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1803 | u64 logical; |
| 1804 | |
| 1805 | ret = get_state_private(failure_tree, start, &private); |
| 1806 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1807 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1808 | if (!failrec) |
| 1809 | return -ENOMEM; |
| 1810 | failrec->start = start; |
| 1811 | failrec->len = end - start + 1; |
| 1812 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1813 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1814 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1815 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1816 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1817 | if (em->start > start || em->start + em->len < start) { |
| 1818 | free_extent_map(em); |
| 1819 | em = NULL; |
| 1820 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1821 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1822 | |
| 1823 | if (!em || IS_ERR(em)) { |
| 1824 | kfree(failrec); |
| 1825 | return -EIO; |
| 1826 | } |
| 1827 | logical = start - em->start; |
| 1828 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1829 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1830 | logical = em->block_start; |
| 1831 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 1832 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1833 | failrec->logical = logical; |
| 1834 | free_extent_map(em); |
| 1835 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1836 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1837 | set_state_private(failure_tree, start, |
| 1838 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1839 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1840 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1841 | } |
| 1842 | num_copies = btrfs_num_copies( |
| 1843 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1844 | failrec->logical, failrec->len); |
| 1845 | failrec->last_mirror++; |
| 1846 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1847 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1848 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1849 | failrec->start, |
| 1850 | EXTENT_LOCKED); |
| 1851 | if (state && state->start != failrec->start) |
| 1852 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1853 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1854 | } |
| 1855 | if (!state || failrec->last_mirror > num_copies) { |
| 1856 | set_state_private(failure_tree, failrec->start, 0); |
| 1857 | clear_extent_bits(failure_tree, failrec->start, |
| 1858 | failrec->start + failrec->len - 1, |
| 1859 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1860 | kfree(failrec); |
| 1861 | return -EIO; |
| 1862 | } |
| 1863 | bio = bio_alloc(GFP_NOFS, 1); |
| 1864 | bio->bi_private = state; |
| 1865 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1866 | bio->bi_sector = failrec->logical >> 9; |
| 1867 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1868 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1869 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1870 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1871 | if (failed_bio->bi_rw & REQ_WRITE) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1872 | rw = WRITE; |
| 1873 | else |
| 1874 | rw = READ; |
| 1875 | |
| 1876 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1877 | failrec->last_mirror, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1878 | failrec->bio_flags, 0); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1879 | return 0; |
| 1880 | } |
| 1881 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1882 | /* |
| 1883 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1884 | * to see if we need to process or clean up an io_failure_record |
| 1885 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1886 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1887 | { |
| 1888 | u64 private; |
| 1889 | u64 private_failure; |
| 1890 | struct io_failure_record *failure; |
| 1891 | int ret; |
| 1892 | |
| 1893 | private = 0; |
| 1894 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 1895 | (u64)-1, 1, EXTENT_DIRTY)) { |
| 1896 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1897 | start, &private_failure); |
| 1898 | if (ret == 0) { |
| 1899 | failure = (struct io_failure_record *)(unsigned long) |
| 1900 | private_failure; |
| 1901 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1902 | failure->start, 0); |
| 1903 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1904 | failure->start, |
| 1905 | failure->start + failure->len - 1, |
| 1906 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1907 | GFP_NOFS); |
| 1908 | kfree(failure); |
| 1909 | } |
| 1910 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1911 | return 0; |
| 1912 | } |
| 1913 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1914 | /* |
| 1915 | * when reads are done, we need to check csums to verify the data is correct |
| 1916 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1917 | * the io_failure_record routines to find good copies |
| 1918 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1919 | 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] | 1920 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1921 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1922 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1923 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1924 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1925 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1926 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1927 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1928 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1929 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1930 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1931 | if (PageChecked(page)) { |
| 1932 | ClearPageChecked(page); |
| 1933 | goto good; |
| 1934 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1935 | |
| 1936 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1937 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1938 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1939 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1940 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1941 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1942 | GFP_NOFS); |
| 1943 | return 0; |
| 1944 | } |
| 1945 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1946 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1947 | private = state->private; |
| 1948 | ret = 0; |
| 1949 | } else { |
| 1950 | ret = get_state_private(io_tree, start, &private); |
| 1951 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1952 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1953 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1954 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1955 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1956 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1957 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1958 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1959 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1960 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1961 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1962 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1963 | /* if the io failure tree for this inode is non-empty, |
| 1964 | * check to see if we've recovered from a failed IO |
| 1965 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1966 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1967 | return 0; |
| 1968 | |
| 1969 | zeroit: |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 1970 | if (printk_ratelimit()) { |
| 1971 | printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " |
| 1972 | "private %llu\n", page->mapping->host->i_ino, |
| 1973 | (unsigned long long)start, csum, |
| 1974 | (unsigned long long)private); |
| 1975 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1976 | memset(kaddr + offset, 1, end - start + 1); |
| 1977 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1978 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1979 | if (private == 0) |
| 1980 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1981 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1982 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1983 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 1984 | struct delayed_iput { |
| 1985 | struct list_head list; |
| 1986 | struct inode *inode; |
| 1987 | }; |
| 1988 | |
| 1989 | void btrfs_add_delayed_iput(struct inode *inode) |
| 1990 | { |
| 1991 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 1992 | struct delayed_iput *delayed; |
| 1993 | |
| 1994 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 1995 | return; |
| 1996 | |
| 1997 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 1998 | delayed->inode = inode; |
| 1999 | |
| 2000 | spin_lock(&fs_info->delayed_iput_lock); |
| 2001 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2002 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2003 | } |
| 2004 | |
| 2005 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2006 | { |
| 2007 | LIST_HEAD(list); |
| 2008 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2009 | struct delayed_iput *delayed; |
| 2010 | int empty; |
| 2011 | |
| 2012 | spin_lock(&fs_info->delayed_iput_lock); |
| 2013 | empty = list_empty(&fs_info->delayed_iputs); |
| 2014 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2015 | if (empty) |
| 2016 | return; |
| 2017 | |
| 2018 | down_read(&root->fs_info->cleanup_work_sem); |
| 2019 | spin_lock(&fs_info->delayed_iput_lock); |
| 2020 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2021 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2022 | |
| 2023 | while (!list_empty(&list)) { |
| 2024 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2025 | list_del(&delayed->list); |
| 2026 | iput(delayed->inode); |
| 2027 | kfree(delayed); |
| 2028 | } |
| 2029 | up_read(&root->fs_info->cleanup_work_sem); |
| 2030 | } |
| 2031 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2032 | /* |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2033 | * calculate extra metadata reservation when snapshotting a subvolume |
| 2034 | * contains orphan files. |
| 2035 | */ |
| 2036 | void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans, |
| 2037 | struct btrfs_pending_snapshot *pending, |
| 2038 | u64 *bytes_to_reserve) |
| 2039 | { |
| 2040 | struct btrfs_root *root; |
| 2041 | struct btrfs_block_rsv *block_rsv; |
| 2042 | u64 num_bytes; |
| 2043 | int index; |
| 2044 | |
| 2045 | root = pending->root; |
| 2046 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2047 | return; |
| 2048 | |
| 2049 | block_rsv = root->orphan_block_rsv; |
| 2050 | |
| 2051 | /* orphan block reservation for the snapshot */ |
| 2052 | num_bytes = block_rsv->size; |
| 2053 | |
| 2054 | /* |
| 2055 | * after the snapshot is created, COWing tree blocks may use more |
| 2056 | * space than it frees. So we should make sure there is enough |
| 2057 | * reserved space. |
| 2058 | */ |
| 2059 | index = trans->transid & 0x1; |
| 2060 | if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) { |
| 2061 | num_bytes += block_rsv->size - |
| 2062 | (block_rsv->reserved + block_rsv->freed[index]); |
| 2063 | } |
| 2064 | |
| 2065 | *bytes_to_reserve += num_bytes; |
| 2066 | } |
| 2067 | |
| 2068 | void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans, |
| 2069 | struct btrfs_pending_snapshot *pending) |
| 2070 | { |
| 2071 | struct btrfs_root *root = pending->root; |
| 2072 | struct btrfs_root *snap = pending->snap; |
| 2073 | struct btrfs_block_rsv *block_rsv; |
| 2074 | u64 num_bytes; |
| 2075 | int index; |
| 2076 | int ret; |
| 2077 | |
| 2078 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2079 | return; |
| 2080 | |
| 2081 | /* refill source subvolume's orphan block reservation */ |
| 2082 | block_rsv = root->orphan_block_rsv; |
| 2083 | index = trans->transid & 0x1; |
| 2084 | if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) { |
| 2085 | num_bytes = block_rsv->size - |
| 2086 | (block_rsv->reserved + block_rsv->freed[index]); |
| 2087 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2088 | root->orphan_block_rsv, |
| 2089 | num_bytes); |
| 2090 | BUG_ON(ret); |
| 2091 | } |
| 2092 | |
| 2093 | /* setup orphan block reservation for the snapshot */ |
| 2094 | block_rsv = btrfs_alloc_block_rsv(snap); |
| 2095 | BUG_ON(!block_rsv); |
| 2096 | |
| 2097 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2098 | snap->orphan_block_rsv = block_rsv; |
| 2099 | |
| 2100 | num_bytes = root->orphan_block_rsv->size; |
| 2101 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2102 | block_rsv, num_bytes); |
| 2103 | BUG_ON(ret); |
| 2104 | |
| 2105 | #if 0 |
| 2106 | /* insert orphan item for the snapshot */ |
| 2107 | WARN_ON(!root->orphan_item_inserted); |
| 2108 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2109 | snap->root_key.objectid); |
| 2110 | BUG_ON(ret); |
| 2111 | snap->orphan_item_inserted = 1; |
| 2112 | #endif |
| 2113 | } |
| 2114 | |
| 2115 | enum btrfs_orphan_cleanup_state { |
| 2116 | ORPHAN_CLEANUP_STARTED = 1, |
| 2117 | ORPHAN_CLEANUP_DONE = 2, |
| 2118 | }; |
| 2119 | |
| 2120 | /* |
| 2121 | * This is called in transaction commmit time. If there are no orphan |
| 2122 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2123 | * structure. |
| 2124 | */ |
| 2125 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2126 | struct btrfs_root *root) |
| 2127 | { |
| 2128 | int ret; |
| 2129 | |
| 2130 | if (!list_empty(&root->orphan_list) || |
| 2131 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2132 | return; |
| 2133 | |
| 2134 | if (root->orphan_item_inserted && |
| 2135 | btrfs_root_refs(&root->root_item) > 0) { |
| 2136 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2137 | root->root_key.objectid); |
| 2138 | BUG_ON(ret); |
| 2139 | root->orphan_item_inserted = 0; |
| 2140 | } |
| 2141 | |
| 2142 | if (root->orphan_block_rsv) { |
| 2143 | WARN_ON(root->orphan_block_rsv->size > 0); |
| 2144 | btrfs_free_block_rsv(root, root->orphan_block_rsv); |
| 2145 | root->orphan_block_rsv = NULL; |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2150 | * This creates an orphan entry for the given inode in case something goes |
| 2151 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2152 | * |
| 2153 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2154 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2155 | */ |
| 2156 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2157 | { |
| 2158 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2159 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2160 | int reserve = 0; |
| 2161 | int insert = 0; |
| 2162 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2163 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2164 | if (!root->orphan_block_rsv) { |
| 2165 | block_rsv = btrfs_alloc_block_rsv(root); |
| 2166 | BUG_ON(!block_rsv); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2167 | } |
| 2168 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2169 | spin_lock(&root->orphan_lock); |
| 2170 | if (!root->orphan_block_rsv) { |
| 2171 | root->orphan_block_rsv = block_rsv; |
| 2172 | } else if (block_rsv) { |
| 2173 | btrfs_free_block_rsv(root, block_rsv); |
| 2174 | block_rsv = NULL; |
| 2175 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2176 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2177 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2178 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2179 | #if 0 |
| 2180 | /* |
| 2181 | * For proper ENOSPC handling, we should do orphan |
| 2182 | * cleanup when mounting. But this introduces backward |
| 2183 | * compatibility issue. |
| 2184 | */ |
| 2185 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2186 | insert = 2; |
| 2187 | else |
| 2188 | insert = 1; |
| 2189 | #endif |
| 2190 | insert = 1; |
| 2191 | } else { |
| 2192 | WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved); |
| 2193 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2194 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2195 | if (!BTRFS_I(inode)->orphan_meta_reserved) { |
| 2196 | BTRFS_I(inode)->orphan_meta_reserved = 1; |
| 2197 | reserve = 1; |
| 2198 | } |
| 2199 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2200 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2201 | if (block_rsv) |
| 2202 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2203 | |
| 2204 | /* grab metadata reservation from transaction handle */ |
| 2205 | if (reserve) { |
| 2206 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
| 2207 | BUG_ON(ret); |
| 2208 | } |
| 2209 | |
| 2210 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2211 | if (insert >= 1) { |
| 2212 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 2213 | BUG_ON(ret); |
| 2214 | } |
| 2215 | |
| 2216 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2217 | if (insert >= 2) { |
| 2218 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2219 | root->root_key.objectid); |
| 2220 | BUG_ON(ret); |
| 2221 | } |
| 2222 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | /* |
| 2226 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2227 | * item for this particular inode. |
| 2228 | */ |
| 2229 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2230 | { |
| 2231 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2232 | int delete_item = 0; |
| 2233 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2234 | int ret = 0; |
| 2235 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2236 | spin_lock(&root->orphan_lock); |
| 2237 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2238 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2239 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2240 | } |
| 2241 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2242 | if (BTRFS_I(inode)->orphan_meta_reserved) { |
| 2243 | BTRFS_I(inode)->orphan_meta_reserved = 0; |
| 2244 | release_rsv = 1; |
| 2245 | } |
| 2246 | spin_unlock(&root->orphan_lock); |
| 2247 | |
| 2248 | if (trans && delete_item) { |
| 2249 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 2250 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2251 | } |
| 2252 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2253 | if (release_rsv) |
| 2254 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2255 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2256 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | /* |
| 2260 | * this cleans up any orphans that may be left on the list from the last use |
| 2261 | * of this root. |
| 2262 | */ |
| 2263 | void btrfs_orphan_cleanup(struct btrfs_root *root) |
| 2264 | { |
| 2265 | struct btrfs_path *path; |
| 2266 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2267 | struct btrfs_key key, found_key; |
| 2268 | struct btrfs_trans_handle *trans; |
| 2269 | struct inode *inode; |
| 2270 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2271 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2272 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2273 | return; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2274 | |
| 2275 | path = btrfs_alloc_path(); |
| 2276 | BUG_ON(!path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2277 | path->reada = -1; |
| 2278 | |
| 2279 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2280 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2281 | key.offset = (u64)-1; |
| 2282 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2283 | while (1) { |
| 2284 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2285 | if (ret < 0) { |
| 2286 | printk(KERN_ERR "Error searching slot for orphan: %d" |
| 2287 | "\n", ret); |
| 2288 | break; |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * if ret == 0 means we found what we were searching for, which |
| 2293 | * is weird, but possible, so only screw with path if we didnt |
| 2294 | * find the key and see if we have stuff that matches |
| 2295 | */ |
| 2296 | if (ret > 0) { |
| 2297 | if (path->slots[0] == 0) |
| 2298 | break; |
| 2299 | path->slots[0]--; |
| 2300 | } |
| 2301 | |
| 2302 | /* pull out the item */ |
| 2303 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2304 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2305 | |
| 2306 | /* make sure the item matches what we want */ |
| 2307 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2308 | break; |
| 2309 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2310 | break; |
| 2311 | |
| 2312 | /* release the path since we're done with it */ |
| 2313 | btrfs_release_path(root, path); |
| 2314 | |
| 2315 | /* |
| 2316 | * this is where we are basically btrfs_lookup, without the |
| 2317 | * crossing root thing. we store the inode number in the |
| 2318 | * offset of the orphan item. |
| 2319 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2320 | found_key.objectid = found_key.offset; |
| 2321 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2322 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2323 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2324 | BUG_ON(IS_ERR(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2325 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2326 | /* |
| 2327 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2328 | * the proper thing when we hit it |
| 2329 | */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2330 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2331 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2332 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2333 | |
| 2334 | /* |
| 2335 | * if this is a bad inode, means we actually succeeded in |
| 2336 | * removing the inode, but not the orphan record, which means |
| 2337 | * we need to manually delete the orphan since iput will just |
| 2338 | * do a destroy_inode |
| 2339 | */ |
| 2340 | if (is_bad_inode(inode)) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2341 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2342 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2343 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2344 | iput(inode); |
| 2345 | continue; |
| 2346 | } |
| 2347 | |
| 2348 | /* if we have links, this was a truncate, lets do that */ |
| 2349 | if (inode->i_nlink) { |
| 2350 | nr_truncate++; |
| 2351 | btrfs_truncate(inode); |
| 2352 | } else { |
| 2353 | nr_unlink++; |
| 2354 | } |
| 2355 | |
| 2356 | /* this will do delete_inode and everything for us */ |
| 2357 | iput(inode); |
| 2358 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2359 | btrfs_free_path(path); |
| 2360 | |
| 2361 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 2362 | |
| 2363 | if (root->orphan_block_rsv) |
| 2364 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 2365 | (u64)-1); |
| 2366 | |
| 2367 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
| 2368 | trans = btrfs_join_transaction(root, 1); |
| 2369 | btrfs_end_transaction(trans, root); |
| 2370 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2371 | |
| 2372 | if (nr_unlink) |
| 2373 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2374 | if (nr_truncate) |
| 2375 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2376 | } |
| 2377 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2378 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2379 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2380 | * don't find any xattrs, we know there can't be any acls. |
| 2381 | * |
| 2382 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2383 | */ |
| 2384 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2385 | int slot, u64 objectid) |
| 2386 | { |
| 2387 | u32 nritems = btrfs_header_nritems(leaf); |
| 2388 | struct btrfs_key found_key; |
| 2389 | int scanned = 0; |
| 2390 | |
| 2391 | slot++; |
| 2392 | while (slot < nritems) { |
| 2393 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2394 | |
| 2395 | /* we found a different objectid, there must not be acls */ |
| 2396 | if (found_key.objectid != objectid) |
| 2397 | return 0; |
| 2398 | |
| 2399 | /* we found an xattr, assume we've got an acl */ |
| 2400 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2401 | return 1; |
| 2402 | |
| 2403 | /* |
| 2404 | * we found a key greater than an xattr key, there can't |
| 2405 | * be any acls later on |
| 2406 | */ |
| 2407 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2408 | return 0; |
| 2409 | |
| 2410 | slot++; |
| 2411 | scanned++; |
| 2412 | |
| 2413 | /* |
| 2414 | * it goes inode, inode backrefs, xattrs, extents, |
| 2415 | * so if there are a ton of hard links to an inode there can |
| 2416 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2417 | * this is just an optimization |
| 2418 | */ |
| 2419 | if (scanned >= 8) |
| 2420 | break; |
| 2421 | } |
| 2422 | /* we hit the end of the leaf before we found an xattr or |
| 2423 | * something larger than an xattr. We have to assume the inode |
| 2424 | * has acls |
| 2425 | */ |
| 2426 | return 1; |
| 2427 | } |
| 2428 | |
| 2429 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2430 | * read an inode from the btree into the in-memory inode |
| 2431 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2432 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2433 | { |
| 2434 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2435 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2436 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2437 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2438 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2439 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2440 | int maybe_acls; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2441 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2442 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2443 | int ret; |
| 2444 | |
| 2445 | path = btrfs_alloc_path(); |
| 2446 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2447 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2448 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2449 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2450 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2451 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2452 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2453 | leaf = path->nodes[0]; |
| 2454 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2455 | struct btrfs_inode_item); |
| 2456 | |
| 2457 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2458 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2459 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2460 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2461 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2462 | |
| 2463 | tspec = btrfs_inode_atime(inode_item); |
| 2464 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2465 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2466 | |
| 2467 | tspec = btrfs_inode_mtime(inode_item); |
| 2468 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2469 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2470 | |
| 2471 | tspec = btrfs_inode_ctime(inode_item); |
| 2472 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2473 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2474 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2475 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2476 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2477 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2478 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2479 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2480 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2481 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2482 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2483 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2484 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2485 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2486 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2487 | /* |
| 2488 | * try to precache a NULL acl entry for files that don't have |
| 2489 | * any xattrs or acls |
| 2490 | */ |
| 2491 | 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] | 2492 | if (!maybe_acls) |
| 2493 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2494 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2495 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2496 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2497 | btrfs_free_path(path); |
| 2498 | inode_item = NULL; |
| 2499 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2500 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2501 | case S_IFREG: |
| 2502 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2503 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2504 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2505 | inode->i_fop = &btrfs_file_operations; |
| 2506 | inode->i_op = &btrfs_file_inode_operations; |
| 2507 | break; |
| 2508 | case S_IFDIR: |
| 2509 | inode->i_fop = &btrfs_dir_file_operations; |
| 2510 | if (root == root->fs_info->tree_root) |
| 2511 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2512 | else |
| 2513 | inode->i_op = &btrfs_dir_inode_operations; |
| 2514 | break; |
| 2515 | case S_IFLNK: |
| 2516 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2517 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2518 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2519 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2520 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2521 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2522 | init_special_inode(inode, inode->i_mode, rdev); |
| 2523 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2524 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2525 | |
| 2526 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2527 | return; |
| 2528 | |
| 2529 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2530 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2531 | make_bad_inode(inode); |
| 2532 | } |
| 2533 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2534 | /* |
| 2535 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2536 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2537 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2538 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2539 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2540 | struct inode *inode) |
| 2541 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2542 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2543 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2544 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2545 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2546 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2547 | |
| 2548 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2549 | inode->i_atime.tv_sec); |
| 2550 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2551 | inode->i_atime.tv_nsec); |
| 2552 | |
| 2553 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2554 | inode->i_mtime.tv_sec); |
| 2555 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2556 | inode->i_mtime.tv_nsec); |
| 2557 | |
| 2558 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2559 | inode->i_ctime.tv_sec); |
| 2560 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2561 | inode->i_ctime.tv_nsec); |
| 2562 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2563 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2564 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2565 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2566 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2567 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2568 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2569 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2570 | } |
| 2571 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2572 | /* |
| 2573 | * copy everything in the in-memory inode into the btree. |
| 2574 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2575 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2576 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2577 | { |
| 2578 | struct btrfs_inode_item *inode_item; |
| 2579 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2580 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2581 | int ret; |
| 2582 | |
| 2583 | path = btrfs_alloc_path(); |
| 2584 | BUG_ON(!path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2585 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2586 | ret = btrfs_lookup_inode(trans, root, path, |
| 2587 | &BTRFS_I(inode)->location, 1); |
| 2588 | if (ret) { |
| 2589 | if (ret > 0) |
| 2590 | ret = -ENOENT; |
| 2591 | goto failed; |
| 2592 | } |
| 2593 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2594 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2595 | leaf = path->nodes[0]; |
| 2596 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2597 | struct btrfs_inode_item); |
| 2598 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2599 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2600 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2601 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2602 | ret = 0; |
| 2603 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2604 | btrfs_free_path(path); |
| 2605 | return ret; |
| 2606 | } |
| 2607 | |
| 2608 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2609 | /* |
| 2610 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2611 | * recovery code. It remove a link in a directory with a given name, and |
| 2612 | * also drops the back refs in the inode to the directory |
| 2613 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2614 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2615 | struct btrfs_root *root, |
| 2616 | struct inode *dir, struct inode *inode, |
| 2617 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2618 | { |
| 2619 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2620 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2621 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2622 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2623 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2624 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2625 | |
| 2626 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2627 | if (!path) { |
| 2628 | ret = -ENOMEM; |
| 2629 | goto err; |
| 2630 | } |
| 2631 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2632 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2633 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2634 | name, name_len, -1); |
| 2635 | if (IS_ERR(di)) { |
| 2636 | ret = PTR_ERR(di); |
| 2637 | goto err; |
| 2638 | } |
| 2639 | if (!di) { |
| 2640 | ret = -ENOENT; |
| 2641 | goto err; |
| 2642 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2643 | leaf = path->nodes[0]; |
| 2644 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2645 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2646 | if (ret) |
| 2647 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2648 | btrfs_release_path(root, path); |
| 2649 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2650 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2651 | inode->i_ino, |
| 2652 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2653 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2654 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2655 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2656 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2657 | goto err; |
| 2658 | } |
| 2659 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2660 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2661 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2662 | if (IS_ERR(di)) { |
| 2663 | ret = PTR_ERR(di); |
| 2664 | goto err; |
| 2665 | } |
| 2666 | if (!di) { |
| 2667 | ret = -ENOENT; |
| 2668 | goto err; |
| 2669 | } |
| 2670 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2671 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2672 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2673 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 2674 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2675 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2676 | |
| 2677 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2678 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 2679 | if (ret == -ENOENT) |
| 2680 | ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2681 | err: |
| 2682 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2683 | if (ret) |
| 2684 | goto out; |
| 2685 | |
| 2686 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2687 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2688 | btrfs_update_inode(trans, root, dir); |
| 2689 | btrfs_drop_nlink(inode); |
| 2690 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2691 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2692 | return ret; |
| 2693 | } |
| 2694 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2695 | /* helper to check if there is any shared block in the path */ |
| 2696 | static int check_path_shared(struct btrfs_root *root, |
| 2697 | struct btrfs_path *path) |
| 2698 | { |
| 2699 | struct extent_buffer *eb; |
| 2700 | int level; |
Dan Carpenter | 0e4dcbe | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 2701 | u64 refs = 1; |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 2702 | int uninitialized_var(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2703 | |
| 2704 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
| 2705 | if (!path->nodes[level]) |
| 2706 | break; |
| 2707 | eb = path->nodes[level]; |
| 2708 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2709 | continue; |
| 2710 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2711 | &refs, NULL); |
| 2712 | if (refs > 1) |
| 2713 | return 1; |
| 2714 | } |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 2715 | return ret; /* XXX callers? */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | /* |
| 2719 | * helper to start transaction for unlink and rmdir. |
| 2720 | * |
| 2721 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2722 | * so in enospc case, we should make sure they will free space before |
| 2723 | * allowing them to use the global metadata reservation. |
| 2724 | */ |
| 2725 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2726 | struct dentry *dentry) |
| 2727 | { |
| 2728 | struct btrfs_trans_handle *trans; |
| 2729 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2730 | struct btrfs_path *path; |
| 2731 | struct btrfs_inode_ref *ref; |
| 2732 | struct btrfs_dir_item *di; |
| 2733 | struct inode *inode = dentry->d_inode; |
| 2734 | u64 index; |
| 2735 | int check_link = 1; |
| 2736 | int err = -ENOSPC; |
| 2737 | int ret; |
| 2738 | |
| 2739 | trans = btrfs_start_transaction(root, 10); |
| 2740 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 2741 | return trans; |
| 2742 | |
| 2743 | if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 2744 | return ERR_PTR(-ENOSPC); |
| 2745 | |
| 2746 | /* check if there is someone else holds reference */ |
| 2747 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 2748 | return ERR_PTR(-ENOSPC); |
| 2749 | |
| 2750 | if (atomic_read(&inode->i_count) > 2) |
| 2751 | return ERR_PTR(-ENOSPC); |
| 2752 | |
| 2753 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 2754 | return ERR_PTR(-ENOSPC); |
| 2755 | |
| 2756 | path = btrfs_alloc_path(); |
| 2757 | if (!path) { |
| 2758 | root->fs_info->enospc_unlink = 0; |
| 2759 | return ERR_PTR(-ENOMEM); |
| 2760 | } |
| 2761 | |
| 2762 | trans = btrfs_start_transaction(root, 0); |
| 2763 | if (IS_ERR(trans)) { |
| 2764 | btrfs_free_path(path); |
| 2765 | root->fs_info->enospc_unlink = 0; |
| 2766 | return trans; |
| 2767 | } |
| 2768 | |
| 2769 | path->skip_locking = 1; |
| 2770 | path->search_commit_root = 1; |
| 2771 | |
| 2772 | ret = btrfs_lookup_inode(trans, root, path, |
| 2773 | &BTRFS_I(dir)->location, 0); |
| 2774 | if (ret < 0) { |
| 2775 | err = ret; |
| 2776 | goto out; |
| 2777 | } |
| 2778 | if (ret == 0) { |
| 2779 | if (check_path_shared(root, path)) |
| 2780 | goto out; |
| 2781 | } else { |
| 2782 | check_link = 0; |
| 2783 | } |
| 2784 | btrfs_release_path(root, path); |
| 2785 | |
| 2786 | ret = btrfs_lookup_inode(trans, root, path, |
| 2787 | &BTRFS_I(inode)->location, 0); |
| 2788 | if (ret < 0) { |
| 2789 | err = ret; |
| 2790 | goto out; |
| 2791 | } |
| 2792 | if (ret == 0) { |
| 2793 | if (check_path_shared(root, path)) |
| 2794 | goto out; |
| 2795 | } else { |
| 2796 | check_link = 0; |
| 2797 | } |
| 2798 | btrfs_release_path(root, path); |
| 2799 | |
| 2800 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 2801 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 2802 | inode->i_ino, (u64)-1, 0); |
| 2803 | if (ret < 0) { |
| 2804 | err = ret; |
| 2805 | goto out; |
| 2806 | } |
| 2807 | BUG_ON(ret == 0); |
| 2808 | if (check_path_shared(root, path)) |
| 2809 | goto out; |
| 2810 | btrfs_release_path(root, path); |
| 2811 | } |
| 2812 | |
| 2813 | if (!check_link) { |
| 2814 | err = 0; |
| 2815 | goto out; |
| 2816 | } |
| 2817 | |
| 2818 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2819 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2820 | if (IS_ERR(di)) { |
| 2821 | err = PTR_ERR(di); |
| 2822 | goto out; |
| 2823 | } |
| 2824 | if (di) { |
| 2825 | if (check_path_shared(root, path)) |
| 2826 | goto out; |
| 2827 | } else { |
| 2828 | err = 0; |
| 2829 | goto out; |
| 2830 | } |
| 2831 | btrfs_release_path(root, path); |
| 2832 | |
| 2833 | ref = btrfs_lookup_inode_ref(trans, root, path, |
| 2834 | dentry->d_name.name, dentry->d_name.len, |
| 2835 | inode->i_ino, dir->i_ino, 0); |
| 2836 | if (IS_ERR(ref)) { |
| 2837 | err = PTR_ERR(ref); |
| 2838 | goto out; |
| 2839 | } |
| 2840 | BUG_ON(!ref); |
| 2841 | if (check_path_shared(root, path)) |
| 2842 | goto out; |
| 2843 | index = btrfs_inode_ref_index(path->nodes[0], ref); |
| 2844 | btrfs_release_path(root, path); |
| 2845 | |
| 2846 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index, |
| 2847 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2848 | if (IS_ERR(di)) { |
| 2849 | err = PTR_ERR(di); |
| 2850 | goto out; |
| 2851 | } |
| 2852 | BUG_ON(ret == -ENOENT); |
| 2853 | if (check_path_shared(root, path)) |
| 2854 | goto out; |
| 2855 | |
| 2856 | err = 0; |
| 2857 | out: |
| 2858 | btrfs_free_path(path); |
| 2859 | if (err) { |
| 2860 | btrfs_end_transaction(trans, root); |
| 2861 | root->fs_info->enospc_unlink = 0; |
| 2862 | return ERR_PTR(err); |
| 2863 | } |
| 2864 | |
| 2865 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 2866 | return trans; |
| 2867 | } |
| 2868 | |
| 2869 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 2870 | struct btrfs_root *root) |
| 2871 | { |
| 2872 | if (trans->block_rsv == &root->fs_info->global_block_rsv) { |
| 2873 | BUG_ON(!root->fs_info->enospc_unlink); |
| 2874 | root->fs_info->enospc_unlink = 0; |
| 2875 | } |
| 2876 | btrfs_end_transaction_throttle(trans, root); |
| 2877 | } |
| 2878 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2879 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2880 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2881 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2882 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2883 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2884 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2885 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2886 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2887 | trans = __unlink_start_trans(dir, dentry); |
| 2888 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2889 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2890 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2891 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2892 | |
| 2893 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2894 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2895 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2896 | dentry->d_name.name, dentry->d_name.len); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2897 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2898 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2899 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2900 | ret = btrfs_orphan_add(trans, inode); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2901 | BUG_ON(ret); |
| 2902 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2903 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2904 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2905 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2906 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2907 | return ret; |
| 2908 | } |
| 2909 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2910 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2911 | struct btrfs_root *root, |
| 2912 | struct inode *dir, u64 objectid, |
| 2913 | const char *name, int name_len) |
| 2914 | { |
| 2915 | struct btrfs_path *path; |
| 2916 | struct extent_buffer *leaf; |
| 2917 | struct btrfs_dir_item *di; |
| 2918 | struct btrfs_key key; |
| 2919 | u64 index; |
| 2920 | int ret; |
| 2921 | |
| 2922 | path = btrfs_alloc_path(); |
| 2923 | if (!path) |
| 2924 | return -ENOMEM; |
| 2925 | |
| 2926 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2927 | 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 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2938 | objectid, root->root_key.objectid, |
| 2939 | dir->i_ino, &index, name, name_len); |
| 2940 | if (ret < 0) { |
| 2941 | BUG_ON(ret != -ENOENT); |
| 2942 | di = btrfs_search_dir_index_item(root, path, dir->i_ino, |
| 2943 | name, name_len); |
| 2944 | BUG_ON(!di || IS_ERR(di)); |
| 2945 | |
| 2946 | leaf = path->nodes[0]; |
| 2947 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2948 | btrfs_release_path(root, path); |
| 2949 | index = key.offset; |
| 2950 | } |
| 2951 | |
| 2952 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 2953 | index, name, name_len, -1); |
| 2954 | BUG_ON(!di || IS_ERR(di)); |
| 2955 | |
| 2956 | leaf = path->nodes[0]; |
| 2957 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2958 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2959 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2960 | BUG_ON(ret); |
| 2961 | btrfs_release_path(root, path); |
| 2962 | |
| 2963 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2964 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2965 | ret = btrfs_update_inode(trans, root, dir); |
| 2966 | BUG_ON(ret); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2967 | |
| 2968 | btrfs_free_path(path); |
| 2969 | return 0; |
| 2970 | } |
| 2971 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2972 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 2973 | { |
| 2974 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2975 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2976 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2977 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2978 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2979 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 2980 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2981 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 2982 | return -ENOTEMPTY; |
| 2983 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2984 | trans = __unlink_start_trans(dir, dentry); |
| 2985 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2986 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2987 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2988 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2989 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2990 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 2991 | err = btrfs_unlink_subvol(trans, root, dir, |
| 2992 | BTRFS_I(inode)->location.objectid, |
| 2993 | dentry->d_name.name, |
| 2994 | dentry->d_name.len); |
| 2995 | goto out; |
| 2996 | } |
| 2997 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2998 | err = btrfs_orphan_add(trans, inode); |
| 2999 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3000 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3001 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3002 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3003 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3004 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3005 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3006 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3007 | out: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3008 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3009 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3010 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3011 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3012 | return err; |
| 3013 | } |
| 3014 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3015 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3016 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3017 | * when truncating bytes in a file, it is possible to avoid reading |
| 3018 | * the leaves that contain only checksum items. This can be the |
| 3019 | * majority of the IO required to delete a large file, but it must |
| 3020 | * be done carefully. |
| 3021 | * |
| 3022 | * The keys in the level just above the leaves are checked to make sure |
| 3023 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 3024 | * after the new size. |
| 3025 | * |
| 3026 | * Then the key for the next leaf is checked to make sure it also has |
| 3027 | * a checksum item for the same file. If it does, we know our target leaf |
| 3028 | * contains only checksum items, and it can be safely freed without reading |
| 3029 | * it. |
| 3030 | * |
| 3031 | * This is just an optimization targeted at large files. It may do |
| 3032 | * nothing. It will return 0 unless things went badly. |
| 3033 | */ |
| 3034 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 3035 | struct btrfs_root *root, |
| 3036 | struct btrfs_path *path, |
| 3037 | struct inode *inode, u64 new_size) |
| 3038 | { |
| 3039 | struct btrfs_key key; |
| 3040 | int ret; |
| 3041 | int nritems; |
| 3042 | struct btrfs_key found_key; |
| 3043 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3044 | struct btrfs_leaf_ref *ref; |
| 3045 | u64 leaf_gen; |
| 3046 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3047 | |
| 3048 | path->lowest_level = 1; |
| 3049 | key.objectid = inode->i_ino; |
| 3050 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 3051 | key.offset = new_size; |
| 3052 | again: |
| 3053 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3054 | if (ret < 0) |
| 3055 | goto out; |
| 3056 | |
| 3057 | if (path->nodes[1] == NULL) { |
| 3058 | ret = 0; |
| 3059 | goto out; |
| 3060 | } |
| 3061 | ret = 0; |
| 3062 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 3063 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 3064 | |
| 3065 | if (!nritems) |
| 3066 | goto out; |
| 3067 | |
| 3068 | if (path->slots[1] >= nritems) |
| 3069 | goto next_node; |
| 3070 | |
| 3071 | /* did we find a key greater than anything we want to delete? */ |
| 3072 | if (found_key.objectid > inode->i_ino || |
| 3073 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 3074 | goto out; |
| 3075 | |
| 3076 | /* we check the next key in the node to make sure the leave contains |
| 3077 | * only checksum items. This comparison doesn't work if our |
| 3078 | * leaf is the last one in the node |
| 3079 | */ |
| 3080 | if (path->slots[1] + 1 >= nritems) { |
| 3081 | next_node: |
| 3082 | /* search forward from the last key in the node, this |
| 3083 | * will bring us into the next node in the tree |
| 3084 | */ |
| 3085 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 3086 | |
| 3087 | /* unlikely, but we inc below, so check to be safe */ |
| 3088 | if (found_key.offset == (u64)-1) |
| 3089 | goto out; |
| 3090 | |
| 3091 | /* search_forward needs a path with locks held, do the |
| 3092 | * search again for the original key. It is possible |
| 3093 | * this will race with a balance and return a path that |
| 3094 | * we could modify, but this drop is just an optimization |
| 3095 | * and is allowed to miss some leaves. |
| 3096 | */ |
| 3097 | btrfs_release_path(root, path); |
| 3098 | found_key.offset++; |
| 3099 | |
| 3100 | /* setup a max key for search_forward */ |
| 3101 | other_key.offset = (u64)-1; |
| 3102 | other_key.type = key.type; |
| 3103 | other_key.objectid = key.objectid; |
| 3104 | |
| 3105 | path->keep_locks = 1; |
| 3106 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 3107 | path, 0, 0); |
| 3108 | path->keep_locks = 0; |
| 3109 | if (ret || found_key.objectid != key.objectid || |
| 3110 | found_key.type != key.type) { |
| 3111 | ret = 0; |
| 3112 | goto out; |
| 3113 | } |
| 3114 | |
| 3115 | key.offset = found_key.offset; |
| 3116 | btrfs_release_path(root, path); |
| 3117 | cond_resched(); |
| 3118 | goto again; |
| 3119 | } |
| 3120 | |
| 3121 | /* we know there's one more slot after us in the tree, |
| 3122 | * read that key so we can verify it is also a checksum item |
| 3123 | */ |
| 3124 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 3125 | |
| 3126 | if (found_key.objectid < inode->i_ino) |
| 3127 | goto next_key; |
| 3128 | |
| 3129 | if (found_key.type != key.type || found_key.offset < new_size) |
| 3130 | goto next_key; |
| 3131 | |
| 3132 | /* |
| 3133 | * if the key for the next leaf isn't a csum key from this objectid, |
| 3134 | * we can't be sure there aren't good items inside this leaf. |
| 3135 | * Bail out |
| 3136 | */ |
| 3137 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 3138 | goto out; |
| 3139 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3140 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 3141 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3142 | /* |
| 3143 | * it is safe to delete this leaf, it contains only |
| 3144 | * csum items from this inode at an offset >= new_size |
| 3145 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3146 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3147 | BUG_ON(ret); |
| 3148 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3149 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 3150 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 3151 | if (ref) { |
| 3152 | ref->root_gen = root->root_key.offset; |
| 3153 | ref->bytenr = leaf_start; |
| 3154 | ref->owner = 0; |
| 3155 | ref->generation = leaf_gen; |
| 3156 | ref->nritems = 0; |
| 3157 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3158 | btrfs_sort_leaf_ref(ref); |
| 3159 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3160 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 3161 | WARN_ON(ret); |
| 3162 | btrfs_free_leaf_ref(root, ref); |
| 3163 | } else { |
| 3164 | WARN_ON(1); |
| 3165 | } |
| 3166 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3167 | next_key: |
| 3168 | btrfs_release_path(root, path); |
| 3169 | |
| 3170 | if (other_key.objectid == inode->i_ino && |
| 3171 | other_key.type == key.type && other_key.offset > key.offset) { |
| 3172 | key.offset = other_key.offset; |
| 3173 | cond_resched(); |
| 3174 | goto again; |
| 3175 | } |
| 3176 | ret = 0; |
| 3177 | out: |
| 3178 | /* fixup any changes we've made to the path */ |
| 3179 | path->lowest_level = 0; |
| 3180 | path->keep_locks = 0; |
| 3181 | btrfs_release_path(root, path); |
| 3182 | return ret; |
| 3183 | } |
| 3184 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3185 | #endif |
| 3186 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3187 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3188 | * this can truncate away extent items, csum items and directory items. |
| 3189 | * 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] | 3190 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3191 | * |
| 3192 | * csum items that cross the new i_size are truncated to the new size |
| 3193 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3194 | * |
| 3195 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3196 | * 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] | 3197 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3198 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3199 | struct btrfs_root *root, |
| 3200 | struct inode *inode, |
| 3201 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3202 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3203 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3204 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3205 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3206 | struct btrfs_key key; |
| 3207 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3208 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3209 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3210 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3211 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3212 | u64 mask = root->sectorsize - 1; |
| 3213 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3214 | int found_extent; |
| 3215 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3216 | int pending_del_nr = 0; |
| 3217 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3218 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3219 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3220 | int ret; |
| 3221 | int err = 0; |
| 3222 | |
| 3223 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3224 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3225 | if (root->ref_cows || root == root->fs_info->tree_root) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3226 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3227 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3228 | path = btrfs_alloc_path(); |
| 3229 | BUG_ON(!path); |
Julia Lawall | 33c17ad | 2009-07-22 16:49:01 -0400 | [diff] [blame] | 3230 | path->reada = -1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3231 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3232 | key.objectid = inode->i_ino; |
| 3233 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3234 | key.type = (u8)-1; |
| 3235 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3236 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3237 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3238 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3239 | if (ret < 0) { |
| 3240 | err = ret; |
| 3241 | goto out; |
| 3242 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3243 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3244 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3245 | /* there are no items in the tree for us to truncate, we're |
| 3246 | * done |
| 3247 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3248 | if (path->slots[0] == 0) |
| 3249 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3250 | path->slots[0]--; |
| 3251 | } |
| 3252 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3253 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3254 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3255 | leaf = path->nodes[0]; |
| 3256 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3257 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3258 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3259 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3260 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3261 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3262 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3263 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3264 | break; |
| 3265 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3266 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3267 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3268 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3269 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3270 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3271 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 3272 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 3273 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 3274 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3275 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3276 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3277 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3278 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3279 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3280 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3281 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3282 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3283 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3284 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3285 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3286 | } else { |
| 3287 | if (item_end < new_size) |
| 3288 | break; |
| 3289 | if (found_key.offset >= new_size) |
| 3290 | del_item = 1; |
| 3291 | else |
| 3292 | del_item = 0; |
| 3293 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3294 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3295 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3296 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3297 | goto delete; |
| 3298 | |
| 3299 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3300 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3301 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3302 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3303 | u64 orig_num_bytes = |
| 3304 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3305 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3306 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3307 | extent_num_bytes = extent_num_bytes & |
| 3308 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3309 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3310 | extent_num_bytes); |
| 3311 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3312 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3313 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3314 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3315 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3316 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3317 | extent_num_bytes = |
| 3318 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3319 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3320 | extent_offset = found_key.offset - |
| 3321 | btrfs_file_extent_offset(leaf, fi); |
| 3322 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3323 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3324 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3325 | if (extent_start != 0) { |
| 3326 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3327 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3328 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3329 | } |
| 3330 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3331 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3332 | /* |
| 3333 | * we can't truncate inline items that have had |
| 3334 | * special encodings |
| 3335 | */ |
| 3336 | if (!del_item && |
| 3337 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3338 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3339 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3340 | u32 size = new_size - found_key.offset; |
| 3341 | |
| 3342 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3343 | inode_sub_bytes(inode, item_end + 1 - |
| 3344 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3345 | } |
| 3346 | size = |
| 3347 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3348 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3349 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3350 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3351 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3352 | inode_sub_bytes(inode, item_end + 1 - |
| 3353 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3354 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3355 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3356 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3357 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3358 | if (!pending_del_nr) { |
| 3359 | /* no pending yet, add ourselves */ |
| 3360 | pending_del_slot = path->slots[0]; |
| 3361 | pending_del_nr = 1; |
| 3362 | } else if (pending_del_nr && |
| 3363 | path->slots[0] + 1 == pending_del_slot) { |
| 3364 | /* hop on the pending chunk */ |
| 3365 | pending_del_nr++; |
| 3366 | pending_del_slot = path->slots[0]; |
| 3367 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3368 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3369 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3370 | } else { |
| 3371 | break; |
| 3372 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3373 | if (found_extent && (root->ref_cows || |
| 3374 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3375 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3376 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3377 | extent_num_bytes, 0, |
| 3378 | btrfs_header_owner(leaf), |
| 3379 | inode->i_ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3380 | BUG_ON(ret); |
| 3381 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3382 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3383 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3384 | break; |
| 3385 | |
| 3386 | if (path->slots[0] == 0 || |
| 3387 | path->slots[0] != pending_del_slot) { |
| 3388 | if (root->ref_cows) { |
| 3389 | err = -EAGAIN; |
| 3390 | goto out; |
| 3391 | } |
| 3392 | if (pending_del_nr) { |
| 3393 | ret = btrfs_del_items(trans, root, path, |
| 3394 | pending_del_slot, |
| 3395 | pending_del_nr); |
| 3396 | BUG_ON(ret); |
| 3397 | pending_del_nr = 0; |
| 3398 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3399 | btrfs_release_path(root, path); |
| 3400 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3401 | } else { |
| 3402 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3403 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3404 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3405 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3406 | if (pending_del_nr) { |
| 3407 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3408 | pending_del_nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3409 | BUG_ON(ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3410 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3411 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3412 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3413 | } |
| 3414 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3415 | /* |
| 3416 | * taken from block_truncate_page, but does cow as it zeros out |
| 3417 | * any bytes left in the last page in the file. |
| 3418 | */ |
| 3419 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3420 | { |
| 3421 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3422 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3423 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3424 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3425 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3426 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3427 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3428 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3429 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3430 | struct page *page; |
| 3431 | int ret = 0; |
| 3432 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3433 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3434 | |
| 3435 | if ((offset & (blocksize - 1)) == 0) |
| 3436 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3437 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3438 | if (ret) |
| 3439 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3440 | |
| 3441 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3442 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3443 | page = grab_cache_page(mapping, index); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3444 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3445 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3446 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3447 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3448 | |
| 3449 | page_start = page_offset(page); |
| 3450 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3451 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3452 | if (!PageUptodate(page)) { |
| 3453 | ret = btrfs_readpage(NULL, page); |
| 3454 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3455 | if (page->mapping != mapping) { |
| 3456 | unlock_page(page); |
| 3457 | page_cache_release(page); |
| 3458 | goto again; |
| 3459 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3460 | if (!PageUptodate(page)) { |
| 3461 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3462 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3463 | } |
| 3464 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3465 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3466 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3467 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 3468 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3469 | set_page_extent_mapped(page); |
| 3470 | |
| 3471 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3472 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3473 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3474 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3475 | unlock_page(page); |
| 3476 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3477 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3478 | btrfs_put_ordered_extent(ordered); |
| 3479 | goto again; |
| 3480 | } |
| 3481 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3482 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3483 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3484 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3485 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3486 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3487 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3488 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3489 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3490 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3491 | goto out_unlock; |
| 3492 | } |
| 3493 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3494 | ret = 0; |
| 3495 | if (offset != PAGE_CACHE_SIZE) { |
| 3496 | kaddr = kmap(page); |
| 3497 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3498 | flush_dcache_page(page); |
| 3499 | kunmap(page); |
| 3500 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3501 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3502 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3503 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3504 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3505 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3506 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3507 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3508 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3509 | unlock_page(page); |
| 3510 | page_cache_release(page); |
| 3511 | out: |
| 3512 | return ret; |
| 3513 | } |
| 3514 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3515 | int btrfs_cont_expand(struct inode *inode, loff_t size) |
| 3516 | { |
| 3517 | struct btrfs_trans_handle *trans; |
| 3518 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3519 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3520 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3521 | struct extent_state *cached_state = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3522 | u64 mask = root->sectorsize - 1; |
| 3523 | u64 hole_start = (inode->i_size + mask) & ~mask; |
| 3524 | u64 block_end = (size + mask) & ~mask; |
| 3525 | u64 last_byte; |
| 3526 | u64 cur_offset; |
| 3527 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3528 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3529 | |
| 3530 | if (size <= hole_start) |
| 3531 | return 0; |
| 3532 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3533 | while (1) { |
| 3534 | struct btrfs_ordered_extent *ordered; |
| 3535 | btrfs_wait_ordered_range(inode, hole_start, |
| 3536 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3537 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
| 3538 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3539 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3540 | if (!ordered) |
| 3541 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3542 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3543 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3544 | btrfs_put_ordered_extent(ordered); |
| 3545 | } |
| 3546 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3547 | cur_offset = hole_start; |
| 3548 | while (1) { |
| 3549 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3550 | block_end - cur_offset, 0); |
| 3551 | BUG_ON(IS_ERR(em) || !em); |
| 3552 | last_byte = min(extent_map_end(em), block_end); |
| 3553 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3554 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3555 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3556 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3557 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3558 | trans = btrfs_start_transaction(root, 2); |
| 3559 | if (IS_ERR(trans)) { |
| 3560 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3561 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3562 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3563 | btrfs_set_trans_block_group(trans, inode); |
| 3564 | |
| 3565 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3566 | cur_offset + hole_size, |
| 3567 | &hint_byte, 1); |
| 3568 | BUG_ON(err); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3569 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3570 | err = btrfs_insert_file_extent(trans, root, |
| 3571 | inode->i_ino, cur_offset, 0, |
| 3572 | 0, hole_size, 0, hole_size, |
| 3573 | 0, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3574 | BUG_ON(err); |
| 3575 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3576 | btrfs_drop_extent_cache(inode, hole_start, |
| 3577 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3578 | |
| 3579 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3580 | } |
| 3581 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3582 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3583 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3584 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3585 | break; |
| 3586 | } |
| 3587 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3588 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3589 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3590 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3591 | return err; |
| 3592 | } |
| 3593 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3594 | static int btrfs_setattr_size(struct inode *inode, struct iattr *attr) |
| 3595 | { |
| 3596 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3597 | struct btrfs_trans_handle *trans; |
| 3598 | unsigned long nr; |
| 3599 | int ret; |
| 3600 | |
| 3601 | if (attr->ia_size == inode->i_size) |
| 3602 | return 0; |
| 3603 | |
| 3604 | if (attr->ia_size > inode->i_size) { |
| 3605 | unsigned long limit; |
| 3606 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
| 3607 | if (attr->ia_size > inode->i_sb->s_maxbytes) |
| 3608 | return -EFBIG; |
| 3609 | if (limit != RLIM_INFINITY && attr->ia_size > limit) { |
| 3610 | send_sig(SIGXFSZ, current, 0); |
| 3611 | return -EFBIG; |
| 3612 | } |
| 3613 | } |
| 3614 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3615 | trans = btrfs_start_transaction(root, 5); |
| 3616 | if (IS_ERR(trans)) |
| 3617 | return PTR_ERR(trans); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3618 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3619 | btrfs_set_trans_block_group(trans, inode); |
| 3620 | |
| 3621 | ret = btrfs_orphan_add(trans, inode); |
| 3622 | BUG_ON(ret); |
| 3623 | |
| 3624 | nr = trans->blocks_used; |
| 3625 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3626 | btrfs_btree_balance_dirty(root, nr); |
| 3627 | |
| 3628 | if (attr->ia_size > inode->i_size) { |
| 3629 | ret = btrfs_cont_expand(inode, attr->ia_size); |
| 3630 | if (ret) { |
| 3631 | btrfs_truncate(inode); |
| 3632 | return ret; |
| 3633 | } |
| 3634 | |
| 3635 | i_size_write(inode, attr->ia_size); |
| 3636 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
| 3637 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3638 | trans = btrfs_start_transaction(root, 0); |
| 3639 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3640 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3641 | trans->block_rsv = root->orphan_block_rsv; |
| 3642 | BUG_ON(!trans->block_rsv); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3643 | |
| 3644 | ret = btrfs_update_inode(trans, root, inode); |
| 3645 | BUG_ON(ret); |
| 3646 | if (inode->i_nlink > 0) { |
| 3647 | ret = btrfs_orphan_del(trans, inode); |
| 3648 | BUG_ON(ret); |
| 3649 | } |
| 3650 | nr = trans->blocks_used; |
| 3651 | btrfs_end_transaction(trans, root); |
| 3652 | btrfs_btree_balance_dirty(root, nr); |
| 3653 | return 0; |
| 3654 | } |
| 3655 | |
| 3656 | /* |
| 3657 | * We're truncating a file that used to have good data down to |
| 3658 | * zero. Make sure it gets into the ordered flush list so that |
| 3659 | * any new writes get down to disk quickly. |
| 3660 | */ |
| 3661 | if (attr->ia_size == 0) |
| 3662 | BTRFS_I(inode)->ordered_data_close = 1; |
| 3663 | |
| 3664 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3665 | ret = vmtruncate(inode, attr->ia_size); |
| 3666 | BUG_ON(ret); |
| 3667 | |
| 3668 | return 0; |
| 3669 | } |
| 3670 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3671 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3672 | { |
| 3673 | struct inode *inode = dentry->d_inode; |
| 3674 | int err; |
| 3675 | |
| 3676 | err = inode_change_ok(inode, attr); |
| 3677 | if (err) |
| 3678 | return err; |
| 3679 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3680 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3681 | err = btrfs_setattr_size(inode, attr); |
| 3682 | if (err) |
| 3683 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3684 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3685 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3686 | if (attr->ia_valid) { |
| 3687 | setattr_copy(inode, attr); |
| 3688 | mark_inode_dirty(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3689 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3690 | if (attr->ia_valid & ATTR_MODE) |
| 3691 | err = btrfs_acl_chmod(inode); |
| 3692 | } |
| 3693 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3694 | return err; |
| 3695 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3696 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3697 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3698 | { |
| 3699 | struct btrfs_trans_handle *trans; |
| 3700 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3701 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3702 | int ret; |
| 3703 | |
| 3704 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3705 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
| 3706 | root == root->fs_info->tree_root)) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3707 | goto no_delete; |
| 3708 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3709 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3710 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3711 | goto no_delete; |
| 3712 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3713 | /* 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] | 3714 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3715 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3716 | if (root->fs_info->log_root_recovering) { |
| 3717 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3718 | goto no_delete; |
| 3719 | } |
| 3720 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3721 | if (inode->i_nlink > 0) { |
| 3722 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3723 | goto no_delete; |
| 3724 | } |
| 3725 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3726 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3727 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3728 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3729 | trans = btrfs_start_transaction(root, 0); |
| 3730 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3731 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3732 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3733 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3734 | ret = btrfs_block_rsv_check(trans, root, |
| 3735 | root->orphan_block_rsv, 0, 5); |
| 3736 | if (ret) { |
| 3737 | BUG_ON(ret != -EAGAIN); |
| 3738 | ret = btrfs_commit_transaction(trans, root); |
| 3739 | BUG_ON(ret); |
| 3740 | continue; |
| 3741 | } |
| 3742 | |
| 3743 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3744 | if (ret != -EAGAIN) |
| 3745 | break; |
| 3746 | |
| 3747 | nr = trans->blocks_used; |
| 3748 | btrfs_end_transaction(trans, root); |
| 3749 | trans = NULL; |
| 3750 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3751 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3752 | } |
| 3753 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3754 | if (ret == 0) { |
| 3755 | ret = btrfs_orphan_del(trans, inode); |
| 3756 | BUG_ON(ret); |
| 3757 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3758 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3759 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3760 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3761 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3762 | no_delete: |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3763 | end_writeback(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3764 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | /* |
| 3768 | * this returns the key found in the dir entry in the location pointer. |
| 3769 | * If no dir entries were found, location->objectid is 0. |
| 3770 | */ |
| 3771 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3772 | struct btrfs_key *location) |
| 3773 | { |
| 3774 | const char *name = dentry->d_name.name; |
| 3775 | int namelen = dentry->d_name.len; |
| 3776 | struct btrfs_dir_item *di; |
| 3777 | struct btrfs_path *path; |
| 3778 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3779 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3780 | |
| 3781 | path = btrfs_alloc_path(); |
| 3782 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3783 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3784 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 3785 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3786 | if (IS_ERR(di)) |
| 3787 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3788 | |
| 3789 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3790 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3791 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3792 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3793 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3794 | btrfs_free_path(path); |
| 3795 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3796 | out_err: |
| 3797 | location->objectid = 0; |
| 3798 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | /* |
| 3802 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3803 | * needs to be changed to reflect the root directory of the tree root. This |
| 3804 | * is kind of like crossing a mount point. |
| 3805 | */ |
| 3806 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3807 | struct inode *dir, |
| 3808 | struct dentry *dentry, |
| 3809 | struct btrfs_key *location, |
| 3810 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3811 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3812 | struct btrfs_path *path; |
| 3813 | struct btrfs_root *new_root; |
| 3814 | struct btrfs_root_ref *ref; |
| 3815 | struct extent_buffer *leaf; |
| 3816 | int ret; |
| 3817 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3818 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3819 | path = btrfs_alloc_path(); |
| 3820 | if (!path) { |
| 3821 | err = -ENOMEM; |
| 3822 | goto out; |
| 3823 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3824 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3825 | err = -ENOENT; |
| 3826 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3827 | BTRFS_I(dir)->root->root_key.objectid, |
| 3828 | location->objectid); |
| 3829 | if (ret) { |
| 3830 | if (ret < 0) |
| 3831 | err = ret; |
| 3832 | goto out; |
| 3833 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3834 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3835 | leaf = path->nodes[0]; |
| 3836 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 3837 | if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || |
| 3838 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3839 | goto out; |
| 3840 | |
| 3841 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3842 | (unsigned long)(ref + 1), |
| 3843 | dentry->d_name.len); |
| 3844 | if (ret) |
| 3845 | goto out; |
| 3846 | |
| 3847 | btrfs_release_path(root->fs_info->tree_root, path); |
| 3848 | |
| 3849 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3850 | if (IS_ERR(new_root)) { |
| 3851 | err = PTR_ERR(new_root); |
| 3852 | goto out; |
| 3853 | } |
| 3854 | |
| 3855 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3856 | err = -ENOENT; |
| 3857 | goto out; |
| 3858 | } |
| 3859 | |
| 3860 | *sub_root = new_root; |
| 3861 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3862 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3863 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3864 | err = 0; |
| 3865 | out: |
| 3866 | btrfs_free_path(path); |
| 3867 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3868 | } |
| 3869 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3870 | static void inode_tree_add(struct inode *inode) |
| 3871 | { |
| 3872 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3873 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3874 | struct rb_node **p; |
| 3875 | struct rb_node *parent; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3876 | again: |
| 3877 | p = &root->inode_tree.rb_node; |
| 3878 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3879 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3880 | if (hlist_unhashed(&inode->i_hash)) |
| 3881 | return; |
| 3882 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3883 | spin_lock(&root->inode_lock); |
| 3884 | while (*p) { |
| 3885 | parent = *p; |
| 3886 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3887 | |
| 3888 | if (inode->i_ino < entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3889 | p = &parent->rb_left; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3890 | else if (inode->i_ino > entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3891 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3892 | else { |
| 3893 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 3894 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3895 | rb_erase(parent, &root->inode_tree); |
| 3896 | RB_CLEAR_NODE(parent); |
| 3897 | spin_unlock(&root->inode_lock); |
| 3898 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3899 | } |
| 3900 | } |
| 3901 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3902 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3903 | spin_unlock(&root->inode_lock); |
| 3904 | } |
| 3905 | |
| 3906 | static void inode_tree_del(struct inode *inode) |
| 3907 | { |
| 3908 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3909 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3910 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3911 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3912 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3913 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3914 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3915 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3916 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3917 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3918 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3919 | /* |
| 3920 | * Free space cache has inodes in the tree root, but the tree root has a |
| 3921 | * root_refs of 0, so this could end up dropping the tree root as a |
| 3922 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 3923 | * make sure we don't drop it. |
| 3924 | */ |
| 3925 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 3926 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3927 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3928 | spin_lock(&root->inode_lock); |
| 3929 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3930 | spin_unlock(&root->inode_lock); |
| 3931 | if (empty) |
| 3932 | btrfs_add_dead_root(root); |
| 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 3937 | { |
| 3938 | struct rb_node *node; |
| 3939 | struct rb_node *prev; |
| 3940 | struct btrfs_inode *entry; |
| 3941 | struct inode *inode; |
| 3942 | u64 objectid = 0; |
| 3943 | |
| 3944 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3945 | |
| 3946 | spin_lock(&root->inode_lock); |
| 3947 | again: |
| 3948 | node = root->inode_tree.rb_node; |
| 3949 | prev = NULL; |
| 3950 | while (node) { |
| 3951 | prev = node; |
| 3952 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3953 | |
| 3954 | if (objectid < entry->vfs_inode.i_ino) |
| 3955 | node = node->rb_left; |
| 3956 | else if (objectid > entry->vfs_inode.i_ino) |
| 3957 | node = node->rb_right; |
| 3958 | else |
| 3959 | break; |
| 3960 | } |
| 3961 | if (!node) { |
| 3962 | while (prev) { |
| 3963 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
| 3964 | if (objectid <= entry->vfs_inode.i_ino) { |
| 3965 | node = prev; |
| 3966 | break; |
| 3967 | } |
| 3968 | prev = rb_next(prev); |
| 3969 | } |
| 3970 | } |
| 3971 | while (node) { |
| 3972 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3973 | objectid = entry->vfs_inode.i_ino + 1; |
| 3974 | inode = igrab(&entry->vfs_inode); |
| 3975 | if (inode) { |
| 3976 | spin_unlock(&root->inode_lock); |
| 3977 | if (atomic_read(&inode->i_count) > 1) |
| 3978 | d_prune_aliases(inode); |
| 3979 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 3980 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3981 | * the inode cache when its usage count |
| 3982 | * hits zero. |
| 3983 | */ |
| 3984 | iput(inode); |
| 3985 | cond_resched(); |
| 3986 | spin_lock(&root->inode_lock); |
| 3987 | goto again; |
| 3988 | } |
| 3989 | |
| 3990 | if (cond_resched_lock(&root->inode_lock)) |
| 3991 | goto again; |
| 3992 | |
| 3993 | node = rb_next(node); |
| 3994 | } |
| 3995 | spin_unlock(&root->inode_lock); |
| 3996 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3997 | } |
| 3998 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3999 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4000 | { |
| 4001 | struct btrfs_iget_args *args = p; |
| 4002 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4003 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4004 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4005 | return 0; |
| 4006 | } |
| 4007 | |
| 4008 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4009 | { |
| 4010 | struct btrfs_iget_args *args = opaque; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4011 | return args->ino == inode->i_ino && |
| 4012 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4013 | } |
| 4014 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4015 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4016 | u64 objectid, |
| 4017 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4018 | { |
| 4019 | struct inode *inode; |
| 4020 | struct btrfs_iget_args args; |
| 4021 | args.ino = objectid; |
| 4022 | args.root = root; |
| 4023 | |
| 4024 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 4025 | btrfs_init_locked_inode, |
| 4026 | (void *)&args); |
| 4027 | return inode; |
| 4028 | } |
| 4029 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4030 | /* Get an inode object given its location and corresponding root. |
| 4031 | * Returns in *is_new if the inode was read from disk |
| 4032 | */ |
| 4033 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4034 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4035 | { |
| 4036 | struct inode *inode; |
| 4037 | |
| 4038 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4039 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4040 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4041 | |
| 4042 | if (inode->i_state & I_NEW) { |
| 4043 | BTRFS_I(inode)->root = root; |
| 4044 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4045 | btrfs_read_locked_inode(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4046 | |
| 4047 | inode_tree_add(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4048 | unlock_new_inode(inode); |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4049 | if (new) |
| 4050 | *new = 1; |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | return inode; |
| 4054 | } |
| 4055 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4056 | static struct inode *new_simple_dir(struct super_block *s, |
| 4057 | struct btrfs_key *key, |
| 4058 | struct btrfs_root *root) |
| 4059 | { |
| 4060 | struct inode *inode = new_inode(s); |
| 4061 | |
| 4062 | if (!inode) |
| 4063 | return ERR_PTR(-ENOMEM); |
| 4064 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4065 | BTRFS_I(inode)->root = root; |
| 4066 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 4067 | BTRFS_I(inode)->dummy_inode = 1; |
| 4068 | |
| 4069 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 4070 | inode->i_op = &simple_dir_inode_operations; |
| 4071 | inode->i_fop = &simple_dir_operations; |
| 4072 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 4073 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 4074 | |
| 4075 | return inode; |
| 4076 | } |
| 4077 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4078 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4079 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4080 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4081 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4082 | struct btrfs_root *sub_root = root; |
| 4083 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4084 | int index; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4085 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4086 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4087 | dentry->d_op = &btrfs_dentry_operations; |
| 4088 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4089 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 4090 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4091 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4092 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4093 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4094 | if (ret < 0) |
| 4095 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4096 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4097 | if (location.objectid == 0) |
| 4098 | return NULL; |
| 4099 | |
| 4100 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4101 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4102 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4103 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4104 | |
| 4105 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4106 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4107 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4108 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4109 | &location, &sub_root); |
| 4110 | if (ret < 0) { |
| 4111 | if (ret != -ENOENT) |
| 4112 | inode = ERR_PTR(ret); |
| 4113 | else |
| 4114 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4115 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4116 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4117 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4118 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4119 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4120 | if (root != sub_root) { |
| 4121 | down_read(&root->fs_info->cleanup_work_sem); |
| 4122 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
| 4123 | btrfs_orphan_cleanup(sub_root); |
| 4124 | up_read(&root->fs_info->cleanup_work_sem); |
| 4125 | } |
| 4126 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4127 | return inode; |
| 4128 | } |
| 4129 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4130 | static int btrfs_dentry_delete(struct dentry *dentry) |
| 4131 | { |
| 4132 | struct btrfs_root *root; |
| 4133 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4134 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 4135 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4136 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4137 | if (dentry->d_inode) { |
| 4138 | root = BTRFS_I(dentry->d_inode)->root; |
| 4139 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4140 | return 1; |
| 4141 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4142 | return 0; |
| 4143 | } |
| 4144 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4145 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 4146 | struct nameidata *nd) |
| 4147 | { |
| 4148 | struct inode *inode; |
| 4149 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4150 | inode = btrfs_lookup_dentry(dir, dentry); |
| 4151 | if (IS_ERR(inode)) |
| 4152 | return ERR_CAST(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4153 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4154 | return d_splice_alias(inode, dentry); |
| 4155 | } |
| 4156 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4157 | static unsigned char btrfs_filetype_table[] = { |
| 4158 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 4159 | }; |
| 4160 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 4161 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 4162 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4163 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4164 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4165 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4166 | struct btrfs_item *item; |
| 4167 | struct btrfs_dir_item *di; |
| 4168 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4169 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4170 | struct btrfs_path *path; |
| 4171 | int ret; |
| 4172 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4173 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4174 | int slot; |
| 4175 | int advance; |
| 4176 | unsigned char d_type; |
| 4177 | int over = 0; |
| 4178 | u32 di_cur; |
| 4179 | u32 di_total; |
| 4180 | u32 di_len; |
| 4181 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4182 | char tmp_name[32]; |
| 4183 | char *name_ptr; |
| 4184 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4185 | |
| 4186 | /* FIXME, use a real flag for deciding about the key type */ |
| 4187 | if (root->fs_info->tree_root == root) |
| 4188 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4189 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4190 | /* special case for "." */ |
| 4191 | if (filp->f_pos == 0) { |
| 4192 | over = filldir(dirent, ".", 1, |
| 4193 | 1, inode->i_ino, |
| 4194 | DT_DIR); |
| 4195 | if (over) |
| 4196 | return 0; |
| 4197 | filp->f_pos = 1; |
| 4198 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4199 | /* special case for .., just use the back ref */ |
| 4200 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4201 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4202 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4203 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4204 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4205 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4206 | filp->f_pos = 2; |
| 4207 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4208 | path = btrfs_alloc_path(); |
| 4209 | path->reada = 2; |
| 4210 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4211 | btrfs_set_key_type(&key, key_type); |
| 4212 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4213 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4214 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4215 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4216 | if (ret < 0) |
| 4217 | goto err; |
| 4218 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4219 | |
| 4220 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4221 | leaf = path->nodes[0]; |
| 4222 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4223 | slot = path->slots[0]; |
| 4224 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4225 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4226 | ret = btrfs_next_leaf(root, path); |
| 4227 | if (ret) |
| 4228 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4229 | leaf = path->nodes[0]; |
| 4230 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4231 | slot = path->slots[0]; |
| 4232 | } else { |
| 4233 | slot++; |
| 4234 | path->slots[0]++; |
| 4235 | } |
| 4236 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4237 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4238 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4239 | item = btrfs_item_nr(leaf, slot); |
| 4240 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4241 | |
| 4242 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4243 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4244 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4245 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4246 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4247 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4248 | |
| 4249 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4250 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4251 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 4252 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4253 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4254 | |
| 4255 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4256 | struct btrfs_key location; |
| 4257 | |
| 4258 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4259 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4260 | name_ptr = tmp_name; |
| 4261 | } else { |
| 4262 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4263 | if (!name_ptr) { |
| 4264 | ret = -ENOMEM; |
| 4265 | goto err; |
| 4266 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4267 | } |
| 4268 | read_extent_buffer(leaf, name_ptr, |
| 4269 | (unsigned long)(di + 1), name_len); |
| 4270 | |
| 4271 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4272 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4273 | |
| 4274 | /* is this a reference to our own snapshot? If so |
| 4275 | * skip it |
| 4276 | */ |
| 4277 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4278 | location.objectid == root->root_key.objectid) { |
| 4279 | over = 0; |
| 4280 | goto skip; |
| 4281 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4282 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4283 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4284 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4285 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4286 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4287 | if (name_ptr != tmp_name) |
| 4288 | kfree(name_ptr); |
| 4289 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4290 | if (over) |
| 4291 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4292 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4293 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4294 | di_cur += di_len; |
| 4295 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4296 | } |
| 4297 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4298 | |
| 4299 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4300 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4301 | /* |
| 4302 | * 32-bit glibc will use getdents64, but then strtol - |
| 4303 | * so the last number we can serve is this. |
| 4304 | */ |
| 4305 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4306 | else |
| 4307 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4308 | nopos: |
| 4309 | ret = 0; |
| 4310 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4311 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4312 | return ret; |
| 4313 | } |
| 4314 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4315 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4316 | { |
| 4317 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4318 | struct btrfs_trans_handle *trans; |
| 4319 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4320 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4321 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4322 | if (BTRFS_I(inode)->dummy_inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4323 | return 0; |
| 4324 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4325 | smp_mb(); |
| 4326 | nolock = (root->fs_info->closing && root == root->fs_info->tree_root); |
| 4327 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4328 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4329 | if (nolock) |
| 4330 | trans = btrfs_join_transaction_nolock(root, 1); |
| 4331 | else |
| 4332 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4333 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4334 | if (nolock) |
| 4335 | ret = btrfs_end_transaction_nolock(trans, root); |
| 4336 | else |
| 4337 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4338 | } |
| 4339 | return ret; |
| 4340 | } |
| 4341 | |
| 4342 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4343 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4344 | * inode changes. But, it is most likely to find the inode in cache. |
| 4345 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4346 | * to keep or drop this code. |
| 4347 | */ |
| 4348 | void btrfs_dirty_inode(struct inode *inode) |
| 4349 | { |
| 4350 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4351 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4352 | int ret; |
| 4353 | |
| 4354 | if (BTRFS_I(inode)->dummy_inode) |
| 4355 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4356 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4357 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4358 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4359 | |
| 4360 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4361 | if (ret && ret == -ENOSPC) { |
| 4362 | /* whoops, lets try again with the full transaction */ |
| 4363 | btrfs_end_transaction(trans, root); |
| 4364 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4365 | if (IS_ERR(trans)) { |
| 4366 | if (printk_ratelimit()) { |
| 4367 | printk(KERN_ERR "btrfs: fail to " |
| 4368 | "dirty inode %lu error %ld\n", |
| 4369 | inode->i_ino, PTR_ERR(trans)); |
| 4370 | } |
| 4371 | return; |
| 4372 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4373 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4374 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4375 | ret = btrfs_update_inode(trans, root, inode); |
| 4376 | if (ret) { |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4377 | if (printk_ratelimit()) { |
| 4378 | printk(KERN_ERR "btrfs: fail to " |
| 4379 | "dirty inode %lu error %d\n", |
| 4380 | inode->i_ino, ret); |
| 4381 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4382 | } |
| 4383 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4384 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4385 | } |
| 4386 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4387 | /* |
| 4388 | * find the highest existing sequence number in a directory |
| 4389 | * and then set the in-memory index_cnt variable to reflect |
| 4390 | * free sequence numbers |
| 4391 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4392 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4393 | { |
| 4394 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4395 | struct btrfs_key key, found_key; |
| 4396 | struct btrfs_path *path; |
| 4397 | struct extent_buffer *leaf; |
| 4398 | int ret; |
| 4399 | |
| 4400 | key.objectid = inode->i_ino; |
| 4401 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4402 | key.offset = (u64)-1; |
| 4403 | |
| 4404 | path = btrfs_alloc_path(); |
| 4405 | if (!path) |
| 4406 | return -ENOMEM; |
| 4407 | |
| 4408 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4409 | if (ret < 0) |
| 4410 | goto out; |
| 4411 | /* FIXME: we should be able to handle this */ |
| 4412 | if (ret == 0) |
| 4413 | goto out; |
| 4414 | ret = 0; |
| 4415 | |
| 4416 | /* |
| 4417 | * MAGIC NUMBER EXPLANATION: |
| 4418 | * since we search a directory based on f_pos we have to start at 2 |
| 4419 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4420 | * else has to start at 2 |
| 4421 | */ |
| 4422 | if (path->slots[0] == 0) { |
| 4423 | BTRFS_I(inode)->index_cnt = 2; |
| 4424 | goto out; |
| 4425 | } |
| 4426 | |
| 4427 | path->slots[0]--; |
| 4428 | |
| 4429 | leaf = path->nodes[0]; |
| 4430 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4431 | |
| 4432 | if (found_key.objectid != inode->i_ino || |
| 4433 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4434 | BTRFS_I(inode)->index_cnt = 2; |
| 4435 | goto out; |
| 4436 | } |
| 4437 | |
| 4438 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4439 | out: |
| 4440 | btrfs_free_path(path); |
| 4441 | return ret; |
| 4442 | } |
| 4443 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4444 | /* |
| 4445 | * helper to find a free sequence number in a given directory. This current |
| 4446 | * code is very simple, later versions will do smarter things in the btree |
| 4447 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4448 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4449 | { |
| 4450 | int ret = 0; |
| 4451 | |
| 4452 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 4453 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4454 | if (ret) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4455 | return ret; |
| 4456 | } |
| 4457 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4458 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4459 | BTRFS_I(dir)->index_cnt++; |
| 4460 | |
| 4461 | return ret; |
| 4462 | } |
| 4463 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4464 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4465 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4466 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4467 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4468 | u64 ref_objectid, u64 objectid, |
| 4469 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4470 | { |
| 4471 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4472 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4473 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4474 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4475 | struct btrfs_inode_ref *ref; |
| 4476 | struct btrfs_key key[2]; |
| 4477 | u32 sizes[2]; |
| 4478 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4479 | int ret; |
| 4480 | int owner; |
| 4481 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4482 | path = btrfs_alloc_path(); |
| 4483 | BUG_ON(!path); |
| 4484 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4485 | inode = new_inode(root->fs_info->sb); |
| 4486 | if (!inode) |
| 4487 | return ERR_PTR(-ENOMEM); |
| 4488 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4489 | if (dir) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4490 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4491 | if (ret) { |
| 4492 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4493 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4494 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4495 | } |
| 4496 | /* |
| 4497 | * index_cnt is ignored for everything but a dir, |
| 4498 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4499 | * number |
| 4500 | */ |
| 4501 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4502 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4503 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 4504 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4505 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4506 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4507 | if (mode & S_IFDIR) |
| 4508 | owner = 0; |
| 4509 | else |
| 4510 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4511 | BTRFS_I(inode)->block_group = |
| 4512 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4513 | |
| 4514 | key[0].objectid = objectid; |
| 4515 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4516 | key[0].offset = 0; |
| 4517 | |
| 4518 | key[1].objectid = objectid; |
| 4519 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4520 | key[1].offset = ref_objectid; |
| 4521 | |
| 4522 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4523 | sizes[1] = name_len + sizeof(*ref); |
| 4524 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4525 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4526 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4527 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4528 | goto fail; |
| 4529 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 4530 | inode_init_owner(inode, dir, mode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4531 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4532 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4533 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4534 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4535 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4536 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4537 | |
| 4538 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4539 | struct btrfs_inode_ref); |
| 4540 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4541 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4542 | ptr = (unsigned long)(ref + 1); |
| 4543 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4544 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4545 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4546 | btrfs_free_path(path); |
| 4547 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4548 | location = &BTRFS_I(inode)->location; |
| 4549 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4550 | location->offset = 0; |
| 4551 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4552 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4553 | btrfs_inherit_iflags(inode, dir); |
| 4554 | |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4555 | if ((mode & S_IFREG)) { |
| 4556 | if (btrfs_test_opt(root, NODATASUM)) |
| 4557 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 4558 | if (btrfs_test_opt(root, NODATACOW)) |
| 4559 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4560 | } |
| 4561 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4562 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4563 | inode_tree_add(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4564 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4565 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4566 | if (dir) |
| 4567 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4568 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4569 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4570 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4574 | { |
| 4575 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4576 | } |
| 4577 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4578 | /* |
| 4579 | * utility function to add 'inode' into 'parent_inode' with |
| 4580 | * a give name and a given sequence number. |
| 4581 | * if 'add_backref' is true, also insert a backref from the |
| 4582 | * inode to the parent directory. |
| 4583 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4584 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4585 | struct inode *parent_inode, struct inode *inode, |
| 4586 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4587 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4588 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4589 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4590 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4591 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4592 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4593 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4594 | } else { |
| 4595 | key.objectid = inode->i_ino; |
| 4596 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4597 | key.offset = 0; |
| 4598 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4599 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4600 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4601 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4602 | key.objectid, root->root_key.objectid, |
| 4603 | parent_inode->i_ino, |
| 4604 | index, name, name_len); |
| 4605 | } else if (add_backref) { |
| 4606 | ret = btrfs_insert_inode_ref(trans, root, |
| 4607 | name, name_len, inode->i_ino, |
| 4608 | parent_inode->i_ino, index); |
| 4609 | } |
| 4610 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4611 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4612 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4613 | parent_inode->i_ino, &key, |
| 4614 | btrfs_inode_type(inode), index); |
| 4615 | BUG_ON(ret); |
| 4616 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4617 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4618 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4619 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4620 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4621 | } |
| 4622 | return ret; |
| 4623 | } |
| 4624 | |
| 4625 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4626 | struct dentry *dentry, struct inode *inode, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4627 | int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4628 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4629 | int err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4630 | inode, dentry->d_name.name, |
| 4631 | dentry->d_name.len, backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4632 | if (!err) { |
| 4633 | d_instantiate(dentry, inode); |
| 4634 | return 0; |
| 4635 | } |
| 4636 | if (err > 0) |
| 4637 | err = -EEXIST; |
| 4638 | return err; |
| 4639 | } |
| 4640 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4641 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4642 | int mode, dev_t rdev) |
| 4643 | { |
| 4644 | struct btrfs_trans_handle *trans; |
| 4645 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4646 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4647 | int err; |
| 4648 | int drop_inode = 0; |
| 4649 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4650 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4651 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4652 | |
| 4653 | if (!new_valid_dev(rdev)) |
| 4654 | return -EINVAL; |
| 4655 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4656 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4657 | if (err) |
| 4658 | return err; |
| 4659 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4660 | /* |
| 4661 | * 2 for inode item and ref |
| 4662 | * 2 for dir items |
| 4663 | * 1 for xattr if selinux is on |
| 4664 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4665 | trans = btrfs_start_transaction(root, 5); |
| 4666 | if (IS_ERR(trans)) |
| 4667 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4668 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4669 | btrfs_set_trans_block_group(trans, dir); |
| 4670 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4671 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4672 | dentry->d_name.len, |
| 4673 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4674 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4675 | err = PTR_ERR(inode); |
| 4676 | if (IS_ERR(inode)) |
| 4677 | goto out_unlock; |
| 4678 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4679 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4680 | if (err) { |
| 4681 | drop_inode = 1; |
| 4682 | goto out_unlock; |
| 4683 | } |
| 4684 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4685 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4686 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4687 | if (err) |
| 4688 | drop_inode = 1; |
| 4689 | else { |
| 4690 | inode->i_op = &btrfs_special_inode_operations; |
| 4691 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4692 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4693 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4694 | btrfs_update_inode_block_group(trans, inode); |
| 4695 | btrfs_update_inode_block_group(trans, dir); |
| 4696 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4697 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4698 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4699 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4700 | if (drop_inode) { |
| 4701 | inode_dec_link_count(inode); |
| 4702 | iput(inode); |
| 4703 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4704 | return err; |
| 4705 | } |
| 4706 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4707 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4708 | int mode, struct nameidata *nd) |
| 4709 | { |
| 4710 | struct btrfs_trans_handle *trans; |
| 4711 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4712 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4713 | int drop_inode = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4714 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4715 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4716 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4717 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4718 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4719 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4720 | if (err) |
| 4721 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4722 | /* |
| 4723 | * 2 for inode item and ref |
| 4724 | * 2 for dir items |
| 4725 | * 1 for xattr if selinux is on |
| 4726 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4727 | trans = btrfs_start_transaction(root, 5); |
| 4728 | if (IS_ERR(trans)) |
| 4729 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4730 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4731 | btrfs_set_trans_block_group(trans, dir); |
| 4732 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4733 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4734 | dentry->d_name.len, |
| 4735 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4736 | objectid, BTRFS_I(dir)->block_group, mode, |
| 4737 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4738 | err = PTR_ERR(inode); |
| 4739 | if (IS_ERR(inode)) |
| 4740 | goto out_unlock; |
| 4741 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4742 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4743 | if (err) { |
| 4744 | drop_inode = 1; |
| 4745 | goto out_unlock; |
| 4746 | } |
| 4747 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4748 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4749 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4750 | if (err) |
| 4751 | drop_inode = 1; |
| 4752 | else { |
| 4753 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4754 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4755 | inode->i_fop = &btrfs_file_operations; |
| 4756 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4757 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4758 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4759 | btrfs_update_inode_block_group(trans, inode); |
| 4760 | btrfs_update_inode_block_group(trans, dir); |
| 4761 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4762 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4763 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4764 | if (drop_inode) { |
| 4765 | inode_dec_link_count(inode); |
| 4766 | iput(inode); |
| 4767 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4768 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4769 | return err; |
| 4770 | } |
| 4771 | |
| 4772 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4773 | struct dentry *dentry) |
| 4774 | { |
| 4775 | struct btrfs_trans_handle *trans; |
| 4776 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4777 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4778 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4779 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4780 | int err; |
| 4781 | int drop_inode = 0; |
| 4782 | |
| 4783 | if (inode->i_nlink == 0) |
| 4784 | return -ENOENT; |
| 4785 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4786 | /* do not allow sys_link's with other subvols of the same device */ |
| 4787 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
| 4788 | return -EPERM; |
| 4789 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4790 | btrfs_inc_nlink(inode); |
| 4791 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4792 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4793 | if (err) |
| 4794 | goto fail; |
| 4795 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4796 | /* |
| 4797 | * 1 item for inode ref |
| 4798 | * 2 items for dir items |
| 4799 | */ |
| 4800 | trans = btrfs_start_transaction(root, 3); |
| 4801 | if (IS_ERR(trans)) { |
| 4802 | err = PTR_ERR(trans); |
| 4803 | goto fail; |
| 4804 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4805 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4806 | btrfs_set_trans_block_group(trans, dir); |
| 4807 | atomic_inc(&inode->i_count); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4808 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4809 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4810 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4811 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4812 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4813 | } else { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4814 | struct dentry *parent = dget_parent(dentry); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4815 | btrfs_update_inode_block_group(trans, dir); |
| 4816 | err = btrfs_update_inode(trans, root, inode); |
| 4817 | BUG_ON(err); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4818 | btrfs_log_new_name(trans, inode, NULL, parent); |
| 4819 | dput(parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4820 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4821 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4822 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4823 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4824 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4825 | if (drop_inode) { |
| 4826 | inode_dec_link_count(inode); |
| 4827 | iput(inode); |
| 4828 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4829 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4830 | return err; |
| 4831 | } |
| 4832 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4833 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4834 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4835 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4836 | struct btrfs_trans_handle *trans; |
| 4837 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4838 | int err = 0; |
| 4839 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4840 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4841 | u64 index = 0; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4842 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4843 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4844 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4845 | if (err) |
| 4846 | return err; |
| 4847 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4848 | /* |
| 4849 | * 2 items for inode and ref |
| 4850 | * 2 items for dir items |
| 4851 | * 1 for xattr if selinux is on |
| 4852 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4853 | trans = btrfs_start_transaction(root, 5); |
| 4854 | if (IS_ERR(trans)) |
| 4855 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4856 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4857 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4858 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4859 | dentry->d_name.len, |
| 4860 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4861 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 4862 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4863 | if (IS_ERR(inode)) { |
| 4864 | err = PTR_ERR(inode); |
| 4865 | goto out_fail; |
| 4866 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4867 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4868 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4869 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4870 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4871 | if (err) |
| 4872 | goto out_fail; |
| 4873 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4874 | inode->i_op = &btrfs_dir_inode_operations; |
| 4875 | inode->i_fop = &btrfs_dir_file_operations; |
| 4876 | btrfs_set_trans_block_group(trans, inode); |
| 4877 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4878 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4879 | err = btrfs_update_inode(trans, root, inode); |
| 4880 | if (err) |
| 4881 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4882 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4883 | err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4884 | inode, dentry->d_name.name, |
| 4885 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4886 | if (err) |
| 4887 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4888 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4889 | d_instantiate(dentry, inode); |
| 4890 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4891 | btrfs_update_inode_block_group(trans, inode); |
| 4892 | btrfs_update_inode_block_group(trans, dir); |
| 4893 | |
| 4894 | out_fail: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4895 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4896 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4897 | if (drop_on_err) |
| 4898 | iput(inode); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4899 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4900 | return err; |
| 4901 | } |
| 4902 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4903 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4904 | * and an extent that you want to insert, deal with overlap and insert |
| 4905 | * the new extent into the tree. |
| 4906 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4907 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4908 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4909 | struct extent_map *em, |
| 4910 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4911 | { |
| 4912 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4913 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4914 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4915 | start_diff = map_start - em->start; |
| 4916 | em->start = map_start; |
| 4917 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4918 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4919 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4920 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4921 | em->block_len -= start_diff; |
| 4922 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4923 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4924 | } |
| 4925 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4926 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4927 | struct inode *inode, struct page *page, |
| 4928 | size_t pg_offset, u64 extent_offset, |
| 4929 | struct btrfs_file_extent_item *item) |
| 4930 | { |
| 4931 | int ret; |
| 4932 | struct extent_buffer *leaf = path->nodes[0]; |
| 4933 | char *tmp; |
| 4934 | size_t max_size; |
| 4935 | unsigned long inline_size; |
| 4936 | unsigned long ptr; |
| 4937 | |
| 4938 | WARN_ON(pg_offset != 0); |
| 4939 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4940 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4941 | btrfs_item_nr(leaf, path->slots[0])); |
| 4942 | tmp = kmalloc(inline_size, GFP_NOFS); |
| 4943 | ptr = btrfs_file_extent_inline_start(item); |
| 4944 | |
| 4945 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4946 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 4947 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4948 | ret = btrfs_zlib_decompress(tmp, page, extent_offset, |
| 4949 | inline_size, max_size); |
| 4950 | if (ret) { |
| 4951 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 4952 | unsigned long copy_size = min_t(u64, |
| 4953 | PAGE_CACHE_SIZE - pg_offset, |
| 4954 | max_size - extent_offset); |
| 4955 | memset(kaddr + pg_offset, 0, copy_size); |
| 4956 | kunmap_atomic(kaddr, KM_USER0); |
| 4957 | } |
| 4958 | kfree(tmp); |
| 4959 | return 0; |
| 4960 | } |
| 4961 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4962 | /* |
| 4963 | * 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] | 4964 | * the ugly parts come from merging extents from the disk with the in-ram |
| 4965 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4966 | * where the in-ram extents might be locked pending data=ordered completion. |
| 4967 | * |
| 4968 | * This also copies inline extents directly into the page. |
| 4969 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4970 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4971 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4972 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4973 | int create) |
| 4974 | { |
| 4975 | int ret; |
| 4976 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4977 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4978 | u64 extent_start = 0; |
| 4979 | u64 extent_end = 0; |
| 4980 | u64 objectid = inode->i_ino; |
| 4981 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4982 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4983 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4984 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4985 | struct extent_buffer *leaf; |
| 4986 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4987 | struct extent_map *em = NULL; |
| 4988 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4989 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4990 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4991 | int compressed; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4992 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4993 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4994 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4995 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4996 | if (em) |
| 4997 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4998 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4999 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5000 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5001 | if (em->start > start || em->start + em->len <= start) |
| 5002 | free_extent_map(em); |
| 5003 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5004 | free_extent_map(em); |
| 5005 | else |
| 5006 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5007 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5008 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5009 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5010 | err = -ENOMEM; |
| 5011 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5012 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5013 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5014 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 5015 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5016 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5017 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5018 | |
| 5019 | if (!path) { |
| 5020 | path = btrfs_alloc_path(); |
| 5021 | BUG_ON(!path); |
| 5022 | } |
| 5023 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5024 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 5025 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5026 | if (ret < 0) { |
| 5027 | err = ret; |
| 5028 | goto out; |
| 5029 | } |
| 5030 | |
| 5031 | if (ret != 0) { |
| 5032 | if (path->slots[0] == 0) |
| 5033 | goto not_found; |
| 5034 | path->slots[0]--; |
| 5035 | } |
| 5036 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5037 | leaf = path->nodes[0]; |
| 5038 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5039 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5040 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5041 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5042 | found_type = btrfs_key_type(&found_key); |
| 5043 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5044 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 5045 | goto not_found; |
| 5046 | } |
| 5047 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5048 | found_type = btrfs_file_extent_type(leaf, item); |
| 5049 | extent_start = found_key.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5050 | compressed = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5051 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5052 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5053 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5054 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5055 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5056 | size_t size; |
| 5057 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5058 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 5059 | ~((u64)root->sectorsize - 1); |
| 5060 | } |
| 5061 | |
| 5062 | if (start >= extent_end) { |
| 5063 | path->slots[0]++; |
| 5064 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 5065 | ret = btrfs_next_leaf(root, path); |
| 5066 | if (ret < 0) { |
| 5067 | err = ret; |
| 5068 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5069 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5070 | if (ret > 0) |
| 5071 | goto not_found; |
| 5072 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5073 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5074 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5075 | if (found_key.objectid != objectid || |
| 5076 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 5077 | goto not_found; |
| 5078 | if (start + len <= found_key.offset) |
| 5079 | goto not_found; |
| 5080 | em->start = start; |
| 5081 | em->len = found_key.offset - start; |
| 5082 | goto not_found_em; |
| 5083 | } |
| 5084 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5085 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5086 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5087 | em->start = extent_start; |
| 5088 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5089 | em->orig_start = extent_start - |
| 5090 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5091 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 5092 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5093 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5094 | goto insert; |
| 5095 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5096 | if (compressed) { |
| 5097 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 5098 | em->block_start = bytenr; |
| 5099 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 5100 | item); |
| 5101 | } else { |
| 5102 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 5103 | em->block_start = bytenr; |
| 5104 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5105 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 5106 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5107 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5108 | goto insert; |
| 5109 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5110 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5111 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5112 | size_t size; |
| 5113 | size_t extent_offset; |
| 5114 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5115 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5116 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5117 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5118 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5119 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5120 | goto out; |
| 5121 | } |
| 5122 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5123 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5124 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5125 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5126 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5127 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5128 | em->len = (copy_size + root->sectorsize - 1) & |
| 5129 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5130 | em->orig_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5131 | if (compressed) |
| 5132 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5133 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5134 | if (create == 0 && !PageUptodate(page)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5135 | if (btrfs_file_extent_compression(leaf, item) == |
| 5136 | BTRFS_COMPRESS_ZLIB) { |
| 5137 | ret = uncompress_inline(path, inode, page, |
| 5138 | pg_offset, |
| 5139 | extent_offset, item); |
| 5140 | BUG_ON(ret); |
| 5141 | } else { |
| 5142 | map = kmap(page); |
| 5143 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 5144 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 5145 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 5146 | memset(map + pg_offset + copy_size, 0, |
| 5147 | PAGE_CACHE_SIZE - pg_offset - |
| 5148 | copy_size); |
| 5149 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5150 | kunmap(page); |
| 5151 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5152 | flush_dcache_page(page); |
| 5153 | } else if (create && PageUptodate(page)) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5154 | WARN_ON(1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5155 | if (!trans) { |
| 5156 | kunmap(page); |
| 5157 | free_extent_map(em); |
| 5158 | em = NULL; |
| 5159 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 5160 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5161 | goto again; |
| 5162 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5163 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5164 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5165 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5166 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5167 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5168 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5169 | set_extent_uptodate(io_tree, em->start, |
| 5170 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5171 | goto insert; |
| 5172 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5173 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5174 | WARN_ON(1); |
| 5175 | } |
| 5176 | not_found: |
| 5177 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5178 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5179 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5180 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5181 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5182 | insert: |
| 5183 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5184 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5185 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 5186 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 5187 | (unsigned long long)em->len, |
| 5188 | (unsigned long long)start, |
| 5189 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5190 | err = -EIO; |
| 5191 | goto out; |
| 5192 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5193 | |
| 5194 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5195 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5196 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5197 | /* it is possible that someone inserted the extent into the tree |
| 5198 | * while we had the lock dropped. It is also possible that |
| 5199 | * an overlapping map exists in the tree |
| 5200 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5201 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5202 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5203 | |
| 5204 | ret = 0; |
| 5205 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5206 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5207 | if (existing && (existing->start > start || |
| 5208 | existing->start + existing->len <= start)) { |
| 5209 | free_extent_map(existing); |
| 5210 | existing = NULL; |
| 5211 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5212 | if (!existing) { |
| 5213 | existing = lookup_extent_mapping(em_tree, em->start, |
| 5214 | em->len); |
| 5215 | if (existing) { |
| 5216 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5217 | em, start, |
| 5218 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5219 | free_extent_map(existing); |
| 5220 | if (err) { |
| 5221 | free_extent_map(em); |
| 5222 | em = NULL; |
| 5223 | } |
| 5224 | } else { |
| 5225 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5226 | free_extent_map(em); |
| 5227 | em = NULL; |
| 5228 | } |
| 5229 | } else { |
| 5230 | free_extent_map(em); |
| 5231 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5232 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5233 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5234 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5235 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5236 | out: |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5237 | if (path) |
| 5238 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5239 | if (trans) { |
| 5240 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5241 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5242 | err = ret; |
| 5243 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5244 | if (err) { |
| 5245 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5246 | return ERR_PTR(err); |
| 5247 | } |
| 5248 | return em; |
| 5249 | } |
| 5250 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5251 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 5252 | u64 start, u64 len) |
| 5253 | { |
| 5254 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5255 | struct btrfs_trans_handle *trans; |
| 5256 | struct extent_map *em; |
| 5257 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 5258 | struct btrfs_key ins; |
| 5259 | u64 alloc_hint; |
| 5260 | int ret; |
| 5261 | |
| 5262 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
| 5263 | |
| 5264 | trans = btrfs_join_transaction(root, 0); |
| 5265 | if (!trans) |
| 5266 | return ERR_PTR(-ENOMEM); |
| 5267 | |
| 5268 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5269 | |
| 5270 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 5271 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
| 5272 | alloc_hint, (u64)-1, &ins, 1); |
| 5273 | if (ret) { |
| 5274 | em = ERR_PTR(ret); |
| 5275 | goto out; |
| 5276 | } |
| 5277 | |
| 5278 | em = alloc_extent_map(GFP_NOFS); |
| 5279 | if (!em) { |
| 5280 | em = ERR_PTR(-ENOMEM); |
| 5281 | goto out; |
| 5282 | } |
| 5283 | |
| 5284 | em->start = start; |
| 5285 | em->orig_start = em->start; |
| 5286 | em->len = ins.offset; |
| 5287 | |
| 5288 | em->block_start = ins.objectid; |
| 5289 | em->block_len = ins.offset; |
| 5290 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 5291 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5292 | |
| 5293 | while (1) { |
| 5294 | write_lock(&em_tree->lock); |
| 5295 | ret = add_extent_mapping(em_tree, em); |
| 5296 | write_unlock(&em_tree->lock); |
| 5297 | if (ret != -EEXIST) |
| 5298 | break; |
| 5299 | btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0); |
| 5300 | } |
| 5301 | |
| 5302 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 5303 | ins.offset, ins.offset, 0); |
| 5304 | if (ret) { |
| 5305 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 5306 | em = ERR_PTR(ret); |
| 5307 | } |
| 5308 | out: |
| 5309 | btrfs_end_transaction(trans, root); |
| 5310 | return em; |
| 5311 | } |
| 5312 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5313 | /* |
| 5314 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 5315 | * block must be cow'd |
| 5316 | */ |
| 5317 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 5318 | struct inode *inode, u64 offset, u64 len) |
| 5319 | { |
| 5320 | struct btrfs_path *path; |
| 5321 | int ret; |
| 5322 | struct extent_buffer *leaf; |
| 5323 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5324 | struct btrfs_file_extent_item *fi; |
| 5325 | struct btrfs_key key; |
| 5326 | u64 disk_bytenr; |
| 5327 | u64 backref_offset; |
| 5328 | u64 extent_end; |
| 5329 | u64 num_bytes; |
| 5330 | int slot; |
| 5331 | int found_type; |
| 5332 | |
| 5333 | path = btrfs_alloc_path(); |
| 5334 | if (!path) |
| 5335 | return -ENOMEM; |
| 5336 | |
| 5337 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 5338 | offset, 0); |
| 5339 | if (ret < 0) |
| 5340 | goto out; |
| 5341 | |
| 5342 | slot = path->slots[0]; |
| 5343 | if (ret == 1) { |
| 5344 | if (slot == 0) { |
| 5345 | /* can't find the item, must cow */ |
| 5346 | ret = 0; |
| 5347 | goto out; |
| 5348 | } |
| 5349 | slot--; |
| 5350 | } |
| 5351 | ret = 0; |
| 5352 | leaf = path->nodes[0]; |
| 5353 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5354 | if (key.objectid != inode->i_ino || |
| 5355 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5356 | /* not our file or wrong item type, must cow */ |
| 5357 | goto out; |
| 5358 | } |
| 5359 | |
| 5360 | if (key.offset > offset) { |
| 5361 | /* Wrong offset, must cow */ |
| 5362 | goto out; |
| 5363 | } |
| 5364 | |
| 5365 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5366 | found_type = btrfs_file_extent_type(leaf, fi); |
| 5367 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 5368 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 5369 | /* not a regular extent, must cow */ |
| 5370 | goto out; |
| 5371 | } |
| 5372 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5373 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 5374 | |
| 5375 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 5376 | if (extent_end < offset + len) { |
| 5377 | /* extent doesn't include our full range, must cow */ |
| 5378 | goto out; |
| 5379 | } |
| 5380 | |
| 5381 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 5382 | goto out; |
| 5383 | |
| 5384 | /* |
| 5385 | * look for other files referencing this extent, if we |
| 5386 | * find any we must cow |
| 5387 | */ |
| 5388 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
| 5389 | key.offset - backref_offset, disk_bytenr)) |
| 5390 | goto out; |
| 5391 | |
| 5392 | /* |
| 5393 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 5394 | * in this extent we are about to write. If there |
| 5395 | * are any csums in that range we have to cow in order |
| 5396 | * to keep the csums correct |
| 5397 | */ |
| 5398 | disk_bytenr += backref_offset; |
| 5399 | disk_bytenr += offset - key.offset; |
| 5400 | num_bytes = min(offset + len, extent_end) - offset; |
| 5401 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 5402 | goto out; |
| 5403 | /* |
| 5404 | * all of the above have passed, it is safe to overwrite this extent |
| 5405 | * without cow |
| 5406 | */ |
| 5407 | ret = 1; |
| 5408 | out: |
| 5409 | btrfs_free_path(path); |
| 5410 | return ret; |
| 5411 | } |
| 5412 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5413 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 5414 | struct buffer_head *bh_result, int create) |
| 5415 | { |
| 5416 | struct extent_map *em; |
| 5417 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5418 | u64 start = iblock << inode->i_blkbits; |
| 5419 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5420 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5421 | |
| 5422 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
| 5423 | if (IS_ERR(em)) |
| 5424 | return PTR_ERR(em); |
| 5425 | |
| 5426 | /* |
| 5427 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 5428 | * io. INLINE is special, and we could probably kludge it in here, but |
| 5429 | * it's still buffered so for safety lets just fall back to the generic |
| 5430 | * buffered path. |
| 5431 | * |
| 5432 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 5433 | * decompress it, so there will be buffering required no matter what we |
| 5434 | * do, so go ahead and fallback to buffered. |
| 5435 | * |
| 5436 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 5437 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 5438 | * the generic code. |
| 5439 | */ |
| 5440 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 5441 | em->block_start == EXTENT_MAP_INLINE) { |
| 5442 | free_extent_map(em); |
| 5443 | return -ENOTBLK; |
| 5444 | } |
| 5445 | |
| 5446 | /* Just a good old fashioned hole, return */ |
| 5447 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 5448 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5449 | free_extent_map(em); |
| 5450 | /* DIO will do one hole at a time, so just unlock a sector */ |
| 5451 | unlock_extent(&BTRFS_I(inode)->io_tree, start, |
| 5452 | start + root->sectorsize - 1, GFP_NOFS); |
| 5453 | return 0; |
| 5454 | } |
| 5455 | |
| 5456 | /* |
| 5457 | * We don't allocate a new extent in the following cases |
| 5458 | * |
| 5459 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 5460 | * existing extent. |
| 5461 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 5462 | * just use the extent. |
| 5463 | * |
| 5464 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5465 | if (!create) { |
| 5466 | len = em->len - (start - em->start); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5467 | goto map; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5468 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5469 | |
| 5470 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 5471 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 5472 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5473 | int type; |
| 5474 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5475 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5476 | |
| 5477 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5478 | type = BTRFS_ORDERED_PREALLOC; |
| 5479 | else |
| 5480 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5481 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5482 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5483 | |
| 5484 | /* |
| 5485 | * we're not going to log anything, but we do need |
| 5486 | * to make sure the current transaction stays open |
| 5487 | * while we look for nocow cross refs |
| 5488 | */ |
| 5489 | trans = btrfs_join_transaction(root, 0); |
| 5490 | if (!trans) |
| 5491 | goto must_cow; |
| 5492 | |
| 5493 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
| 5494 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 5495 | block_start, len, len, type); |
| 5496 | btrfs_end_transaction(trans, root); |
| 5497 | if (ret) { |
| 5498 | free_extent_map(em); |
| 5499 | return ret; |
| 5500 | } |
| 5501 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5502 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5503 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5504 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5505 | must_cow: |
| 5506 | /* |
| 5507 | * this will cow the extent, reset the len in case we changed |
| 5508 | * it above |
| 5509 | */ |
| 5510 | len = bh_result->b_size; |
| 5511 | free_extent_map(em); |
| 5512 | em = btrfs_new_extent_direct(inode, start, len); |
| 5513 | if (IS_ERR(em)) |
| 5514 | return PTR_ERR(em); |
| 5515 | len = min(len, em->len - (start - em->start)); |
| 5516 | unlock: |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5517 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
| 5518 | EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1, |
| 5519 | 0, NULL, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5520 | map: |
| 5521 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 5522 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5523 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5524 | bh_result->b_bdev = em->bdev; |
| 5525 | set_buffer_mapped(bh_result); |
| 5526 | if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5527 | set_buffer_new(bh_result); |
| 5528 | |
| 5529 | free_extent_map(em); |
| 5530 | |
| 5531 | return 0; |
| 5532 | } |
| 5533 | |
| 5534 | struct btrfs_dio_private { |
| 5535 | struct inode *inode; |
| 5536 | u64 logical_offset; |
| 5537 | u64 disk_bytenr; |
| 5538 | u64 bytes; |
| 5539 | u32 *csums; |
| 5540 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5541 | |
| 5542 | /* number of bios pending for this dio */ |
| 5543 | atomic_t pending_bios; |
| 5544 | |
| 5545 | /* IO errors */ |
| 5546 | int errors; |
| 5547 | |
| 5548 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5549 | }; |
| 5550 | |
| 5551 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 5552 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5553 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5554 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 5555 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5556 | struct inode *inode = dip->inode; |
| 5557 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5558 | u64 start; |
| 5559 | u32 *private = dip->csums; |
| 5560 | |
| 5561 | start = dip->logical_offset; |
| 5562 | do { |
| 5563 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 5564 | struct page *page = bvec->bv_page; |
| 5565 | char *kaddr; |
| 5566 | u32 csum = ~(u32)0; |
| 5567 | unsigned long flags; |
| 5568 | |
| 5569 | local_irq_save(flags); |
| 5570 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 5571 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 5572 | csum, bvec->bv_len); |
| 5573 | btrfs_csum_final(csum, (char *)&csum); |
| 5574 | kunmap_atomic(kaddr, KM_IRQ0); |
| 5575 | local_irq_restore(flags); |
| 5576 | |
| 5577 | flush_dcache_page(bvec->bv_page); |
| 5578 | if (csum != *private) { |
| 5579 | printk(KERN_ERR "btrfs csum failed ino %lu off" |
| 5580 | " %llu csum %u private %u\n", |
| 5581 | inode->i_ino, (unsigned long long)start, |
| 5582 | csum, *private); |
| 5583 | err = -EIO; |
| 5584 | } |
| 5585 | } |
| 5586 | |
| 5587 | start += bvec->bv_len; |
| 5588 | private++; |
| 5589 | bvec++; |
| 5590 | } while (bvec <= bvec_end); |
| 5591 | |
| 5592 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
| 5593 | dip->logical_offset + dip->bytes - 1, GFP_NOFS); |
| 5594 | bio->bi_private = dip->private; |
| 5595 | |
| 5596 | kfree(dip->csums); |
| 5597 | kfree(dip); |
| 5598 | dio_end_io(bio, err); |
| 5599 | } |
| 5600 | |
| 5601 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 5602 | { |
| 5603 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5604 | struct inode *inode = dip->inode; |
| 5605 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5606 | struct btrfs_trans_handle *trans; |
| 5607 | struct btrfs_ordered_extent *ordered = NULL; |
| 5608 | struct extent_state *cached_state = NULL; |
| 5609 | int ret; |
| 5610 | |
| 5611 | if (err) |
| 5612 | goto out_done; |
| 5613 | |
| 5614 | ret = btrfs_dec_test_ordered_pending(inode, &ordered, |
| 5615 | dip->logical_offset, dip->bytes); |
| 5616 | if (!ret) |
| 5617 | goto out_done; |
| 5618 | |
| 5619 | BUG_ON(!ordered); |
| 5620 | |
| 5621 | trans = btrfs_join_transaction(root, 1); |
| 5622 | if (!trans) { |
| 5623 | err = -ENOMEM; |
| 5624 | goto out; |
| 5625 | } |
| 5626 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5627 | |
| 5628 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) { |
| 5629 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5630 | if (!ret) |
| 5631 | ret = btrfs_update_inode(trans, root, inode); |
| 5632 | err = ret; |
| 5633 | goto out; |
| 5634 | } |
| 5635 | |
| 5636 | lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5637 | ordered->file_offset + ordered->len - 1, 0, |
| 5638 | &cached_state, GFP_NOFS); |
| 5639 | |
| 5640 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) { |
| 5641 | ret = btrfs_mark_extent_written(trans, inode, |
| 5642 | ordered->file_offset, |
| 5643 | ordered->file_offset + |
| 5644 | ordered->len); |
| 5645 | if (ret) { |
| 5646 | err = ret; |
| 5647 | goto out_unlock; |
| 5648 | } |
| 5649 | } else { |
| 5650 | ret = insert_reserved_file_extent(trans, inode, |
| 5651 | ordered->file_offset, |
| 5652 | ordered->start, |
| 5653 | ordered->disk_len, |
| 5654 | ordered->len, |
| 5655 | ordered->len, |
| 5656 | 0, 0, 0, |
| 5657 | BTRFS_FILE_EXTENT_REG); |
| 5658 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 5659 | ordered->file_offset, ordered->len); |
| 5660 | if (ret) { |
| 5661 | err = ret; |
| 5662 | WARN_ON(1); |
| 5663 | goto out_unlock; |
| 5664 | } |
| 5665 | } |
| 5666 | |
| 5667 | add_pending_csums(trans, inode, ordered->file_offset, &ordered->list); |
| 5668 | btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5669 | btrfs_update_inode(trans, root, inode); |
| 5670 | out_unlock: |
| 5671 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5672 | ordered->file_offset + ordered->len - 1, |
| 5673 | &cached_state, GFP_NOFS); |
| 5674 | out: |
| 5675 | btrfs_delalloc_release_metadata(inode, ordered->len); |
| 5676 | btrfs_end_transaction(trans, root); |
| 5677 | btrfs_put_ordered_extent(ordered); |
| 5678 | btrfs_put_ordered_extent(ordered); |
| 5679 | out_done: |
| 5680 | bio->bi_private = dip->private; |
| 5681 | |
| 5682 | kfree(dip->csums); |
| 5683 | kfree(dip); |
| 5684 | dio_end_io(bio, err); |
| 5685 | } |
| 5686 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5687 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 5688 | struct bio *bio, int mirror_num, |
| 5689 | unsigned long bio_flags, u64 offset) |
| 5690 | { |
| 5691 | int ret; |
| 5692 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5693 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
| 5694 | BUG_ON(ret); |
| 5695 | return 0; |
| 5696 | } |
| 5697 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5698 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 5699 | { |
| 5700 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5701 | |
| 5702 | if (err) { |
| 5703 | printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu " |
| 5704 | "disk_bytenr %lu len %u err no %d\n", |
| 5705 | dip->inode->i_ino, bio->bi_rw, bio->bi_sector, |
| 5706 | bio->bi_size, err); |
| 5707 | dip->errors = 1; |
| 5708 | |
| 5709 | /* |
| 5710 | * before atomic variable goto zero, we must make sure |
| 5711 | * dip->errors is perceived to be set. |
| 5712 | */ |
| 5713 | smp_mb__before_atomic_dec(); |
| 5714 | } |
| 5715 | |
| 5716 | /* if there are more bios still pending for this dio, just exit */ |
| 5717 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 5718 | goto out; |
| 5719 | |
| 5720 | if (dip->errors) |
| 5721 | bio_io_error(dip->orig_bio); |
| 5722 | else { |
| 5723 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 5724 | bio_endio(dip->orig_bio, 0); |
| 5725 | } |
| 5726 | out: |
| 5727 | bio_put(bio); |
| 5728 | } |
| 5729 | |
| 5730 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 5731 | u64 first_sector, gfp_t gfp_flags) |
| 5732 | { |
| 5733 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 5734 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 5735 | } |
| 5736 | |
| 5737 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 5738 | int rw, u64 file_offset, int skip_sum, |
| 5739 | u32 *csums) |
| 5740 | { |
| 5741 | int write = rw & REQ_WRITE; |
| 5742 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5743 | int ret; |
| 5744 | |
| 5745 | bio_get(bio); |
| 5746 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 5747 | if (ret) |
| 5748 | goto err; |
| 5749 | |
| 5750 | if (write && !skip_sum) { |
| 5751 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 5752 | inode, rw, bio, 0, 0, |
| 5753 | file_offset, |
| 5754 | __btrfs_submit_bio_start_direct_io, |
| 5755 | __btrfs_submit_bio_done); |
| 5756 | goto err; |
| 5757 | } else if (!skip_sum) |
| 5758 | btrfs_lookup_bio_sums_dio(root, inode, bio, |
| 5759 | file_offset, csums); |
| 5760 | |
| 5761 | ret = btrfs_map_bio(root, rw, bio, 0, 1); |
| 5762 | err: |
| 5763 | bio_put(bio); |
| 5764 | return ret; |
| 5765 | } |
| 5766 | |
| 5767 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 5768 | int skip_sum) |
| 5769 | { |
| 5770 | struct inode *inode = dip->inode; |
| 5771 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5772 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5773 | struct bio *bio; |
| 5774 | struct bio *orig_bio = dip->orig_bio; |
| 5775 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 5776 | u64 start_sector = orig_bio->bi_sector; |
| 5777 | u64 file_offset = dip->logical_offset; |
| 5778 | u64 submit_len = 0; |
| 5779 | u64 map_length; |
| 5780 | int nr_pages = 0; |
| 5781 | u32 *csums = dip->csums; |
| 5782 | int ret = 0; |
| 5783 | |
| 5784 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 5785 | if (!bio) |
| 5786 | return -ENOMEM; |
| 5787 | bio->bi_private = dip; |
| 5788 | bio->bi_end_io = btrfs_end_dio_bio; |
| 5789 | atomic_inc(&dip->pending_bios); |
| 5790 | |
| 5791 | map_length = orig_bio->bi_size; |
| 5792 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 5793 | &map_length, NULL, 0); |
| 5794 | if (ret) { |
| 5795 | bio_put(bio); |
| 5796 | return -EIO; |
| 5797 | } |
| 5798 | |
| 5799 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 5800 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 5801 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5802 | bvec->bv_offset) < bvec->bv_len)) { |
| 5803 | /* |
| 5804 | * inc the count before we submit the bio so |
| 5805 | * we know the end IO handler won't happen before |
| 5806 | * we inc the count. Otherwise, the dip might get freed |
| 5807 | * before we're done setting it up |
| 5808 | */ |
| 5809 | atomic_inc(&dip->pending_bios); |
| 5810 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 5811 | file_offset, skip_sum, |
| 5812 | csums); |
| 5813 | if (ret) { |
| 5814 | bio_put(bio); |
| 5815 | atomic_dec(&dip->pending_bios); |
| 5816 | goto out_err; |
| 5817 | } |
| 5818 | |
| 5819 | if (!skip_sum) |
| 5820 | csums = csums + nr_pages; |
| 5821 | start_sector += submit_len >> 9; |
| 5822 | file_offset += submit_len; |
| 5823 | |
| 5824 | submit_len = 0; |
| 5825 | nr_pages = 0; |
| 5826 | |
| 5827 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 5828 | start_sector, GFP_NOFS); |
| 5829 | if (!bio) |
| 5830 | goto out_err; |
| 5831 | bio->bi_private = dip; |
| 5832 | bio->bi_end_io = btrfs_end_dio_bio; |
| 5833 | |
| 5834 | map_length = orig_bio->bi_size; |
| 5835 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 5836 | &map_length, NULL, 0); |
| 5837 | if (ret) { |
| 5838 | bio_put(bio); |
| 5839 | goto out_err; |
| 5840 | } |
| 5841 | } else { |
| 5842 | submit_len += bvec->bv_len; |
| 5843 | nr_pages ++; |
| 5844 | bvec++; |
| 5845 | } |
| 5846 | } |
| 5847 | |
| 5848 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
| 5849 | csums); |
| 5850 | if (!ret) |
| 5851 | return 0; |
| 5852 | |
| 5853 | bio_put(bio); |
| 5854 | out_err: |
| 5855 | dip->errors = 1; |
| 5856 | /* |
| 5857 | * before atomic variable goto zero, we must |
| 5858 | * make sure dip->errors is perceived to be set. |
| 5859 | */ |
| 5860 | smp_mb__before_atomic_dec(); |
| 5861 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 5862 | bio_io_error(dip->orig_bio); |
| 5863 | |
| 5864 | /* bio_end_io() will handle error, so we needn't return it */ |
| 5865 | return 0; |
| 5866 | } |
| 5867 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5868 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 5869 | loff_t file_offset) |
| 5870 | { |
| 5871 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5872 | struct btrfs_dio_private *dip; |
| 5873 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5874 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5875 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5876 | int ret = 0; |
| 5877 | |
| 5878 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 5879 | |
| 5880 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 5881 | if (!dip) { |
| 5882 | ret = -ENOMEM; |
| 5883 | goto free_ordered; |
| 5884 | } |
| 5885 | dip->csums = NULL; |
| 5886 | |
| 5887 | if (!skip_sum) { |
| 5888 | dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); |
| 5889 | if (!dip->csums) { |
| 5890 | ret = -ENOMEM; |
| 5891 | goto free_ordered; |
| 5892 | } |
| 5893 | } |
| 5894 | |
| 5895 | dip->private = bio->bi_private; |
| 5896 | dip->inode = inode; |
| 5897 | dip->logical_offset = file_offset; |
| 5898 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5899 | dip->bytes = 0; |
| 5900 | do { |
| 5901 | dip->bytes += bvec->bv_len; |
| 5902 | bvec++; |
| 5903 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 5904 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5905 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5906 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5907 | dip->errors = 0; |
| 5908 | dip->orig_bio = bio; |
| 5909 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5910 | |
| 5911 | if (write) |
| 5912 | bio->bi_end_io = btrfs_endio_direct_write; |
| 5913 | else |
| 5914 | bio->bi_end_io = btrfs_endio_direct_read; |
| 5915 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5916 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 5917 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5918 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5919 | free_ordered: |
| 5920 | /* |
| 5921 | * If this is a write, we need to clean up the reserved space and kill |
| 5922 | * the ordered extent. |
| 5923 | */ |
| 5924 | if (write) { |
| 5925 | struct btrfs_ordered_extent *ordered; |
| 5926 | ordered = btrfs_lookup_ordered_extent(inode, |
| 5927 | dip->logical_offset); |
| 5928 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 5929 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 5930 | btrfs_free_reserved_extent(root, ordered->start, |
| 5931 | ordered->disk_len); |
| 5932 | btrfs_put_ordered_extent(ordered); |
| 5933 | btrfs_put_ordered_extent(ordered); |
| 5934 | } |
| 5935 | bio_endio(bio, ret); |
| 5936 | } |
| 5937 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 5938 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 5939 | const struct iovec *iov, loff_t offset, |
| 5940 | unsigned long nr_segs) |
| 5941 | { |
| 5942 | int seg; |
| 5943 | size_t size; |
| 5944 | unsigned long addr; |
| 5945 | unsigned blocksize_mask = root->sectorsize - 1; |
| 5946 | ssize_t retval = -EINVAL; |
| 5947 | loff_t end = offset; |
| 5948 | |
| 5949 | if (offset & blocksize_mask) |
| 5950 | goto out; |
| 5951 | |
| 5952 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 5953 | for (seg = 0; seg < nr_segs; seg++) { |
| 5954 | addr = (unsigned long)iov[seg].iov_base; |
| 5955 | size = iov[seg].iov_len; |
| 5956 | end += size; |
| 5957 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
| 5958 | goto out; |
| 5959 | } |
| 5960 | retval = 0; |
| 5961 | out: |
| 5962 | return retval; |
| 5963 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 5964 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 5965 | const struct iovec *iov, loff_t offset, |
| 5966 | unsigned long nr_segs) |
| 5967 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5968 | struct file *file = iocb->ki_filp; |
| 5969 | struct inode *inode = file->f_mapping->host; |
| 5970 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5971 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5972 | u64 lockstart, lockend; |
| 5973 | ssize_t ret; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5974 | int writing = rw & WRITE; |
| 5975 | int write_bits = 0; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 5976 | size_t count = iov_length(iov, nr_segs); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5977 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 5978 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
| 5979 | offset, nr_segs)) { |
| 5980 | return 0; |
| 5981 | } |
| 5982 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5983 | lockstart = offset; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 5984 | lockend = offset + count - 1; |
| 5985 | |
| 5986 | if (writing) { |
| 5987 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 5988 | if (ret) |
| 5989 | goto out; |
| 5990 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5991 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5992 | while (1) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5993 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 5994 | 0, &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5995 | /* |
| 5996 | * We're concerned with the entire range that we're going to be |
| 5997 | * doing DIO to, so we need to make sure theres no ordered |
| 5998 | * extents in this range. |
| 5999 | */ |
| 6000 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6001 | lockend - lockstart + 1); |
| 6002 | if (!ordered) |
| 6003 | break; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6004 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6005 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6006 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6007 | btrfs_put_ordered_extent(ordered); |
| 6008 | cond_resched(); |
| 6009 | } |
| 6010 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6011 | /* |
| 6012 | * we don't use btrfs_set_extent_delalloc because we don't want |
| 6013 | * the dirty or uptodate bits |
| 6014 | */ |
| 6015 | if (writing) { |
| 6016 | write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING; |
| 6017 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6018 | EXTENT_DELALLOC, 0, NULL, &cached_state, |
| 6019 | GFP_NOFS); |
| 6020 | if (ret) { |
| 6021 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6022 | lockend, EXTENT_LOCKED | write_bits, |
| 6023 | 1, 0, &cached_state, GFP_NOFS); |
| 6024 | goto out; |
| 6025 | } |
| 6026 | } |
| 6027 | |
| 6028 | free_extent_state(cached_state); |
| 6029 | cached_state = NULL; |
| 6030 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6031 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 6032 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 6033 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
| 6034 | btrfs_submit_direct, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6035 | |
| 6036 | if (ret < 0 && ret != -EIOCBQUEUED) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6037 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset, |
| 6038 | offset + iov_length(iov, nr_segs) - 1, |
| 6039 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6040 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6041 | } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) { |
| 6042 | /* |
| 6043 | * We're falling back to buffered, unlock the section we didn't |
| 6044 | * do IO on. |
| 6045 | */ |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6046 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret, |
| 6047 | offset + iov_length(iov, nr_segs) - 1, |
| 6048 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6049 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6050 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6051 | out: |
| 6052 | free_extent_state(cached_state); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6053 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6054 | } |
| 6055 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6056 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 6057 | __u64 start, __u64 len) |
| 6058 | { |
| 6059 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent); |
| 6060 | } |
| 6061 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6062 | int btrfs_readpage(struct file *file, struct page *page) |
| 6063 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6064 | struct extent_io_tree *tree; |
| 6065 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6066 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6067 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6068 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6069 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 6070 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6071 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6072 | |
| 6073 | |
| 6074 | if (current->flags & PF_MEMALLOC) { |
| 6075 | redirty_page_for_writepage(wbc, page); |
| 6076 | unlock_page(page); |
| 6077 | return 0; |
| 6078 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6079 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6080 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 6081 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6082 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6083 | int btrfs_writepages(struct address_space *mapping, |
| 6084 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6085 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6086 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6087 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6088 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6089 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 6090 | } |
| 6091 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6092 | static int |
| 6093 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 6094 | struct list_head *pages, unsigned nr_pages) |
| 6095 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6096 | struct extent_io_tree *tree; |
| 6097 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6098 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 6099 | btrfs_get_extent); |
| 6100 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6101 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6102 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6103 | struct extent_io_tree *tree; |
| 6104 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6105 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6106 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6107 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 6108 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6109 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6110 | if (ret == 1) { |
| 6111 | ClearPagePrivate(page); |
| 6112 | set_page_private(page, 0); |
| 6113 | page_cache_release(page); |
| 6114 | } |
| 6115 | return ret; |
| 6116 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6117 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6118 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 6119 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 6120 | if (PageWriteback(page) || PageDirty(page)) |
| 6121 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 6122 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6123 | } |
| 6124 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6125 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 6126 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6127 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6128 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6129 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6130 | u64 page_start = page_offset(page); |
| 6131 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6132 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6133 | |
| 6134 | /* |
| 6135 | * we have the page locked, so new writeback can't start, |
| 6136 | * and the dirty bit won't be cleared while we are here. |
| 6137 | * |
| 6138 | * Wait for IO on this page so that we can safely clear |
| 6139 | * the PagePrivate2 bit and do ordered accounting |
| 6140 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6141 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6142 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6143 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6144 | if (offset) { |
| 6145 | btrfs_releasepage(page, GFP_NOFS); |
| 6146 | return; |
| 6147 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6148 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6149 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6150 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 6151 | page_offset(page)); |
| 6152 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6153 | /* |
| 6154 | * IO on this page will never be started, so we need |
| 6155 | * to account for any ordered extents now |
| 6156 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6157 | clear_extent_bit(tree, page_start, page_end, |
| 6158 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6159 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6160 | &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6161 | /* |
| 6162 | * whoever cleared the private bit is responsible |
| 6163 | * for the finish_ordered_io |
| 6164 | */ |
| 6165 | if (TestClearPagePrivate2(page)) { |
| 6166 | btrfs_finish_ordered_io(page->mapping->host, |
| 6167 | page_start, page_end); |
| 6168 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6169 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6170 | cached_state = NULL; |
| 6171 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6172 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6173 | } |
| 6174 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6175 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6176 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6177 | __btrfs_releasepage(page, GFP_NOFS); |
| 6178 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6179 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6180 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6181 | ClearPagePrivate(page); |
| 6182 | set_page_private(page, 0); |
| 6183 | page_cache_release(page); |
| 6184 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6185 | } |
| 6186 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6187 | /* |
| 6188 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 6189 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 6190 | * be careful to check for EOF conditions here. We set the page up correctly |
| 6191 | * for a written page which means we get ENOSPC checking when writing into |
| 6192 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 6193 | * support these features. |
| 6194 | * |
| 6195 | * We are not allowed to take the i_mutex here so we have to play games to |
| 6196 | * protect against truncate races as the page could now be beyond EOF. Because |
| 6197 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 6198 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 6199 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 6200 | * unlock the page. |
| 6201 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6202 | 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] | 6203 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6204 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 6205 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6206 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6207 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 6208 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6209 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6210 | char *kaddr; |
| 6211 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6212 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6213 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6214 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6215 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6216 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6217 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6218 | if (ret) { |
| 6219 | if (ret == -ENOMEM) |
| 6220 | ret = VM_FAULT_OOM; |
| 6221 | else /* -ENOSPC, -EIO, etc */ |
| 6222 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6223 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6224 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6225 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6226 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6227 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6228 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6229 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6230 | page_start = page_offset(page); |
| 6231 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6232 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6233 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6234 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6235 | /* page got truncated out from underneath us */ |
| 6236 | goto out_unlock; |
| 6237 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6238 | wait_on_page_writeback(page); |
| 6239 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6240 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 6241 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6242 | set_page_extent_mapped(page); |
| 6243 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6244 | /* |
| 6245 | * we can't set the delalloc bits if there are pending ordered |
| 6246 | * extents. Drop our locks and wait for them to finish |
| 6247 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6248 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6249 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6250 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6251 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6252 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6253 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6254 | btrfs_put_ordered_extent(ordered); |
| 6255 | goto again; |
| 6256 | } |
| 6257 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6258 | /* |
| 6259 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 6260 | * if it was already dirty, so for space accounting reasons we need to |
| 6261 | * clear any delalloc bits for the range we are fixing to save. There |
| 6262 | * is probably a better way to do this, but for now keep consistent with |
| 6263 | * prepare_pages in the normal write path. |
| 6264 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6265 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6266 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6267 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6268 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6269 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 6270 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6271 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6272 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6273 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6274 | ret = VM_FAULT_SIGBUS; |
| 6275 | goto out_unlock; |
| 6276 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6277 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6278 | |
| 6279 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6280 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6281 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6282 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6283 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6284 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6285 | if (zero_start != PAGE_CACHE_SIZE) { |
| 6286 | kaddr = kmap(page); |
| 6287 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 6288 | flush_dcache_page(page); |
| 6289 | kunmap(page); |
| 6290 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6291 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6292 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6293 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6294 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6295 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 6296 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 6297 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6298 | 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] | 6299 | |
| 6300 | out_unlock: |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6301 | if (!ret) |
| 6302 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6303 | unlock_page(page); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6304 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6305 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6306 | return ret; |
| 6307 | } |
| 6308 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6309 | static void btrfs_truncate(struct inode *inode) |
| 6310 | { |
| 6311 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6312 | int ret; |
| 6313 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6314 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6315 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6316 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6317 | if (!S_ISREG(inode->i_mode)) { |
| 6318 | WARN_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6319 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6320 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6321 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 6322 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 6323 | if (ret) |
| 6324 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6325 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6326 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6327 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6328 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6329 | trans = btrfs_start_transaction(root, 0); |
| 6330 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6331 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6332 | trans->block_rsv = root->orphan_block_rsv; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6333 | |
| 6334 | /* |
| 6335 | * setattr is responsible for setting the ordered_data_close flag, |
| 6336 | * but that is only tested during the last file release. That |
| 6337 | * could happen well after the next commit, leaving a great big |
| 6338 | * window where new writes may get lost if someone chooses to write |
| 6339 | * to this file after truncating to zero |
| 6340 | * |
| 6341 | * The inode doesn't have any dirty data here, and so if we commit |
| 6342 | * this is a noop. If someone immediately starts writing to the inode |
| 6343 | * it is very likely we'll catch some of their writes in this |
| 6344 | * transaction, and the commit will find this file on the ordered |
| 6345 | * data list with good things to send down. |
| 6346 | * |
| 6347 | * This is a best effort solution, there is still a window where |
| 6348 | * using truncate to replace the contents of the file will |
| 6349 | * end up with a zero length file after a crash. |
| 6350 | */ |
| 6351 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 6352 | btrfs_add_ordered_operation(trans, root, inode); |
| 6353 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6354 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6355 | if (!trans) { |
| 6356 | trans = btrfs_start_transaction(root, 0); |
| 6357 | BUG_ON(IS_ERR(trans)); |
| 6358 | btrfs_set_trans_block_group(trans, inode); |
| 6359 | trans->block_rsv = root->orphan_block_rsv; |
| 6360 | } |
| 6361 | |
| 6362 | ret = btrfs_block_rsv_check(trans, root, |
| 6363 | root->orphan_block_rsv, 0, 5); |
| 6364 | if (ret) { |
| 6365 | BUG_ON(ret != -EAGAIN); |
| 6366 | ret = btrfs_commit_transaction(trans, root); |
| 6367 | BUG_ON(ret); |
| 6368 | trans = NULL; |
| 6369 | continue; |
| 6370 | } |
| 6371 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6372 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 6373 | inode->i_size, |
| 6374 | BTRFS_EXTENT_DATA_KEY); |
| 6375 | if (ret != -EAGAIN) |
| 6376 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6377 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6378 | ret = btrfs_update_inode(trans, root, inode); |
| 6379 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6380 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6381 | nr = trans->blocks_used; |
| 6382 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6383 | trans = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6384 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
| 6387 | if (ret == 0 && inode->i_nlink > 0) { |
| 6388 | ret = btrfs_orphan_del(trans, inode); |
| 6389 | BUG_ON(ret); |
| 6390 | } |
| 6391 | |
| 6392 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6393 | BUG_ON(ret); |
| 6394 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6395 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6396 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6397 | BUG_ON(ret); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6398 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6399 | } |
| 6400 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6401 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6402 | * create a new subvolume directory/inode (helper for the ioctl). |
| 6403 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6404 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6405 | struct btrfs_root *new_root, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6406 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6407 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6408 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6409 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6410 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6411 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6412 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6413 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6414 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 6415 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6416 | inode->i_op = &btrfs_dir_inode_operations; |
| 6417 | inode->i_fop = &btrfs_dir_file_operations; |
| 6418 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6419 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6420 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6421 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6422 | err = btrfs_update_inode(trans, new_root, inode); |
| 6423 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6424 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6425 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6426 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6427 | } |
| 6428 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6429 | /* helper function for file defrag and space balancing. This |
| 6430 | * forces readahead on a given range of bytes in an inode |
| 6431 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6432 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6433 | struct file_ra_state *ra, struct file *file, |
| 6434 | pgoff_t offset, pgoff_t last_index) |
| 6435 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6436 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6437 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6438 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 6439 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6440 | } |
| 6441 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6442 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 6443 | { |
| 6444 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6445 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6446 | |
| 6447 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 6448 | if (!ei) |
| 6449 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6450 | |
| 6451 | ei->root = NULL; |
| 6452 | ei->space_info = NULL; |
| 6453 | ei->generation = 0; |
| 6454 | ei->sequence = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 6455 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6456 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6457 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6458 | ei->delalloc_bytes = 0; |
| 6459 | ei->reserved_bytes = 0; |
| 6460 | ei->disk_i_size = 0; |
| 6461 | ei->flags = 0; |
| 6462 | ei->index_cnt = (u64)-1; |
| 6463 | ei->last_unlink_trans = 0; |
| 6464 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6465 | spin_lock_init(&ei->accounting_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6466 | atomic_set(&ei->outstanding_extents, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6467 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6468 | |
| 6469 | ei->ordered_data_close = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6470 | ei->orphan_meta_reserved = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6471 | ei->dummy_inode = 0; |
| 6472 | ei->force_compress = 0; |
| 6473 | |
| 6474 | inode = &ei->vfs_inode; |
| 6475 | extent_map_tree_init(&ei->extent_tree, GFP_NOFS); |
| 6476 | extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS); |
| 6477 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS); |
| 6478 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6479 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6480 | INIT_LIST_HEAD(&ei->i_orphan); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6481 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6482 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6483 | RB_CLEAR_NODE(&ei->rb_node); |
| 6484 | |
| 6485 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6486 | } |
| 6487 | |
| 6488 | void btrfs_destroy_inode(struct inode *inode) |
| 6489 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6490 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6491 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6492 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6493 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 6494 | WARN_ON(inode->i_data.nrpages); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6495 | WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents)); |
| 6496 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6497 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6498 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6499 | * This can happen where we create an inode, but somebody else also |
| 6500 | * created the same inode and we need to destroy the one we already |
| 6501 | * created. |
| 6502 | */ |
| 6503 | if (!root) |
| 6504 | goto free; |
| 6505 | |
| 6506 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6507 | * Make sure we're properly removed from the ordered operation |
| 6508 | * lists. |
| 6509 | */ |
| 6510 | smp_mb(); |
| 6511 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 6512 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 6513 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 6514 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 6515 | } |
| 6516 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6517 | if (root == root->fs_info->tree_root) { |
| 6518 | struct btrfs_block_group_cache *block_group; |
| 6519 | |
| 6520 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 6521 | BTRFS_I(inode)->block_group); |
| 6522 | if (block_group && block_group->inode == inode) { |
| 6523 | spin_lock(&block_group->lock); |
| 6524 | block_group->inode = NULL; |
| 6525 | spin_unlock(&block_group->lock); |
| 6526 | btrfs_put_block_group(block_group); |
| 6527 | } else if (block_group) { |
| 6528 | btrfs_put_block_group(block_group); |
| 6529 | } |
| 6530 | } |
| 6531 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6532 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6533 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6534 | printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", |
| 6535 | inode->i_ino); |
| 6536 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6537 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6538 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6539 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6540 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6541 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 6542 | if (!ordered) |
| 6543 | break; |
| 6544 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6545 | printk(KERN_ERR "btrfs found ordered " |
| 6546 | "extent %llu %llu on inode cleanup\n", |
| 6547 | (unsigned long long)ordered->file_offset, |
| 6548 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6549 | btrfs_remove_ordered_extent(inode, ordered); |
| 6550 | btrfs_put_ordered_extent(ordered); |
| 6551 | btrfs_put_ordered_extent(ordered); |
| 6552 | } |
| 6553 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6554 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6555 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6556 | free: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6557 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 6558 | } |
| 6559 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6560 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6561 | { |
| 6562 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6563 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6564 | if (btrfs_root_refs(&root->root_item) == 0 && |
| 6565 | root != root->fs_info->tree_root) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6566 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6567 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6568 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6569 | } |
| 6570 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 6571 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6572 | { |
| 6573 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 6574 | |
| 6575 | inode_init_once(&ei->vfs_inode); |
| 6576 | } |
| 6577 | |
| 6578 | void btrfs_destroy_cachep(void) |
| 6579 | { |
| 6580 | if (btrfs_inode_cachep) |
| 6581 | kmem_cache_destroy(btrfs_inode_cachep); |
| 6582 | if (btrfs_trans_handle_cachep) |
| 6583 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 6584 | if (btrfs_transaction_cachep) |
| 6585 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6586 | if (btrfs_path_cachep) |
| 6587 | kmem_cache_destroy(btrfs_path_cachep); |
| 6588 | } |
| 6589 | |
| 6590 | int btrfs_init_cachep(void) |
| 6591 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6592 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 6593 | sizeof(struct btrfs_inode), 0, |
| 6594 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6595 | if (!btrfs_inode_cachep) |
| 6596 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6597 | |
| 6598 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 6599 | sizeof(struct btrfs_trans_handle), 0, |
| 6600 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6601 | if (!btrfs_trans_handle_cachep) |
| 6602 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6603 | |
| 6604 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 6605 | sizeof(struct btrfs_transaction), 0, |
| 6606 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6607 | if (!btrfs_transaction_cachep) |
| 6608 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6609 | |
| 6610 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 6611 | sizeof(struct btrfs_path), 0, |
| 6612 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6613 | if (!btrfs_path_cachep) |
| 6614 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6615 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6616 | return 0; |
| 6617 | fail: |
| 6618 | btrfs_destroy_cachep(); |
| 6619 | return -ENOMEM; |
| 6620 | } |
| 6621 | |
| 6622 | static int btrfs_getattr(struct vfsmount *mnt, |
| 6623 | struct dentry *dentry, struct kstat *stat) |
| 6624 | { |
| 6625 | struct inode *inode = dentry->d_inode; |
| 6626 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6627 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 6628 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6629 | stat->blocks = (inode_get_bytes(inode) + |
| 6630 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6631 | return 0; |
| 6632 | } |
| 6633 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6634 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 6635 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6636 | { |
| 6637 | struct btrfs_trans_handle *trans; |
| 6638 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6639 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6640 | struct inode *new_inode = new_dentry->d_inode; |
| 6641 | struct inode *old_inode = old_dentry->d_inode; |
| 6642 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6643 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6644 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6645 | int ret; |
| 6646 | |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6647 | if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 6648 | return -EPERM; |
| 6649 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6650 | /* we only allow rename subvolume link between subvolumes */ |
| 6651 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6652 | return -EXDEV; |
| 6653 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6654 | if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 6655 | (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) |
| 6656 | return -ENOTEMPTY; |
| 6657 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6658 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6659 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6660 | return -ENOTEMPTY; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6661 | /* |
| 6662 | * we're using rename to replace one file with another. |
| 6663 | * and the replacement file is large. Start IO on it now so |
| 6664 | * we don't add too much work to the end of the transaction |
| 6665 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 6666 | 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] | 6667 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 6668 | filemap_flush(old_inode->i_mapping); |
| 6669 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6670 | /* close the racy window with snapshot create/destroy ioctl */ |
| 6671 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 6672 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6673 | /* |
| 6674 | * We want to reserve the absolute worst case amount of items. So if |
| 6675 | * both inodes are subvols and we need to unlink them then that would |
| 6676 | * require 4 item modifications, but if they are both normal inodes it |
| 6677 | * would require 5 item modifications, so we'll assume their normal |
| 6678 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 6679 | * should cover the worst case number of items we'll modify. |
| 6680 | */ |
| 6681 | trans = btrfs_start_transaction(root, 20); |
| 6682 | if (IS_ERR(trans)) |
| 6683 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6684 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6685 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6686 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6687 | if (dest != root) |
| 6688 | btrfs_record_root_in_trans(trans, dest); |
| 6689 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6690 | ret = btrfs_set_inode_index(new_dir, &index); |
| 6691 | if (ret) |
| 6692 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6693 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6694 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6695 | /* force full log commit if subvolume involved. */ |
| 6696 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 6697 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6698 | ret = btrfs_insert_inode_ref(trans, dest, |
| 6699 | new_dentry->d_name.name, |
| 6700 | new_dentry->d_name.len, |
| 6701 | old_inode->i_ino, |
| 6702 | new_dir->i_ino, index); |
| 6703 | if (ret) |
| 6704 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6705 | /* |
| 6706 | * this is an ugly little race, but the rename is required |
| 6707 | * to make sure that if we crash, the inode is either at the |
| 6708 | * old name or the new one. pinning the log transaction lets |
| 6709 | * us make sure we don't allow a log commit to come in after |
| 6710 | * we unlink the name but before we add the new name back in. |
| 6711 | */ |
| 6712 | btrfs_pin_log_trans(root); |
| 6713 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6714 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6715 | * make sure the inode gets flushed if it is replacing |
| 6716 | * something. |
| 6717 | */ |
| 6718 | if (new_inode && new_inode->i_size && |
| 6719 | old_inode && S_ISREG(old_inode->i_mode)) { |
| 6720 | btrfs_add_ordered_operation(trans, root, old_inode); |
| 6721 | } |
| 6722 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6723 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 6724 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 6725 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6726 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6727 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 6728 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 6729 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6730 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6731 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 6732 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 6733 | old_dentry->d_name.name, |
| 6734 | old_dentry->d_name.len); |
| 6735 | } else { |
| 6736 | btrfs_inc_nlink(old_dentry->d_inode); |
| 6737 | ret = btrfs_unlink_inode(trans, root, old_dir, |
| 6738 | old_dentry->d_inode, |
| 6739 | old_dentry->d_name.name, |
| 6740 | old_dentry->d_name.len); |
| 6741 | } |
| 6742 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6743 | |
| 6744 | if (new_inode) { |
| 6745 | new_inode->i_ctime = CURRENT_TIME; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6746 | if (unlikely(new_inode->i_ino == |
| 6747 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 6748 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 6749 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 6750 | root_objectid, |
| 6751 | new_dentry->d_name.name, |
| 6752 | new_dentry->d_name.len); |
| 6753 | BUG_ON(new_inode->i_nlink == 0); |
| 6754 | } else { |
| 6755 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 6756 | new_dentry->d_inode, |
| 6757 | new_dentry->d_name.name, |
| 6758 | new_dentry->d_name.len); |
| 6759 | } |
| 6760 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6761 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6762 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6763 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6764 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6765 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6766 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6767 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 6768 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6769 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6770 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6771 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6772 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6773 | struct dentry *parent = dget_parent(new_dentry); |
| 6774 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
| 6775 | dput(parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6776 | btrfs_end_log_trans(root); |
| 6777 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6778 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 6779 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6780 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6781 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 6782 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6783 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6784 | return ret; |
| 6785 | } |
| 6786 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6787 | /* |
| 6788 | * some fairly slow code that needs optimization. This walks the list |
| 6789 | * of all the inodes with pending delalloc and forces them to disk. |
| 6790 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 6791 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6792 | { |
| 6793 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 6794 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6795 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6796 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 6797 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 6798 | return -EROFS; |
| 6799 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6800 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6801 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6802 | binode = list_entry(head->next, struct btrfs_inode, |
| 6803 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6804 | inode = igrab(&binode->vfs_inode); |
| 6805 | if (!inode) |
| 6806 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6807 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6808 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6809 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 6810 | if (delay_iput) |
| 6811 | btrfs_add_delayed_iput(inode); |
| 6812 | else |
| 6813 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6814 | } |
| 6815 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6816 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6817 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6818 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6819 | |
| 6820 | /* the filemap_flush will queue IO into the worker threads, but |
| 6821 | * we have to make sure the IO is actually started and that |
| 6822 | * ordered extents get created before we return |
| 6823 | */ |
| 6824 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6825 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6826 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6827 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6828 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 6829 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6830 | } |
| 6831 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6832 | return 0; |
| 6833 | } |
| 6834 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 6835 | int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput, |
| 6836 | int sync) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6837 | { |
| 6838 | struct btrfs_inode *binode; |
| 6839 | struct inode *inode = NULL; |
| 6840 | |
| 6841 | spin_lock(&root->fs_info->delalloc_lock); |
| 6842 | while (!list_empty(&root->fs_info->delalloc_inodes)) { |
| 6843 | binode = list_entry(root->fs_info->delalloc_inodes.next, |
| 6844 | struct btrfs_inode, delalloc_inodes); |
| 6845 | inode = igrab(&binode->vfs_inode); |
| 6846 | if (inode) { |
| 6847 | list_move_tail(&binode->delalloc_inodes, |
| 6848 | &root->fs_info->delalloc_inodes); |
| 6849 | break; |
| 6850 | } |
| 6851 | |
| 6852 | list_del_init(&binode->delalloc_inodes); |
| 6853 | cond_resched_lock(&root->fs_info->delalloc_lock); |
| 6854 | } |
| 6855 | spin_unlock(&root->fs_info->delalloc_lock); |
| 6856 | |
| 6857 | if (inode) { |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 6858 | if (sync) { |
| 6859 | filemap_write_and_wait(inode->i_mapping); |
| 6860 | /* |
| 6861 | * We have to do this because compression doesn't |
| 6862 | * actually set PG_writeback until it submits the pages |
| 6863 | * for IO, which happens in an async thread, so we could |
| 6864 | * race and not actually wait for any writeback pages |
| 6865 | * because they've not been submitted yet. Technically |
| 6866 | * this could still be the case for the ordered stuff |
| 6867 | * since the async thread may not have started to do its |
| 6868 | * work yet. If this becomes the case then we need to |
| 6869 | * figure out a way to make sure that in writepage we |
| 6870 | * wait for any async pages to be submitted before |
| 6871 | * returning so that fdatawait does what its supposed to |
| 6872 | * do. |
| 6873 | */ |
| 6874 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 6875 | } else { |
| 6876 | filemap_flush(inode->i_mapping); |
| 6877 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6878 | if (delay_iput) |
| 6879 | btrfs_add_delayed_iput(inode); |
| 6880 | else |
| 6881 | iput(inode); |
| 6882 | return 1; |
| 6883 | } |
| 6884 | return 0; |
| 6885 | } |
| 6886 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6887 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 6888 | const char *symname) |
| 6889 | { |
| 6890 | struct btrfs_trans_handle *trans; |
| 6891 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6892 | struct btrfs_path *path; |
| 6893 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6894 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6895 | int err; |
| 6896 | int drop_inode = 0; |
| 6897 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6898 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6899 | int name_len; |
| 6900 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6901 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6902 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6903 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6904 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6905 | |
| 6906 | name_len = strlen(symname) + 1; |
| 6907 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 6908 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6909 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6910 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 6911 | if (err) |
| 6912 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6913 | /* |
| 6914 | * 2 items for inode item and ref |
| 6915 | * 2 items for dir items |
| 6916 | * 1 item for xattr if selinux is on |
| 6917 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6918 | trans = btrfs_start_transaction(root, 5); |
| 6919 | if (IS_ERR(trans)) |
| 6920 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6921 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6922 | btrfs_set_trans_block_group(trans, dir); |
| 6923 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6924 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6925 | dentry->d_name.len, |
| 6926 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6927 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 6928 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6929 | err = PTR_ERR(inode); |
| 6930 | if (IS_ERR(inode)) |
| 6931 | goto out_unlock; |
| 6932 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 6933 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6934 | if (err) { |
| 6935 | drop_inode = 1; |
| 6936 | goto out_unlock; |
| 6937 | } |
| 6938 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6939 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6940 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6941 | if (err) |
| 6942 | drop_inode = 1; |
| 6943 | else { |
| 6944 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 6945 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6946 | inode->i_fop = &btrfs_file_operations; |
| 6947 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6948 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6949 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6950 | btrfs_update_inode_block_group(trans, inode); |
| 6951 | btrfs_update_inode_block_group(trans, dir); |
| 6952 | if (drop_inode) |
| 6953 | goto out_unlock; |
| 6954 | |
| 6955 | path = btrfs_alloc_path(); |
| 6956 | BUG_ON(!path); |
| 6957 | key.objectid = inode->i_ino; |
| 6958 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6959 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 6960 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 6961 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 6962 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6963 | if (err) { |
| 6964 | drop_inode = 1; |
| 6965 | goto out_unlock; |
| 6966 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6967 | leaf = path->nodes[0]; |
| 6968 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 6969 | struct btrfs_file_extent_item); |
| 6970 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 6971 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6972 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6973 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 6974 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 6975 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 6976 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 6977 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6978 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6979 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 6980 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6981 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6982 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6983 | inode->i_op = &btrfs_symlink_inode_operations; |
| 6984 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 6985 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6986 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6987 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6988 | err = btrfs_update_inode(trans, root, inode); |
| 6989 | if (err) |
| 6990 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6991 | |
| 6992 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6993 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 6994 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6995 | if (drop_inode) { |
| 6996 | inode_dec_link_count(inode); |
| 6997 | iput(inode); |
| 6998 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6999 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7000 | return err; |
| 7001 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7002 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7003 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7004 | u64 start, u64 num_bytes, u64 min_size, |
| 7005 | loff_t actual_len, u64 *alloc_hint, |
| 7006 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7007 | { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7008 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7009 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7010 | u64 cur_offset = start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7011 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7012 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7013 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7014 | if (trans) |
| 7015 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7016 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7017 | if (own_trans) { |
| 7018 | trans = btrfs_start_transaction(root, 3); |
| 7019 | if (IS_ERR(trans)) { |
| 7020 | ret = PTR_ERR(trans); |
| 7021 | break; |
| 7022 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7023 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7024 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7025 | ret = btrfs_reserve_extent(trans, root, num_bytes, min_size, |
| 7026 | 0, *alloc_hint, (u64)-1, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7027 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7028 | if (own_trans) |
| 7029 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7030 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7031 | } |
| 7032 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7033 | ret = insert_reserved_file_extent(trans, inode, |
| 7034 | cur_offset, ins.objectid, |
| 7035 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 7036 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7037 | BTRFS_FILE_EXTENT_PREALLOC); |
| 7038 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 7039 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7040 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7041 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7042 | num_bytes -= ins.offset; |
| 7043 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7044 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7045 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7046 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7047 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7048 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7049 | (actual_len > inode->i_size) && |
| 7050 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7051 | if (cur_offset > actual_len) |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7052 | i_size_write(inode, actual_len); |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7053 | else |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7054 | i_size_write(inode, cur_offset); |
| 7055 | i_size_write(inode, cur_offset); |
| 7056 | btrfs_ordered_update_i_size(inode, cur_offset, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7057 | } |
| 7058 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7059 | ret = btrfs_update_inode(trans, root, inode); |
| 7060 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7061 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7062 | if (own_trans) |
| 7063 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7064 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7065 | return ret; |
| 7066 | } |
| 7067 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7068 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7069 | u64 start, u64 num_bytes, u64 min_size, |
| 7070 | loff_t actual_len, u64 *alloc_hint) |
| 7071 | { |
| 7072 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7073 | min_size, actual_len, alloc_hint, |
| 7074 | NULL); |
| 7075 | } |
| 7076 | |
| 7077 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 7078 | struct btrfs_trans_handle *trans, int mode, |
| 7079 | u64 start, u64 num_bytes, u64 min_size, |
| 7080 | loff_t actual_len, u64 *alloc_hint) |
| 7081 | { |
| 7082 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7083 | min_size, actual_len, alloc_hint, trans); |
| 7084 | } |
| 7085 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7086 | static long btrfs_fallocate(struct inode *inode, int mode, |
| 7087 | loff_t offset, loff_t len) |
| 7088 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7089 | struct extent_state *cached_state = NULL; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7090 | u64 cur_offset; |
| 7091 | u64 last_byte; |
| 7092 | u64 alloc_start; |
| 7093 | u64 alloc_end; |
| 7094 | u64 alloc_hint = 0; |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 7095 | u64 locked_end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7096 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 7097 | struct extent_map *em; |
| 7098 | int ret; |
| 7099 | |
| 7100 | alloc_start = offset & ~mask; |
| 7101 | alloc_end = (offset + len + mask) & ~mask; |
| 7102 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 7103 | /* |
| 7104 | * wait for ordered IO before we have any locks. We'll loop again |
| 7105 | * below with the locks held. |
| 7106 | */ |
| 7107 | btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start); |
| 7108 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7109 | mutex_lock(&inode->i_mutex); |
| 7110 | if (alloc_start > inode->i_size) { |
| 7111 | ret = btrfs_cont_expand(inode, alloc_start); |
| 7112 | if (ret) |
| 7113 | goto out; |
| 7114 | } |
| 7115 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7116 | ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start); |
Josef Bacik | a970b0a | 2009-06-27 21:07:34 -0400 | [diff] [blame] | 7117 | if (ret) |
| 7118 | goto out; |
| 7119 | |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 7120 | locked_end = alloc_end - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7121 | while (1) { |
| 7122 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 7123 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 7124 | /* the extent lock is ordered inside the running |
| 7125 | * transaction |
| 7126 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7127 | lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start, |
| 7128 | locked_end, 0, &cached_state, GFP_NOFS); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7129 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 7130 | alloc_end - 1); |
| 7131 | if (ordered && |
| 7132 | ordered->file_offset + ordered->len > alloc_start && |
| 7133 | ordered->file_offset < alloc_end) { |
| 7134 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7135 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 7136 | alloc_start, locked_end, |
| 7137 | &cached_state, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 7138 | /* |
| 7139 | * we can't wait on the range with the transaction |
| 7140 | * running or with the extent lock held |
| 7141 | */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7142 | btrfs_wait_ordered_range(inode, alloc_start, |
| 7143 | alloc_end - alloc_start); |
| 7144 | } else { |
| 7145 | if (ordered) |
| 7146 | btrfs_put_ordered_extent(ordered); |
| 7147 | break; |
| 7148 | } |
| 7149 | } |
| 7150 | |
| 7151 | cur_offset = alloc_start; |
| 7152 | while (1) { |
| 7153 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 7154 | alloc_end - cur_offset, 0); |
| 7155 | BUG_ON(IS_ERR(em) || !em); |
| 7156 | last_byte = min(extent_map_end(em), alloc_end); |
| 7157 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7158 | if (em->block_start == EXTENT_MAP_HOLE || |
| 7159 | (cur_offset >= inode->i_size && |
| 7160 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
Josef Bacik | 8360977 | 2010-06-07 18:26:37 +0000 | [diff] [blame] | 7161 | ret = btrfs_prealloc_file_range(inode, mode, cur_offset, |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7162 | last_byte - cur_offset, |
| 7163 | 1 << inode->i_blkbits, |
| 7164 | offset + len, |
| 7165 | &alloc_hint); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7166 | if (ret < 0) { |
| 7167 | free_extent_map(em); |
| 7168 | break; |
| 7169 | } |
| 7170 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7171 | free_extent_map(em); |
| 7172 | |
| 7173 | cur_offset = last_byte; |
| 7174 | if (cur_offset >= alloc_end) { |
| 7175 | ret = 0; |
| 7176 | break; |
| 7177 | } |
| 7178 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7179 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
| 7180 | &cached_state, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 7181 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7182 | btrfs_free_reserved_data_space(inode, alloc_end - alloc_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7183 | out: |
| 7184 | mutex_unlock(&inode->i_mutex); |
| 7185 | return ret; |
| 7186 | } |
| 7187 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7188 | static int btrfs_set_page_dirty(struct page *page) |
| 7189 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7190 | return __set_page_dirty_nobuffers(page); |
| 7191 | } |
| 7192 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 7193 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7194 | { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7195 | if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7196 | return -EACCES; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7197 | return generic_permission(inode, mask, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7198 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7199 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7200 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7201 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7202 | .lookup = btrfs_lookup, |
| 7203 | .create = btrfs_create, |
| 7204 | .unlink = btrfs_unlink, |
| 7205 | .link = btrfs_link, |
| 7206 | .mkdir = btrfs_mkdir, |
| 7207 | .rmdir = btrfs_rmdir, |
| 7208 | .rename = btrfs_rename, |
| 7209 | .symlink = btrfs_symlink, |
| 7210 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7211 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7212 | .setxattr = btrfs_setxattr, |
| 7213 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7214 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7215 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7216 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7217 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7218 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7219 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7220 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7221 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7222 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 7223 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7224 | .llseek = generic_file_llseek, |
| 7225 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 7226 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7227 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7228 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7229 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7230 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 7231 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7232 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7233 | }; |
| 7234 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7235 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7236 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 7237 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7238 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7239 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7240 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7241 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 7242 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 7243 | .set_bit_hook = btrfs_set_bit_hook, |
| 7244 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7245 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 7246 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7247 | }; |
| 7248 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 7249 | /* |
| 7250 | * btrfs doesn't support the bmap operation because swapfiles |
| 7251 | * use bmap to make a mapping of extents in the file. They assume |
| 7252 | * these extents won't change over the life of the file and they |
| 7253 | * use the bmap result to do IO directly to the drive. |
| 7254 | * |
| 7255 | * the btrfs bmap call would return logical addresses that aren't |
| 7256 | * suitable for IO and they also will change frequently as COW |
| 7257 | * operations happen. So, swapfile + btrfs == corruption. |
| 7258 | * |
| 7259 | * For now we're avoiding this by dropping bmap. |
| 7260 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7261 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7262 | .readpage = btrfs_readpage, |
| 7263 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7264 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7265 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7266 | .sync_page = block_sync_page, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7267 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7268 | .invalidatepage = btrfs_invalidatepage, |
| 7269 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7270 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 7271 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7272 | }; |
| 7273 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7274 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7275 | .readpage = btrfs_readpage, |
| 7276 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 7277 | .invalidatepage = btrfs_invalidatepage, |
| 7278 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7279 | }; |
| 7280 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7281 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7282 | .truncate = btrfs_truncate, |
| 7283 | .getattr = btrfs_getattr, |
| 7284 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7285 | .setxattr = btrfs_setxattr, |
| 7286 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7287 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7288 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7289 | .permission = btrfs_permission, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7290 | .fallocate = btrfs_fallocate, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7291 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7292 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7293 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7294 | .getattr = btrfs_getattr, |
| 7295 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7296 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7297 | .setxattr = btrfs_setxattr, |
| 7298 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7299 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7300 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7301 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7302 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7303 | .readlink = generic_readlink, |
| 7304 | .follow_link = page_follow_link_light, |
| 7305 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 7306 | .getattr = btrfs_getattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7307 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 7308 | .setxattr = btrfs_setxattr, |
| 7309 | .getxattr = btrfs_getxattr, |
| 7310 | .listxattr = btrfs_listxattr, |
| 7311 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7312 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7313 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 7314 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7315 | .d_delete = btrfs_dentry_delete, |
| 7316 | }; |