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" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 53 | #include "free-space-cache.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 54 | |
| 55 | struct btrfs_iget_args { |
| 56 | u64 ino; |
| 57 | struct btrfs_root *root; |
| 58 | }; |
| 59 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 60 | static const struct inode_operations btrfs_dir_inode_operations; |
| 61 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 62 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 63 | static const struct inode_operations btrfs_special_inode_operations; |
| 64 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 65 | static const struct address_space_operations btrfs_aops; |
| 66 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 67 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 68 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 69 | |
| 70 | static struct kmem_cache *btrfs_inode_cachep; |
| 71 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 72 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 73 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 74 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 75 | |
| 76 | #define S_SHIFT 12 |
| 77 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 78 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 79 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 80 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 81 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 82 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 83 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 84 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 85 | }; |
| 86 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 87 | static int btrfs_setsize(struct inode *inode, loff_t newsize); |
| 88 | static int btrfs_truncate(struct inode *inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 89 | 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] | 90 | static noinline int cow_file_range(struct inode *inode, |
| 91 | struct page *locked_page, |
| 92 | u64 start, u64 end, int *page_started, |
| 93 | unsigned long *nr_written, int unlock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 94 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 95 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
| 96 | struct inode *inode, struct inode *dir) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 97 | { |
| 98 | int err; |
| 99 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 100 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 101 | if (!err) |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 102 | err = btrfs_xattr_security_init(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 103 | return err; |
| 104 | } |
| 105 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 106 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 107 | * this does all the hard work for inserting an inline extent into |
| 108 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 109 | * no overlapping inline items exist in the btree |
| 110 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 111 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 112 | struct btrfs_root *root, struct inode *inode, |
| 113 | u64 start, size_t size, size_t compressed_size, |
| 114 | struct page **compressed_pages) |
| 115 | { |
| 116 | struct btrfs_key key; |
| 117 | struct btrfs_path *path; |
| 118 | struct extent_buffer *leaf; |
| 119 | struct page *page = NULL; |
| 120 | char *kaddr; |
| 121 | unsigned long ptr; |
| 122 | struct btrfs_file_extent_item *ei; |
| 123 | int err = 0; |
| 124 | int ret; |
| 125 | size_t cur_size = size; |
| 126 | size_t datasize; |
| 127 | unsigned long offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 128 | int compress_type = BTRFS_COMPRESS_NONE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 129 | |
| 130 | if (compressed_size && compressed_pages) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 131 | compress_type = root->fs_info->compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 132 | cur_size = compressed_size; |
| 133 | } |
| 134 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 135 | path = btrfs_alloc_path(); |
| 136 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 137 | return -ENOMEM; |
| 138 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 139 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 140 | btrfs_set_trans_block_group(trans, inode); |
| 141 | |
| 142 | key.objectid = inode->i_ino; |
| 143 | key.offset = start; |
| 144 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 145 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 146 | |
| 147 | inode_add_bytes(inode, size); |
| 148 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 149 | datasize); |
| 150 | BUG_ON(ret); |
| 151 | if (ret) { |
| 152 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 153 | goto fail; |
| 154 | } |
| 155 | leaf = path->nodes[0]; |
| 156 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 157 | struct btrfs_file_extent_item); |
| 158 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 159 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 160 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 161 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 162 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 163 | ptr = btrfs_file_extent_inline_start(ei); |
| 164 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 165 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | struct page *cpage; |
| 167 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 168 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 169 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 170 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | PAGE_CACHE_SIZE); |
| 172 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 173 | kaddr = kmap_atomic(cpage, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 174 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 175 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 176 | |
| 177 | i++; |
| 178 | ptr += cur_size; |
| 179 | compressed_size -= cur_size; |
| 180 | } |
| 181 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 182 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 183 | } else { |
| 184 | page = find_get_page(inode->i_mapping, |
| 185 | start >> PAGE_CACHE_SHIFT); |
| 186 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 187 | kaddr = kmap_atomic(page, KM_USER0); |
| 188 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 189 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
| 190 | kunmap_atomic(kaddr, KM_USER0); |
| 191 | page_cache_release(page); |
| 192 | } |
| 193 | btrfs_mark_buffer_dirty(leaf); |
| 194 | btrfs_free_path(path); |
| 195 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 196 | /* |
| 197 | * we're an inline extent, so nobody can |
| 198 | * extend the file past i_size without locking |
| 199 | * a page we already have locked. |
| 200 | * |
| 201 | * We must do any isize and inode updates |
| 202 | * before we unlock the pages. Otherwise we |
| 203 | * could end up racing with unlink. |
| 204 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 205 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
| 206 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 207 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 208 | return 0; |
| 209 | fail: |
| 210 | btrfs_free_path(path); |
| 211 | return err; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /* |
| 216 | * conditionally insert an inline extent into the file. This |
| 217 | * does the checks required to make sure the data is small enough |
| 218 | * to fit as an inline extent. |
| 219 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 220 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 221 | struct btrfs_root *root, |
| 222 | struct inode *inode, u64 start, u64 end, |
| 223 | size_t compressed_size, |
| 224 | struct page **compressed_pages) |
| 225 | { |
| 226 | u64 isize = i_size_read(inode); |
| 227 | u64 actual_end = min(end + 1, isize); |
| 228 | u64 inline_len = actual_end - start; |
| 229 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 230 | ~((u64)root->sectorsize - 1); |
| 231 | u64 hint_byte; |
| 232 | u64 data_len = inline_len; |
| 233 | int ret; |
| 234 | |
| 235 | if (compressed_size) |
| 236 | data_len = compressed_size; |
| 237 | |
| 238 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 239 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 240 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 241 | (!compressed_size && |
| 242 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 243 | end + 1 < isize || |
| 244 | data_len > root->fs_info->max_inline) { |
| 245 | return 1; |
| 246 | } |
| 247 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 248 | ret = btrfs_drop_extents(trans, inode, start, aligned_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 249 | &hint_byte, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 250 | BUG_ON(ret); |
| 251 | |
| 252 | if (isize > actual_end) |
| 253 | inline_len = min_t(u64, isize, actual_end); |
| 254 | ret = insert_inline_extent(trans, root, inode, start, |
| 255 | inline_len, compressed_size, |
| 256 | compressed_pages); |
| 257 | BUG_ON(ret); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 258 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 259 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 263 | struct async_extent { |
| 264 | u64 start; |
| 265 | u64 ram_size; |
| 266 | u64 compressed_size; |
| 267 | struct page **pages; |
| 268 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 269 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 270 | struct list_head list; |
| 271 | }; |
| 272 | |
| 273 | struct async_cow { |
| 274 | struct inode *inode; |
| 275 | struct btrfs_root *root; |
| 276 | struct page *locked_page; |
| 277 | u64 start; |
| 278 | u64 end; |
| 279 | struct list_head extents; |
| 280 | struct btrfs_work work; |
| 281 | }; |
| 282 | |
| 283 | static noinline int add_async_extent(struct async_cow *cow, |
| 284 | u64 start, u64 ram_size, |
| 285 | u64 compressed_size, |
| 286 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 287 | unsigned long nr_pages, |
| 288 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 289 | { |
| 290 | struct async_extent *async_extent; |
| 291 | |
| 292 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
| 293 | async_extent->start = start; |
| 294 | async_extent->ram_size = ram_size; |
| 295 | async_extent->compressed_size = compressed_size; |
| 296 | async_extent->pages = pages; |
| 297 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 298 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 299 | list_add_tail(&async_extent->list, &cow->extents); |
| 300 | return 0; |
| 301 | } |
| 302 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 303 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 304 | * we create compressed extents in two phases. The first |
| 305 | * phase compresses a range of pages that have already been |
| 306 | * locked (both pages and state bits are locked). |
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 | * This is done inside an ordered work queue, and the compression |
| 309 | * is spread across many cpus. The actual IO submission is step |
| 310 | * two, and the ordered work queue takes care of making sure that |
| 311 | * happens in the same order things were put onto the queue by |
| 312 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 313 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 314 | * If this code finds it can't get good compression, it puts an |
| 315 | * entry onto the work queue to write the uncompressed bytes. This |
| 316 | * makes sure that both compressed inodes and uncompressed inodes |
| 317 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 318 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 319 | static noinline int compress_file_range(struct inode *inode, |
| 320 | struct page *locked_page, |
| 321 | u64 start, u64 end, |
| 322 | struct async_cow *async_cow, |
| 323 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 324 | { |
| 325 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 326 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 327 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 328 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 329 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 330 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 331 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 332 | struct page **pages = NULL; |
| 333 | unsigned long nr_pages; |
| 334 | unsigned long nr_pages_ret = 0; |
| 335 | unsigned long total_compressed = 0; |
| 336 | unsigned long total_in = 0; |
| 337 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 338 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 339 | int i; |
| 340 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 341 | int compress_type = root->fs_info->compress_type; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 342 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 343 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 344 | again: |
| 345 | will_compress = 0; |
| 346 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 347 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 348 | |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 349 | /* |
| 350 | * we don't want to send crud past the end of i_size through |
| 351 | * compression, that's just a waste of CPU time. So, if the |
| 352 | * end of the file is before the start of our current |
| 353 | * requested range of bytes, we bail out to the uncompressed |
| 354 | * cleanup code that can deal with all of this. |
| 355 | * |
| 356 | * It isn't really the fastest way to fix things, but this is a |
| 357 | * very uncommon corner. |
| 358 | */ |
| 359 | if (actual_end <= start) |
| 360 | goto cleanup_and_bail_uncompressed; |
| 361 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 362 | total_compressed = actual_end - start; |
| 363 | |
| 364 | /* we want to make sure that amount of ram required to uncompress |
| 365 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 366 | * of a compressed extent to 128k. This is a crucial number |
| 367 | * because it also controls how easily we can spread reads across |
| 368 | * cpus for decompression. |
| 369 | * |
| 370 | * We also want to make sure the amount of IO required to do |
| 371 | * a random read is reasonably small, so we limit the size of |
| 372 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 373 | */ |
| 374 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 375 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 376 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 377 | total_in = 0; |
| 378 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 379 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 380 | /* |
| 381 | * we do compression for mount -o compress and when the |
| 382 | * inode has not been flagged as nocompress. This flag can |
| 383 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 384 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 385 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 386 | (btrfs_test_opt(root, COMPRESS) || |
| 387 | (BTRFS_I(inode)->force_compress))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 388 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 389 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 390 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 391 | if (BTRFS_I(inode)->force_compress) |
| 392 | compress_type = BTRFS_I(inode)->force_compress; |
| 393 | |
| 394 | ret = btrfs_compress_pages(compress_type, |
| 395 | inode->i_mapping, start, |
| 396 | total_compressed, pages, |
| 397 | nr_pages, &nr_pages_ret, |
| 398 | &total_in, |
| 399 | &total_compressed, |
| 400 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 401 | |
| 402 | if (!ret) { |
| 403 | unsigned long offset = total_compressed & |
| 404 | (PAGE_CACHE_SIZE - 1); |
| 405 | struct page *page = pages[nr_pages_ret - 1]; |
| 406 | char *kaddr; |
| 407 | |
| 408 | /* zero the tail end of the last page, we might be |
| 409 | * sending it down to disk |
| 410 | */ |
| 411 | if (offset) { |
| 412 | kaddr = kmap_atomic(page, KM_USER0); |
| 413 | memset(kaddr + offset, 0, |
| 414 | PAGE_CACHE_SIZE - offset); |
| 415 | kunmap_atomic(kaddr, KM_USER0); |
| 416 | } |
| 417 | will_compress = 1; |
| 418 | } |
| 419 | } |
| 420 | if (start == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 421 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 422 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 423 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 424 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 425 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 426 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 427 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 428 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 429 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 430 | */ |
| 431 | ret = cow_file_range_inline(trans, root, inode, |
| 432 | start, end, 0, NULL); |
| 433 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 434 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 435 | ret = cow_file_range_inline(trans, root, inode, |
| 436 | start, end, |
| 437 | total_compressed, pages); |
| 438 | } |
| 439 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 440 | /* |
| 441 | * inline extent creation worked, we don't need |
| 442 | * to create any more async work items. Unlock |
| 443 | * and free up our temp pages. |
| 444 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 445 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 446 | &BTRFS_I(inode)->io_tree, |
| 447 | start, end, NULL, |
| 448 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 449 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 450 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 451 | |
| 452 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 453 | goto free_pages_out; |
| 454 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 455 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | if (will_compress) { |
| 459 | /* |
| 460 | * we aren't doing an inline extent round the compressed size |
| 461 | * up to a block size boundary so the allocator does sane |
| 462 | * things |
| 463 | */ |
| 464 | total_compressed = (total_compressed + blocksize - 1) & |
| 465 | ~(blocksize - 1); |
| 466 | |
| 467 | /* |
| 468 | * one last check to make sure the compression is really a |
| 469 | * win, compare the page count read with the blocks on disk |
| 470 | */ |
| 471 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 472 | ~(PAGE_CACHE_SIZE - 1); |
| 473 | if (total_compressed >= total_in) { |
| 474 | will_compress = 0; |
| 475 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 476 | num_bytes = total_in; |
| 477 | } |
| 478 | } |
| 479 | if (!will_compress && pages) { |
| 480 | /* |
| 481 | * the compression code ran but failed to make things smaller, |
| 482 | * free any pages it allocated and our page pointer array |
| 483 | */ |
| 484 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 485 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 486 | page_cache_release(pages[i]); |
| 487 | } |
| 488 | kfree(pages); |
| 489 | pages = NULL; |
| 490 | total_compressed = 0; |
| 491 | nr_pages_ret = 0; |
| 492 | |
| 493 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 494 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 495 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 496 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 497 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 498 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 499 | if (will_compress) { |
| 500 | *num_added += 1; |
| 501 | |
| 502 | /* the async work queues will take care of doing actual |
| 503 | * allocation on disk for these compressed pages, |
| 504 | * and will submit them to the elevator. |
| 505 | */ |
| 506 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 507 | total_compressed, pages, nr_pages_ret, |
| 508 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 509 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 510 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 511 | start += num_bytes; |
| 512 | pages = NULL; |
| 513 | cond_resched(); |
| 514 | goto again; |
| 515 | } |
| 516 | } else { |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 517 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 518 | /* |
| 519 | * No compression, but we still need to write the pages in |
| 520 | * the file we've been given so far. redirty the locked |
| 521 | * page if it corresponds to our extent and set things up |
| 522 | * for the async work queue to run cow_file_range to do |
| 523 | * the normal delalloc dance |
| 524 | */ |
| 525 | if (page_offset(locked_page) >= start && |
| 526 | page_offset(locked_page) <= end) { |
| 527 | __set_page_dirty_nobuffers(locked_page); |
| 528 | /* unlocked later on in the async handlers */ |
| 529 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 530 | add_async_extent(async_cow, start, end - start + 1, |
| 531 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 532 | *num_added += 1; |
| 533 | } |
| 534 | |
| 535 | out: |
| 536 | return 0; |
| 537 | |
| 538 | free_pages_out: |
| 539 | for (i = 0; i < nr_pages_ret; i++) { |
| 540 | WARN_ON(pages[i]->mapping); |
| 541 | page_cache_release(pages[i]); |
| 542 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 543 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 544 | |
| 545 | goto out; |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * phase two of compressed writeback. This is the ordered portion |
| 550 | * of the code, which only gets called in the order the work was |
| 551 | * queued. We walk all the async extents created by compress_file_range |
| 552 | * and send them down to the disk. |
| 553 | */ |
| 554 | static noinline int submit_compressed_extents(struct inode *inode, |
| 555 | struct async_cow *async_cow) |
| 556 | { |
| 557 | struct async_extent *async_extent; |
| 558 | u64 alloc_hint = 0; |
| 559 | struct btrfs_trans_handle *trans; |
| 560 | struct btrfs_key ins; |
| 561 | struct extent_map *em; |
| 562 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 563 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 564 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 565 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 566 | |
| 567 | if (list_empty(&async_cow->extents)) |
| 568 | return 0; |
| 569 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 570 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 571 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 572 | async_extent = list_entry(async_cow->extents.next, |
| 573 | struct async_extent, list); |
| 574 | list_del(&async_extent->list); |
| 575 | |
| 576 | io_tree = &BTRFS_I(inode)->io_tree; |
| 577 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 578 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 579 | /* did the compression code fall back to uncompressed IO? */ |
| 580 | if (!async_extent->pages) { |
| 581 | int page_started = 0; |
| 582 | unsigned long nr_written = 0; |
| 583 | |
| 584 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 585 | async_extent->start + |
| 586 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 587 | |
| 588 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 589 | ret = cow_file_range(inode, async_cow->locked_page, |
| 590 | async_extent->start, |
| 591 | async_extent->start + |
| 592 | async_extent->ram_size - 1, |
| 593 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 594 | |
| 595 | /* |
| 596 | * if page_started, cow_file_range inserted an |
| 597 | * inline extent and took care of all the unlocking |
| 598 | * and IO for us. Otherwise, we need to submit |
| 599 | * all those pages down to the drive. |
| 600 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 601 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 602 | extent_write_locked_range(io_tree, |
| 603 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 604 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 605 | async_extent->ram_size - 1, |
| 606 | btrfs_get_extent, |
| 607 | WB_SYNC_ALL); |
| 608 | kfree(async_extent); |
| 609 | cond_resched(); |
| 610 | continue; |
| 611 | } |
| 612 | |
| 613 | lock_extent(io_tree, async_extent->start, |
| 614 | async_extent->start + async_extent->ram_size - 1, |
| 615 | GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 616 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 617 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 618 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 619 | ret = btrfs_reserve_extent(trans, root, |
| 620 | async_extent->compressed_size, |
| 621 | async_extent->compressed_size, |
| 622 | 0, alloc_hint, |
| 623 | (u64)-1, &ins, 1); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 624 | btrfs_end_transaction(trans, root); |
| 625 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 626 | if (ret) { |
| 627 | int i; |
| 628 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 629 | WARN_ON(async_extent->pages[i]->mapping); |
| 630 | page_cache_release(async_extent->pages[i]); |
| 631 | } |
| 632 | kfree(async_extent->pages); |
| 633 | async_extent->nr_pages = 0; |
| 634 | async_extent->pages = NULL; |
| 635 | unlock_extent(io_tree, async_extent->start, |
| 636 | async_extent->start + |
| 637 | async_extent->ram_size - 1, GFP_NOFS); |
| 638 | goto retry; |
| 639 | } |
| 640 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 641 | /* |
| 642 | * here we're doing allocation and writeback of the |
| 643 | * compressed pages |
| 644 | */ |
| 645 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 646 | async_extent->start + |
| 647 | async_extent->ram_size - 1, 0); |
| 648 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 649 | em = alloc_extent_map(GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 650 | BUG_ON(!em); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 651 | em->start = async_extent->start; |
| 652 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 653 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 654 | |
| 655 | em->block_start = ins.objectid; |
| 656 | em->block_len = ins.offset; |
| 657 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 658 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 659 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 660 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 661 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 662 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 663 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 664 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 665 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 666 | if (ret != -EEXIST) { |
| 667 | free_extent_map(em); |
| 668 | break; |
| 669 | } |
| 670 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 671 | async_extent->start + |
| 672 | async_extent->ram_size - 1, 0); |
| 673 | } |
| 674 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 675 | ret = btrfs_add_ordered_extent_compress(inode, |
| 676 | async_extent->start, |
| 677 | ins.objectid, |
| 678 | async_extent->ram_size, |
| 679 | ins.offset, |
| 680 | BTRFS_ORDERED_COMPRESSED, |
| 681 | async_extent->compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 682 | BUG_ON(ret); |
| 683 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 684 | /* |
| 685 | * clear dirty, set writeback and unlock the pages. |
| 686 | */ |
| 687 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 688 | &BTRFS_I(inode)->io_tree, |
| 689 | async_extent->start, |
| 690 | async_extent->start + |
| 691 | async_extent->ram_size - 1, |
| 692 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 693 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 694 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 695 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 696 | |
| 697 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 698 | async_extent->start, |
| 699 | async_extent->ram_size, |
| 700 | ins.objectid, |
| 701 | ins.offset, async_extent->pages, |
| 702 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 703 | |
| 704 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 705 | alloc_hint = ins.objectid + ins.offset; |
| 706 | kfree(async_extent); |
| 707 | cond_resched(); |
| 708 | } |
| 709 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 710 | return 0; |
| 711 | } |
| 712 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 713 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 714 | u64 num_bytes) |
| 715 | { |
| 716 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 717 | struct extent_map *em; |
| 718 | u64 alloc_hint = 0; |
| 719 | |
| 720 | read_lock(&em_tree->lock); |
| 721 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 722 | if (em) { |
| 723 | /* |
| 724 | * if block start isn't an actual block number then find the |
| 725 | * first block in this inode and use that as a hint. If that |
| 726 | * block is also bogus then just don't worry about it. |
| 727 | */ |
| 728 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 729 | free_extent_map(em); |
| 730 | em = search_extent_mapping(em_tree, 0, 0); |
| 731 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 732 | alloc_hint = em->block_start; |
| 733 | if (em) |
| 734 | free_extent_map(em); |
| 735 | } else { |
| 736 | alloc_hint = em->block_start; |
| 737 | free_extent_map(em); |
| 738 | } |
| 739 | } |
| 740 | read_unlock(&em_tree->lock); |
| 741 | |
| 742 | return alloc_hint; |
| 743 | } |
| 744 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 745 | /* |
| 746 | * when extent_io.c finds a delayed allocation range in the file, |
| 747 | * the call backs end up in this code. The basic idea is to |
| 748 | * allocate extents on disk for the range, and create ordered data structs |
| 749 | * in ram to track those extents. |
| 750 | * |
| 751 | * locked_page is the page that writepage had locked already. We use |
| 752 | * it to make sure we don't do extra locks or unlocks. |
| 753 | * |
| 754 | * *page_started is set to one if we unlock locked_page and do everything |
| 755 | * required to start IO on it. It may be clean and already done with |
| 756 | * IO when we return. |
| 757 | */ |
| 758 | static noinline int cow_file_range(struct inode *inode, |
| 759 | struct page *locked_page, |
| 760 | u64 start, u64 end, int *page_started, |
| 761 | unsigned long *nr_written, |
| 762 | int unlock) |
| 763 | { |
| 764 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 765 | struct btrfs_trans_handle *trans; |
| 766 | u64 alloc_hint = 0; |
| 767 | u64 num_bytes; |
| 768 | unsigned long ram_size; |
| 769 | u64 disk_num_bytes; |
| 770 | u64 cur_alloc_size; |
| 771 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 772 | struct btrfs_key ins; |
| 773 | struct extent_map *em; |
| 774 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 775 | int ret = 0; |
| 776 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 777 | BUG_ON(root == root->fs_info->tree_root); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 778 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 779 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 780 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 781 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 782 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 783 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 784 | num_bytes = max(blocksize, num_bytes); |
| 785 | disk_num_bytes = num_bytes; |
| 786 | ret = 0; |
| 787 | |
| 788 | if (start == 0) { |
| 789 | /* lets try to make an inline extent */ |
| 790 | ret = cow_file_range_inline(trans, root, inode, |
| 791 | start, end, 0, NULL); |
| 792 | if (ret == 0) { |
| 793 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 794 | &BTRFS_I(inode)->io_tree, |
| 795 | start, end, NULL, |
| 796 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 797 | EXTENT_CLEAR_UNLOCK | |
| 798 | EXTENT_CLEAR_DELALLOC | |
| 799 | EXTENT_CLEAR_DIRTY | |
| 800 | EXTENT_SET_WRITEBACK | |
| 801 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 802 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 803 | *nr_written = *nr_written + |
| 804 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 805 | *page_started = 1; |
| 806 | ret = 0; |
| 807 | goto out; |
| 808 | } |
| 809 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 810 | |
| 811 | BUG_ON(disk_num_bytes > |
| 812 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 813 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 814 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 815 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 816 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 817 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 818 | unsigned long op; |
| 819 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 820 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 821 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 822 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 823 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 824 | BUG_ON(ret); |
| 825 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 826 | em = alloc_extent_map(GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 827 | BUG_ON(!em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 828 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 829 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 830 | ram_size = ins.offset; |
| 831 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 832 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 833 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 834 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 835 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 836 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 837 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 838 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 839 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 840 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 841 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 842 | if (ret != -EEXIST) { |
| 843 | free_extent_map(em); |
| 844 | break; |
| 845 | } |
| 846 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 847 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 848 | } |
| 849 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 850 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 851 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 852 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 853 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 854 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 855 | if (root->root_key.objectid == |
| 856 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 857 | ret = btrfs_reloc_clone_csums(inode, start, |
| 858 | cur_alloc_size); |
| 859 | BUG_ON(ret); |
| 860 | } |
| 861 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 862 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 863 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 864 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 865 | /* we're not doing compressed IO, don't unlock the first |
| 866 | * page (which the caller expects to stay locked), don't |
| 867 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 868 | * |
| 869 | * Do set the Private2 bit so we know this page was properly |
| 870 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 871 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 872 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 873 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 874 | EXTENT_SET_PRIVATE2; |
| 875 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 876 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 877 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 878 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 879 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 880 | num_bytes -= cur_alloc_size; |
| 881 | alloc_hint = ins.objectid + ins.offset; |
| 882 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 883 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 884 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 885 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 886 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 887 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 888 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 889 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 890 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 891 | /* |
| 892 | * work queue call back to started compression on a file and pages |
| 893 | */ |
| 894 | static noinline void async_cow_start(struct btrfs_work *work) |
| 895 | { |
| 896 | struct async_cow *async_cow; |
| 897 | int num_added = 0; |
| 898 | async_cow = container_of(work, struct async_cow, work); |
| 899 | |
| 900 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 901 | async_cow->start, async_cow->end, async_cow, |
| 902 | &num_added); |
| 903 | if (num_added == 0) |
| 904 | async_cow->inode = NULL; |
| 905 | } |
| 906 | |
| 907 | /* |
| 908 | * work queue call back to submit previously compressed pages |
| 909 | */ |
| 910 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 911 | { |
| 912 | struct async_cow *async_cow; |
| 913 | struct btrfs_root *root; |
| 914 | unsigned long nr_pages; |
| 915 | |
| 916 | async_cow = container_of(work, struct async_cow, work); |
| 917 | |
| 918 | root = async_cow->root; |
| 919 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 920 | PAGE_CACHE_SHIFT; |
| 921 | |
| 922 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 923 | |
| 924 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 925 | 5 * 1042 * 1024 && |
| 926 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 927 | wake_up(&root->fs_info->async_submit_wait); |
| 928 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 929 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 930 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 931 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 932 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 933 | static noinline void async_cow_free(struct btrfs_work *work) |
| 934 | { |
| 935 | struct async_cow *async_cow; |
| 936 | async_cow = container_of(work, struct async_cow, work); |
| 937 | kfree(async_cow); |
| 938 | } |
| 939 | |
| 940 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 941 | u64 start, u64 end, int *page_started, |
| 942 | unsigned long *nr_written) |
| 943 | { |
| 944 | struct async_cow *async_cow; |
| 945 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 946 | unsigned long nr_pages; |
| 947 | u64 cur_end; |
| 948 | int limit = 10 * 1024 * 1042; |
| 949 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 950 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 951 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 952 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 953 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
| 954 | async_cow->inode = inode; |
| 955 | async_cow->root = root; |
| 956 | async_cow->locked_page = locked_page; |
| 957 | async_cow->start = start; |
| 958 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 959 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 960 | cur_end = end; |
| 961 | else |
| 962 | cur_end = min(end, start + 512 * 1024 - 1); |
| 963 | |
| 964 | async_cow->end = cur_end; |
| 965 | INIT_LIST_HEAD(&async_cow->extents); |
| 966 | |
| 967 | async_cow->work.func = async_cow_start; |
| 968 | async_cow->work.ordered_func = async_cow_submit; |
| 969 | async_cow->work.ordered_free = async_cow_free; |
| 970 | async_cow->work.flags = 0; |
| 971 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 972 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 973 | PAGE_CACHE_SHIFT; |
| 974 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 975 | |
| 976 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 977 | &async_cow->work); |
| 978 | |
| 979 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 980 | wait_event(root->fs_info->async_submit_wait, |
| 981 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 982 | limit)); |
| 983 | } |
| 984 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 985 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 986 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 987 | wait_event(root->fs_info->async_submit_wait, |
| 988 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 989 | 0)); |
| 990 | } |
| 991 | |
| 992 | *nr_written += nr_pages; |
| 993 | start = cur_end + 1; |
| 994 | } |
| 995 | *page_started = 1; |
| 996 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 997 | } |
| 998 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 999 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1000 | u64 bytenr, u64 num_bytes) |
| 1001 | { |
| 1002 | int ret; |
| 1003 | struct btrfs_ordered_sum *sums; |
| 1004 | LIST_HEAD(list); |
| 1005 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1006 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
| 1007 | bytenr + num_bytes - 1, &list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1008 | if (ret == 0 && list_empty(&list)) |
| 1009 | return 0; |
| 1010 | |
| 1011 | while (!list_empty(&list)) { |
| 1012 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1013 | list_del(&sums->list); |
| 1014 | kfree(sums); |
| 1015 | } |
| 1016 | return 1; |
| 1017 | } |
| 1018 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1019 | /* |
| 1020 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1021 | * of the extents that exist in the file, and COWs the file as required. |
| 1022 | * |
| 1023 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1024 | * blocks on disk |
| 1025 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1026 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1027 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1028 | u64 start, u64 end, int *page_started, int force, |
| 1029 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1030 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1031 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1032 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1033 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1034 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1035 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1036 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1037 | u64 cow_start; |
| 1038 | u64 cur_offset; |
| 1039 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1040 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1041 | u64 disk_bytenr; |
| 1042 | u64 num_bytes; |
| 1043 | int extent_type; |
| 1044 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1045 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1046 | int nocow; |
| 1047 | int check_prev = 1; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1048 | bool nolock = false; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1049 | |
| 1050 | path = btrfs_alloc_path(); |
| 1051 | BUG_ON(!path); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1052 | if (root == root->fs_info->tree_root) { |
| 1053 | nolock = true; |
| 1054 | trans = btrfs_join_transaction_nolock(root, 1); |
| 1055 | } else { |
| 1056 | trans = btrfs_join_transaction(root, 1); |
| 1057 | } |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1058 | BUG_ON(IS_ERR(trans)); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1059 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1060 | cow_start = (u64)-1; |
| 1061 | cur_offset = start; |
| 1062 | while (1) { |
| 1063 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 1064 | cur_offset, 0); |
| 1065 | BUG_ON(ret < 0); |
| 1066 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1067 | leaf = path->nodes[0]; |
| 1068 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1069 | path->slots[0] - 1); |
| 1070 | if (found_key.objectid == inode->i_ino && |
| 1071 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1072 | path->slots[0]--; |
| 1073 | } |
| 1074 | check_prev = 0; |
| 1075 | next_slot: |
| 1076 | leaf = path->nodes[0]; |
| 1077 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1078 | ret = btrfs_next_leaf(root, path); |
| 1079 | if (ret < 0) |
| 1080 | BUG_ON(1); |
| 1081 | if (ret > 0) |
| 1082 | break; |
| 1083 | leaf = path->nodes[0]; |
| 1084 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1085 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1086 | nocow = 0; |
| 1087 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1088 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1089 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1090 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1091 | if (found_key.objectid > inode->i_ino || |
| 1092 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1093 | found_key.offset > end) |
| 1094 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1095 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1096 | if (found_key.offset > cur_offset) { |
| 1097 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1098 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1099 | goto out_check; |
| 1100 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1101 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1102 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1103 | struct btrfs_file_extent_item); |
| 1104 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1105 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1106 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1107 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1108 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1109 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1110 | extent_end = found_key.offset + |
| 1111 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1112 | if (extent_end <= start) { |
| 1113 | path->slots[0]++; |
| 1114 | goto next_slot; |
| 1115 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1116 | if (disk_bytenr == 0) |
| 1117 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1118 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1119 | btrfs_file_extent_encryption(leaf, fi) || |
| 1120 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1121 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1122 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1123 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1124 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1125 | goto out_check; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1126 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1127 | found_key.offset - |
| 1128 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1129 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1130 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1131 | disk_bytenr += cur_offset - found_key.offset; |
| 1132 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1133 | /* |
| 1134 | * force cow if csum exists in the range. |
| 1135 | * this ensure that csum for a given extent are |
| 1136 | * either valid or do not exist. |
| 1137 | */ |
| 1138 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1139 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1140 | nocow = 1; |
| 1141 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1142 | extent_end = found_key.offset + |
| 1143 | btrfs_file_extent_inline_len(leaf, fi); |
| 1144 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1145 | } else { |
| 1146 | BUG_ON(1); |
| 1147 | } |
| 1148 | out_check: |
| 1149 | if (extent_end <= start) { |
| 1150 | path->slots[0]++; |
| 1151 | goto next_slot; |
| 1152 | } |
| 1153 | if (!nocow) { |
| 1154 | if (cow_start == (u64)-1) |
| 1155 | cow_start = cur_offset; |
| 1156 | cur_offset = extent_end; |
| 1157 | if (cur_offset > end) |
| 1158 | break; |
| 1159 | path->slots[0]++; |
| 1160 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1161 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1162 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1163 | btrfs_release_path(root, path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1164 | if (cow_start != (u64)-1) { |
| 1165 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1166 | found_key.offset - 1, page_started, |
| 1167 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1168 | BUG_ON(ret); |
| 1169 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1170 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1171 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1172 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1173 | struct extent_map *em; |
| 1174 | struct extent_map_tree *em_tree; |
| 1175 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 1176 | em = alloc_extent_map(GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 1177 | BUG_ON(!em); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1178 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1179 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1180 | em->len = num_bytes; |
| 1181 | em->block_len = num_bytes; |
| 1182 | em->block_start = disk_bytenr; |
| 1183 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1184 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1185 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1186 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1187 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1188 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1189 | if (ret != -EEXIST) { |
| 1190 | free_extent_map(em); |
| 1191 | break; |
| 1192 | } |
| 1193 | btrfs_drop_extent_cache(inode, em->start, |
| 1194 | em->start + em->len - 1, 0); |
| 1195 | } |
| 1196 | type = BTRFS_ORDERED_PREALLOC; |
| 1197 | } else { |
| 1198 | type = BTRFS_ORDERED_NOCOW; |
| 1199 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1200 | |
| 1201 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1202 | num_bytes, num_bytes, type); |
| 1203 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1204 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1205 | if (root->root_key.objectid == |
| 1206 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1207 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1208 | num_bytes); |
| 1209 | BUG_ON(ret); |
| 1210 | } |
| 1211 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1212 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1213 | cur_offset, cur_offset + num_bytes - 1, |
| 1214 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1215 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1216 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1217 | cur_offset = extent_end; |
| 1218 | if (cur_offset > end) |
| 1219 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1220 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1221 | btrfs_release_path(root, path); |
| 1222 | |
| 1223 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1224 | cow_start = cur_offset; |
| 1225 | if (cow_start != (u64)-1) { |
| 1226 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1227 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1228 | BUG_ON(ret); |
| 1229 | } |
| 1230 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1231 | if (nolock) { |
| 1232 | ret = btrfs_end_transaction_nolock(trans, root); |
| 1233 | BUG_ON(ret); |
| 1234 | } else { |
| 1235 | ret = btrfs_end_transaction(trans, root); |
| 1236 | BUG_ON(ret); |
| 1237 | } |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1238 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1239 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1240 | } |
| 1241 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1242 | /* |
| 1243 | * extent_io.c call back to do delayed allocation processing |
| 1244 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1245 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1246 | u64 start, u64 end, int *page_started, |
| 1247 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1248 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1249 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1250 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1251 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1252 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1253 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1254 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1255 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1256 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1257 | page_started, 0, nr_written); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1258 | else if (!btrfs_test_opt(root, COMPRESS) && |
| 1259 | !(BTRFS_I(inode)->force_compress)) |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1260 | ret = cow_file_range(inode, locked_page, start, end, |
| 1261 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1262 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1263 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1264 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1265 | return ret; |
| 1266 | } |
| 1267 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1268 | static int btrfs_split_extent_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1269 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1270 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1271 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1272 | if (!(orig->state & EXTENT_DELALLOC)) |
| 1273 | return 0; |
| 1274 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1275 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1276 | return 0; |
| 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1281 | * extents so we can keep track of new extents that are just merged onto old |
| 1282 | * extents, such as when we are doing sequential writes, so we can properly |
| 1283 | * account for the metadata space we'll need. |
| 1284 | */ |
| 1285 | static int btrfs_merge_extent_hook(struct inode *inode, |
| 1286 | struct extent_state *new, |
| 1287 | struct extent_state *other) |
| 1288 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1289 | /* not delalloc, ignore it */ |
| 1290 | if (!(other->state & EXTENT_DELALLOC)) |
| 1291 | return 0; |
| 1292 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1293 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1294 | return 0; |
| 1295 | } |
| 1296 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1297 | /* |
| 1298 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1299 | * bytes in this file, and to maintain the list of inodes that |
| 1300 | * have pending delalloc work to be done. |
| 1301 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1302 | static int btrfs_set_bit_hook(struct inode *inode, |
| 1303 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1304 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1305 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1306 | /* |
| 1307 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1308 | * but in this case, we are only testeing for the DELALLOC |
| 1309 | * bit, which is only set or cleared with irqs on |
| 1310 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1311 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1312 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1313 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1314 | int do_list = (root->root_key.objectid != |
| 1315 | BTRFS_ROOT_TREE_OBJECTID); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1316 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1317 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1318 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1319 | else |
| 1320 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1321 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1322 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1323 | BTRFS_I(inode)->delalloc_bytes += len; |
| 1324 | root->fs_info->delalloc_bytes += len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1325 | if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1326 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1327 | &root->fs_info->delalloc_inodes); |
| 1328 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1329 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1330 | } |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1334 | /* |
| 1335 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1336 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1337 | static int btrfs_clear_bit_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1338 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1339 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1340 | /* |
| 1341 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1342 | * but in this case, we are only testeing for the DELALLOC |
| 1343 | * bit, which is only set or cleared with irqs on |
| 1344 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1345 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1346 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1347 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1348 | int do_list = (root->root_key.objectid != |
| 1349 | BTRFS_ROOT_TREE_OBJECTID); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1350 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1351 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1352 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1353 | else if (!(*bits & EXTENT_DO_ACCOUNTING)) |
| 1354 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
| 1355 | |
| 1356 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1357 | btrfs_delalloc_release_metadata(inode, len); |
| 1358 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1359 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 1360 | && do_list) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1361 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1362 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1363 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1364 | root->fs_info->delalloc_bytes -= len; |
| 1365 | BTRFS_I(inode)->delalloc_bytes -= len; |
| 1366 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1367 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1368 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1369 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1370 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1371 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1372 | } |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1376 | /* |
| 1377 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1378 | * we don't create bios that span stripes or chunks |
| 1379 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1380 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1381 | size_t size, struct bio *bio, |
| 1382 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1383 | { |
| 1384 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1385 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1386 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1387 | u64 length = 0; |
| 1388 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1389 | int ret; |
| 1390 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1391 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1392 | return 0; |
| 1393 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1394 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1395 | map_tree = &root->fs_info->mapping_tree; |
| 1396 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1397 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1398 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1399 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1400 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1401 | return 1; |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 1402 | return ret; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1403 | } |
| 1404 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1405 | /* |
| 1406 | * in order to insert checksums into the metadata in large chunks, |
| 1407 | * we wait until bio submission time. All the pages in the bio are |
| 1408 | * checksummed and sums are attached onto the ordered extent record. |
| 1409 | * |
| 1410 | * At IO completion time the cums attached on the ordered extent record |
| 1411 | * are inserted into the btree |
| 1412 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1413 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1414 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1415 | unsigned long bio_flags, |
| 1416 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1417 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1418 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1419 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1420 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1421 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1422 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1425 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1426 | /* |
| 1427 | * in order to insert checksums into the metadata in large chunks, |
| 1428 | * we wait until bio submission time. All the pages in the bio are |
| 1429 | * checksummed and sums are attached onto the ordered extent record. |
| 1430 | * |
| 1431 | * At IO completion time the cums attached on the ordered extent record |
| 1432 | * are inserted into the btree |
| 1433 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1434 | 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] | 1435 | int mirror_num, unsigned long bio_flags, |
| 1436 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1437 | { |
| 1438 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1439 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1440 | } |
| 1441 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1442 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1443 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1444 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1445 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1446 | 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] | 1447 | int mirror_num, unsigned long bio_flags, |
| 1448 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1449 | { |
| 1450 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1451 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1452 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1453 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1454 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1455 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1456 | if (root == root->fs_info->tree_root) |
| 1457 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2); |
| 1458 | else |
| 1459 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1460 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1461 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1462 | if (!(rw & REQ_WRITE)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1463 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1464 | return btrfs_submit_compressed_read(inode, bio, |
| 1465 | mirror_num, bio_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1466 | } else if (!skip_sum) |
| 1467 | btrfs_lookup_bio_sums(root, inode, bio, NULL); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1468 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1469 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1470 | /* csum items have already been cloned */ |
| 1471 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1472 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1473 | /* we're doing a write, do the async checksumming */ |
| 1474 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1475 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1476 | bio_flags, bio_offset, |
| 1477 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1478 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1479 | } |
| 1480 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1481 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1482 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1483 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1484 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1485 | /* |
| 1486 | * given a list of ordered sums record them in the inode. This happens |
| 1487 | * at IO completion time based on sums calculated at bio submission time. |
| 1488 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1489 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1490 | struct inode *inode, u64 file_offset, |
| 1491 | struct list_head *list) |
| 1492 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1493 | struct btrfs_ordered_sum *sum; |
| 1494 | |
| 1495 | btrfs_set_trans_block_group(trans, inode); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1496 | |
| 1497 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1498 | btrfs_csum_file_blocks(trans, |
| 1499 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1500 | } |
| 1501 | return 0; |
| 1502 | } |
| 1503 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1504 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1505 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1506 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1507 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1508 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1509 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1510 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1511 | } |
| 1512 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1513 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1514 | struct btrfs_writepage_fixup { |
| 1515 | struct page *page; |
| 1516 | struct btrfs_work work; |
| 1517 | }; |
| 1518 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1519 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1520 | { |
| 1521 | struct btrfs_writepage_fixup *fixup; |
| 1522 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1523 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1524 | struct page *page; |
| 1525 | struct inode *inode; |
| 1526 | u64 page_start; |
| 1527 | u64 page_end; |
| 1528 | |
| 1529 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1530 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1531 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1532 | lock_page(page); |
| 1533 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1534 | ClearPageChecked(page); |
| 1535 | goto out_page; |
| 1536 | } |
| 1537 | |
| 1538 | inode = page->mapping->host; |
| 1539 | page_start = page_offset(page); |
| 1540 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1541 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1542 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
| 1543 | &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1544 | |
| 1545 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1546 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1547 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1548 | |
| 1549 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1550 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1551 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1552 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1553 | unlock_page(page); |
| 1554 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1555 | goto again; |
| 1556 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1557 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1558 | BUG(); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1559 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1560 | ClearPageChecked(page); |
| 1561 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1562 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1563 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1564 | out_page: |
| 1565 | unlock_page(page); |
| 1566 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1567 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | * There are a few paths in the higher layers of the kernel that directly |
| 1572 | * set the page dirty bit without asking the filesystem if it is a |
| 1573 | * good idea. This causes problems because we want to make sure COW |
| 1574 | * properly happens and the data=ordered rules are followed. |
| 1575 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1576 | * 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] | 1577 | * hasn't been properly setup for IO. We kick off an async process |
| 1578 | * to fix it up. The async helper will wait for ordered extents, set |
| 1579 | * the delalloc bit and make it safe to write the page. |
| 1580 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1581 | 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] | 1582 | { |
| 1583 | struct inode *inode = page->mapping->host; |
| 1584 | struct btrfs_writepage_fixup *fixup; |
| 1585 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1586 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1587 | /* this page is properly in the ordered list */ |
| 1588 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1589 | return 0; |
| 1590 | |
| 1591 | if (PageChecked(page)) |
| 1592 | return -EAGAIN; |
| 1593 | |
| 1594 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1595 | if (!fixup) |
| 1596 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1597 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1598 | SetPageChecked(page); |
| 1599 | page_cache_get(page); |
| 1600 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1601 | fixup->page = page; |
| 1602 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1603 | return -EAGAIN; |
| 1604 | } |
| 1605 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1606 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1607 | struct inode *inode, u64 file_pos, |
| 1608 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1609 | u64 num_bytes, u64 ram_bytes, |
| 1610 | u8 compression, u8 encryption, |
| 1611 | u16 other_encoding, int extent_type) |
| 1612 | { |
| 1613 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1614 | struct btrfs_file_extent_item *fi; |
| 1615 | struct btrfs_path *path; |
| 1616 | struct extent_buffer *leaf; |
| 1617 | struct btrfs_key ins; |
| 1618 | u64 hint; |
| 1619 | int ret; |
| 1620 | |
| 1621 | path = btrfs_alloc_path(); |
| 1622 | BUG_ON(!path); |
| 1623 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1624 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1625 | |
| 1626 | /* |
| 1627 | * we may be replacing one extent in the tree with another. |
| 1628 | * The new extent is pinned in the extent map, and we don't want |
| 1629 | * to drop it from the cache until it is completely in the btree. |
| 1630 | * |
| 1631 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1632 | * the caller is expected to unpin it and allow it to be merged |
| 1633 | * with the others. |
| 1634 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1635 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1636 | &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1637 | BUG_ON(ret); |
| 1638 | |
| 1639 | ins.objectid = inode->i_ino; |
| 1640 | ins.offset = file_pos; |
| 1641 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1642 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1643 | BUG_ON(ret); |
| 1644 | leaf = path->nodes[0]; |
| 1645 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1646 | struct btrfs_file_extent_item); |
| 1647 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1648 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1649 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1650 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1651 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1652 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1653 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1654 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1655 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1656 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1657 | |
| 1658 | btrfs_unlock_up_safe(path, 1); |
| 1659 | btrfs_set_lock_blocking(leaf); |
| 1660 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1661 | btrfs_mark_buffer_dirty(leaf); |
| 1662 | |
| 1663 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1664 | |
| 1665 | ins.objectid = disk_bytenr; |
| 1666 | ins.offset = disk_num_bytes; |
| 1667 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1668 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1669 | root->root_key.objectid, |
| 1670 | inode->i_ino, file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1671 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1672 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1673 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1674 | return 0; |
| 1675 | } |
| 1676 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1677 | /* |
| 1678 | * helper function for btrfs_finish_ordered_io, this |
| 1679 | * just reads in some of the csum leaves to prime them into ram |
| 1680 | * before we start the transaction. It limits the amount of btree |
| 1681 | * reads required while inside the transaction. |
| 1682 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1683 | /* as ordered data IO finishes, this gets called so we can finish |
| 1684 | * an ordered extent if the range of bytes in the file it covers are |
| 1685 | * fully written. |
| 1686 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1687 | 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] | 1688 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1689 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1690 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1691 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1692 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1693 | struct extent_state *cached_state = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1694 | int compress_type = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1695 | int ret; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1696 | bool nolock = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1697 | |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 1698 | ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 1699 | end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1700 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1701 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1702 | BUG_ON(!ordered_extent); |
Josef Bacik | efd049f | 2010-02-02 20:50:10 +0000 | [diff] [blame] | 1703 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1704 | nolock = (root == root->fs_info->tree_root); |
| 1705 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1706 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
| 1707 | BUG_ON(!list_empty(&ordered_extent->list)); |
| 1708 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1709 | if (!ret) { |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1710 | if (nolock) |
| 1711 | trans = btrfs_join_transaction_nolock(root, 1); |
| 1712 | else |
| 1713 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1714 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1715 | btrfs_set_trans_block_group(trans, inode); |
| 1716 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1717 | ret = btrfs_update_inode(trans, root, inode); |
| 1718 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1719 | } |
| 1720 | goto out; |
| 1721 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1722 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1723 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 1724 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1725 | 0, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1726 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1727 | if (nolock) |
| 1728 | trans = btrfs_join_transaction_nolock(root, 1); |
| 1729 | else |
| 1730 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1731 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1732 | btrfs_set_trans_block_group(trans, inode); |
| 1733 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1734 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1735 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1736 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1737 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1738 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1739 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1740 | ordered_extent->file_offset, |
| 1741 | ordered_extent->file_offset + |
| 1742 | ordered_extent->len); |
| 1743 | BUG_ON(ret); |
| 1744 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 1745 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1746 | ret = insert_reserved_file_extent(trans, inode, |
| 1747 | ordered_extent->file_offset, |
| 1748 | ordered_extent->start, |
| 1749 | ordered_extent->disk_len, |
| 1750 | ordered_extent->len, |
| 1751 | ordered_extent->len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1752 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1753 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1754 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1755 | ordered_extent->file_offset, |
| 1756 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1757 | BUG_ON(ret); |
| 1758 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1759 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 1760 | ordered_extent->file_offset + |
| 1761 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
| 1762 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1763 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1764 | &ordered_extent->list); |
| 1765 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1766 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1767 | ret = btrfs_update_inode(trans, root, inode); |
| 1768 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1769 | out: |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1770 | if (nolock) { |
| 1771 | if (trans) |
| 1772 | btrfs_end_transaction_nolock(trans, root); |
| 1773 | } else { |
| 1774 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
| 1775 | if (trans) |
| 1776 | btrfs_end_transaction(trans, root); |
| 1777 | } |
| 1778 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1779 | /* once for us */ |
| 1780 | btrfs_put_ordered_extent(ordered_extent); |
| 1781 | /* once for the tree */ |
| 1782 | btrfs_put_ordered_extent(ordered_extent); |
| 1783 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1784 | return 0; |
| 1785 | } |
| 1786 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1787 | 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] | 1788 | struct extent_state *state, int uptodate) |
| 1789 | { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1790 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1791 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1792 | } |
| 1793 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1794 | /* |
| 1795 | * When IO fails, either with EIO or csum verification fails, we |
| 1796 | * try other mirrors that might have a good copy of the data. This |
| 1797 | * io_failure_record is used to record state as we go through all the |
| 1798 | * mirrors. If another mirror has good data, the page is set up to date |
| 1799 | * and things continue. If a good mirror can't be found, the original |
| 1800 | * bio end_io callback is called to indicate things have failed. |
| 1801 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1802 | struct io_failure_record { |
| 1803 | struct page *page; |
| 1804 | u64 start; |
| 1805 | u64 len; |
| 1806 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1807 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1808 | int last_mirror; |
| 1809 | }; |
| 1810 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1811 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1812 | struct page *page, u64 start, u64 end, |
| 1813 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1814 | { |
| 1815 | struct io_failure_record *failrec = NULL; |
| 1816 | u64 private; |
| 1817 | struct extent_map *em; |
| 1818 | struct inode *inode = page->mapping->host; |
| 1819 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1820 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1821 | struct bio *bio; |
| 1822 | int num_copies; |
| 1823 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1824 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1825 | u64 logical; |
| 1826 | |
| 1827 | ret = get_state_private(failure_tree, start, &private); |
| 1828 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1829 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1830 | if (!failrec) |
| 1831 | return -ENOMEM; |
| 1832 | failrec->start = start; |
| 1833 | failrec->len = end - start + 1; |
| 1834 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1835 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1836 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1837 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1838 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1839 | if (em->start > start || em->start + em->len < start) { |
| 1840 | free_extent_map(em); |
| 1841 | em = NULL; |
| 1842 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1843 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1844 | |
| 1845 | if (!em || IS_ERR(em)) { |
| 1846 | kfree(failrec); |
| 1847 | return -EIO; |
| 1848 | } |
| 1849 | logical = start - em->start; |
| 1850 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1851 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1852 | logical = em->block_start; |
| 1853 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1854 | extent_set_compress_type(&failrec->bio_flags, |
| 1855 | em->compress_type); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1856 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1857 | failrec->logical = logical; |
| 1858 | free_extent_map(em); |
| 1859 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1860 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1861 | set_state_private(failure_tree, start, |
| 1862 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1863 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1864 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1865 | } |
| 1866 | num_copies = btrfs_num_copies( |
| 1867 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1868 | failrec->logical, failrec->len); |
| 1869 | failrec->last_mirror++; |
| 1870 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1871 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1872 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1873 | failrec->start, |
| 1874 | EXTENT_LOCKED); |
| 1875 | if (state && state->start != failrec->start) |
| 1876 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1877 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1878 | } |
| 1879 | if (!state || failrec->last_mirror > num_copies) { |
| 1880 | set_state_private(failure_tree, failrec->start, 0); |
| 1881 | clear_extent_bits(failure_tree, failrec->start, |
| 1882 | failrec->start + failrec->len - 1, |
| 1883 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1884 | kfree(failrec); |
| 1885 | return -EIO; |
| 1886 | } |
| 1887 | bio = bio_alloc(GFP_NOFS, 1); |
| 1888 | bio->bi_private = state; |
| 1889 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1890 | bio->bi_sector = failrec->logical >> 9; |
| 1891 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1892 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1893 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1894 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1895 | if (failed_bio->bi_rw & REQ_WRITE) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1896 | rw = WRITE; |
| 1897 | else |
| 1898 | rw = READ; |
| 1899 | |
| 1900 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1901 | failrec->last_mirror, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1902 | failrec->bio_flags, 0); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1903 | return 0; |
| 1904 | } |
| 1905 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1906 | /* |
| 1907 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1908 | * to see if we need to process or clean up an io_failure_record |
| 1909 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1910 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1911 | { |
| 1912 | u64 private; |
| 1913 | u64 private_failure; |
| 1914 | struct io_failure_record *failure; |
| 1915 | int ret; |
| 1916 | |
| 1917 | private = 0; |
| 1918 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1919 | (u64)-1, 1, EXTENT_DIRTY, 0)) { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1920 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1921 | start, &private_failure); |
| 1922 | if (ret == 0) { |
| 1923 | failure = (struct io_failure_record *)(unsigned long) |
| 1924 | private_failure; |
| 1925 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1926 | failure->start, 0); |
| 1927 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1928 | failure->start, |
| 1929 | failure->start + failure->len - 1, |
| 1930 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1931 | GFP_NOFS); |
| 1932 | kfree(failure); |
| 1933 | } |
| 1934 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1935 | return 0; |
| 1936 | } |
| 1937 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1938 | /* |
| 1939 | * when reads are done, we need to check csums to verify the data is correct |
| 1940 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1941 | * the io_failure_record routines to find good copies |
| 1942 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1943 | 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] | 1944 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1945 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1946 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1947 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1948 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1949 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1950 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1951 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1952 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1953 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1954 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1955 | if (PageChecked(page)) { |
| 1956 | ClearPageChecked(page); |
| 1957 | goto good; |
| 1958 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1959 | |
| 1960 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1961 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1962 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1963 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1964 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1965 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1966 | GFP_NOFS); |
| 1967 | return 0; |
| 1968 | } |
| 1969 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1970 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1971 | private = state->private; |
| 1972 | ret = 0; |
| 1973 | } else { |
| 1974 | ret = get_state_private(io_tree, start, &private); |
| 1975 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1976 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1977 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1978 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1979 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1980 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1981 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1982 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1983 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1984 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1985 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1986 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1987 | /* if the io failure tree for this inode is non-empty, |
| 1988 | * check to see if we've recovered from a failed IO |
| 1989 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1990 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1991 | return 0; |
| 1992 | |
| 1993 | zeroit: |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 1994 | if (printk_ratelimit()) { |
| 1995 | printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " |
| 1996 | "private %llu\n", page->mapping->host->i_ino, |
| 1997 | (unsigned long long)start, csum, |
| 1998 | (unsigned long long)private); |
| 1999 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2000 | memset(kaddr + offset, 1, end - start + 1); |
| 2001 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2002 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2003 | if (private == 0) |
| 2004 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2005 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2006 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2007 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2008 | struct delayed_iput { |
| 2009 | struct list_head list; |
| 2010 | struct inode *inode; |
| 2011 | }; |
| 2012 | |
| 2013 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2014 | { |
| 2015 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2016 | struct delayed_iput *delayed; |
| 2017 | |
| 2018 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2019 | return; |
| 2020 | |
| 2021 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2022 | delayed->inode = inode; |
| 2023 | |
| 2024 | spin_lock(&fs_info->delayed_iput_lock); |
| 2025 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2026 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2027 | } |
| 2028 | |
| 2029 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2030 | { |
| 2031 | LIST_HEAD(list); |
| 2032 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2033 | struct delayed_iput *delayed; |
| 2034 | int empty; |
| 2035 | |
| 2036 | spin_lock(&fs_info->delayed_iput_lock); |
| 2037 | empty = list_empty(&fs_info->delayed_iputs); |
| 2038 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2039 | if (empty) |
| 2040 | return; |
| 2041 | |
| 2042 | down_read(&root->fs_info->cleanup_work_sem); |
| 2043 | spin_lock(&fs_info->delayed_iput_lock); |
| 2044 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2045 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2046 | |
| 2047 | while (!list_empty(&list)) { |
| 2048 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2049 | list_del(&delayed->list); |
| 2050 | iput(delayed->inode); |
| 2051 | kfree(delayed); |
| 2052 | } |
| 2053 | up_read(&root->fs_info->cleanup_work_sem); |
| 2054 | } |
| 2055 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2056 | /* |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2057 | * calculate extra metadata reservation when snapshotting a subvolume |
| 2058 | * contains orphan files. |
| 2059 | */ |
| 2060 | void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans, |
| 2061 | struct btrfs_pending_snapshot *pending, |
| 2062 | u64 *bytes_to_reserve) |
| 2063 | { |
| 2064 | struct btrfs_root *root; |
| 2065 | struct btrfs_block_rsv *block_rsv; |
| 2066 | u64 num_bytes; |
| 2067 | int index; |
| 2068 | |
| 2069 | root = pending->root; |
| 2070 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2071 | return; |
| 2072 | |
| 2073 | block_rsv = root->orphan_block_rsv; |
| 2074 | |
| 2075 | /* orphan block reservation for the snapshot */ |
| 2076 | num_bytes = block_rsv->size; |
| 2077 | |
| 2078 | /* |
| 2079 | * after the snapshot is created, COWing tree blocks may use more |
| 2080 | * space than it frees. So we should make sure there is enough |
| 2081 | * reserved space. |
| 2082 | */ |
| 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 | } |
| 2088 | |
| 2089 | *bytes_to_reserve += num_bytes; |
| 2090 | } |
| 2091 | |
| 2092 | void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans, |
| 2093 | struct btrfs_pending_snapshot *pending) |
| 2094 | { |
| 2095 | struct btrfs_root *root = pending->root; |
| 2096 | struct btrfs_root *snap = pending->snap; |
| 2097 | struct btrfs_block_rsv *block_rsv; |
| 2098 | u64 num_bytes; |
| 2099 | int index; |
| 2100 | int ret; |
| 2101 | |
| 2102 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2103 | return; |
| 2104 | |
| 2105 | /* refill source subvolume's orphan block reservation */ |
| 2106 | block_rsv = root->orphan_block_rsv; |
| 2107 | index = trans->transid & 0x1; |
| 2108 | if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) { |
| 2109 | num_bytes = block_rsv->size - |
| 2110 | (block_rsv->reserved + block_rsv->freed[index]); |
| 2111 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2112 | root->orphan_block_rsv, |
| 2113 | num_bytes); |
| 2114 | BUG_ON(ret); |
| 2115 | } |
| 2116 | |
| 2117 | /* setup orphan block reservation for the snapshot */ |
| 2118 | block_rsv = btrfs_alloc_block_rsv(snap); |
| 2119 | BUG_ON(!block_rsv); |
| 2120 | |
| 2121 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2122 | snap->orphan_block_rsv = block_rsv; |
| 2123 | |
| 2124 | num_bytes = root->orphan_block_rsv->size; |
| 2125 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2126 | block_rsv, num_bytes); |
| 2127 | BUG_ON(ret); |
| 2128 | |
| 2129 | #if 0 |
| 2130 | /* insert orphan item for the snapshot */ |
| 2131 | WARN_ON(!root->orphan_item_inserted); |
| 2132 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2133 | snap->root_key.objectid); |
| 2134 | BUG_ON(ret); |
| 2135 | snap->orphan_item_inserted = 1; |
| 2136 | #endif |
| 2137 | } |
| 2138 | |
| 2139 | enum btrfs_orphan_cleanup_state { |
| 2140 | ORPHAN_CLEANUP_STARTED = 1, |
| 2141 | ORPHAN_CLEANUP_DONE = 2, |
| 2142 | }; |
| 2143 | |
| 2144 | /* |
| 2145 | * This is called in transaction commmit time. If there are no orphan |
| 2146 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2147 | * structure. |
| 2148 | */ |
| 2149 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2150 | struct btrfs_root *root) |
| 2151 | { |
| 2152 | int ret; |
| 2153 | |
| 2154 | if (!list_empty(&root->orphan_list) || |
| 2155 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2156 | return; |
| 2157 | |
| 2158 | if (root->orphan_item_inserted && |
| 2159 | btrfs_root_refs(&root->root_item) > 0) { |
| 2160 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2161 | root->root_key.objectid); |
| 2162 | BUG_ON(ret); |
| 2163 | root->orphan_item_inserted = 0; |
| 2164 | } |
| 2165 | |
| 2166 | if (root->orphan_block_rsv) { |
| 2167 | WARN_ON(root->orphan_block_rsv->size > 0); |
| 2168 | btrfs_free_block_rsv(root, root->orphan_block_rsv); |
| 2169 | root->orphan_block_rsv = NULL; |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2174 | * This creates an orphan entry for the given inode in case something goes |
| 2175 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2176 | * |
| 2177 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2178 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2179 | */ |
| 2180 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2181 | { |
| 2182 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2183 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2184 | int reserve = 0; |
| 2185 | int insert = 0; |
| 2186 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2187 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2188 | if (!root->orphan_block_rsv) { |
| 2189 | block_rsv = btrfs_alloc_block_rsv(root); |
| 2190 | BUG_ON(!block_rsv); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2191 | } |
| 2192 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2193 | spin_lock(&root->orphan_lock); |
| 2194 | if (!root->orphan_block_rsv) { |
| 2195 | root->orphan_block_rsv = block_rsv; |
| 2196 | } else if (block_rsv) { |
| 2197 | btrfs_free_block_rsv(root, block_rsv); |
| 2198 | block_rsv = NULL; |
| 2199 | } |
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 (list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2202 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2203 | #if 0 |
| 2204 | /* |
| 2205 | * For proper ENOSPC handling, we should do orphan |
| 2206 | * cleanup when mounting. But this introduces backward |
| 2207 | * compatibility issue. |
| 2208 | */ |
| 2209 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2210 | insert = 2; |
| 2211 | else |
| 2212 | insert = 1; |
| 2213 | #endif |
| 2214 | insert = 1; |
| 2215 | } else { |
| 2216 | WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved); |
| 2217 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2218 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2219 | if (!BTRFS_I(inode)->orphan_meta_reserved) { |
| 2220 | BTRFS_I(inode)->orphan_meta_reserved = 1; |
| 2221 | reserve = 1; |
| 2222 | } |
| 2223 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2224 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2225 | if (block_rsv) |
| 2226 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2227 | |
| 2228 | /* grab metadata reservation from transaction handle */ |
| 2229 | if (reserve) { |
| 2230 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
| 2231 | BUG_ON(ret); |
| 2232 | } |
| 2233 | |
| 2234 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2235 | if (insert >= 1) { |
| 2236 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 2237 | BUG_ON(ret); |
| 2238 | } |
| 2239 | |
| 2240 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2241 | if (insert >= 2) { |
| 2242 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2243 | root->root_key.objectid); |
| 2244 | BUG_ON(ret); |
| 2245 | } |
| 2246 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | /* |
| 2250 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2251 | * item for this particular inode. |
| 2252 | */ |
| 2253 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2254 | { |
| 2255 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2256 | int delete_item = 0; |
| 2257 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2258 | int ret = 0; |
| 2259 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2260 | spin_lock(&root->orphan_lock); |
| 2261 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2262 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2263 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2264 | } |
| 2265 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2266 | if (BTRFS_I(inode)->orphan_meta_reserved) { |
| 2267 | BTRFS_I(inode)->orphan_meta_reserved = 0; |
| 2268 | release_rsv = 1; |
| 2269 | } |
| 2270 | spin_unlock(&root->orphan_lock); |
| 2271 | |
| 2272 | if (trans && delete_item) { |
| 2273 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 2274 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2275 | } |
| 2276 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2277 | if (release_rsv) |
| 2278 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2279 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2280 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | /* |
| 2284 | * this cleans up any orphans that may be left on the list from the last use |
| 2285 | * of this root. |
| 2286 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 2287 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2288 | { |
| 2289 | struct btrfs_path *path; |
| 2290 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2291 | struct btrfs_key key, found_key; |
| 2292 | struct btrfs_trans_handle *trans; |
| 2293 | struct inode *inode; |
| 2294 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2295 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2296 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 2297 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2298 | |
| 2299 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 2300 | if (!path) { |
| 2301 | ret = -ENOMEM; |
| 2302 | goto out; |
| 2303 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2304 | path->reada = -1; |
| 2305 | |
| 2306 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2307 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2308 | key.offset = (u64)-1; |
| 2309 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2310 | while (1) { |
| 2311 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 2312 | if (ret < 0) |
| 2313 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2314 | |
| 2315 | /* |
| 2316 | * if ret == 0 means we found what we were searching for, which |
| 2317 | * is weird, but possible, so only screw with path if we didnt |
| 2318 | * find the key and see if we have stuff that matches |
| 2319 | */ |
| 2320 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 2321 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2322 | if (path->slots[0] == 0) |
| 2323 | break; |
| 2324 | path->slots[0]--; |
| 2325 | } |
| 2326 | |
| 2327 | /* pull out the item */ |
| 2328 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2329 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2330 | |
| 2331 | /* make sure the item matches what we want */ |
| 2332 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2333 | break; |
| 2334 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2335 | break; |
| 2336 | |
| 2337 | /* release the path since we're done with it */ |
| 2338 | btrfs_release_path(root, path); |
| 2339 | |
| 2340 | /* |
| 2341 | * this is where we are basically btrfs_lookup, without the |
| 2342 | * crossing root thing. we store the inode number in the |
| 2343 | * offset of the orphan item. |
| 2344 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2345 | found_key.objectid = found_key.offset; |
| 2346 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2347 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2348 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 2349 | if (IS_ERR(inode)) { |
| 2350 | ret = PTR_ERR(inode); |
| 2351 | goto out; |
| 2352 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2353 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2354 | /* |
| 2355 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2356 | * the proper thing when we hit it |
| 2357 | */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2358 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2359 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2360 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2361 | |
| 2362 | /* |
| 2363 | * if this is a bad inode, means we actually succeeded in |
| 2364 | * removing the inode, but not the orphan record, which means |
| 2365 | * we need to manually delete the orphan since iput will just |
| 2366 | * do a destroy_inode |
| 2367 | */ |
| 2368 | if (is_bad_inode(inode)) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2369 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 2370 | if (IS_ERR(trans)) { |
| 2371 | ret = PTR_ERR(trans); |
| 2372 | goto out; |
| 2373 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2374 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2375 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2376 | iput(inode); |
| 2377 | continue; |
| 2378 | } |
| 2379 | |
| 2380 | /* if we have links, this was a truncate, lets do that */ |
| 2381 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 2382 | if (!S_ISREG(inode->i_mode)) { |
| 2383 | WARN_ON(1); |
| 2384 | iput(inode); |
| 2385 | continue; |
| 2386 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2387 | nr_truncate++; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 2388 | ret = btrfs_truncate(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2389 | } else { |
| 2390 | nr_unlink++; |
| 2391 | } |
| 2392 | |
| 2393 | /* this will do delete_inode and everything for us */ |
| 2394 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 2395 | if (ret) |
| 2396 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2397 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2398 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 2399 | |
| 2400 | if (root->orphan_block_rsv) |
| 2401 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 2402 | (u64)-1); |
| 2403 | |
| 2404 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
| 2405 | trans = btrfs_join_transaction(root, 1); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 2406 | if (!IS_ERR(trans)) |
| 2407 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2408 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2409 | |
| 2410 | if (nr_unlink) |
| 2411 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2412 | if (nr_truncate) |
| 2413 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 2414 | |
| 2415 | out: |
| 2416 | if (ret) |
| 2417 | printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret); |
| 2418 | btrfs_free_path(path); |
| 2419 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2420 | } |
| 2421 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2422 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2423 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2424 | * don't find any xattrs, we know there can't be any acls. |
| 2425 | * |
| 2426 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2427 | */ |
| 2428 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2429 | int slot, u64 objectid) |
| 2430 | { |
| 2431 | u32 nritems = btrfs_header_nritems(leaf); |
| 2432 | struct btrfs_key found_key; |
| 2433 | int scanned = 0; |
| 2434 | |
| 2435 | slot++; |
| 2436 | while (slot < nritems) { |
| 2437 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2438 | |
| 2439 | /* we found a different objectid, there must not be acls */ |
| 2440 | if (found_key.objectid != objectid) |
| 2441 | return 0; |
| 2442 | |
| 2443 | /* we found an xattr, assume we've got an acl */ |
| 2444 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2445 | return 1; |
| 2446 | |
| 2447 | /* |
| 2448 | * we found a key greater than an xattr key, there can't |
| 2449 | * be any acls later on |
| 2450 | */ |
| 2451 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2452 | return 0; |
| 2453 | |
| 2454 | slot++; |
| 2455 | scanned++; |
| 2456 | |
| 2457 | /* |
| 2458 | * it goes inode, inode backrefs, xattrs, extents, |
| 2459 | * so if there are a ton of hard links to an inode there can |
| 2460 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2461 | * this is just an optimization |
| 2462 | */ |
| 2463 | if (scanned >= 8) |
| 2464 | break; |
| 2465 | } |
| 2466 | /* we hit the end of the leaf before we found an xattr or |
| 2467 | * something larger than an xattr. We have to assume the inode |
| 2468 | * has acls |
| 2469 | */ |
| 2470 | return 1; |
| 2471 | } |
| 2472 | |
| 2473 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2474 | * read an inode from the btree into the in-memory inode |
| 2475 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2476 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2477 | { |
| 2478 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2479 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2480 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2481 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2482 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2483 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2484 | int maybe_acls; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2485 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2486 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2487 | int ret; |
| 2488 | |
| 2489 | path = btrfs_alloc_path(); |
| 2490 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2491 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2492 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2493 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2494 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2495 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2496 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2497 | leaf = path->nodes[0]; |
| 2498 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2499 | struct btrfs_inode_item); |
| 2500 | |
| 2501 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2502 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2503 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2504 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2505 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2506 | |
| 2507 | tspec = btrfs_inode_atime(inode_item); |
| 2508 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2509 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2510 | |
| 2511 | tspec = btrfs_inode_mtime(inode_item); |
| 2512 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2513 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2514 | |
| 2515 | tspec = btrfs_inode_ctime(inode_item); |
| 2516 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2517 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2518 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2519 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2520 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2521 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2522 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2523 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2524 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2525 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2526 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2527 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2528 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2529 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2530 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2531 | /* |
| 2532 | * try to precache a NULL acl entry for files that don't have |
| 2533 | * any xattrs or acls |
| 2534 | */ |
| 2535 | 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] | 2536 | if (!maybe_acls) |
| 2537 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2538 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2539 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2540 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2541 | btrfs_free_path(path); |
| 2542 | inode_item = NULL; |
| 2543 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2544 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2545 | case S_IFREG: |
| 2546 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2547 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2548 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2549 | inode->i_fop = &btrfs_file_operations; |
| 2550 | inode->i_op = &btrfs_file_inode_operations; |
| 2551 | break; |
| 2552 | case S_IFDIR: |
| 2553 | inode->i_fop = &btrfs_dir_file_operations; |
| 2554 | if (root == root->fs_info->tree_root) |
| 2555 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2556 | else |
| 2557 | inode->i_op = &btrfs_dir_inode_operations; |
| 2558 | break; |
| 2559 | case S_IFLNK: |
| 2560 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2561 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2562 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2563 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2564 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2565 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2566 | init_special_inode(inode, inode->i_mode, rdev); |
| 2567 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2568 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2569 | |
| 2570 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2571 | return; |
| 2572 | |
| 2573 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2574 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2575 | make_bad_inode(inode); |
| 2576 | } |
| 2577 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2578 | /* |
| 2579 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2580 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2581 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2582 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2583 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2584 | struct inode *inode) |
| 2585 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2586 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2587 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2588 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2589 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2590 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2591 | |
| 2592 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2593 | inode->i_atime.tv_sec); |
| 2594 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2595 | inode->i_atime.tv_nsec); |
| 2596 | |
| 2597 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2598 | inode->i_mtime.tv_sec); |
| 2599 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2600 | inode->i_mtime.tv_nsec); |
| 2601 | |
| 2602 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2603 | inode->i_ctime.tv_sec); |
| 2604 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2605 | inode->i_ctime.tv_nsec); |
| 2606 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2607 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2608 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2609 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2610 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2611 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2612 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2613 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2614 | } |
| 2615 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2616 | /* |
| 2617 | * copy everything in the in-memory inode into the btree. |
| 2618 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2619 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2620 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2621 | { |
| 2622 | struct btrfs_inode_item *inode_item; |
| 2623 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2624 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2625 | int ret; |
| 2626 | |
| 2627 | path = btrfs_alloc_path(); |
| 2628 | BUG_ON(!path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2629 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2630 | ret = btrfs_lookup_inode(trans, root, path, |
| 2631 | &BTRFS_I(inode)->location, 1); |
| 2632 | if (ret) { |
| 2633 | if (ret > 0) |
| 2634 | ret = -ENOENT; |
| 2635 | goto failed; |
| 2636 | } |
| 2637 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2638 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2639 | leaf = path->nodes[0]; |
| 2640 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2641 | struct btrfs_inode_item); |
| 2642 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2643 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2644 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2645 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2646 | ret = 0; |
| 2647 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2648 | btrfs_free_path(path); |
| 2649 | return ret; |
| 2650 | } |
| 2651 | |
| 2652 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2653 | /* |
| 2654 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2655 | * recovery code. It remove a link in a directory with a given name, and |
| 2656 | * also drops the back refs in the inode to the directory |
| 2657 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2658 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2659 | struct btrfs_root *root, |
| 2660 | struct inode *dir, struct inode *inode, |
| 2661 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2662 | { |
| 2663 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2664 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2665 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2666 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2667 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2668 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2669 | |
| 2670 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2671 | if (!path) { |
| 2672 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 2673 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2674 | } |
| 2675 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2676 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2677 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2678 | name, name_len, -1); |
| 2679 | if (IS_ERR(di)) { |
| 2680 | ret = PTR_ERR(di); |
| 2681 | goto err; |
| 2682 | } |
| 2683 | if (!di) { |
| 2684 | ret = -ENOENT; |
| 2685 | goto err; |
| 2686 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2687 | leaf = path->nodes[0]; |
| 2688 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2689 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2690 | if (ret) |
| 2691 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2692 | btrfs_release_path(root, path); |
| 2693 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2694 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2695 | inode->i_ino, |
| 2696 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2697 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2698 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2699 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2700 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2701 | goto err; |
| 2702 | } |
| 2703 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2704 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2705 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2706 | if (IS_ERR(di)) { |
| 2707 | ret = PTR_ERR(di); |
| 2708 | goto err; |
| 2709 | } |
| 2710 | if (!di) { |
| 2711 | ret = -ENOENT; |
| 2712 | goto err; |
| 2713 | } |
| 2714 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2715 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2716 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2717 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 2718 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2719 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2720 | |
| 2721 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2722 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 2723 | if (ret == -ENOENT) |
| 2724 | ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2725 | err: |
| 2726 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2727 | if (ret) |
| 2728 | goto out; |
| 2729 | |
| 2730 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2731 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2732 | btrfs_update_inode(trans, root, dir); |
| 2733 | btrfs_drop_nlink(inode); |
| 2734 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2735 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2736 | return ret; |
| 2737 | } |
| 2738 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2739 | /* helper to check if there is any shared block in the path */ |
| 2740 | static int check_path_shared(struct btrfs_root *root, |
| 2741 | struct btrfs_path *path) |
| 2742 | { |
| 2743 | struct extent_buffer *eb; |
| 2744 | int level; |
Dan Carpenter | 0e4dcbe | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 2745 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2746 | |
| 2747 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2748 | int ret; |
| 2749 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2750 | if (!path->nodes[level]) |
| 2751 | break; |
| 2752 | eb = path->nodes[level]; |
| 2753 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2754 | continue; |
| 2755 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2756 | &refs, NULL); |
| 2757 | if (refs > 1) |
| 2758 | return 1; |
| 2759 | } |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2760 | return 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | /* |
| 2764 | * helper to start transaction for unlink and rmdir. |
| 2765 | * |
| 2766 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2767 | * so in enospc case, we should make sure they will free space before |
| 2768 | * allowing them to use the global metadata reservation. |
| 2769 | */ |
| 2770 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2771 | struct dentry *dentry) |
| 2772 | { |
| 2773 | struct btrfs_trans_handle *trans; |
| 2774 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2775 | struct btrfs_path *path; |
| 2776 | struct btrfs_inode_ref *ref; |
| 2777 | struct btrfs_dir_item *di; |
| 2778 | struct inode *inode = dentry->d_inode; |
| 2779 | u64 index; |
| 2780 | int check_link = 1; |
| 2781 | int err = -ENOSPC; |
| 2782 | int ret; |
| 2783 | |
| 2784 | trans = btrfs_start_transaction(root, 10); |
| 2785 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 2786 | return trans; |
| 2787 | |
| 2788 | if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 2789 | return ERR_PTR(-ENOSPC); |
| 2790 | |
| 2791 | /* check if there is someone else holds reference */ |
| 2792 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 2793 | return ERR_PTR(-ENOSPC); |
| 2794 | |
| 2795 | if (atomic_read(&inode->i_count) > 2) |
| 2796 | return ERR_PTR(-ENOSPC); |
| 2797 | |
| 2798 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 2799 | return ERR_PTR(-ENOSPC); |
| 2800 | |
| 2801 | path = btrfs_alloc_path(); |
| 2802 | if (!path) { |
| 2803 | root->fs_info->enospc_unlink = 0; |
| 2804 | return ERR_PTR(-ENOMEM); |
| 2805 | } |
| 2806 | |
| 2807 | trans = btrfs_start_transaction(root, 0); |
| 2808 | if (IS_ERR(trans)) { |
| 2809 | btrfs_free_path(path); |
| 2810 | root->fs_info->enospc_unlink = 0; |
| 2811 | return trans; |
| 2812 | } |
| 2813 | |
| 2814 | path->skip_locking = 1; |
| 2815 | path->search_commit_root = 1; |
| 2816 | |
| 2817 | ret = btrfs_lookup_inode(trans, root, path, |
| 2818 | &BTRFS_I(dir)->location, 0); |
| 2819 | if (ret < 0) { |
| 2820 | err = ret; |
| 2821 | goto out; |
| 2822 | } |
| 2823 | if (ret == 0) { |
| 2824 | if (check_path_shared(root, path)) |
| 2825 | goto out; |
| 2826 | } else { |
| 2827 | check_link = 0; |
| 2828 | } |
| 2829 | btrfs_release_path(root, path); |
| 2830 | |
| 2831 | ret = btrfs_lookup_inode(trans, root, path, |
| 2832 | &BTRFS_I(inode)->location, 0); |
| 2833 | if (ret < 0) { |
| 2834 | err = ret; |
| 2835 | goto out; |
| 2836 | } |
| 2837 | if (ret == 0) { |
| 2838 | if (check_path_shared(root, path)) |
| 2839 | goto out; |
| 2840 | } else { |
| 2841 | check_link = 0; |
| 2842 | } |
| 2843 | btrfs_release_path(root, path); |
| 2844 | |
| 2845 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 2846 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 2847 | inode->i_ino, (u64)-1, 0); |
| 2848 | if (ret < 0) { |
| 2849 | err = ret; |
| 2850 | goto out; |
| 2851 | } |
| 2852 | BUG_ON(ret == 0); |
| 2853 | if (check_path_shared(root, path)) |
| 2854 | goto out; |
| 2855 | btrfs_release_path(root, path); |
| 2856 | } |
| 2857 | |
| 2858 | if (!check_link) { |
| 2859 | err = 0; |
| 2860 | goto out; |
| 2861 | } |
| 2862 | |
| 2863 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2864 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2865 | if (IS_ERR(di)) { |
| 2866 | err = PTR_ERR(di); |
| 2867 | goto out; |
| 2868 | } |
| 2869 | if (di) { |
| 2870 | if (check_path_shared(root, path)) |
| 2871 | goto out; |
| 2872 | } else { |
| 2873 | err = 0; |
| 2874 | goto out; |
| 2875 | } |
| 2876 | btrfs_release_path(root, path); |
| 2877 | |
| 2878 | ref = btrfs_lookup_inode_ref(trans, root, path, |
| 2879 | dentry->d_name.name, dentry->d_name.len, |
| 2880 | inode->i_ino, dir->i_ino, 0); |
| 2881 | if (IS_ERR(ref)) { |
| 2882 | err = PTR_ERR(ref); |
| 2883 | goto out; |
| 2884 | } |
| 2885 | BUG_ON(!ref); |
| 2886 | if (check_path_shared(root, path)) |
| 2887 | goto out; |
| 2888 | index = btrfs_inode_ref_index(path->nodes[0], ref); |
| 2889 | btrfs_release_path(root, path); |
| 2890 | |
| 2891 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index, |
| 2892 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2893 | if (IS_ERR(di)) { |
| 2894 | err = PTR_ERR(di); |
| 2895 | goto out; |
| 2896 | } |
| 2897 | BUG_ON(ret == -ENOENT); |
| 2898 | if (check_path_shared(root, path)) |
| 2899 | goto out; |
| 2900 | |
| 2901 | err = 0; |
| 2902 | out: |
| 2903 | btrfs_free_path(path); |
| 2904 | if (err) { |
| 2905 | btrfs_end_transaction(trans, root); |
| 2906 | root->fs_info->enospc_unlink = 0; |
| 2907 | return ERR_PTR(err); |
| 2908 | } |
| 2909 | |
| 2910 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 2911 | return trans; |
| 2912 | } |
| 2913 | |
| 2914 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 2915 | struct btrfs_root *root) |
| 2916 | { |
| 2917 | if (trans->block_rsv == &root->fs_info->global_block_rsv) { |
| 2918 | BUG_ON(!root->fs_info->enospc_unlink); |
| 2919 | root->fs_info->enospc_unlink = 0; |
| 2920 | } |
| 2921 | btrfs_end_transaction_throttle(trans, root); |
| 2922 | } |
| 2923 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2924 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2925 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2926 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2927 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2928 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2929 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2930 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2931 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2932 | trans = __unlink_start_trans(dir, dentry); |
| 2933 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2934 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2935 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2936 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2937 | |
| 2938 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2939 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2940 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2941 | dentry->d_name.name, dentry->d_name.len); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2942 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2943 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2944 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2945 | ret = btrfs_orphan_add(trans, inode); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2946 | BUG_ON(ret); |
| 2947 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2948 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2949 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2950 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2951 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2952 | return ret; |
| 2953 | } |
| 2954 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2955 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2956 | struct btrfs_root *root, |
| 2957 | struct inode *dir, u64 objectid, |
| 2958 | const char *name, int name_len) |
| 2959 | { |
| 2960 | struct btrfs_path *path; |
| 2961 | struct extent_buffer *leaf; |
| 2962 | struct btrfs_dir_item *di; |
| 2963 | struct btrfs_key key; |
| 2964 | u64 index; |
| 2965 | int ret; |
| 2966 | |
| 2967 | path = btrfs_alloc_path(); |
| 2968 | if (!path) |
| 2969 | return -ENOMEM; |
| 2970 | |
| 2971 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2972 | name, name_len, -1); |
| 2973 | BUG_ON(!di || IS_ERR(di)); |
| 2974 | |
| 2975 | leaf = path->nodes[0]; |
| 2976 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2977 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2978 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2979 | BUG_ON(ret); |
| 2980 | btrfs_release_path(root, path); |
| 2981 | |
| 2982 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2983 | objectid, root->root_key.objectid, |
| 2984 | dir->i_ino, &index, name, name_len); |
| 2985 | if (ret < 0) { |
| 2986 | BUG_ON(ret != -ENOENT); |
| 2987 | di = btrfs_search_dir_index_item(root, path, dir->i_ino, |
| 2988 | name, name_len); |
| 2989 | BUG_ON(!di || IS_ERR(di)); |
| 2990 | |
| 2991 | leaf = path->nodes[0]; |
| 2992 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2993 | btrfs_release_path(root, path); |
| 2994 | index = key.offset; |
| 2995 | } |
| 2996 | |
| 2997 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 2998 | index, name, name_len, -1); |
| 2999 | BUG_ON(!di || IS_ERR(di)); |
| 3000 | |
| 3001 | leaf = path->nodes[0]; |
| 3002 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3003 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3004 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 3005 | BUG_ON(ret); |
| 3006 | btrfs_release_path(root, path); |
| 3007 | |
| 3008 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 3009 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 3010 | ret = btrfs_update_inode(trans, root, dir); |
| 3011 | BUG_ON(ret); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3012 | |
| 3013 | btrfs_free_path(path); |
| 3014 | return 0; |
| 3015 | } |
| 3016 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3017 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3018 | { |
| 3019 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3020 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3021 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3022 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3023 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3024 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 3025 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3026 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3027 | return -ENOTEMPTY; |
| 3028 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3029 | trans = __unlink_start_trans(dir, dentry); |
| 3030 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3031 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3032 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3033 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3034 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3035 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 3036 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3037 | BTRFS_I(inode)->location.objectid, |
| 3038 | dentry->d_name.name, |
| 3039 | dentry->d_name.len); |
| 3040 | goto out; |
| 3041 | } |
| 3042 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3043 | err = btrfs_orphan_add(trans, inode); |
| 3044 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3045 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3046 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3047 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3048 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3049 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3050 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3051 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3052 | out: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3053 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3054 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3055 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3056 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3057 | return err; |
| 3058 | } |
| 3059 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3060 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3061 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3062 | * when truncating bytes in a file, it is possible to avoid reading |
| 3063 | * the leaves that contain only checksum items. This can be the |
| 3064 | * majority of the IO required to delete a large file, but it must |
| 3065 | * be done carefully. |
| 3066 | * |
| 3067 | * The keys in the level just above the leaves are checked to make sure |
| 3068 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 3069 | * after the new size. |
| 3070 | * |
| 3071 | * Then the key for the next leaf is checked to make sure it also has |
| 3072 | * a checksum item for the same file. If it does, we know our target leaf |
| 3073 | * contains only checksum items, and it can be safely freed without reading |
| 3074 | * it. |
| 3075 | * |
| 3076 | * This is just an optimization targeted at large files. It may do |
| 3077 | * nothing. It will return 0 unless things went badly. |
| 3078 | */ |
| 3079 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 3080 | struct btrfs_root *root, |
| 3081 | struct btrfs_path *path, |
| 3082 | struct inode *inode, u64 new_size) |
| 3083 | { |
| 3084 | struct btrfs_key key; |
| 3085 | int ret; |
| 3086 | int nritems; |
| 3087 | struct btrfs_key found_key; |
| 3088 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3089 | struct btrfs_leaf_ref *ref; |
| 3090 | u64 leaf_gen; |
| 3091 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3092 | |
| 3093 | path->lowest_level = 1; |
| 3094 | key.objectid = inode->i_ino; |
| 3095 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 3096 | key.offset = new_size; |
| 3097 | again: |
| 3098 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3099 | if (ret < 0) |
| 3100 | goto out; |
| 3101 | |
| 3102 | if (path->nodes[1] == NULL) { |
| 3103 | ret = 0; |
| 3104 | goto out; |
| 3105 | } |
| 3106 | ret = 0; |
| 3107 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 3108 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 3109 | |
| 3110 | if (!nritems) |
| 3111 | goto out; |
| 3112 | |
| 3113 | if (path->slots[1] >= nritems) |
| 3114 | goto next_node; |
| 3115 | |
| 3116 | /* did we find a key greater than anything we want to delete? */ |
| 3117 | if (found_key.objectid > inode->i_ino || |
| 3118 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 3119 | goto out; |
| 3120 | |
| 3121 | /* we check the next key in the node to make sure the leave contains |
| 3122 | * only checksum items. This comparison doesn't work if our |
| 3123 | * leaf is the last one in the node |
| 3124 | */ |
| 3125 | if (path->slots[1] + 1 >= nritems) { |
| 3126 | next_node: |
| 3127 | /* search forward from the last key in the node, this |
| 3128 | * will bring us into the next node in the tree |
| 3129 | */ |
| 3130 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 3131 | |
| 3132 | /* unlikely, but we inc below, so check to be safe */ |
| 3133 | if (found_key.offset == (u64)-1) |
| 3134 | goto out; |
| 3135 | |
| 3136 | /* search_forward needs a path with locks held, do the |
| 3137 | * search again for the original key. It is possible |
| 3138 | * this will race with a balance and return a path that |
| 3139 | * we could modify, but this drop is just an optimization |
| 3140 | * and is allowed to miss some leaves. |
| 3141 | */ |
| 3142 | btrfs_release_path(root, path); |
| 3143 | found_key.offset++; |
| 3144 | |
| 3145 | /* setup a max key for search_forward */ |
| 3146 | other_key.offset = (u64)-1; |
| 3147 | other_key.type = key.type; |
| 3148 | other_key.objectid = key.objectid; |
| 3149 | |
| 3150 | path->keep_locks = 1; |
| 3151 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 3152 | path, 0, 0); |
| 3153 | path->keep_locks = 0; |
| 3154 | if (ret || found_key.objectid != key.objectid || |
| 3155 | found_key.type != key.type) { |
| 3156 | ret = 0; |
| 3157 | goto out; |
| 3158 | } |
| 3159 | |
| 3160 | key.offset = found_key.offset; |
| 3161 | btrfs_release_path(root, path); |
| 3162 | cond_resched(); |
| 3163 | goto again; |
| 3164 | } |
| 3165 | |
| 3166 | /* we know there's one more slot after us in the tree, |
| 3167 | * read that key so we can verify it is also a checksum item |
| 3168 | */ |
| 3169 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 3170 | |
| 3171 | if (found_key.objectid < inode->i_ino) |
| 3172 | goto next_key; |
| 3173 | |
| 3174 | if (found_key.type != key.type || found_key.offset < new_size) |
| 3175 | goto next_key; |
| 3176 | |
| 3177 | /* |
| 3178 | * if the key for the next leaf isn't a csum key from this objectid, |
| 3179 | * we can't be sure there aren't good items inside this leaf. |
| 3180 | * Bail out |
| 3181 | */ |
| 3182 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 3183 | goto out; |
| 3184 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3185 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 3186 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3187 | /* |
| 3188 | * it is safe to delete this leaf, it contains only |
| 3189 | * csum items from this inode at an offset >= new_size |
| 3190 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3191 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3192 | BUG_ON(ret); |
| 3193 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3194 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 3195 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 3196 | if (ref) { |
| 3197 | ref->root_gen = root->root_key.offset; |
| 3198 | ref->bytenr = leaf_start; |
| 3199 | ref->owner = 0; |
| 3200 | ref->generation = leaf_gen; |
| 3201 | ref->nritems = 0; |
| 3202 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3203 | btrfs_sort_leaf_ref(ref); |
| 3204 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3205 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 3206 | WARN_ON(ret); |
| 3207 | btrfs_free_leaf_ref(root, ref); |
| 3208 | } else { |
| 3209 | WARN_ON(1); |
| 3210 | } |
| 3211 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3212 | next_key: |
| 3213 | btrfs_release_path(root, path); |
| 3214 | |
| 3215 | if (other_key.objectid == inode->i_ino && |
| 3216 | other_key.type == key.type && other_key.offset > key.offset) { |
| 3217 | key.offset = other_key.offset; |
| 3218 | cond_resched(); |
| 3219 | goto again; |
| 3220 | } |
| 3221 | ret = 0; |
| 3222 | out: |
| 3223 | /* fixup any changes we've made to the path */ |
| 3224 | path->lowest_level = 0; |
| 3225 | path->keep_locks = 0; |
| 3226 | btrfs_release_path(root, path); |
| 3227 | return ret; |
| 3228 | } |
| 3229 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3230 | #endif |
| 3231 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3232 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3233 | * this can truncate away extent items, csum items and directory items. |
| 3234 | * 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] | 3235 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3236 | * |
| 3237 | * csum items that cross the new i_size are truncated to the new size |
| 3238 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3239 | * |
| 3240 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3241 | * 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] | 3242 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3243 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3244 | struct btrfs_root *root, |
| 3245 | struct inode *inode, |
| 3246 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3247 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3248 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3249 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3250 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3251 | struct btrfs_key key; |
| 3252 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3253 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3254 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3255 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3256 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3257 | u64 mask = root->sectorsize - 1; |
| 3258 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3259 | int found_extent; |
| 3260 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3261 | int pending_del_nr = 0; |
| 3262 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3263 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3264 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3265 | int ret; |
| 3266 | int err = 0; |
| 3267 | |
| 3268 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3269 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3270 | if (root->ref_cows || root == root->fs_info->tree_root) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3271 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3272 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3273 | path = btrfs_alloc_path(); |
| 3274 | BUG_ON(!path); |
Julia Lawall | 33c17ad | 2009-07-22 16:49:01 -0400 | [diff] [blame] | 3275 | path->reada = -1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3276 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3277 | key.objectid = inode->i_ino; |
| 3278 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3279 | key.type = (u8)-1; |
| 3280 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3281 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3282 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3283 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3284 | if (ret < 0) { |
| 3285 | err = ret; |
| 3286 | goto out; |
| 3287 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3288 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3289 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3290 | /* there are no items in the tree for us to truncate, we're |
| 3291 | * done |
| 3292 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3293 | if (path->slots[0] == 0) |
| 3294 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3295 | path->slots[0]--; |
| 3296 | } |
| 3297 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3298 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3299 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3300 | leaf = path->nodes[0]; |
| 3301 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3302 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3303 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3304 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3305 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3306 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3307 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3308 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3309 | break; |
| 3310 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3311 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3312 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3313 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3314 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3315 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3316 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 3317 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 3318 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 3319 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3320 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3321 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3322 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3323 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3324 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3325 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3326 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3327 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3328 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3329 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3330 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3331 | } else { |
| 3332 | if (item_end < new_size) |
| 3333 | break; |
| 3334 | if (found_key.offset >= new_size) |
| 3335 | del_item = 1; |
| 3336 | else |
| 3337 | del_item = 0; |
| 3338 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3339 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3340 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3341 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3342 | goto delete; |
| 3343 | |
| 3344 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3345 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3346 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3347 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3348 | u64 orig_num_bytes = |
| 3349 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3350 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3351 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3352 | extent_num_bytes = extent_num_bytes & |
| 3353 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3354 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3355 | extent_num_bytes); |
| 3356 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3357 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3358 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3359 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3360 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3361 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3362 | extent_num_bytes = |
| 3363 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3364 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3365 | extent_offset = found_key.offset - |
| 3366 | btrfs_file_extent_offset(leaf, fi); |
| 3367 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3368 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3369 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3370 | if (extent_start != 0) { |
| 3371 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3372 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3373 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3374 | } |
| 3375 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3376 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3377 | /* |
| 3378 | * we can't truncate inline items that have had |
| 3379 | * special encodings |
| 3380 | */ |
| 3381 | if (!del_item && |
| 3382 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3383 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3384 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3385 | u32 size = new_size - found_key.offset; |
| 3386 | |
| 3387 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3388 | inode_sub_bytes(inode, item_end + 1 - |
| 3389 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3390 | } |
| 3391 | size = |
| 3392 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3393 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3394 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3395 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3396 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3397 | inode_sub_bytes(inode, item_end + 1 - |
| 3398 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3399 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3400 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3401 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3402 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3403 | if (!pending_del_nr) { |
| 3404 | /* no pending yet, add ourselves */ |
| 3405 | pending_del_slot = path->slots[0]; |
| 3406 | pending_del_nr = 1; |
| 3407 | } else if (pending_del_nr && |
| 3408 | path->slots[0] + 1 == pending_del_slot) { |
| 3409 | /* hop on the pending chunk */ |
| 3410 | pending_del_nr++; |
| 3411 | pending_del_slot = path->slots[0]; |
| 3412 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3413 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3414 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3415 | } else { |
| 3416 | break; |
| 3417 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3418 | if (found_extent && (root->ref_cows || |
| 3419 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3420 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3421 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3422 | extent_num_bytes, 0, |
| 3423 | btrfs_header_owner(leaf), |
| 3424 | inode->i_ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3425 | BUG_ON(ret); |
| 3426 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3427 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3428 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3429 | break; |
| 3430 | |
| 3431 | if (path->slots[0] == 0 || |
| 3432 | path->slots[0] != pending_del_slot) { |
| 3433 | if (root->ref_cows) { |
| 3434 | err = -EAGAIN; |
| 3435 | goto out; |
| 3436 | } |
| 3437 | if (pending_del_nr) { |
| 3438 | ret = btrfs_del_items(trans, root, path, |
| 3439 | pending_del_slot, |
| 3440 | pending_del_nr); |
| 3441 | BUG_ON(ret); |
| 3442 | pending_del_nr = 0; |
| 3443 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3444 | btrfs_release_path(root, path); |
| 3445 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3446 | } else { |
| 3447 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3448 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3449 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3450 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3451 | if (pending_del_nr) { |
| 3452 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3453 | pending_del_nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3454 | BUG_ON(ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3455 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3456 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3457 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3458 | } |
| 3459 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3460 | /* |
| 3461 | * taken from block_truncate_page, but does cow as it zeros out |
| 3462 | * any bytes left in the last page in the file. |
| 3463 | */ |
| 3464 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3465 | { |
| 3466 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3467 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3468 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3469 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3470 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3471 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3472 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3473 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3474 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3475 | struct page *page; |
| 3476 | int ret = 0; |
| 3477 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3478 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3479 | |
| 3480 | if ((offset & (blocksize - 1)) == 0) |
| 3481 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3482 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3483 | if (ret) |
| 3484 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3485 | |
| 3486 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3487 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3488 | page = grab_cache_page(mapping, index); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3489 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3490 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3491 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3492 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3493 | |
| 3494 | page_start = page_offset(page); |
| 3495 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3496 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3497 | if (!PageUptodate(page)) { |
| 3498 | ret = btrfs_readpage(NULL, page); |
| 3499 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3500 | if (page->mapping != mapping) { |
| 3501 | unlock_page(page); |
| 3502 | page_cache_release(page); |
| 3503 | goto again; |
| 3504 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3505 | if (!PageUptodate(page)) { |
| 3506 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3507 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3508 | } |
| 3509 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3510 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3511 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3512 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 3513 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3514 | set_page_extent_mapped(page); |
| 3515 | |
| 3516 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3517 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3518 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3519 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3520 | unlock_page(page); |
| 3521 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3522 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3523 | btrfs_put_ordered_extent(ordered); |
| 3524 | goto again; |
| 3525 | } |
| 3526 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3527 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3528 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3529 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3530 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3531 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3532 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3533 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3534 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3535 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3536 | goto out_unlock; |
| 3537 | } |
| 3538 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3539 | ret = 0; |
| 3540 | if (offset != PAGE_CACHE_SIZE) { |
| 3541 | kaddr = kmap(page); |
| 3542 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3543 | flush_dcache_page(page); |
| 3544 | kunmap(page); |
| 3545 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3546 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3547 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3548 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3549 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3550 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3551 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3552 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3553 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3554 | unlock_page(page); |
| 3555 | page_cache_release(page); |
| 3556 | out: |
| 3557 | return ret; |
| 3558 | } |
| 3559 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3560 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3561 | { |
| 3562 | struct btrfs_trans_handle *trans; |
| 3563 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3564 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3565 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3566 | struct extent_state *cached_state = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3567 | u64 mask = root->sectorsize - 1; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3568 | u64 hole_start = (oldsize + mask) & ~mask; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3569 | u64 block_end = (size + mask) & ~mask; |
| 3570 | u64 last_byte; |
| 3571 | u64 cur_offset; |
| 3572 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3573 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3574 | |
| 3575 | if (size <= hole_start) |
| 3576 | return 0; |
| 3577 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3578 | while (1) { |
| 3579 | struct btrfs_ordered_extent *ordered; |
| 3580 | btrfs_wait_ordered_range(inode, hole_start, |
| 3581 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3582 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
| 3583 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3584 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3585 | if (!ordered) |
| 3586 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3587 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3588 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3589 | btrfs_put_ordered_extent(ordered); |
| 3590 | } |
| 3591 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3592 | cur_offset = hole_start; |
| 3593 | while (1) { |
| 3594 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3595 | block_end - cur_offset, 0); |
| 3596 | BUG_ON(IS_ERR(em) || !em); |
| 3597 | last_byte = min(extent_map_end(em), block_end); |
| 3598 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3599 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3600 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3601 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3602 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3603 | trans = btrfs_start_transaction(root, 2); |
| 3604 | if (IS_ERR(trans)) { |
| 3605 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3606 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3607 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3608 | btrfs_set_trans_block_group(trans, inode); |
| 3609 | |
| 3610 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3611 | cur_offset + hole_size, |
| 3612 | &hint_byte, 1); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3613 | if (err) |
| 3614 | break; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3615 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3616 | err = btrfs_insert_file_extent(trans, root, |
| 3617 | inode->i_ino, cur_offset, 0, |
| 3618 | 0, hole_size, 0, hole_size, |
| 3619 | 0, 0, 0); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3620 | if (err) |
| 3621 | break; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3622 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3623 | btrfs_drop_extent_cache(inode, hole_start, |
| 3624 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3625 | |
| 3626 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3627 | } |
| 3628 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3629 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3630 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3631 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3632 | break; |
| 3633 | } |
| 3634 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3635 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3636 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3637 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3638 | return err; |
| 3639 | } |
| 3640 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3641 | static int btrfs_setsize(struct inode *inode, loff_t newsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3642 | { |
| 3643 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3644 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3645 | loff_t oldsize = i_size_read(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3646 | unsigned long nr; |
| 3647 | int ret; |
| 3648 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3649 | if (newsize == oldsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3650 | return 0; |
| 3651 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3652 | trans = btrfs_start_transaction(root, 5); |
| 3653 | if (IS_ERR(trans)) |
| 3654 | return PTR_ERR(trans); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3655 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3656 | btrfs_set_trans_block_group(trans, inode); |
| 3657 | |
| 3658 | ret = btrfs_orphan_add(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3659 | if (ret) { |
| 3660 | btrfs_end_transaction(trans, root); |
| 3661 | return ret; |
| 3662 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3663 | |
| 3664 | nr = trans->blocks_used; |
| 3665 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3666 | btrfs_btree_balance_dirty(root, nr); |
| 3667 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3668 | if (newsize > oldsize) { |
| 3669 | i_size_write(inode, newsize); |
| 3670 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 3671 | truncate_pagecache(inode, oldsize, newsize); |
| 3672 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3673 | if (ret) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3674 | btrfs_setsize(inode, oldsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3675 | return ret; |
| 3676 | } |
| 3677 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3678 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3679 | if (IS_ERR(trans)) |
| 3680 | return PTR_ERR(trans); |
| 3681 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3682 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3683 | trans->block_rsv = root->orphan_block_rsv; |
| 3684 | BUG_ON(!trans->block_rsv); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3685 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3686 | /* |
| 3687 | * If this fails just leave the orphan item so that it can get |
| 3688 | * cleaned up next time we mount. |
| 3689 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3690 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3691 | if (ret) { |
| 3692 | btrfs_end_transaction(trans, root); |
| 3693 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3694 | } |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3695 | if (inode->i_nlink > 0) |
| 3696 | ret = btrfs_orphan_del(trans, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3697 | nr = trans->blocks_used; |
| 3698 | btrfs_end_transaction(trans, root); |
| 3699 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3700 | } else { |
| 3701 | |
| 3702 | /* |
| 3703 | * We're truncating a file that used to have good data down to |
| 3704 | * zero. Make sure it gets into the ordered flush list so that |
| 3705 | * any new writes get down to disk quickly. |
| 3706 | */ |
| 3707 | if (newsize == 0) |
| 3708 | BTRFS_I(inode)->ordered_data_close = 1; |
| 3709 | |
| 3710 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3711 | truncate_setsize(inode, newsize); |
| 3712 | ret = btrfs_truncate(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3715 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3718 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3719 | { |
| 3720 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3721 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3722 | int err; |
| 3723 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3724 | if (btrfs_root_readonly(root)) |
| 3725 | return -EROFS; |
| 3726 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3727 | err = inode_change_ok(inode, attr); |
| 3728 | if (err) |
| 3729 | return err; |
| 3730 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3731 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3732 | err = btrfs_setsize(inode, attr->ia_size); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3733 | if (err) |
| 3734 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3735 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3736 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3737 | if (attr->ia_valid) { |
| 3738 | setattr_copy(inode, attr); |
| 3739 | mark_inode_dirty(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3740 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3741 | if (attr->ia_valid & ATTR_MODE) |
| 3742 | err = btrfs_acl_chmod(inode); |
| 3743 | } |
| 3744 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3745 | return err; |
| 3746 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3747 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3748 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3749 | { |
| 3750 | struct btrfs_trans_handle *trans; |
| 3751 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3752 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3753 | int ret; |
| 3754 | |
| 3755 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3756 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
| 3757 | root == root->fs_info->tree_root)) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3758 | goto no_delete; |
| 3759 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3760 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3761 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3762 | goto no_delete; |
| 3763 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3764 | /* 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] | 3765 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3766 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3767 | if (root->fs_info->log_root_recovering) { |
| 3768 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3769 | goto no_delete; |
| 3770 | } |
| 3771 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3772 | if (inode->i_nlink > 0) { |
| 3773 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3774 | goto no_delete; |
| 3775 | } |
| 3776 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3777 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3778 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3779 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3780 | trans = btrfs_start_transaction(root, 0); |
| 3781 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3782 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3783 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3784 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3785 | ret = btrfs_block_rsv_check(trans, root, |
| 3786 | root->orphan_block_rsv, 0, 5); |
| 3787 | if (ret) { |
| 3788 | BUG_ON(ret != -EAGAIN); |
| 3789 | ret = btrfs_commit_transaction(trans, root); |
| 3790 | BUG_ON(ret); |
| 3791 | continue; |
| 3792 | } |
| 3793 | |
| 3794 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3795 | if (ret != -EAGAIN) |
| 3796 | break; |
| 3797 | |
| 3798 | nr = trans->blocks_used; |
| 3799 | btrfs_end_transaction(trans, root); |
| 3800 | trans = NULL; |
| 3801 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3802 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3803 | } |
| 3804 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3805 | if (ret == 0) { |
| 3806 | ret = btrfs_orphan_del(trans, inode); |
| 3807 | BUG_ON(ret); |
| 3808 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3809 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3810 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3811 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3812 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3813 | no_delete: |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3814 | end_writeback(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3815 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | /* |
| 3819 | * this returns the key found in the dir entry in the location pointer. |
| 3820 | * If no dir entries were found, location->objectid is 0. |
| 3821 | */ |
| 3822 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3823 | struct btrfs_key *location) |
| 3824 | { |
| 3825 | const char *name = dentry->d_name.name; |
| 3826 | int namelen = dentry->d_name.len; |
| 3827 | struct btrfs_dir_item *di; |
| 3828 | struct btrfs_path *path; |
| 3829 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3830 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3831 | |
| 3832 | path = btrfs_alloc_path(); |
| 3833 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3834 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3835 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 3836 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3837 | if (IS_ERR(di)) |
| 3838 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3839 | |
| 3840 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3841 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3842 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3843 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3844 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3845 | btrfs_free_path(path); |
| 3846 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3847 | out_err: |
| 3848 | location->objectid = 0; |
| 3849 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | /* |
| 3853 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3854 | * needs to be changed to reflect the root directory of the tree root. This |
| 3855 | * is kind of like crossing a mount point. |
| 3856 | */ |
| 3857 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3858 | struct inode *dir, |
| 3859 | struct dentry *dentry, |
| 3860 | struct btrfs_key *location, |
| 3861 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3862 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3863 | struct btrfs_path *path; |
| 3864 | struct btrfs_root *new_root; |
| 3865 | struct btrfs_root_ref *ref; |
| 3866 | struct extent_buffer *leaf; |
| 3867 | int ret; |
| 3868 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3869 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3870 | path = btrfs_alloc_path(); |
| 3871 | if (!path) { |
| 3872 | err = -ENOMEM; |
| 3873 | goto out; |
| 3874 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3875 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3876 | err = -ENOENT; |
| 3877 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3878 | BTRFS_I(dir)->root->root_key.objectid, |
| 3879 | location->objectid); |
| 3880 | if (ret) { |
| 3881 | if (ret < 0) |
| 3882 | err = ret; |
| 3883 | goto out; |
| 3884 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3885 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3886 | leaf = path->nodes[0]; |
| 3887 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 3888 | if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || |
| 3889 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3890 | goto out; |
| 3891 | |
| 3892 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3893 | (unsigned long)(ref + 1), |
| 3894 | dentry->d_name.len); |
| 3895 | if (ret) |
| 3896 | goto out; |
| 3897 | |
| 3898 | btrfs_release_path(root->fs_info->tree_root, path); |
| 3899 | |
| 3900 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3901 | if (IS_ERR(new_root)) { |
| 3902 | err = PTR_ERR(new_root); |
| 3903 | goto out; |
| 3904 | } |
| 3905 | |
| 3906 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3907 | err = -ENOENT; |
| 3908 | goto out; |
| 3909 | } |
| 3910 | |
| 3911 | *sub_root = new_root; |
| 3912 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3913 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3914 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3915 | err = 0; |
| 3916 | out: |
| 3917 | btrfs_free_path(path); |
| 3918 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3919 | } |
| 3920 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3921 | static void inode_tree_add(struct inode *inode) |
| 3922 | { |
| 3923 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3924 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3925 | struct rb_node **p; |
| 3926 | struct rb_node *parent; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3927 | again: |
| 3928 | p = &root->inode_tree.rb_node; |
| 3929 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3930 | |
Al Viro | 1d3382c | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 3931 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3932 | return; |
| 3933 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3934 | spin_lock(&root->inode_lock); |
| 3935 | while (*p) { |
| 3936 | parent = *p; |
| 3937 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3938 | |
| 3939 | if (inode->i_ino < entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3940 | p = &parent->rb_left; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3941 | else if (inode->i_ino > entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3942 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3943 | else { |
| 3944 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 3945 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3946 | rb_erase(parent, &root->inode_tree); |
| 3947 | RB_CLEAR_NODE(parent); |
| 3948 | spin_unlock(&root->inode_lock); |
| 3949 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3950 | } |
| 3951 | } |
| 3952 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3953 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3954 | spin_unlock(&root->inode_lock); |
| 3955 | } |
| 3956 | |
| 3957 | static void inode_tree_del(struct inode *inode) |
| 3958 | { |
| 3959 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3960 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3961 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3962 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3963 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3964 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3965 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3966 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3967 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3968 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3969 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3970 | /* |
| 3971 | * Free space cache has inodes in the tree root, but the tree root has a |
| 3972 | * root_refs of 0, so this could end up dropping the tree root as a |
| 3973 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 3974 | * make sure we don't drop it. |
| 3975 | */ |
| 3976 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 3977 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3978 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3979 | spin_lock(&root->inode_lock); |
| 3980 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3981 | spin_unlock(&root->inode_lock); |
| 3982 | if (empty) |
| 3983 | btrfs_add_dead_root(root); |
| 3984 | } |
| 3985 | } |
| 3986 | |
| 3987 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 3988 | { |
| 3989 | struct rb_node *node; |
| 3990 | struct rb_node *prev; |
| 3991 | struct btrfs_inode *entry; |
| 3992 | struct inode *inode; |
| 3993 | u64 objectid = 0; |
| 3994 | |
| 3995 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3996 | |
| 3997 | spin_lock(&root->inode_lock); |
| 3998 | again: |
| 3999 | node = root->inode_tree.rb_node; |
| 4000 | prev = NULL; |
| 4001 | while (node) { |
| 4002 | prev = node; |
| 4003 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4004 | |
| 4005 | if (objectid < entry->vfs_inode.i_ino) |
| 4006 | node = node->rb_left; |
| 4007 | else if (objectid > entry->vfs_inode.i_ino) |
| 4008 | node = node->rb_right; |
| 4009 | else |
| 4010 | break; |
| 4011 | } |
| 4012 | if (!node) { |
| 4013 | while (prev) { |
| 4014 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
| 4015 | if (objectid <= entry->vfs_inode.i_ino) { |
| 4016 | node = prev; |
| 4017 | break; |
| 4018 | } |
| 4019 | prev = rb_next(prev); |
| 4020 | } |
| 4021 | } |
| 4022 | while (node) { |
| 4023 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4024 | objectid = entry->vfs_inode.i_ino + 1; |
| 4025 | inode = igrab(&entry->vfs_inode); |
| 4026 | if (inode) { |
| 4027 | spin_unlock(&root->inode_lock); |
| 4028 | if (atomic_read(&inode->i_count) > 1) |
| 4029 | d_prune_aliases(inode); |
| 4030 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4031 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4032 | * the inode cache when its usage count |
| 4033 | * hits zero. |
| 4034 | */ |
| 4035 | iput(inode); |
| 4036 | cond_resched(); |
| 4037 | spin_lock(&root->inode_lock); |
| 4038 | goto again; |
| 4039 | } |
| 4040 | |
| 4041 | if (cond_resched_lock(&root->inode_lock)) |
| 4042 | goto again; |
| 4043 | |
| 4044 | node = rb_next(node); |
| 4045 | } |
| 4046 | spin_unlock(&root->inode_lock); |
| 4047 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4048 | } |
| 4049 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4050 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4051 | { |
| 4052 | struct btrfs_iget_args *args = p; |
| 4053 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4054 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4055 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4056 | return 0; |
| 4057 | } |
| 4058 | |
| 4059 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4060 | { |
| 4061 | struct btrfs_iget_args *args = opaque; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4062 | return args->ino == inode->i_ino && |
| 4063 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4064 | } |
| 4065 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4066 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4067 | u64 objectid, |
| 4068 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4069 | { |
| 4070 | struct inode *inode; |
| 4071 | struct btrfs_iget_args args; |
| 4072 | args.ino = objectid; |
| 4073 | args.root = root; |
| 4074 | |
| 4075 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 4076 | btrfs_init_locked_inode, |
| 4077 | (void *)&args); |
| 4078 | return inode; |
| 4079 | } |
| 4080 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4081 | /* Get an inode object given its location and corresponding root. |
| 4082 | * Returns in *is_new if the inode was read from disk |
| 4083 | */ |
| 4084 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4085 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4086 | { |
| 4087 | struct inode *inode; |
| 4088 | |
| 4089 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4090 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4091 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4092 | |
| 4093 | if (inode->i_state & I_NEW) { |
| 4094 | BTRFS_I(inode)->root = root; |
| 4095 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4096 | btrfs_read_locked_inode(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4097 | |
| 4098 | inode_tree_add(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4099 | unlock_new_inode(inode); |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4100 | if (new) |
| 4101 | *new = 1; |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | return inode; |
| 4105 | } |
| 4106 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4107 | static struct inode *new_simple_dir(struct super_block *s, |
| 4108 | struct btrfs_key *key, |
| 4109 | struct btrfs_root *root) |
| 4110 | { |
| 4111 | struct inode *inode = new_inode(s); |
| 4112 | |
| 4113 | if (!inode) |
| 4114 | return ERR_PTR(-ENOMEM); |
| 4115 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4116 | BTRFS_I(inode)->root = root; |
| 4117 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 4118 | BTRFS_I(inode)->dummy_inode = 1; |
| 4119 | |
| 4120 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 4121 | inode->i_op = &simple_dir_inode_operations; |
| 4122 | inode->i_fop = &simple_dir_operations; |
| 4123 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 4124 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 4125 | |
| 4126 | return inode; |
| 4127 | } |
| 4128 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4129 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4130 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4131 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4132 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4133 | struct btrfs_root *sub_root = root; |
| 4134 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4135 | int index; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4136 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4137 | |
| 4138 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 4139 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4140 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4141 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4142 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4143 | if (ret < 0) |
| 4144 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4145 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4146 | if (location.objectid == 0) |
| 4147 | return NULL; |
| 4148 | |
| 4149 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4150 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4151 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4152 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4153 | |
| 4154 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4155 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4156 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4157 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4158 | &location, &sub_root); |
| 4159 | if (ret < 0) { |
| 4160 | if (ret != -ENOENT) |
| 4161 | inode = ERR_PTR(ret); |
| 4162 | else |
| 4163 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4164 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4165 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4166 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4167 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4168 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 4169 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4170 | down_read(&root->fs_info->cleanup_work_sem); |
| 4171 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 4172 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4173 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame^] | 4174 | if (ret) |
| 4175 | inode = ERR_PTR(ret); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4176 | } |
| 4177 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4178 | return inode; |
| 4179 | } |
| 4180 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 4181 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4182 | { |
| 4183 | struct btrfs_root *root; |
| 4184 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4185 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 4186 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4187 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4188 | if (dentry->d_inode) { |
| 4189 | root = BTRFS_I(dentry->d_inode)->root; |
| 4190 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4191 | return 1; |
| 4192 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4193 | return 0; |
| 4194 | } |
| 4195 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4196 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 4197 | struct nameidata *nd) |
| 4198 | { |
| 4199 | struct inode *inode; |
| 4200 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4201 | inode = btrfs_lookup_dentry(dir, dentry); |
| 4202 | if (IS_ERR(inode)) |
| 4203 | return ERR_CAST(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4204 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4205 | return d_splice_alias(inode, dentry); |
| 4206 | } |
| 4207 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4208 | static unsigned char btrfs_filetype_table[] = { |
| 4209 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 4210 | }; |
| 4211 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 4212 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 4213 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4214 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4215 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4216 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4217 | struct btrfs_item *item; |
| 4218 | struct btrfs_dir_item *di; |
| 4219 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4220 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4221 | struct btrfs_path *path; |
| 4222 | int ret; |
| 4223 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4224 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4225 | int slot; |
| 4226 | int advance; |
| 4227 | unsigned char d_type; |
| 4228 | int over = 0; |
| 4229 | u32 di_cur; |
| 4230 | u32 di_total; |
| 4231 | u32 di_len; |
| 4232 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4233 | char tmp_name[32]; |
| 4234 | char *name_ptr; |
| 4235 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4236 | |
| 4237 | /* FIXME, use a real flag for deciding about the key type */ |
| 4238 | if (root->fs_info->tree_root == root) |
| 4239 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4240 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4241 | /* special case for "." */ |
| 4242 | if (filp->f_pos == 0) { |
| 4243 | over = filldir(dirent, ".", 1, |
| 4244 | 1, inode->i_ino, |
| 4245 | DT_DIR); |
| 4246 | if (over) |
| 4247 | return 0; |
| 4248 | filp->f_pos = 1; |
| 4249 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4250 | /* special case for .., just use the back ref */ |
| 4251 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4252 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4253 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4254 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4255 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4256 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4257 | filp->f_pos = 2; |
| 4258 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4259 | path = btrfs_alloc_path(); |
| 4260 | path->reada = 2; |
| 4261 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4262 | btrfs_set_key_type(&key, key_type); |
| 4263 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4264 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4265 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4266 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4267 | if (ret < 0) |
| 4268 | goto err; |
| 4269 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4270 | |
| 4271 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4272 | leaf = path->nodes[0]; |
| 4273 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4274 | slot = path->slots[0]; |
| 4275 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4276 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4277 | ret = btrfs_next_leaf(root, path); |
| 4278 | if (ret) |
| 4279 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4280 | leaf = path->nodes[0]; |
| 4281 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4282 | slot = path->slots[0]; |
| 4283 | } else { |
| 4284 | slot++; |
| 4285 | path->slots[0]++; |
| 4286 | } |
| 4287 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4288 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4289 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4290 | item = btrfs_item_nr(leaf, slot); |
| 4291 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4292 | |
| 4293 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4294 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4295 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4296 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4297 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4298 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4299 | |
| 4300 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4301 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4302 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 4303 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4304 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4305 | |
| 4306 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4307 | struct btrfs_key location; |
| 4308 | |
| 4309 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4310 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4311 | name_ptr = tmp_name; |
| 4312 | } else { |
| 4313 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4314 | if (!name_ptr) { |
| 4315 | ret = -ENOMEM; |
| 4316 | goto err; |
| 4317 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4318 | } |
| 4319 | read_extent_buffer(leaf, name_ptr, |
| 4320 | (unsigned long)(di + 1), name_len); |
| 4321 | |
| 4322 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4323 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4324 | |
| 4325 | /* is this a reference to our own snapshot? If so |
| 4326 | * skip it |
| 4327 | */ |
| 4328 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4329 | location.objectid == root->root_key.objectid) { |
| 4330 | over = 0; |
| 4331 | goto skip; |
| 4332 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4333 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4334 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4335 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4336 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4337 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4338 | if (name_ptr != tmp_name) |
| 4339 | kfree(name_ptr); |
| 4340 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4341 | if (over) |
| 4342 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4343 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4344 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4345 | di_cur += di_len; |
| 4346 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4347 | } |
| 4348 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4349 | |
| 4350 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4351 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4352 | /* |
| 4353 | * 32-bit glibc will use getdents64, but then strtol - |
| 4354 | * so the last number we can serve is this. |
| 4355 | */ |
| 4356 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4357 | else |
| 4358 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4359 | nopos: |
| 4360 | ret = 0; |
| 4361 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4362 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4363 | return ret; |
| 4364 | } |
| 4365 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4366 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4367 | { |
| 4368 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4369 | struct btrfs_trans_handle *trans; |
| 4370 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4371 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4372 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4373 | if (BTRFS_I(inode)->dummy_inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4374 | return 0; |
| 4375 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4376 | smp_mb(); |
| 4377 | nolock = (root->fs_info->closing && root == root->fs_info->tree_root); |
| 4378 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4379 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4380 | if (nolock) |
| 4381 | trans = btrfs_join_transaction_nolock(root, 1); |
| 4382 | else |
| 4383 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4384 | if (IS_ERR(trans)) |
| 4385 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4386 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4387 | if (nolock) |
| 4388 | ret = btrfs_end_transaction_nolock(trans, root); |
| 4389 | else |
| 4390 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4391 | } |
| 4392 | return ret; |
| 4393 | } |
| 4394 | |
| 4395 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4396 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4397 | * inode changes. But, it is most likely to find the inode in cache. |
| 4398 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4399 | * to keep or drop this code. |
| 4400 | */ |
| 4401 | void btrfs_dirty_inode(struct inode *inode) |
| 4402 | { |
| 4403 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4404 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4405 | int ret; |
| 4406 | |
| 4407 | if (BTRFS_I(inode)->dummy_inode) |
| 4408 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4409 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4410 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4411 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4412 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4413 | |
| 4414 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4415 | if (ret && ret == -ENOSPC) { |
| 4416 | /* whoops, lets try again with the full transaction */ |
| 4417 | btrfs_end_transaction(trans, root); |
| 4418 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4419 | if (IS_ERR(trans)) { |
| 4420 | if (printk_ratelimit()) { |
| 4421 | printk(KERN_ERR "btrfs: fail to " |
| 4422 | "dirty inode %lu error %ld\n", |
| 4423 | inode->i_ino, PTR_ERR(trans)); |
| 4424 | } |
| 4425 | return; |
| 4426 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4427 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4428 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4429 | ret = btrfs_update_inode(trans, root, inode); |
| 4430 | if (ret) { |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4431 | if (printk_ratelimit()) { |
| 4432 | printk(KERN_ERR "btrfs: fail to " |
| 4433 | "dirty inode %lu error %d\n", |
| 4434 | inode->i_ino, ret); |
| 4435 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4436 | } |
| 4437 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4438 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4439 | } |
| 4440 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4441 | /* |
| 4442 | * find the highest existing sequence number in a directory |
| 4443 | * and then set the in-memory index_cnt variable to reflect |
| 4444 | * free sequence numbers |
| 4445 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4446 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4447 | { |
| 4448 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4449 | struct btrfs_key key, found_key; |
| 4450 | struct btrfs_path *path; |
| 4451 | struct extent_buffer *leaf; |
| 4452 | int ret; |
| 4453 | |
| 4454 | key.objectid = inode->i_ino; |
| 4455 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4456 | key.offset = (u64)-1; |
| 4457 | |
| 4458 | path = btrfs_alloc_path(); |
| 4459 | if (!path) |
| 4460 | return -ENOMEM; |
| 4461 | |
| 4462 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4463 | if (ret < 0) |
| 4464 | goto out; |
| 4465 | /* FIXME: we should be able to handle this */ |
| 4466 | if (ret == 0) |
| 4467 | goto out; |
| 4468 | ret = 0; |
| 4469 | |
| 4470 | /* |
| 4471 | * MAGIC NUMBER EXPLANATION: |
| 4472 | * since we search a directory based on f_pos we have to start at 2 |
| 4473 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4474 | * else has to start at 2 |
| 4475 | */ |
| 4476 | if (path->slots[0] == 0) { |
| 4477 | BTRFS_I(inode)->index_cnt = 2; |
| 4478 | goto out; |
| 4479 | } |
| 4480 | |
| 4481 | path->slots[0]--; |
| 4482 | |
| 4483 | leaf = path->nodes[0]; |
| 4484 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4485 | |
| 4486 | if (found_key.objectid != inode->i_ino || |
| 4487 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4488 | BTRFS_I(inode)->index_cnt = 2; |
| 4489 | goto out; |
| 4490 | } |
| 4491 | |
| 4492 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4493 | out: |
| 4494 | btrfs_free_path(path); |
| 4495 | return ret; |
| 4496 | } |
| 4497 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4498 | /* |
| 4499 | * helper to find a free sequence number in a given directory. This current |
| 4500 | * code is very simple, later versions will do smarter things in the btree |
| 4501 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4502 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4503 | { |
| 4504 | int ret = 0; |
| 4505 | |
| 4506 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 4507 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4508 | if (ret) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4509 | return ret; |
| 4510 | } |
| 4511 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4512 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4513 | BTRFS_I(dir)->index_cnt++; |
| 4514 | |
| 4515 | return ret; |
| 4516 | } |
| 4517 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4518 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4519 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4520 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4521 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4522 | u64 ref_objectid, u64 objectid, |
| 4523 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4524 | { |
| 4525 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4526 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4527 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4528 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4529 | struct btrfs_inode_ref *ref; |
| 4530 | struct btrfs_key key[2]; |
| 4531 | u32 sizes[2]; |
| 4532 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4533 | int ret; |
| 4534 | int owner; |
| 4535 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4536 | path = btrfs_alloc_path(); |
| 4537 | BUG_ON(!path); |
| 4538 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4539 | inode = new_inode(root->fs_info->sb); |
| 4540 | if (!inode) |
| 4541 | return ERR_PTR(-ENOMEM); |
| 4542 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4543 | if (dir) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4544 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4545 | if (ret) { |
| 4546 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4547 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4548 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4549 | } |
| 4550 | /* |
| 4551 | * index_cnt is ignored for everything but a dir, |
| 4552 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4553 | * number |
| 4554 | */ |
| 4555 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4556 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4557 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 4558 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4559 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4560 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4561 | if (mode & S_IFDIR) |
| 4562 | owner = 0; |
| 4563 | else |
| 4564 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4565 | BTRFS_I(inode)->block_group = |
| 4566 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4567 | |
| 4568 | key[0].objectid = objectid; |
| 4569 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4570 | key[0].offset = 0; |
| 4571 | |
| 4572 | key[1].objectid = objectid; |
| 4573 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4574 | key[1].offset = ref_objectid; |
| 4575 | |
| 4576 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4577 | sizes[1] = name_len + sizeof(*ref); |
| 4578 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4579 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4580 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4581 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4582 | goto fail; |
| 4583 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 4584 | inode_init_owner(inode, dir, mode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4585 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4586 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4587 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4588 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4589 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4590 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4591 | |
| 4592 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4593 | struct btrfs_inode_ref); |
| 4594 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4595 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4596 | ptr = (unsigned long)(ref + 1); |
| 4597 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4598 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4599 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4600 | btrfs_free_path(path); |
| 4601 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4602 | location = &BTRFS_I(inode)->location; |
| 4603 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4604 | location->offset = 0; |
| 4605 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4606 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4607 | btrfs_inherit_iflags(inode, dir); |
| 4608 | |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4609 | if ((mode & S_IFREG)) { |
| 4610 | if (btrfs_test_opt(root, NODATASUM)) |
| 4611 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 4612 | if (btrfs_test_opt(root, NODATACOW)) |
| 4613 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4614 | } |
| 4615 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4616 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4617 | inode_tree_add(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4618 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4619 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4620 | if (dir) |
| 4621 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4622 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4623 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4624 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4625 | } |
| 4626 | |
| 4627 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4628 | { |
| 4629 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4630 | } |
| 4631 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4632 | /* |
| 4633 | * utility function to add 'inode' into 'parent_inode' with |
| 4634 | * a give name and a given sequence number. |
| 4635 | * if 'add_backref' is true, also insert a backref from the |
| 4636 | * inode to the parent directory. |
| 4637 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4638 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4639 | struct inode *parent_inode, struct inode *inode, |
| 4640 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4641 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4642 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4643 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4644 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4645 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4646 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4647 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4648 | } else { |
| 4649 | key.objectid = inode->i_ino; |
| 4650 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4651 | key.offset = 0; |
| 4652 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4653 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4654 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4655 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4656 | key.objectid, root->root_key.objectid, |
| 4657 | parent_inode->i_ino, |
| 4658 | index, name, name_len); |
| 4659 | } else if (add_backref) { |
| 4660 | ret = btrfs_insert_inode_ref(trans, root, |
| 4661 | name, name_len, inode->i_ino, |
| 4662 | parent_inode->i_ino, index); |
| 4663 | } |
| 4664 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4665 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4666 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4667 | parent_inode->i_ino, &key, |
| 4668 | btrfs_inode_type(inode), index); |
| 4669 | BUG_ON(ret); |
| 4670 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4671 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4672 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4673 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4674 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4675 | } |
| 4676 | return ret; |
| 4677 | } |
| 4678 | |
| 4679 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4680 | struct inode *dir, struct dentry *dentry, |
| 4681 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4682 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4683 | int err = btrfs_add_link(trans, dir, inode, |
| 4684 | dentry->d_name.name, dentry->d_name.len, |
| 4685 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4686 | if (!err) { |
| 4687 | d_instantiate(dentry, inode); |
| 4688 | return 0; |
| 4689 | } |
| 4690 | if (err > 0) |
| 4691 | err = -EEXIST; |
| 4692 | return err; |
| 4693 | } |
| 4694 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4695 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4696 | int mode, dev_t rdev) |
| 4697 | { |
| 4698 | struct btrfs_trans_handle *trans; |
| 4699 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4700 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4701 | int err; |
| 4702 | int drop_inode = 0; |
| 4703 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4704 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4705 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4706 | |
| 4707 | if (!new_valid_dev(rdev)) |
| 4708 | return -EINVAL; |
| 4709 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4710 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4711 | if (err) |
| 4712 | return err; |
| 4713 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4714 | /* |
| 4715 | * 2 for inode item and ref |
| 4716 | * 2 for dir items |
| 4717 | * 1 for xattr if selinux is on |
| 4718 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4719 | trans = btrfs_start_transaction(root, 5); |
| 4720 | if (IS_ERR(trans)) |
| 4721 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4722 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4723 | btrfs_set_trans_block_group(trans, dir); |
| 4724 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4725 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4726 | dentry->d_name.len, dir->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4727 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4728 | err = PTR_ERR(inode); |
| 4729 | if (IS_ERR(inode)) |
| 4730 | goto out_unlock; |
| 4731 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4732 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4733 | if (err) { |
| 4734 | drop_inode = 1; |
| 4735 | goto out_unlock; |
| 4736 | } |
| 4737 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4738 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4739 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4740 | if (err) |
| 4741 | drop_inode = 1; |
| 4742 | else { |
| 4743 | inode->i_op = &btrfs_special_inode_operations; |
| 4744 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4745 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4746 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4747 | btrfs_update_inode_block_group(trans, inode); |
| 4748 | btrfs_update_inode_block_group(trans, dir); |
| 4749 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4750 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4751 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4752 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4753 | if (drop_inode) { |
| 4754 | inode_dec_link_count(inode); |
| 4755 | iput(inode); |
| 4756 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4757 | return err; |
| 4758 | } |
| 4759 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4760 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4761 | int mode, struct nameidata *nd) |
| 4762 | { |
| 4763 | struct btrfs_trans_handle *trans; |
| 4764 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4765 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4766 | int drop_inode = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4767 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4768 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4769 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4770 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4771 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4772 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4773 | if (err) |
| 4774 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4775 | /* |
| 4776 | * 2 for inode item and ref |
| 4777 | * 2 for dir items |
| 4778 | * 1 for xattr if selinux is on |
| 4779 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4780 | trans = btrfs_start_transaction(root, 5); |
| 4781 | if (IS_ERR(trans)) |
| 4782 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4783 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4784 | btrfs_set_trans_block_group(trans, dir); |
| 4785 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4786 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4787 | dentry->d_name.len, dir->i_ino, objectid, |
| 4788 | BTRFS_I(dir)->block_group, mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4789 | err = PTR_ERR(inode); |
| 4790 | if (IS_ERR(inode)) |
| 4791 | goto out_unlock; |
| 4792 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4793 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4794 | if (err) { |
| 4795 | drop_inode = 1; |
| 4796 | goto out_unlock; |
| 4797 | } |
| 4798 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4799 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4800 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4801 | if (err) |
| 4802 | drop_inode = 1; |
| 4803 | else { |
| 4804 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4805 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4806 | inode->i_fop = &btrfs_file_operations; |
| 4807 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4808 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4809 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4810 | btrfs_update_inode_block_group(trans, inode); |
| 4811 | btrfs_update_inode_block_group(trans, dir); |
| 4812 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4813 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4814 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4815 | if (drop_inode) { |
| 4816 | inode_dec_link_count(inode); |
| 4817 | iput(inode); |
| 4818 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4819 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4820 | return err; |
| 4821 | } |
| 4822 | |
| 4823 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4824 | struct dentry *dentry) |
| 4825 | { |
| 4826 | struct btrfs_trans_handle *trans; |
| 4827 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4828 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4829 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4830 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4831 | int err; |
| 4832 | int drop_inode = 0; |
| 4833 | |
| 4834 | if (inode->i_nlink == 0) |
| 4835 | return -ENOENT; |
| 4836 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4837 | /* do not allow sys_link's with other subvols of the same device */ |
| 4838 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
| 4839 | return -EPERM; |
| 4840 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4841 | btrfs_inc_nlink(inode); |
Josef Bacik | bc1cbf1 | 2010-11-23 19:50:59 +0000 | [diff] [blame] | 4842 | inode->i_ctime = CURRENT_TIME; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4843 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4844 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4845 | if (err) |
| 4846 | goto fail; |
| 4847 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4848 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4849 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4850 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4851 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4852 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4853 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4854 | if (IS_ERR(trans)) { |
| 4855 | err = PTR_ERR(trans); |
| 4856 | goto fail; |
| 4857 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4858 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4859 | btrfs_set_trans_block_group(trans, dir); |
Al Viro | 7de9c6e | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 4860 | ihold(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4861 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4862 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4863 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4864 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4865 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4866 | } else { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4867 | struct dentry *parent = dget_parent(dentry); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4868 | btrfs_update_inode_block_group(trans, dir); |
| 4869 | err = btrfs_update_inode(trans, root, inode); |
| 4870 | BUG_ON(err); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4871 | btrfs_log_new_name(trans, inode, NULL, parent); |
| 4872 | dput(parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4873 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4874 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4875 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4876 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4877 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4878 | if (drop_inode) { |
| 4879 | inode_dec_link_count(inode); |
| 4880 | iput(inode); |
| 4881 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4882 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4883 | return err; |
| 4884 | } |
| 4885 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4886 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4887 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4888 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4889 | struct btrfs_trans_handle *trans; |
| 4890 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4891 | int err = 0; |
| 4892 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4893 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4894 | u64 index = 0; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4895 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4896 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4897 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4898 | if (err) |
| 4899 | return err; |
| 4900 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4901 | /* |
| 4902 | * 2 items for inode and ref |
| 4903 | * 2 items for dir items |
| 4904 | * 1 for xattr if selinux is on |
| 4905 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4906 | trans = btrfs_start_transaction(root, 5); |
| 4907 | if (IS_ERR(trans)) |
| 4908 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4909 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4910 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4911 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4912 | dentry->d_name.len, dir->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4913 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 4914 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4915 | if (IS_ERR(inode)) { |
| 4916 | err = PTR_ERR(inode); |
| 4917 | goto out_fail; |
| 4918 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4919 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4920 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4921 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4922 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4923 | if (err) |
| 4924 | goto out_fail; |
| 4925 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4926 | inode->i_op = &btrfs_dir_inode_operations; |
| 4927 | inode->i_fop = &btrfs_dir_file_operations; |
| 4928 | btrfs_set_trans_block_group(trans, inode); |
| 4929 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4930 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4931 | err = btrfs_update_inode(trans, root, inode); |
| 4932 | if (err) |
| 4933 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4934 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4935 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 4936 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4937 | if (err) |
| 4938 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4939 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4940 | d_instantiate(dentry, inode); |
| 4941 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4942 | btrfs_update_inode_block_group(trans, inode); |
| 4943 | btrfs_update_inode_block_group(trans, dir); |
| 4944 | |
| 4945 | out_fail: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4946 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4947 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4948 | if (drop_on_err) |
| 4949 | iput(inode); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4950 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4951 | return err; |
| 4952 | } |
| 4953 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4954 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4955 | * and an extent that you want to insert, deal with overlap and insert |
| 4956 | * the new extent into the tree. |
| 4957 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4958 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4959 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4960 | struct extent_map *em, |
| 4961 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4962 | { |
| 4963 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4964 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4965 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4966 | start_diff = map_start - em->start; |
| 4967 | em->start = map_start; |
| 4968 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4969 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4970 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4971 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4972 | em->block_len -= start_diff; |
| 4973 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4974 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4975 | } |
| 4976 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4977 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4978 | struct inode *inode, struct page *page, |
| 4979 | size_t pg_offset, u64 extent_offset, |
| 4980 | struct btrfs_file_extent_item *item) |
| 4981 | { |
| 4982 | int ret; |
| 4983 | struct extent_buffer *leaf = path->nodes[0]; |
| 4984 | char *tmp; |
| 4985 | size_t max_size; |
| 4986 | unsigned long inline_size; |
| 4987 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4988 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4989 | |
| 4990 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4991 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4992 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4993 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4994 | btrfs_item_nr(leaf, path->slots[0])); |
| 4995 | tmp = kmalloc(inline_size, GFP_NOFS); |
| 4996 | ptr = btrfs_file_extent_inline_start(item); |
| 4997 | |
| 4998 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4999 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 5000 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5001 | ret = btrfs_decompress(compress_type, tmp, page, |
| 5002 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5003 | if (ret) { |
| 5004 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 5005 | unsigned long copy_size = min_t(u64, |
| 5006 | PAGE_CACHE_SIZE - pg_offset, |
| 5007 | max_size - extent_offset); |
| 5008 | memset(kaddr + pg_offset, 0, copy_size); |
| 5009 | kunmap_atomic(kaddr, KM_USER0); |
| 5010 | } |
| 5011 | kfree(tmp); |
| 5012 | return 0; |
| 5013 | } |
| 5014 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5015 | /* |
| 5016 | * 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] | 5017 | * the ugly parts come from merging extents from the disk with the in-ram |
| 5018 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5019 | * where the in-ram extents might be locked pending data=ordered completion. |
| 5020 | * |
| 5021 | * This also copies inline extents directly into the page. |
| 5022 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5023 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5024 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5025 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5026 | int create) |
| 5027 | { |
| 5028 | int ret; |
| 5029 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5030 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5031 | u64 extent_start = 0; |
| 5032 | u64 extent_end = 0; |
| 5033 | u64 objectid = inode->i_ino; |
| 5034 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5035 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5036 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5037 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5038 | struct extent_buffer *leaf; |
| 5039 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5040 | struct extent_map *em = NULL; |
| 5041 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5042 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5043 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5044 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5045 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5046 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5047 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5048 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5049 | if (em) |
| 5050 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5051 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5052 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5053 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5054 | if (em->start > start || em->start + em->len <= start) |
| 5055 | free_extent_map(em); |
| 5056 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5057 | free_extent_map(em); |
| 5058 | else |
| 5059 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5060 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5061 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5062 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5063 | err = -ENOMEM; |
| 5064 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5065 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5066 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5067 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 5068 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5069 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5070 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5071 | |
| 5072 | if (!path) { |
| 5073 | path = btrfs_alloc_path(); |
| 5074 | BUG_ON(!path); |
| 5075 | } |
| 5076 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5077 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 5078 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5079 | if (ret < 0) { |
| 5080 | err = ret; |
| 5081 | goto out; |
| 5082 | } |
| 5083 | |
| 5084 | if (ret != 0) { |
| 5085 | if (path->slots[0] == 0) |
| 5086 | goto not_found; |
| 5087 | path->slots[0]--; |
| 5088 | } |
| 5089 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5090 | leaf = path->nodes[0]; |
| 5091 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5092 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5093 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5094 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5095 | found_type = btrfs_key_type(&found_key); |
| 5096 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5097 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 5098 | goto not_found; |
| 5099 | } |
| 5100 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5101 | found_type = btrfs_file_extent_type(leaf, item); |
| 5102 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5103 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5104 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5105 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5106 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5107 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5108 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5109 | size_t size; |
| 5110 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5111 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 5112 | ~((u64)root->sectorsize - 1); |
| 5113 | } |
| 5114 | |
| 5115 | if (start >= extent_end) { |
| 5116 | path->slots[0]++; |
| 5117 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 5118 | ret = btrfs_next_leaf(root, path); |
| 5119 | if (ret < 0) { |
| 5120 | err = ret; |
| 5121 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5122 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5123 | if (ret > 0) |
| 5124 | goto not_found; |
| 5125 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5126 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5127 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5128 | if (found_key.objectid != objectid || |
| 5129 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 5130 | goto not_found; |
| 5131 | if (start + len <= found_key.offset) |
| 5132 | goto not_found; |
| 5133 | em->start = start; |
| 5134 | em->len = found_key.offset - start; |
| 5135 | goto not_found_em; |
| 5136 | } |
| 5137 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5138 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5139 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5140 | em->start = extent_start; |
| 5141 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5142 | em->orig_start = extent_start - |
| 5143 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5144 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 5145 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5146 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5147 | goto insert; |
| 5148 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5149 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5150 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5151 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5152 | em->block_start = bytenr; |
| 5153 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 5154 | item); |
| 5155 | } else { |
| 5156 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 5157 | em->block_start = bytenr; |
| 5158 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5159 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 5160 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5161 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5162 | goto insert; |
| 5163 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5164 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5165 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5166 | size_t size; |
| 5167 | size_t extent_offset; |
| 5168 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5169 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5170 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5171 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5172 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5173 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5174 | goto out; |
| 5175 | } |
| 5176 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5177 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5178 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5179 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5180 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5181 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5182 | em->len = (copy_size + root->sectorsize - 1) & |
| 5183 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5184 | em->orig_start = EXTENT_MAP_INLINE; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5185 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5186 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5187 | em->compress_type = compress_type; |
| 5188 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5189 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5190 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5191 | if (btrfs_file_extent_compression(leaf, item) != |
| 5192 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5193 | ret = uncompress_inline(path, inode, page, |
| 5194 | pg_offset, |
| 5195 | extent_offset, item); |
| 5196 | BUG_ON(ret); |
| 5197 | } else { |
| 5198 | map = kmap(page); |
| 5199 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 5200 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 5201 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 5202 | memset(map + pg_offset + copy_size, 0, |
| 5203 | PAGE_CACHE_SIZE - pg_offset - |
| 5204 | copy_size); |
| 5205 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5206 | kunmap(page); |
| 5207 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5208 | flush_dcache_page(page); |
| 5209 | } else if (create && PageUptodate(page)) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5210 | WARN_ON(1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5211 | if (!trans) { |
| 5212 | kunmap(page); |
| 5213 | free_extent_map(em); |
| 5214 | em = NULL; |
| 5215 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 5216 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5217 | if (IS_ERR(trans)) |
| 5218 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5219 | goto again; |
| 5220 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5221 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5222 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5223 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5224 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5225 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5226 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5227 | set_extent_uptodate(io_tree, em->start, |
| 5228 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5229 | goto insert; |
| 5230 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5231 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5232 | WARN_ON(1); |
| 5233 | } |
| 5234 | not_found: |
| 5235 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5236 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5237 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5238 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5239 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5240 | insert: |
| 5241 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5242 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5243 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 5244 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 5245 | (unsigned long long)em->len, |
| 5246 | (unsigned long long)start, |
| 5247 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5248 | err = -EIO; |
| 5249 | goto out; |
| 5250 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5251 | |
| 5252 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5253 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5254 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5255 | /* it is possible that someone inserted the extent into the tree |
| 5256 | * while we had the lock dropped. It is also possible that |
| 5257 | * an overlapping map exists in the tree |
| 5258 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5259 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5260 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5261 | |
| 5262 | ret = 0; |
| 5263 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5264 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5265 | if (existing && (existing->start > start || |
| 5266 | existing->start + existing->len <= start)) { |
| 5267 | free_extent_map(existing); |
| 5268 | existing = NULL; |
| 5269 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5270 | if (!existing) { |
| 5271 | existing = lookup_extent_mapping(em_tree, em->start, |
| 5272 | em->len); |
| 5273 | if (existing) { |
| 5274 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5275 | em, start, |
| 5276 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5277 | free_extent_map(existing); |
| 5278 | if (err) { |
| 5279 | free_extent_map(em); |
| 5280 | em = NULL; |
| 5281 | } |
| 5282 | } else { |
| 5283 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5284 | free_extent_map(em); |
| 5285 | em = NULL; |
| 5286 | } |
| 5287 | } else { |
| 5288 | free_extent_map(em); |
| 5289 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5290 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5291 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5292 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5293 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5294 | out: |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5295 | if (path) |
| 5296 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5297 | if (trans) { |
| 5298 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5299 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5300 | err = ret; |
| 5301 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5302 | if (err) { |
| 5303 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5304 | return ERR_PTR(err); |
| 5305 | } |
| 5306 | return em; |
| 5307 | } |
| 5308 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5309 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 5310 | size_t pg_offset, u64 start, u64 len, |
| 5311 | int create) |
| 5312 | { |
| 5313 | struct extent_map *em; |
| 5314 | struct extent_map *hole_em = NULL; |
| 5315 | u64 range_start = start; |
| 5316 | u64 end; |
| 5317 | u64 found; |
| 5318 | u64 found_end; |
| 5319 | int err = 0; |
| 5320 | |
| 5321 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 5322 | if (IS_ERR(em)) |
| 5323 | return em; |
| 5324 | if (em) { |
| 5325 | /* |
| 5326 | * if our em maps to a hole, there might |
| 5327 | * actually be delalloc bytes behind it |
| 5328 | */ |
| 5329 | if (em->block_start != EXTENT_MAP_HOLE) |
| 5330 | return em; |
| 5331 | else |
| 5332 | hole_em = em; |
| 5333 | } |
| 5334 | |
| 5335 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 5336 | end = start + len; |
| 5337 | if (end < start) |
| 5338 | end = (u64)-1; |
| 5339 | else |
| 5340 | end -= 1; |
| 5341 | |
| 5342 | em = NULL; |
| 5343 | |
| 5344 | /* ok, we didn't find anything, lets look for delalloc */ |
| 5345 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 5346 | end, len, EXTENT_DELALLOC, 1); |
| 5347 | found_end = range_start + found; |
| 5348 | if (found_end < range_start) |
| 5349 | found_end = (u64)-1; |
| 5350 | |
| 5351 | /* |
| 5352 | * we didn't find anything useful, return |
| 5353 | * the original results from get_extent() |
| 5354 | */ |
| 5355 | if (range_start > end || found_end <= start) { |
| 5356 | em = hole_em; |
| 5357 | hole_em = NULL; |
| 5358 | goto out; |
| 5359 | } |
| 5360 | |
| 5361 | /* adjust the range_start to make sure it doesn't |
| 5362 | * go backwards from the start they passed in |
| 5363 | */ |
| 5364 | range_start = max(start,range_start); |
| 5365 | found = found_end - range_start; |
| 5366 | |
| 5367 | if (found > 0) { |
| 5368 | u64 hole_start = start; |
| 5369 | u64 hole_len = len; |
| 5370 | |
| 5371 | em = alloc_extent_map(GFP_NOFS); |
| 5372 | if (!em) { |
| 5373 | err = -ENOMEM; |
| 5374 | goto out; |
| 5375 | } |
| 5376 | /* |
| 5377 | * when btrfs_get_extent can't find anything it |
| 5378 | * returns one huge hole |
| 5379 | * |
| 5380 | * make sure what it found really fits our range, and |
| 5381 | * adjust to make sure it is based on the start from |
| 5382 | * the caller |
| 5383 | */ |
| 5384 | if (hole_em) { |
| 5385 | u64 calc_end = extent_map_end(hole_em); |
| 5386 | |
| 5387 | if (calc_end <= start || (hole_em->start > end)) { |
| 5388 | free_extent_map(hole_em); |
| 5389 | hole_em = NULL; |
| 5390 | } else { |
| 5391 | hole_start = max(hole_em->start, start); |
| 5392 | hole_len = calc_end - hole_start; |
| 5393 | } |
| 5394 | } |
| 5395 | em->bdev = NULL; |
| 5396 | if (hole_em && range_start > hole_start) { |
| 5397 | /* our hole starts before our delalloc, so we |
| 5398 | * have to return just the parts of the hole |
| 5399 | * that go until the delalloc starts |
| 5400 | */ |
| 5401 | em->len = min(hole_len, |
| 5402 | range_start - hole_start); |
| 5403 | em->start = hole_start; |
| 5404 | em->orig_start = hole_start; |
| 5405 | /* |
| 5406 | * don't adjust block start at all, |
| 5407 | * it is fixed at EXTENT_MAP_HOLE |
| 5408 | */ |
| 5409 | em->block_start = hole_em->block_start; |
| 5410 | em->block_len = hole_len; |
| 5411 | } else { |
| 5412 | em->start = range_start; |
| 5413 | em->len = found; |
| 5414 | em->orig_start = range_start; |
| 5415 | em->block_start = EXTENT_MAP_DELALLOC; |
| 5416 | em->block_len = found; |
| 5417 | } |
| 5418 | } else if (hole_em) { |
| 5419 | return hole_em; |
| 5420 | } |
| 5421 | out: |
| 5422 | |
| 5423 | free_extent_map(hole_em); |
| 5424 | if (err) { |
| 5425 | free_extent_map(em); |
| 5426 | return ERR_PTR(err); |
| 5427 | } |
| 5428 | return em; |
| 5429 | } |
| 5430 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5431 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 5432 | u64 start, u64 len) |
| 5433 | { |
| 5434 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5435 | struct btrfs_trans_handle *trans; |
| 5436 | struct extent_map *em; |
| 5437 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 5438 | struct btrfs_key ins; |
| 5439 | u64 alloc_hint; |
| 5440 | int ret; |
| 5441 | |
| 5442 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
| 5443 | |
| 5444 | trans = btrfs_join_transaction(root, 0); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5445 | if (IS_ERR(trans)) |
| 5446 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5447 | |
| 5448 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5449 | |
| 5450 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 5451 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
| 5452 | alloc_hint, (u64)-1, &ins, 1); |
| 5453 | if (ret) { |
| 5454 | em = ERR_PTR(ret); |
| 5455 | goto out; |
| 5456 | } |
| 5457 | |
| 5458 | em = alloc_extent_map(GFP_NOFS); |
| 5459 | if (!em) { |
| 5460 | em = ERR_PTR(-ENOMEM); |
| 5461 | goto out; |
| 5462 | } |
| 5463 | |
| 5464 | em->start = start; |
| 5465 | em->orig_start = em->start; |
| 5466 | em->len = ins.offset; |
| 5467 | |
| 5468 | em->block_start = ins.objectid; |
| 5469 | em->block_len = ins.offset; |
| 5470 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 5471 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5472 | |
| 5473 | while (1) { |
| 5474 | write_lock(&em_tree->lock); |
| 5475 | ret = add_extent_mapping(em_tree, em); |
| 5476 | write_unlock(&em_tree->lock); |
| 5477 | if (ret != -EEXIST) |
| 5478 | break; |
| 5479 | btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0); |
| 5480 | } |
| 5481 | |
| 5482 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 5483 | ins.offset, ins.offset, 0); |
| 5484 | if (ret) { |
| 5485 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 5486 | em = ERR_PTR(ret); |
| 5487 | } |
| 5488 | out: |
| 5489 | btrfs_end_transaction(trans, root); |
| 5490 | return em; |
| 5491 | } |
| 5492 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5493 | /* |
| 5494 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 5495 | * block must be cow'd |
| 5496 | */ |
| 5497 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 5498 | struct inode *inode, u64 offset, u64 len) |
| 5499 | { |
| 5500 | struct btrfs_path *path; |
| 5501 | int ret; |
| 5502 | struct extent_buffer *leaf; |
| 5503 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5504 | struct btrfs_file_extent_item *fi; |
| 5505 | struct btrfs_key key; |
| 5506 | u64 disk_bytenr; |
| 5507 | u64 backref_offset; |
| 5508 | u64 extent_end; |
| 5509 | u64 num_bytes; |
| 5510 | int slot; |
| 5511 | int found_type; |
| 5512 | |
| 5513 | path = btrfs_alloc_path(); |
| 5514 | if (!path) |
| 5515 | return -ENOMEM; |
| 5516 | |
| 5517 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 5518 | offset, 0); |
| 5519 | if (ret < 0) |
| 5520 | goto out; |
| 5521 | |
| 5522 | slot = path->slots[0]; |
| 5523 | if (ret == 1) { |
| 5524 | if (slot == 0) { |
| 5525 | /* can't find the item, must cow */ |
| 5526 | ret = 0; |
| 5527 | goto out; |
| 5528 | } |
| 5529 | slot--; |
| 5530 | } |
| 5531 | ret = 0; |
| 5532 | leaf = path->nodes[0]; |
| 5533 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5534 | if (key.objectid != inode->i_ino || |
| 5535 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5536 | /* not our file or wrong item type, must cow */ |
| 5537 | goto out; |
| 5538 | } |
| 5539 | |
| 5540 | if (key.offset > offset) { |
| 5541 | /* Wrong offset, must cow */ |
| 5542 | goto out; |
| 5543 | } |
| 5544 | |
| 5545 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5546 | found_type = btrfs_file_extent_type(leaf, fi); |
| 5547 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 5548 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 5549 | /* not a regular extent, must cow */ |
| 5550 | goto out; |
| 5551 | } |
| 5552 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5553 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 5554 | |
| 5555 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 5556 | if (extent_end < offset + len) { |
| 5557 | /* extent doesn't include our full range, must cow */ |
| 5558 | goto out; |
| 5559 | } |
| 5560 | |
| 5561 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 5562 | goto out; |
| 5563 | |
| 5564 | /* |
| 5565 | * look for other files referencing this extent, if we |
| 5566 | * find any we must cow |
| 5567 | */ |
| 5568 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
| 5569 | key.offset - backref_offset, disk_bytenr)) |
| 5570 | goto out; |
| 5571 | |
| 5572 | /* |
| 5573 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 5574 | * in this extent we are about to write. If there |
| 5575 | * are any csums in that range we have to cow in order |
| 5576 | * to keep the csums correct |
| 5577 | */ |
| 5578 | disk_bytenr += backref_offset; |
| 5579 | disk_bytenr += offset - key.offset; |
| 5580 | num_bytes = min(offset + len, extent_end) - offset; |
| 5581 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 5582 | goto out; |
| 5583 | /* |
| 5584 | * all of the above have passed, it is safe to overwrite this extent |
| 5585 | * without cow |
| 5586 | */ |
| 5587 | ret = 1; |
| 5588 | out: |
| 5589 | btrfs_free_path(path); |
| 5590 | return ret; |
| 5591 | } |
| 5592 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5593 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 5594 | struct buffer_head *bh_result, int create) |
| 5595 | { |
| 5596 | struct extent_map *em; |
| 5597 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5598 | u64 start = iblock << inode->i_blkbits; |
| 5599 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5600 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5601 | |
| 5602 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
| 5603 | if (IS_ERR(em)) |
| 5604 | return PTR_ERR(em); |
| 5605 | |
| 5606 | /* |
| 5607 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 5608 | * io. INLINE is special, and we could probably kludge it in here, but |
| 5609 | * it's still buffered so for safety lets just fall back to the generic |
| 5610 | * buffered path. |
| 5611 | * |
| 5612 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 5613 | * decompress it, so there will be buffering required no matter what we |
| 5614 | * do, so go ahead and fallback to buffered. |
| 5615 | * |
| 5616 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 5617 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 5618 | * the generic code. |
| 5619 | */ |
| 5620 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 5621 | em->block_start == EXTENT_MAP_INLINE) { |
| 5622 | free_extent_map(em); |
| 5623 | return -ENOTBLK; |
| 5624 | } |
| 5625 | |
| 5626 | /* Just a good old fashioned hole, return */ |
| 5627 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 5628 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5629 | free_extent_map(em); |
| 5630 | /* DIO will do one hole at a time, so just unlock a sector */ |
| 5631 | unlock_extent(&BTRFS_I(inode)->io_tree, start, |
| 5632 | start + root->sectorsize - 1, GFP_NOFS); |
| 5633 | return 0; |
| 5634 | } |
| 5635 | |
| 5636 | /* |
| 5637 | * We don't allocate a new extent in the following cases |
| 5638 | * |
| 5639 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 5640 | * existing extent. |
| 5641 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 5642 | * just use the extent. |
| 5643 | * |
| 5644 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5645 | if (!create) { |
| 5646 | len = em->len - (start - em->start); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5647 | goto map; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5648 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5649 | |
| 5650 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 5651 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 5652 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5653 | int type; |
| 5654 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5655 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5656 | |
| 5657 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5658 | type = BTRFS_ORDERED_PREALLOC; |
| 5659 | else |
| 5660 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5661 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5662 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5663 | |
| 5664 | /* |
| 5665 | * we're not going to log anything, but we do need |
| 5666 | * to make sure the current transaction stays open |
| 5667 | * while we look for nocow cross refs |
| 5668 | */ |
| 5669 | trans = btrfs_join_transaction(root, 0); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5670 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5671 | goto must_cow; |
| 5672 | |
| 5673 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
| 5674 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 5675 | block_start, len, len, type); |
| 5676 | btrfs_end_transaction(trans, root); |
| 5677 | if (ret) { |
| 5678 | free_extent_map(em); |
| 5679 | return ret; |
| 5680 | } |
| 5681 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5682 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5683 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5684 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5685 | must_cow: |
| 5686 | /* |
| 5687 | * this will cow the extent, reset the len in case we changed |
| 5688 | * it above |
| 5689 | */ |
| 5690 | len = bh_result->b_size; |
| 5691 | free_extent_map(em); |
| 5692 | em = btrfs_new_extent_direct(inode, start, len); |
| 5693 | if (IS_ERR(em)) |
| 5694 | return PTR_ERR(em); |
| 5695 | len = min(len, em->len - (start - em->start)); |
| 5696 | unlock: |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5697 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
| 5698 | EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1, |
| 5699 | 0, NULL, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5700 | map: |
| 5701 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 5702 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5703 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5704 | bh_result->b_bdev = em->bdev; |
| 5705 | set_buffer_mapped(bh_result); |
| 5706 | if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5707 | set_buffer_new(bh_result); |
| 5708 | |
| 5709 | free_extent_map(em); |
| 5710 | |
| 5711 | return 0; |
| 5712 | } |
| 5713 | |
| 5714 | struct btrfs_dio_private { |
| 5715 | struct inode *inode; |
| 5716 | u64 logical_offset; |
| 5717 | u64 disk_bytenr; |
| 5718 | u64 bytes; |
| 5719 | u32 *csums; |
| 5720 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5721 | |
| 5722 | /* number of bios pending for this dio */ |
| 5723 | atomic_t pending_bios; |
| 5724 | |
| 5725 | /* IO errors */ |
| 5726 | int errors; |
| 5727 | |
| 5728 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5729 | }; |
| 5730 | |
| 5731 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 5732 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5733 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5734 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 5735 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5736 | struct inode *inode = dip->inode; |
| 5737 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5738 | u64 start; |
| 5739 | u32 *private = dip->csums; |
| 5740 | |
| 5741 | start = dip->logical_offset; |
| 5742 | do { |
| 5743 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 5744 | struct page *page = bvec->bv_page; |
| 5745 | char *kaddr; |
| 5746 | u32 csum = ~(u32)0; |
| 5747 | unsigned long flags; |
| 5748 | |
| 5749 | local_irq_save(flags); |
| 5750 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 5751 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 5752 | csum, bvec->bv_len); |
| 5753 | btrfs_csum_final(csum, (char *)&csum); |
| 5754 | kunmap_atomic(kaddr, KM_IRQ0); |
| 5755 | local_irq_restore(flags); |
| 5756 | |
| 5757 | flush_dcache_page(bvec->bv_page); |
| 5758 | if (csum != *private) { |
| 5759 | printk(KERN_ERR "btrfs csum failed ino %lu off" |
| 5760 | " %llu csum %u private %u\n", |
| 5761 | inode->i_ino, (unsigned long long)start, |
| 5762 | csum, *private); |
| 5763 | err = -EIO; |
| 5764 | } |
| 5765 | } |
| 5766 | |
| 5767 | start += bvec->bv_len; |
| 5768 | private++; |
| 5769 | bvec++; |
| 5770 | } while (bvec <= bvec_end); |
| 5771 | |
| 5772 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
| 5773 | dip->logical_offset + dip->bytes - 1, GFP_NOFS); |
| 5774 | bio->bi_private = dip->private; |
| 5775 | |
| 5776 | kfree(dip->csums); |
| 5777 | kfree(dip); |
| 5778 | dio_end_io(bio, err); |
| 5779 | } |
| 5780 | |
| 5781 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 5782 | { |
| 5783 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5784 | struct inode *inode = dip->inode; |
| 5785 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5786 | struct btrfs_trans_handle *trans; |
| 5787 | struct btrfs_ordered_extent *ordered = NULL; |
| 5788 | struct extent_state *cached_state = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5789 | u64 ordered_offset = dip->logical_offset; |
| 5790 | u64 ordered_bytes = dip->bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5791 | int ret; |
| 5792 | |
| 5793 | if (err) |
| 5794 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5795 | again: |
| 5796 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 5797 | &ordered_offset, |
| 5798 | ordered_bytes); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5799 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5800 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5801 | |
| 5802 | BUG_ON(!ordered); |
| 5803 | |
| 5804 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5805 | if (IS_ERR(trans)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5806 | err = -ENOMEM; |
| 5807 | goto out; |
| 5808 | } |
| 5809 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5810 | |
| 5811 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) { |
| 5812 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5813 | if (!ret) |
| 5814 | ret = btrfs_update_inode(trans, root, inode); |
| 5815 | err = ret; |
| 5816 | goto out; |
| 5817 | } |
| 5818 | |
| 5819 | lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5820 | ordered->file_offset + ordered->len - 1, 0, |
| 5821 | &cached_state, GFP_NOFS); |
| 5822 | |
| 5823 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) { |
| 5824 | ret = btrfs_mark_extent_written(trans, inode, |
| 5825 | ordered->file_offset, |
| 5826 | ordered->file_offset + |
| 5827 | ordered->len); |
| 5828 | if (ret) { |
| 5829 | err = ret; |
| 5830 | goto out_unlock; |
| 5831 | } |
| 5832 | } else { |
| 5833 | ret = insert_reserved_file_extent(trans, inode, |
| 5834 | ordered->file_offset, |
| 5835 | ordered->start, |
| 5836 | ordered->disk_len, |
| 5837 | ordered->len, |
| 5838 | ordered->len, |
| 5839 | 0, 0, 0, |
| 5840 | BTRFS_FILE_EXTENT_REG); |
| 5841 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 5842 | ordered->file_offset, ordered->len); |
| 5843 | if (ret) { |
| 5844 | err = ret; |
| 5845 | WARN_ON(1); |
| 5846 | goto out_unlock; |
| 5847 | } |
| 5848 | } |
| 5849 | |
| 5850 | add_pending_csums(trans, inode, ordered->file_offset, &ordered->list); |
| 5851 | btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5852 | btrfs_update_inode(trans, root, inode); |
| 5853 | out_unlock: |
| 5854 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5855 | ordered->file_offset + ordered->len - 1, |
| 5856 | &cached_state, GFP_NOFS); |
| 5857 | out: |
| 5858 | btrfs_delalloc_release_metadata(inode, ordered->len); |
| 5859 | btrfs_end_transaction(trans, root); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5860 | ordered_offset = ordered->file_offset + ordered->len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5861 | btrfs_put_ordered_extent(ordered); |
| 5862 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5863 | |
| 5864 | out_test: |
| 5865 | /* |
| 5866 | * our bio might span multiple ordered extents. If we haven't |
| 5867 | * completed the accounting for the whole dio, go back and try again |
| 5868 | */ |
| 5869 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 5870 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 5871 | ordered_offset; |
| 5872 | goto again; |
| 5873 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5874 | out_done: |
| 5875 | bio->bi_private = dip->private; |
| 5876 | |
| 5877 | kfree(dip->csums); |
| 5878 | kfree(dip); |
| 5879 | dio_end_io(bio, err); |
| 5880 | } |
| 5881 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5882 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 5883 | struct bio *bio, int mirror_num, |
| 5884 | unsigned long bio_flags, u64 offset) |
| 5885 | { |
| 5886 | int ret; |
| 5887 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5888 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
| 5889 | BUG_ON(ret); |
| 5890 | return 0; |
| 5891 | } |
| 5892 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5893 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 5894 | { |
| 5895 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5896 | |
| 5897 | if (err) { |
| 5898 | printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 5899 | "sector %#Lx len %u err no %d\n", |
| 5900 | dip->inode->i_ino, bio->bi_rw, |
| 5901 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5902 | dip->errors = 1; |
| 5903 | |
| 5904 | /* |
| 5905 | * before atomic variable goto zero, we must make sure |
| 5906 | * dip->errors is perceived to be set. |
| 5907 | */ |
| 5908 | smp_mb__before_atomic_dec(); |
| 5909 | } |
| 5910 | |
| 5911 | /* if there are more bios still pending for this dio, just exit */ |
| 5912 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 5913 | goto out; |
| 5914 | |
| 5915 | if (dip->errors) |
| 5916 | bio_io_error(dip->orig_bio); |
| 5917 | else { |
| 5918 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 5919 | bio_endio(dip->orig_bio, 0); |
| 5920 | } |
| 5921 | out: |
| 5922 | bio_put(bio); |
| 5923 | } |
| 5924 | |
| 5925 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 5926 | u64 first_sector, gfp_t gfp_flags) |
| 5927 | { |
| 5928 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 5929 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 5930 | } |
| 5931 | |
| 5932 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 5933 | int rw, u64 file_offset, int skip_sum, |
| 5934 | u32 *csums) |
| 5935 | { |
| 5936 | int write = rw & REQ_WRITE; |
| 5937 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5938 | int ret; |
| 5939 | |
| 5940 | bio_get(bio); |
| 5941 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 5942 | if (ret) |
| 5943 | goto err; |
| 5944 | |
| 5945 | if (write && !skip_sum) { |
| 5946 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 5947 | inode, rw, bio, 0, 0, |
| 5948 | file_offset, |
| 5949 | __btrfs_submit_bio_start_direct_io, |
| 5950 | __btrfs_submit_bio_done); |
| 5951 | goto err; |
| 5952 | } else if (!skip_sum) |
| 5953 | btrfs_lookup_bio_sums_dio(root, inode, bio, |
| 5954 | file_offset, csums); |
| 5955 | |
| 5956 | ret = btrfs_map_bio(root, rw, bio, 0, 1); |
| 5957 | err: |
| 5958 | bio_put(bio); |
| 5959 | return ret; |
| 5960 | } |
| 5961 | |
| 5962 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 5963 | int skip_sum) |
| 5964 | { |
| 5965 | struct inode *inode = dip->inode; |
| 5966 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5967 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5968 | struct bio *bio; |
| 5969 | struct bio *orig_bio = dip->orig_bio; |
| 5970 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 5971 | u64 start_sector = orig_bio->bi_sector; |
| 5972 | u64 file_offset = dip->logical_offset; |
| 5973 | u64 submit_len = 0; |
| 5974 | u64 map_length; |
| 5975 | int nr_pages = 0; |
| 5976 | u32 *csums = dip->csums; |
| 5977 | int ret = 0; |
| 5978 | |
| 5979 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 5980 | if (!bio) |
| 5981 | return -ENOMEM; |
| 5982 | bio->bi_private = dip; |
| 5983 | bio->bi_end_io = btrfs_end_dio_bio; |
| 5984 | atomic_inc(&dip->pending_bios); |
| 5985 | |
| 5986 | map_length = orig_bio->bi_size; |
| 5987 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 5988 | &map_length, NULL, 0); |
| 5989 | if (ret) { |
| 5990 | bio_put(bio); |
| 5991 | return -EIO; |
| 5992 | } |
| 5993 | |
| 5994 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 5995 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 5996 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5997 | bvec->bv_offset) < bvec->bv_len)) { |
| 5998 | /* |
| 5999 | * inc the count before we submit the bio so |
| 6000 | * we know the end IO handler won't happen before |
| 6001 | * we inc the count. Otherwise, the dip might get freed |
| 6002 | * before we're done setting it up |
| 6003 | */ |
| 6004 | atomic_inc(&dip->pending_bios); |
| 6005 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 6006 | file_offset, skip_sum, |
| 6007 | csums); |
| 6008 | if (ret) { |
| 6009 | bio_put(bio); |
| 6010 | atomic_dec(&dip->pending_bios); |
| 6011 | goto out_err; |
| 6012 | } |
| 6013 | |
| 6014 | if (!skip_sum) |
| 6015 | csums = csums + nr_pages; |
| 6016 | start_sector += submit_len >> 9; |
| 6017 | file_offset += submit_len; |
| 6018 | |
| 6019 | submit_len = 0; |
| 6020 | nr_pages = 0; |
| 6021 | |
| 6022 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 6023 | start_sector, GFP_NOFS); |
| 6024 | if (!bio) |
| 6025 | goto out_err; |
| 6026 | bio->bi_private = dip; |
| 6027 | bio->bi_end_io = btrfs_end_dio_bio; |
| 6028 | |
| 6029 | map_length = orig_bio->bi_size; |
| 6030 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 6031 | &map_length, NULL, 0); |
| 6032 | if (ret) { |
| 6033 | bio_put(bio); |
| 6034 | goto out_err; |
| 6035 | } |
| 6036 | } else { |
| 6037 | submit_len += bvec->bv_len; |
| 6038 | nr_pages ++; |
| 6039 | bvec++; |
| 6040 | } |
| 6041 | } |
| 6042 | |
| 6043 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
| 6044 | csums); |
| 6045 | if (!ret) |
| 6046 | return 0; |
| 6047 | |
| 6048 | bio_put(bio); |
| 6049 | out_err: |
| 6050 | dip->errors = 1; |
| 6051 | /* |
| 6052 | * before atomic variable goto zero, we must |
| 6053 | * make sure dip->errors is perceived to be set. |
| 6054 | */ |
| 6055 | smp_mb__before_atomic_dec(); |
| 6056 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 6057 | bio_io_error(dip->orig_bio); |
| 6058 | |
| 6059 | /* bio_end_io() will handle error, so we needn't return it */ |
| 6060 | return 0; |
| 6061 | } |
| 6062 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6063 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 6064 | loff_t file_offset) |
| 6065 | { |
| 6066 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6067 | struct btrfs_dio_private *dip; |
| 6068 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6069 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 6070 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6071 | int ret = 0; |
| 6072 | |
| 6073 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 6074 | |
| 6075 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 6076 | if (!dip) { |
| 6077 | ret = -ENOMEM; |
| 6078 | goto free_ordered; |
| 6079 | } |
| 6080 | dip->csums = NULL; |
| 6081 | |
| 6082 | if (!skip_sum) { |
| 6083 | dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); |
| 6084 | if (!dip->csums) { |
Daniel J Blueman | b4966b7 | 2011-03-09 16:46:42 +0000 | [diff] [blame] | 6085 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6086 | ret = -ENOMEM; |
| 6087 | goto free_ordered; |
| 6088 | } |
| 6089 | } |
| 6090 | |
| 6091 | dip->private = bio->bi_private; |
| 6092 | dip->inode = inode; |
| 6093 | dip->logical_offset = file_offset; |
| 6094 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6095 | dip->bytes = 0; |
| 6096 | do { |
| 6097 | dip->bytes += bvec->bv_len; |
| 6098 | bvec++; |
| 6099 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 6100 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6101 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6102 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6103 | dip->errors = 0; |
| 6104 | dip->orig_bio = bio; |
| 6105 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6106 | |
| 6107 | if (write) |
| 6108 | bio->bi_end_io = btrfs_endio_direct_write; |
| 6109 | else |
| 6110 | bio->bi_end_io = btrfs_endio_direct_read; |
| 6111 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6112 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 6113 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6114 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6115 | free_ordered: |
| 6116 | /* |
| 6117 | * If this is a write, we need to clean up the reserved space and kill |
| 6118 | * the ordered extent. |
| 6119 | */ |
| 6120 | if (write) { |
| 6121 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 6122 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6123 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 6124 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 6125 | btrfs_free_reserved_extent(root, ordered->start, |
| 6126 | ordered->disk_len); |
| 6127 | btrfs_put_ordered_extent(ordered); |
| 6128 | btrfs_put_ordered_extent(ordered); |
| 6129 | } |
| 6130 | bio_endio(bio, ret); |
| 6131 | } |
| 6132 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6133 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 6134 | const struct iovec *iov, loff_t offset, |
| 6135 | unsigned long nr_segs) |
| 6136 | { |
| 6137 | int seg; |
| 6138 | size_t size; |
| 6139 | unsigned long addr; |
| 6140 | unsigned blocksize_mask = root->sectorsize - 1; |
| 6141 | ssize_t retval = -EINVAL; |
| 6142 | loff_t end = offset; |
| 6143 | |
| 6144 | if (offset & blocksize_mask) |
| 6145 | goto out; |
| 6146 | |
| 6147 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 6148 | for (seg = 0; seg < nr_segs; seg++) { |
| 6149 | addr = (unsigned long)iov[seg].iov_base; |
| 6150 | size = iov[seg].iov_len; |
| 6151 | end += size; |
| 6152 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
| 6153 | goto out; |
| 6154 | } |
| 6155 | retval = 0; |
| 6156 | out: |
| 6157 | return retval; |
| 6158 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6159 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 6160 | const struct iovec *iov, loff_t offset, |
| 6161 | unsigned long nr_segs) |
| 6162 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6163 | struct file *file = iocb->ki_filp; |
| 6164 | struct inode *inode = file->f_mapping->host; |
| 6165 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6166 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6167 | u64 lockstart, lockend; |
| 6168 | ssize_t ret; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6169 | int writing = rw & WRITE; |
| 6170 | int write_bits = 0; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6171 | size_t count = iov_length(iov, nr_segs); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6172 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6173 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
| 6174 | offset, nr_segs)) { |
| 6175 | return 0; |
| 6176 | } |
| 6177 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6178 | lockstart = offset; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6179 | lockend = offset + count - 1; |
| 6180 | |
| 6181 | if (writing) { |
| 6182 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 6183 | if (ret) |
| 6184 | goto out; |
| 6185 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6186 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6187 | while (1) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6188 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6189 | 0, &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6190 | /* |
| 6191 | * We're concerned with the entire range that we're going to be |
| 6192 | * doing DIO to, so we need to make sure theres no ordered |
| 6193 | * extents in this range. |
| 6194 | */ |
| 6195 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6196 | lockend - lockstart + 1); |
| 6197 | if (!ordered) |
| 6198 | break; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6199 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6200 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6201 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6202 | btrfs_put_ordered_extent(ordered); |
| 6203 | cond_resched(); |
| 6204 | } |
| 6205 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6206 | /* |
| 6207 | * we don't use btrfs_set_extent_delalloc because we don't want |
| 6208 | * the dirty or uptodate bits |
| 6209 | */ |
| 6210 | if (writing) { |
| 6211 | write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING; |
| 6212 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6213 | EXTENT_DELALLOC, 0, NULL, &cached_state, |
| 6214 | GFP_NOFS); |
| 6215 | if (ret) { |
| 6216 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6217 | lockend, EXTENT_LOCKED | write_bits, |
| 6218 | 1, 0, &cached_state, GFP_NOFS); |
| 6219 | goto out; |
| 6220 | } |
| 6221 | } |
| 6222 | |
| 6223 | free_extent_state(cached_state); |
| 6224 | cached_state = NULL; |
| 6225 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6226 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 6227 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 6228 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
| 6229 | btrfs_submit_direct, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6230 | |
| 6231 | if (ret < 0 && ret != -EIOCBQUEUED) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6232 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset, |
| 6233 | offset + iov_length(iov, nr_segs) - 1, |
| 6234 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6235 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6236 | } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) { |
| 6237 | /* |
| 6238 | * We're falling back to buffered, unlock the section we didn't |
| 6239 | * do IO on. |
| 6240 | */ |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6241 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret, |
| 6242 | offset + iov_length(iov, nr_segs) - 1, |
| 6243 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6244 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6245 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6246 | out: |
| 6247 | free_extent_state(cached_state); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6248 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6249 | } |
| 6250 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6251 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 6252 | __u64 start, __u64 len) |
| 6253 | { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6254 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6255 | } |
| 6256 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6257 | int btrfs_readpage(struct file *file, struct page *page) |
| 6258 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6259 | struct extent_io_tree *tree; |
| 6260 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6261 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6262 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6263 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6264 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 6265 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6266 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6267 | |
| 6268 | |
| 6269 | if (current->flags & PF_MEMALLOC) { |
| 6270 | redirty_page_for_writepage(wbc, page); |
| 6271 | unlock_page(page); |
| 6272 | return 0; |
| 6273 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6274 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6275 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 6276 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6277 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6278 | int btrfs_writepages(struct address_space *mapping, |
| 6279 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6280 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6281 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6282 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6283 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6284 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 6285 | } |
| 6286 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6287 | static int |
| 6288 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 6289 | struct list_head *pages, unsigned nr_pages) |
| 6290 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6291 | struct extent_io_tree *tree; |
| 6292 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6293 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 6294 | btrfs_get_extent); |
| 6295 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6296 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6297 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6298 | struct extent_io_tree *tree; |
| 6299 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6300 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6301 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6302 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 6303 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6304 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6305 | if (ret == 1) { |
| 6306 | ClearPagePrivate(page); |
| 6307 | set_page_private(page, 0); |
| 6308 | page_cache_release(page); |
| 6309 | } |
| 6310 | return ret; |
| 6311 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6312 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6313 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 6314 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 6315 | if (PageWriteback(page) || PageDirty(page)) |
| 6316 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 6317 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6318 | } |
| 6319 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6320 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 6321 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6322 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6323 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6324 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6325 | u64 page_start = page_offset(page); |
| 6326 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6327 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6328 | |
| 6329 | /* |
| 6330 | * we have the page locked, so new writeback can't start, |
| 6331 | * and the dirty bit won't be cleared while we are here. |
| 6332 | * |
| 6333 | * Wait for IO on this page so that we can safely clear |
| 6334 | * the PagePrivate2 bit and do ordered accounting |
| 6335 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6336 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6337 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6338 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6339 | if (offset) { |
| 6340 | btrfs_releasepage(page, GFP_NOFS); |
| 6341 | return; |
| 6342 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6343 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6344 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6345 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 6346 | page_offset(page)); |
| 6347 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6348 | /* |
| 6349 | * IO on this page will never be started, so we need |
| 6350 | * to account for any ordered extents now |
| 6351 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6352 | clear_extent_bit(tree, page_start, page_end, |
| 6353 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6354 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6355 | &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6356 | /* |
| 6357 | * whoever cleared the private bit is responsible |
| 6358 | * for the finish_ordered_io |
| 6359 | */ |
| 6360 | if (TestClearPagePrivate2(page)) { |
| 6361 | btrfs_finish_ordered_io(page->mapping->host, |
| 6362 | page_start, page_end); |
| 6363 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6364 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6365 | cached_state = NULL; |
| 6366 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6367 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6368 | } |
| 6369 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6370 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6371 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6372 | __btrfs_releasepage(page, GFP_NOFS); |
| 6373 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6374 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6375 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6376 | ClearPagePrivate(page); |
| 6377 | set_page_private(page, 0); |
| 6378 | page_cache_release(page); |
| 6379 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6380 | } |
| 6381 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6382 | /* |
| 6383 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 6384 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 6385 | * be careful to check for EOF conditions here. We set the page up correctly |
| 6386 | * for a written page which means we get ENOSPC checking when writing into |
| 6387 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 6388 | * support these features. |
| 6389 | * |
| 6390 | * We are not allowed to take the i_mutex here so we have to play games to |
| 6391 | * protect against truncate races as the page could now be beyond EOF. Because |
| 6392 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 6393 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 6394 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 6395 | * unlock the page. |
| 6396 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6397 | 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] | 6398 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6399 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 6400 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6401 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6402 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 6403 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6404 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6405 | char *kaddr; |
| 6406 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6407 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6408 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6409 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6410 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6411 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6412 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6413 | if (ret) { |
| 6414 | if (ret == -ENOMEM) |
| 6415 | ret = VM_FAULT_OOM; |
| 6416 | else /* -ENOSPC, -EIO, etc */ |
| 6417 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6418 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6419 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6420 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6421 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6422 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6423 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6424 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6425 | page_start = page_offset(page); |
| 6426 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6427 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6428 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6429 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6430 | /* page got truncated out from underneath us */ |
| 6431 | goto out_unlock; |
| 6432 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6433 | wait_on_page_writeback(page); |
| 6434 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6435 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 6436 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6437 | set_page_extent_mapped(page); |
| 6438 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6439 | /* |
| 6440 | * we can't set the delalloc bits if there are pending ordered |
| 6441 | * extents. Drop our locks and wait for them to finish |
| 6442 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6443 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6444 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6445 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6446 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6447 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6448 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6449 | btrfs_put_ordered_extent(ordered); |
| 6450 | goto again; |
| 6451 | } |
| 6452 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6453 | /* |
| 6454 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 6455 | * if it was already dirty, so for space accounting reasons we need to |
| 6456 | * clear any delalloc bits for the range we are fixing to save. There |
| 6457 | * is probably a better way to do this, but for now keep consistent with |
| 6458 | * prepare_pages in the normal write path. |
| 6459 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6460 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6461 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6462 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6463 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6464 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 6465 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6466 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6467 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6468 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6469 | ret = VM_FAULT_SIGBUS; |
| 6470 | goto out_unlock; |
| 6471 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6472 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6473 | |
| 6474 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6475 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6476 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6477 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6478 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6479 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6480 | if (zero_start != PAGE_CACHE_SIZE) { |
| 6481 | kaddr = kmap(page); |
| 6482 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 6483 | flush_dcache_page(page); |
| 6484 | kunmap(page); |
| 6485 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6486 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6487 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6488 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6489 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6490 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 6491 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 6492 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6493 | 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] | 6494 | |
| 6495 | out_unlock: |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6496 | if (!ret) |
| 6497 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6498 | unlock_page(page); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6499 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6500 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6501 | return ret; |
| 6502 | } |
| 6503 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6504 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6505 | { |
| 6506 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6507 | int ret; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6508 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6509 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6510 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6511 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6512 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 6513 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 6514 | if (ret) |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6515 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6516 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6517 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6518 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6519 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6520 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6521 | if (IS_ERR(trans)) |
| 6522 | return PTR_ERR(trans); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6523 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6524 | trans->block_rsv = root->orphan_block_rsv; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6525 | |
| 6526 | /* |
| 6527 | * setattr is responsible for setting the ordered_data_close flag, |
| 6528 | * but that is only tested during the last file release. That |
| 6529 | * could happen well after the next commit, leaving a great big |
| 6530 | * window where new writes may get lost if someone chooses to write |
| 6531 | * to this file after truncating to zero |
| 6532 | * |
| 6533 | * The inode doesn't have any dirty data here, and so if we commit |
| 6534 | * this is a noop. If someone immediately starts writing to the inode |
| 6535 | * it is very likely we'll catch some of their writes in this |
| 6536 | * transaction, and the commit will find this file on the ordered |
| 6537 | * data list with good things to send down. |
| 6538 | * |
| 6539 | * This is a best effort solution, there is still a window where |
| 6540 | * using truncate to replace the contents of the file will |
| 6541 | * end up with a zero length file after a crash. |
| 6542 | */ |
| 6543 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 6544 | btrfs_add_ordered_operation(trans, root, inode); |
| 6545 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6546 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6547 | if (!trans) { |
| 6548 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6549 | if (IS_ERR(trans)) |
| 6550 | return PTR_ERR(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6551 | btrfs_set_trans_block_group(trans, inode); |
| 6552 | trans->block_rsv = root->orphan_block_rsv; |
| 6553 | } |
| 6554 | |
| 6555 | ret = btrfs_block_rsv_check(trans, root, |
| 6556 | root->orphan_block_rsv, 0, 5); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6557 | if (ret == -EAGAIN) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6558 | ret = btrfs_commit_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6559 | if (ret) |
| 6560 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6561 | trans = NULL; |
| 6562 | continue; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6563 | } else if (ret) { |
| 6564 | err = ret; |
| 6565 | break; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6566 | } |
| 6567 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6568 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 6569 | inode->i_size, |
| 6570 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6571 | if (ret != -EAGAIN) { |
| 6572 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6573 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6574 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6575 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6576 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6577 | if (ret) { |
| 6578 | err = ret; |
| 6579 | break; |
| 6580 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6581 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6582 | nr = trans->blocks_used; |
| 6583 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6584 | trans = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6585 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6586 | } |
| 6587 | |
| 6588 | if (ret == 0 && inode->i_nlink > 0) { |
| 6589 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6590 | if (ret) |
| 6591 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6592 | } |
| 6593 | |
| 6594 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6595 | if (ret && !err) |
| 6596 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6597 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6598 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6599 | ret = btrfs_end_transaction_throttle(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6600 | if (ret && !err) |
| 6601 | err = ret; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6602 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6603 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6604 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6605 | } |
| 6606 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6607 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6608 | * create a new subvolume directory/inode (helper for the ioctl). |
| 6609 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6610 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6611 | struct btrfs_root *new_root, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6612 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6613 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6614 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6615 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6616 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6617 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6618 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6619 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6620 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 6621 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6622 | inode->i_op = &btrfs_dir_inode_operations; |
| 6623 | inode->i_fop = &btrfs_dir_file_operations; |
| 6624 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6625 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6626 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6627 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6628 | err = btrfs_update_inode(trans, new_root, inode); |
| 6629 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6630 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6631 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6632 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6633 | } |
| 6634 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6635 | /* helper function for file defrag and space balancing. This |
| 6636 | * forces readahead on a given range of bytes in an inode |
| 6637 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6638 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6639 | struct file_ra_state *ra, struct file *file, |
| 6640 | pgoff_t offset, pgoff_t last_index) |
| 6641 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6642 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6643 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6644 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 6645 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6646 | } |
| 6647 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6648 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 6649 | { |
| 6650 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6651 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6652 | |
| 6653 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 6654 | if (!ei) |
| 6655 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6656 | |
| 6657 | ei->root = NULL; |
| 6658 | ei->space_info = NULL; |
| 6659 | ei->generation = 0; |
| 6660 | ei->sequence = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 6661 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6662 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6663 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6664 | ei->delalloc_bytes = 0; |
| 6665 | ei->reserved_bytes = 0; |
| 6666 | ei->disk_i_size = 0; |
| 6667 | ei->flags = 0; |
| 6668 | ei->index_cnt = (u64)-1; |
| 6669 | ei->last_unlink_trans = 0; |
| 6670 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6671 | atomic_set(&ei->outstanding_extents, 0); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 6672 | atomic_set(&ei->reserved_extents, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6673 | |
| 6674 | ei->ordered_data_close = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6675 | ei->orphan_meta_reserved = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6676 | ei->dummy_inode = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6677 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6678 | |
| 6679 | inode = &ei->vfs_inode; |
| 6680 | extent_map_tree_init(&ei->extent_tree, GFP_NOFS); |
| 6681 | extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS); |
| 6682 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS); |
| 6683 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6684 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6685 | INIT_LIST_HEAD(&ei->i_orphan); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6686 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6687 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6688 | RB_CLEAR_NODE(&ei->rb_node); |
| 6689 | |
| 6690 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6691 | } |
| 6692 | |
Nick Piggin | fa0d7e3 | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6693 | static void btrfs_i_callback(struct rcu_head *head) |
| 6694 | { |
| 6695 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 6696 | INIT_LIST_HEAD(&inode->i_dentry); |
| 6697 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 6698 | } |
| 6699 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6700 | void btrfs_destroy_inode(struct inode *inode) |
| 6701 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6702 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6703 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6704 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6705 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 6706 | WARN_ON(inode->i_data.nrpages); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6707 | WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents)); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 6708 | WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6709 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6710 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6711 | * This can happen where we create an inode, but somebody else also |
| 6712 | * created the same inode and we need to destroy the one we already |
| 6713 | * created. |
| 6714 | */ |
| 6715 | if (!root) |
| 6716 | goto free; |
| 6717 | |
| 6718 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6719 | * Make sure we're properly removed from the ordered operation |
| 6720 | * lists. |
| 6721 | */ |
| 6722 | smp_mb(); |
| 6723 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 6724 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 6725 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 6726 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 6727 | } |
| 6728 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6729 | if (root == root->fs_info->tree_root) { |
| 6730 | struct btrfs_block_group_cache *block_group; |
| 6731 | |
| 6732 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 6733 | BTRFS_I(inode)->block_group); |
| 6734 | if (block_group && block_group->inode == inode) { |
| 6735 | spin_lock(&block_group->lock); |
| 6736 | block_group->inode = NULL; |
| 6737 | spin_unlock(&block_group->lock); |
| 6738 | btrfs_put_block_group(block_group); |
| 6739 | } else if (block_group) { |
| 6740 | btrfs_put_block_group(block_group); |
| 6741 | } |
| 6742 | } |
| 6743 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6744 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6745 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6746 | printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", |
| 6747 | inode->i_ino); |
| 6748 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6749 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6750 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6751 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6752 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6753 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 6754 | if (!ordered) |
| 6755 | break; |
| 6756 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6757 | printk(KERN_ERR "btrfs found ordered " |
| 6758 | "extent %llu %llu on inode cleanup\n", |
| 6759 | (unsigned long long)ordered->file_offset, |
| 6760 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6761 | btrfs_remove_ordered_extent(inode, ordered); |
| 6762 | btrfs_put_ordered_extent(ordered); |
| 6763 | btrfs_put_ordered_extent(ordered); |
| 6764 | } |
| 6765 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6766 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6767 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6768 | free: |
Nick Piggin | fa0d7e3 | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6769 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6770 | } |
| 6771 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6772 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6773 | { |
| 6774 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6775 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6776 | if (btrfs_root_refs(&root->root_item) == 0 && |
| 6777 | root != root->fs_info->tree_root) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6778 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6779 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6780 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6781 | } |
| 6782 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 6783 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6784 | { |
| 6785 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 6786 | |
| 6787 | inode_init_once(&ei->vfs_inode); |
| 6788 | } |
| 6789 | |
| 6790 | void btrfs_destroy_cachep(void) |
| 6791 | { |
| 6792 | if (btrfs_inode_cachep) |
| 6793 | kmem_cache_destroy(btrfs_inode_cachep); |
| 6794 | if (btrfs_trans_handle_cachep) |
| 6795 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 6796 | if (btrfs_transaction_cachep) |
| 6797 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6798 | if (btrfs_path_cachep) |
| 6799 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 6800 | if (btrfs_free_space_cachep) |
| 6801 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6802 | } |
| 6803 | |
| 6804 | int btrfs_init_cachep(void) |
| 6805 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6806 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 6807 | sizeof(struct btrfs_inode), 0, |
| 6808 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6809 | if (!btrfs_inode_cachep) |
| 6810 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6811 | |
| 6812 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 6813 | sizeof(struct btrfs_trans_handle), 0, |
| 6814 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6815 | if (!btrfs_trans_handle_cachep) |
| 6816 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6817 | |
| 6818 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 6819 | sizeof(struct btrfs_transaction), 0, |
| 6820 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6821 | if (!btrfs_transaction_cachep) |
| 6822 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6823 | |
| 6824 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 6825 | sizeof(struct btrfs_path), 0, |
| 6826 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6827 | if (!btrfs_path_cachep) |
| 6828 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6829 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 6830 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache", |
| 6831 | sizeof(struct btrfs_free_space), 0, |
| 6832 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 6833 | if (!btrfs_free_space_cachep) |
| 6834 | goto fail; |
| 6835 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6836 | return 0; |
| 6837 | fail: |
| 6838 | btrfs_destroy_cachep(); |
| 6839 | return -ENOMEM; |
| 6840 | } |
| 6841 | |
| 6842 | static int btrfs_getattr(struct vfsmount *mnt, |
| 6843 | struct dentry *dentry, struct kstat *stat) |
| 6844 | { |
| 6845 | struct inode *inode = dentry->d_inode; |
| 6846 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6847 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 6848 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6849 | stat->blocks = (inode_get_bytes(inode) + |
| 6850 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6851 | return 0; |
| 6852 | } |
| 6853 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6854 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 6855 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6856 | { |
| 6857 | struct btrfs_trans_handle *trans; |
| 6858 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6859 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6860 | struct inode *new_inode = new_dentry->d_inode; |
| 6861 | struct inode *old_inode = old_dentry->d_inode; |
| 6862 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6863 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6864 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6865 | int ret; |
| 6866 | |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6867 | if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 6868 | return -EPERM; |
| 6869 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6870 | /* we only allow rename subvolume link between subvolumes */ |
| 6871 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6872 | return -EXDEV; |
| 6873 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6874 | if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 6875 | (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) |
| 6876 | return -ENOTEMPTY; |
| 6877 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6878 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6879 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6880 | return -ENOTEMPTY; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6881 | /* |
| 6882 | * we're using rename to replace one file with another. |
| 6883 | * and the replacement file is large. Start IO on it now so |
| 6884 | * we don't add too much work to the end of the transaction |
| 6885 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 6886 | 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] | 6887 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 6888 | filemap_flush(old_inode->i_mapping); |
| 6889 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6890 | /* close the racy window with snapshot create/destroy ioctl */ |
| 6891 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 6892 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6893 | /* |
| 6894 | * We want to reserve the absolute worst case amount of items. So if |
| 6895 | * both inodes are subvols and we need to unlink them then that would |
| 6896 | * require 4 item modifications, but if they are both normal inodes it |
| 6897 | * would require 5 item modifications, so we'll assume their normal |
| 6898 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 6899 | * should cover the worst case number of items we'll modify. |
| 6900 | */ |
| 6901 | trans = btrfs_start_transaction(root, 20); |
| 6902 | if (IS_ERR(trans)) |
| 6903 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6904 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6905 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6906 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6907 | if (dest != root) |
| 6908 | btrfs_record_root_in_trans(trans, dest); |
| 6909 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6910 | ret = btrfs_set_inode_index(new_dir, &index); |
| 6911 | if (ret) |
| 6912 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6913 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6914 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6915 | /* force full log commit if subvolume involved. */ |
| 6916 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 6917 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6918 | ret = btrfs_insert_inode_ref(trans, dest, |
| 6919 | new_dentry->d_name.name, |
| 6920 | new_dentry->d_name.len, |
| 6921 | old_inode->i_ino, |
| 6922 | new_dir->i_ino, index); |
| 6923 | if (ret) |
| 6924 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6925 | /* |
| 6926 | * this is an ugly little race, but the rename is required |
| 6927 | * to make sure that if we crash, the inode is either at the |
| 6928 | * old name or the new one. pinning the log transaction lets |
| 6929 | * us make sure we don't allow a log commit to come in after |
| 6930 | * we unlink the name but before we add the new name back in. |
| 6931 | */ |
| 6932 | btrfs_pin_log_trans(root); |
| 6933 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6934 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6935 | * make sure the inode gets flushed if it is replacing |
| 6936 | * something. |
| 6937 | */ |
| 6938 | if (new_inode && new_inode->i_size && |
| 6939 | old_inode && S_ISREG(old_inode->i_mode)) { |
| 6940 | btrfs_add_ordered_operation(trans, root, old_inode); |
| 6941 | } |
| 6942 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6943 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 6944 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 6945 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6946 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6947 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 6948 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 6949 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6950 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6951 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 6952 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 6953 | old_dentry->d_name.name, |
| 6954 | old_dentry->d_name.len); |
| 6955 | } else { |
| 6956 | btrfs_inc_nlink(old_dentry->d_inode); |
| 6957 | ret = btrfs_unlink_inode(trans, root, old_dir, |
| 6958 | old_dentry->d_inode, |
| 6959 | old_dentry->d_name.name, |
| 6960 | old_dentry->d_name.len); |
| 6961 | } |
| 6962 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6963 | |
| 6964 | if (new_inode) { |
| 6965 | new_inode->i_ctime = CURRENT_TIME; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6966 | if (unlikely(new_inode->i_ino == |
| 6967 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 6968 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 6969 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 6970 | root_objectid, |
| 6971 | new_dentry->d_name.name, |
| 6972 | new_dentry->d_name.len); |
| 6973 | BUG_ON(new_inode->i_nlink == 0); |
| 6974 | } else { |
| 6975 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 6976 | new_dentry->d_inode, |
| 6977 | new_dentry->d_name.name, |
| 6978 | new_dentry->d_name.len); |
| 6979 | } |
| 6980 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6981 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6982 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6983 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6984 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6985 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6986 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6987 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 6988 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6989 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6990 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6991 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6992 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6993 | struct dentry *parent = dget_parent(new_dentry); |
| 6994 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
| 6995 | dput(parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6996 | btrfs_end_log_trans(root); |
| 6997 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6998 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 6999 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7000 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7001 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 7002 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7003 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7004 | return ret; |
| 7005 | } |
| 7006 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7007 | /* |
| 7008 | * some fairly slow code that needs optimization. This walks the list |
| 7009 | * of all the inodes with pending delalloc and forces them to disk. |
| 7010 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7011 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7012 | { |
| 7013 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 7014 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7015 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7016 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7017 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 7018 | return -EROFS; |
| 7019 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7020 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7021 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7022 | binode = list_entry(head->next, struct btrfs_inode, |
| 7023 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7024 | inode = igrab(&binode->vfs_inode); |
| 7025 | if (!inode) |
| 7026 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7027 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7028 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7029 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7030 | if (delay_iput) |
| 7031 | btrfs_add_delayed_iput(inode); |
| 7032 | else |
| 7033 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7034 | } |
| 7035 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7036 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7037 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7038 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7039 | |
| 7040 | /* the filemap_flush will queue IO into the worker threads, but |
| 7041 | * we have to make sure the IO is actually started and that |
| 7042 | * ordered extents get created before we return |
| 7043 | */ |
| 7044 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7045 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7046 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7047 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7048 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 7049 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7050 | } |
| 7051 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7052 | return 0; |
| 7053 | } |
| 7054 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 7055 | int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput, |
| 7056 | int sync) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7057 | { |
| 7058 | struct btrfs_inode *binode; |
| 7059 | struct inode *inode = NULL; |
| 7060 | |
| 7061 | spin_lock(&root->fs_info->delalloc_lock); |
| 7062 | while (!list_empty(&root->fs_info->delalloc_inodes)) { |
| 7063 | binode = list_entry(root->fs_info->delalloc_inodes.next, |
| 7064 | struct btrfs_inode, delalloc_inodes); |
| 7065 | inode = igrab(&binode->vfs_inode); |
| 7066 | if (inode) { |
| 7067 | list_move_tail(&binode->delalloc_inodes, |
| 7068 | &root->fs_info->delalloc_inodes); |
| 7069 | break; |
| 7070 | } |
| 7071 | |
| 7072 | list_del_init(&binode->delalloc_inodes); |
| 7073 | cond_resched_lock(&root->fs_info->delalloc_lock); |
| 7074 | } |
| 7075 | spin_unlock(&root->fs_info->delalloc_lock); |
| 7076 | |
| 7077 | if (inode) { |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 7078 | if (sync) { |
| 7079 | filemap_write_and_wait(inode->i_mapping); |
| 7080 | /* |
| 7081 | * We have to do this because compression doesn't |
| 7082 | * actually set PG_writeback until it submits the pages |
| 7083 | * for IO, which happens in an async thread, so we could |
| 7084 | * race and not actually wait for any writeback pages |
| 7085 | * because they've not been submitted yet. Technically |
| 7086 | * this could still be the case for the ordered stuff |
| 7087 | * since the async thread may not have started to do its |
| 7088 | * work yet. If this becomes the case then we need to |
| 7089 | * figure out a way to make sure that in writepage we |
| 7090 | * wait for any async pages to be submitted before |
| 7091 | * returning so that fdatawait does what its supposed to |
| 7092 | * do. |
| 7093 | */ |
| 7094 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 7095 | } else { |
| 7096 | filemap_flush(inode->i_mapping); |
| 7097 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7098 | if (delay_iput) |
| 7099 | btrfs_add_delayed_iput(inode); |
| 7100 | else |
| 7101 | iput(inode); |
| 7102 | return 1; |
| 7103 | } |
| 7104 | return 0; |
| 7105 | } |
| 7106 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7107 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 7108 | const char *symname) |
| 7109 | { |
| 7110 | struct btrfs_trans_handle *trans; |
| 7111 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 7112 | struct btrfs_path *path; |
| 7113 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7114 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7115 | int err; |
| 7116 | int drop_inode = 0; |
| 7117 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7118 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7119 | int name_len; |
| 7120 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7121 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7122 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7123 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7124 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7125 | |
| 7126 | name_len = strlen(symname) + 1; |
| 7127 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 7128 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7129 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7130 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 7131 | if (err) |
| 7132 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7133 | /* |
| 7134 | * 2 items for inode item and ref |
| 7135 | * 2 items for dir items |
| 7136 | * 1 item for xattr if selinux is on |
| 7137 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7138 | trans = btrfs_start_transaction(root, 5); |
| 7139 | if (IS_ERR(trans)) |
| 7140 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7141 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7142 | btrfs_set_trans_block_group(trans, dir); |
| 7143 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7144 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 7145 | dentry->d_name.len, dir->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7146 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 7147 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7148 | err = PTR_ERR(inode); |
| 7149 | if (IS_ERR(inode)) |
| 7150 | goto out_unlock; |
| 7151 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 7152 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7153 | if (err) { |
| 7154 | drop_inode = 1; |
| 7155 | goto out_unlock; |
| 7156 | } |
| 7157 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7158 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 7159 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7160 | if (err) |
| 7161 | drop_inode = 1; |
| 7162 | else { |
| 7163 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7164 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7165 | inode->i_fop = &btrfs_file_operations; |
| 7166 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7167 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7168 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7169 | btrfs_update_inode_block_group(trans, inode); |
| 7170 | btrfs_update_inode_block_group(trans, dir); |
| 7171 | if (drop_inode) |
| 7172 | goto out_unlock; |
| 7173 | |
| 7174 | path = btrfs_alloc_path(); |
| 7175 | BUG_ON(!path); |
| 7176 | key.objectid = inode->i_ino; |
| 7177 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7178 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 7179 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 7180 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 7181 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7182 | if (err) { |
| 7183 | drop_inode = 1; |
| 7184 | goto out_unlock; |
| 7185 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7186 | leaf = path->nodes[0]; |
| 7187 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 7188 | struct btrfs_file_extent_item); |
| 7189 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 7190 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7191 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7192 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 7193 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 7194 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 7195 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 7196 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7197 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7198 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 7199 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7200 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7201 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7202 | inode->i_op = &btrfs_symlink_inode_operations; |
| 7203 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7204 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7205 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7206 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7207 | err = btrfs_update_inode(trans, root, inode); |
| 7208 | if (err) |
| 7209 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7210 | |
| 7211 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7212 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 7213 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7214 | if (drop_inode) { |
| 7215 | inode_dec_link_count(inode); |
| 7216 | iput(inode); |
| 7217 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7218 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7219 | return err; |
| 7220 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7221 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7222 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7223 | u64 start, u64 num_bytes, u64 min_size, |
| 7224 | loff_t actual_len, u64 *alloc_hint, |
| 7225 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7226 | { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7227 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7228 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7229 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7230 | u64 i_size; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7231 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7232 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7233 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7234 | if (trans) |
| 7235 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7236 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7237 | if (own_trans) { |
| 7238 | trans = btrfs_start_transaction(root, 3); |
| 7239 | if (IS_ERR(trans)) { |
| 7240 | ret = PTR_ERR(trans); |
| 7241 | break; |
| 7242 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7243 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7244 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7245 | ret = btrfs_reserve_extent(trans, root, num_bytes, min_size, |
| 7246 | 0, *alloc_hint, (u64)-1, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7247 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7248 | if (own_trans) |
| 7249 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7250 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7251 | } |
| 7252 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7253 | ret = insert_reserved_file_extent(trans, inode, |
| 7254 | cur_offset, ins.objectid, |
| 7255 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 7256 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7257 | BTRFS_FILE_EXTENT_PREALLOC); |
| 7258 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 7259 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7260 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7261 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7262 | num_bytes -= ins.offset; |
| 7263 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7264 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7265 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7266 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7267 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7268 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7269 | (actual_len > inode->i_size) && |
| 7270 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7271 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7272 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7273 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7274 | i_size = cur_offset; |
| 7275 | i_size_write(inode, i_size); |
| 7276 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7277 | } |
| 7278 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7279 | ret = btrfs_update_inode(trans, root, inode); |
| 7280 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7281 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7282 | if (own_trans) |
| 7283 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7284 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7285 | return ret; |
| 7286 | } |
| 7287 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7288 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7289 | u64 start, u64 num_bytes, u64 min_size, |
| 7290 | loff_t actual_len, u64 *alloc_hint) |
| 7291 | { |
| 7292 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7293 | min_size, actual_len, alloc_hint, |
| 7294 | NULL); |
| 7295 | } |
| 7296 | |
| 7297 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 7298 | struct btrfs_trans_handle *trans, int mode, |
| 7299 | u64 start, u64 num_bytes, u64 min_size, |
| 7300 | loff_t actual_len, u64 *alloc_hint) |
| 7301 | { |
| 7302 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7303 | min_size, actual_len, alloc_hint, trans); |
| 7304 | } |
| 7305 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7306 | static int btrfs_set_page_dirty(struct page *page) |
| 7307 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7308 | return __set_page_dirty_nobuffers(page); |
| 7309 | } |
| 7310 | |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 7311 | static int btrfs_permission(struct inode *inode, int mask, unsigned int flags) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7312 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7313 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7314 | |
| 7315 | if (btrfs_root_readonly(root) && (mask & MAY_WRITE)) |
| 7316 | return -EROFS; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7317 | if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7318 | return -EACCES; |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 7319 | return generic_permission(inode, mask, flags, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7320 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7321 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7322 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7323 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7324 | .lookup = btrfs_lookup, |
| 7325 | .create = btrfs_create, |
| 7326 | .unlink = btrfs_unlink, |
| 7327 | .link = btrfs_link, |
| 7328 | .mkdir = btrfs_mkdir, |
| 7329 | .rmdir = btrfs_rmdir, |
| 7330 | .rename = btrfs_rename, |
| 7331 | .symlink = btrfs_symlink, |
| 7332 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7333 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7334 | .setxattr = btrfs_setxattr, |
| 7335 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7336 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7337 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7338 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7339 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7340 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7341 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7342 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7343 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7344 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 7345 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7346 | .llseek = generic_file_llseek, |
| 7347 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 7348 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7349 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7350 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7351 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7352 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 7353 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7354 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7355 | }; |
| 7356 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7357 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7358 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 7359 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7360 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7361 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7362 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7363 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 7364 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 7365 | .set_bit_hook = btrfs_set_bit_hook, |
| 7366 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7367 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 7368 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7369 | }; |
| 7370 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 7371 | /* |
| 7372 | * btrfs doesn't support the bmap operation because swapfiles |
| 7373 | * use bmap to make a mapping of extents in the file. They assume |
| 7374 | * these extents won't change over the life of the file and they |
| 7375 | * use the bmap result to do IO directly to the drive. |
| 7376 | * |
| 7377 | * the btrfs bmap call would return logical addresses that aren't |
| 7378 | * suitable for IO and they also will change frequently as COW |
| 7379 | * operations happen. So, swapfile + btrfs == corruption. |
| 7380 | * |
| 7381 | * For now we're avoiding this by dropping bmap. |
| 7382 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7383 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7384 | .readpage = btrfs_readpage, |
| 7385 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7386 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7387 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7388 | .sync_page = block_sync_page, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7389 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7390 | .invalidatepage = btrfs_invalidatepage, |
| 7391 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7392 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 7393 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7394 | }; |
| 7395 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7396 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7397 | .readpage = btrfs_readpage, |
| 7398 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 7399 | .invalidatepage = btrfs_invalidatepage, |
| 7400 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7401 | }; |
| 7402 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7403 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7404 | .getattr = btrfs_getattr, |
| 7405 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7406 | .setxattr = btrfs_setxattr, |
| 7407 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7408 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7409 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7410 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7411 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7412 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7413 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7414 | .getattr = btrfs_getattr, |
| 7415 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7416 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7417 | .setxattr = btrfs_setxattr, |
| 7418 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7419 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7420 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7421 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7422 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7423 | .readlink = generic_readlink, |
| 7424 | .follow_link = page_follow_link_light, |
| 7425 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 7426 | .getattr = btrfs_getattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7427 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 7428 | .setxattr = btrfs_setxattr, |
| 7429 | .getxattr = btrfs_getxattr, |
| 7430 | .listxattr = btrfs_listxattr, |
| 7431 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7432 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7433 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 7434 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7435 | .d_delete = btrfs_dentry_delete, |
| 7436 | }; |