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 | f03d9301f | 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 | f03d9301f | 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 | { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame^] | 1790 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 1791 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1792 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1793 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1794 | } |
| 1795 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1796 | /* |
| 1797 | * When IO fails, either with EIO or csum verification fails, we |
| 1798 | * try other mirrors that might have a good copy of the data. This |
| 1799 | * io_failure_record is used to record state as we go through all the |
| 1800 | * mirrors. If another mirror has good data, the page is set up to date |
| 1801 | * and things continue. If a good mirror can't be found, the original |
| 1802 | * bio end_io callback is called to indicate things have failed. |
| 1803 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1804 | struct io_failure_record { |
| 1805 | struct page *page; |
| 1806 | u64 start; |
| 1807 | u64 len; |
| 1808 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1809 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1810 | int last_mirror; |
| 1811 | }; |
| 1812 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1813 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1814 | struct page *page, u64 start, u64 end, |
| 1815 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1816 | { |
| 1817 | struct io_failure_record *failrec = NULL; |
| 1818 | u64 private; |
| 1819 | struct extent_map *em; |
| 1820 | struct inode *inode = page->mapping->host; |
| 1821 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1822 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1823 | struct bio *bio; |
| 1824 | int num_copies; |
| 1825 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1826 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1827 | u64 logical; |
| 1828 | |
| 1829 | ret = get_state_private(failure_tree, start, &private); |
| 1830 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1831 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1832 | if (!failrec) |
| 1833 | return -ENOMEM; |
| 1834 | failrec->start = start; |
| 1835 | failrec->len = end - start + 1; |
| 1836 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1837 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1838 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1839 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1840 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1841 | if (em->start > start || em->start + em->len < start) { |
| 1842 | free_extent_map(em); |
| 1843 | em = NULL; |
| 1844 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1845 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1846 | |
| 1847 | if (!em || IS_ERR(em)) { |
| 1848 | kfree(failrec); |
| 1849 | return -EIO; |
| 1850 | } |
| 1851 | logical = start - em->start; |
| 1852 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1853 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1854 | logical = em->block_start; |
| 1855 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1856 | extent_set_compress_type(&failrec->bio_flags, |
| 1857 | em->compress_type); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1858 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1859 | failrec->logical = logical; |
| 1860 | free_extent_map(em); |
| 1861 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1862 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1863 | set_state_private(failure_tree, start, |
| 1864 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1865 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1866 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1867 | } |
| 1868 | num_copies = btrfs_num_copies( |
| 1869 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1870 | failrec->logical, failrec->len); |
| 1871 | failrec->last_mirror++; |
| 1872 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1873 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1874 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1875 | failrec->start, |
| 1876 | EXTENT_LOCKED); |
| 1877 | if (state && state->start != failrec->start) |
| 1878 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1879 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1880 | } |
| 1881 | if (!state || failrec->last_mirror > num_copies) { |
| 1882 | set_state_private(failure_tree, failrec->start, 0); |
| 1883 | clear_extent_bits(failure_tree, failrec->start, |
| 1884 | failrec->start + failrec->len - 1, |
| 1885 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1886 | kfree(failrec); |
| 1887 | return -EIO; |
| 1888 | } |
| 1889 | bio = bio_alloc(GFP_NOFS, 1); |
| 1890 | bio->bi_private = state; |
| 1891 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1892 | bio->bi_sector = failrec->logical >> 9; |
| 1893 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1894 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1895 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1896 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1897 | if (failed_bio->bi_rw & REQ_WRITE) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1898 | rw = WRITE; |
| 1899 | else |
| 1900 | rw = READ; |
| 1901 | |
| 1902 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1903 | failrec->last_mirror, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1904 | failrec->bio_flags, 0); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1905 | return 0; |
| 1906 | } |
| 1907 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1908 | /* |
| 1909 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1910 | * to see if we need to process or clean up an io_failure_record |
| 1911 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1912 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1913 | { |
| 1914 | u64 private; |
| 1915 | u64 private_failure; |
| 1916 | struct io_failure_record *failure; |
| 1917 | int ret; |
| 1918 | |
| 1919 | private = 0; |
| 1920 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1921 | (u64)-1, 1, EXTENT_DIRTY, 0)) { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1922 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1923 | start, &private_failure); |
| 1924 | if (ret == 0) { |
| 1925 | failure = (struct io_failure_record *)(unsigned long) |
| 1926 | private_failure; |
| 1927 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1928 | failure->start, 0); |
| 1929 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1930 | failure->start, |
| 1931 | failure->start + failure->len - 1, |
| 1932 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1933 | GFP_NOFS); |
| 1934 | kfree(failure); |
| 1935 | } |
| 1936 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1937 | return 0; |
| 1938 | } |
| 1939 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1940 | /* |
| 1941 | * when reads are done, we need to check csums to verify the data is correct |
| 1942 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1943 | * the io_failure_record routines to find good copies |
| 1944 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1945 | 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] | 1946 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1947 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1948 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1949 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1950 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1951 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1952 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1953 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1954 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1955 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1956 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1957 | if (PageChecked(page)) { |
| 1958 | ClearPageChecked(page); |
| 1959 | goto good; |
| 1960 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1961 | |
| 1962 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1963 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1964 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1965 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1966 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1967 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1968 | GFP_NOFS); |
| 1969 | return 0; |
| 1970 | } |
| 1971 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1972 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1973 | private = state->private; |
| 1974 | ret = 0; |
| 1975 | } else { |
| 1976 | ret = get_state_private(io_tree, start, &private); |
| 1977 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1978 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1979 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1980 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1981 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1982 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1983 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1984 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1985 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1986 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1987 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1988 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1989 | /* if the io failure tree for this inode is non-empty, |
| 1990 | * check to see if we've recovered from a failed IO |
| 1991 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1992 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1993 | return 0; |
| 1994 | |
| 1995 | zeroit: |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 1996 | if (printk_ratelimit()) { |
| 1997 | printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " |
| 1998 | "private %llu\n", page->mapping->host->i_ino, |
| 1999 | (unsigned long long)start, csum, |
| 2000 | (unsigned long long)private); |
| 2001 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2002 | memset(kaddr + offset, 1, end - start + 1); |
| 2003 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2004 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2005 | if (private == 0) |
| 2006 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2007 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2008 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2009 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2010 | struct delayed_iput { |
| 2011 | struct list_head list; |
| 2012 | struct inode *inode; |
| 2013 | }; |
| 2014 | |
| 2015 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2016 | { |
| 2017 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2018 | struct delayed_iput *delayed; |
| 2019 | |
| 2020 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2021 | return; |
| 2022 | |
| 2023 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2024 | delayed->inode = inode; |
| 2025 | |
| 2026 | spin_lock(&fs_info->delayed_iput_lock); |
| 2027 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2028 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2029 | } |
| 2030 | |
| 2031 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2032 | { |
| 2033 | LIST_HEAD(list); |
| 2034 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2035 | struct delayed_iput *delayed; |
| 2036 | int empty; |
| 2037 | |
| 2038 | spin_lock(&fs_info->delayed_iput_lock); |
| 2039 | empty = list_empty(&fs_info->delayed_iputs); |
| 2040 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2041 | if (empty) |
| 2042 | return; |
| 2043 | |
| 2044 | down_read(&root->fs_info->cleanup_work_sem); |
| 2045 | spin_lock(&fs_info->delayed_iput_lock); |
| 2046 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2047 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2048 | |
| 2049 | while (!list_empty(&list)) { |
| 2050 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2051 | list_del(&delayed->list); |
| 2052 | iput(delayed->inode); |
| 2053 | kfree(delayed); |
| 2054 | } |
| 2055 | up_read(&root->fs_info->cleanup_work_sem); |
| 2056 | } |
| 2057 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2058 | /* |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2059 | * calculate extra metadata reservation when snapshotting a subvolume |
| 2060 | * contains orphan files. |
| 2061 | */ |
| 2062 | void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans, |
| 2063 | struct btrfs_pending_snapshot *pending, |
| 2064 | u64 *bytes_to_reserve) |
| 2065 | { |
| 2066 | struct btrfs_root *root; |
| 2067 | struct btrfs_block_rsv *block_rsv; |
| 2068 | u64 num_bytes; |
| 2069 | int index; |
| 2070 | |
| 2071 | root = pending->root; |
| 2072 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2073 | return; |
| 2074 | |
| 2075 | block_rsv = root->orphan_block_rsv; |
| 2076 | |
| 2077 | /* orphan block reservation for the snapshot */ |
| 2078 | num_bytes = block_rsv->size; |
| 2079 | |
| 2080 | /* |
| 2081 | * after the snapshot is created, COWing tree blocks may use more |
| 2082 | * space than it frees. So we should make sure there is enough |
| 2083 | * reserved space. |
| 2084 | */ |
| 2085 | index = trans->transid & 0x1; |
| 2086 | if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) { |
| 2087 | num_bytes += block_rsv->size - |
| 2088 | (block_rsv->reserved + block_rsv->freed[index]); |
| 2089 | } |
| 2090 | |
| 2091 | *bytes_to_reserve += num_bytes; |
| 2092 | } |
| 2093 | |
| 2094 | void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans, |
| 2095 | struct btrfs_pending_snapshot *pending) |
| 2096 | { |
| 2097 | struct btrfs_root *root = pending->root; |
| 2098 | struct btrfs_root *snap = pending->snap; |
| 2099 | struct btrfs_block_rsv *block_rsv; |
| 2100 | u64 num_bytes; |
| 2101 | int index; |
| 2102 | int ret; |
| 2103 | |
| 2104 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2105 | return; |
| 2106 | |
| 2107 | /* refill source subvolume's orphan block reservation */ |
| 2108 | block_rsv = root->orphan_block_rsv; |
| 2109 | index = trans->transid & 0x1; |
| 2110 | if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) { |
| 2111 | num_bytes = block_rsv->size - |
| 2112 | (block_rsv->reserved + block_rsv->freed[index]); |
| 2113 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2114 | root->orphan_block_rsv, |
| 2115 | num_bytes); |
| 2116 | BUG_ON(ret); |
| 2117 | } |
| 2118 | |
| 2119 | /* setup orphan block reservation for the snapshot */ |
| 2120 | block_rsv = btrfs_alloc_block_rsv(snap); |
| 2121 | BUG_ON(!block_rsv); |
| 2122 | |
| 2123 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2124 | snap->orphan_block_rsv = block_rsv; |
| 2125 | |
| 2126 | num_bytes = root->orphan_block_rsv->size; |
| 2127 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2128 | block_rsv, num_bytes); |
| 2129 | BUG_ON(ret); |
| 2130 | |
| 2131 | #if 0 |
| 2132 | /* insert orphan item for the snapshot */ |
| 2133 | WARN_ON(!root->orphan_item_inserted); |
| 2134 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2135 | snap->root_key.objectid); |
| 2136 | BUG_ON(ret); |
| 2137 | snap->orphan_item_inserted = 1; |
| 2138 | #endif |
| 2139 | } |
| 2140 | |
| 2141 | enum btrfs_orphan_cleanup_state { |
| 2142 | ORPHAN_CLEANUP_STARTED = 1, |
| 2143 | ORPHAN_CLEANUP_DONE = 2, |
| 2144 | }; |
| 2145 | |
| 2146 | /* |
| 2147 | * This is called in transaction commmit time. If there are no orphan |
| 2148 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2149 | * structure. |
| 2150 | */ |
| 2151 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2152 | struct btrfs_root *root) |
| 2153 | { |
| 2154 | int ret; |
| 2155 | |
| 2156 | if (!list_empty(&root->orphan_list) || |
| 2157 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2158 | return; |
| 2159 | |
| 2160 | if (root->orphan_item_inserted && |
| 2161 | btrfs_root_refs(&root->root_item) > 0) { |
| 2162 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2163 | root->root_key.objectid); |
| 2164 | BUG_ON(ret); |
| 2165 | root->orphan_item_inserted = 0; |
| 2166 | } |
| 2167 | |
| 2168 | if (root->orphan_block_rsv) { |
| 2169 | WARN_ON(root->orphan_block_rsv->size > 0); |
| 2170 | btrfs_free_block_rsv(root, root->orphan_block_rsv); |
| 2171 | root->orphan_block_rsv = NULL; |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2176 | * This creates an orphan entry for the given inode in case something goes |
| 2177 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2178 | * |
| 2179 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2180 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2181 | */ |
| 2182 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2183 | { |
| 2184 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2185 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2186 | int reserve = 0; |
| 2187 | int insert = 0; |
| 2188 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2189 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2190 | if (!root->orphan_block_rsv) { |
| 2191 | block_rsv = btrfs_alloc_block_rsv(root); |
| 2192 | BUG_ON(!block_rsv); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2193 | } |
| 2194 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2195 | spin_lock(&root->orphan_lock); |
| 2196 | if (!root->orphan_block_rsv) { |
| 2197 | root->orphan_block_rsv = block_rsv; |
| 2198 | } else if (block_rsv) { |
| 2199 | btrfs_free_block_rsv(root, block_rsv); |
| 2200 | block_rsv = NULL; |
| 2201 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2202 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2203 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2204 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2205 | #if 0 |
| 2206 | /* |
| 2207 | * For proper ENOSPC handling, we should do orphan |
| 2208 | * cleanup when mounting. But this introduces backward |
| 2209 | * compatibility issue. |
| 2210 | */ |
| 2211 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2212 | insert = 2; |
| 2213 | else |
| 2214 | insert = 1; |
| 2215 | #endif |
| 2216 | insert = 1; |
| 2217 | } else { |
| 2218 | WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved); |
| 2219 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2220 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2221 | if (!BTRFS_I(inode)->orphan_meta_reserved) { |
| 2222 | BTRFS_I(inode)->orphan_meta_reserved = 1; |
| 2223 | reserve = 1; |
| 2224 | } |
| 2225 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2226 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2227 | if (block_rsv) |
| 2228 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2229 | |
| 2230 | /* grab metadata reservation from transaction handle */ |
| 2231 | if (reserve) { |
| 2232 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
| 2233 | BUG_ON(ret); |
| 2234 | } |
| 2235 | |
| 2236 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2237 | if (insert >= 1) { |
| 2238 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 2239 | BUG_ON(ret); |
| 2240 | } |
| 2241 | |
| 2242 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2243 | if (insert >= 2) { |
| 2244 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2245 | root->root_key.objectid); |
| 2246 | BUG_ON(ret); |
| 2247 | } |
| 2248 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | /* |
| 2252 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2253 | * item for this particular inode. |
| 2254 | */ |
| 2255 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2256 | { |
| 2257 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2258 | int delete_item = 0; |
| 2259 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2260 | int ret = 0; |
| 2261 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2262 | spin_lock(&root->orphan_lock); |
| 2263 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2264 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2265 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2266 | } |
| 2267 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2268 | if (BTRFS_I(inode)->orphan_meta_reserved) { |
| 2269 | BTRFS_I(inode)->orphan_meta_reserved = 0; |
| 2270 | release_rsv = 1; |
| 2271 | } |
| 2272 | spin_unlock(&root->orphan_lock); |
| 2273 | |
| 2274 | if (trans && delete_item) { |
| 2275 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 2276 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2277 | } |
| 2278 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2279 | if (release_rsv) |
| 2280 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2281 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2282 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | /* |
| 2286 | * this cleans up any orphans that may be left on the list from the last use |
| 2287 | * of this root. |
| 2288 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2289 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2290 | { |
| 2291 | struct btrfs_path *path; |
| 2292 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2293 | struct btrfs_key key, found_key; |
| 2294 | struct btrfs_trans_handle *trans; |
| 2295 | struct inode *inode; |
| 2296 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2297 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2298 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2299 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2300 | |
| 2301 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2302 | if (!path) { |
| 2303 | ret = -ENOMEM; |
| 2304 | goto out; |
| 2305 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2306 | path->reada = -1; |
| 2307 | |
| 2308 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2309 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2310 | key.offset = (u64)-1; |
| 2311 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2312 | while (1) { |
| 2313 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2314 | if (ret < 0) |
| 2315 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2316 | |
| 2317 | /* |
| 2318 | * if ret == 0 means we found what we were searching for, which |
| 2319 | * is weird, but possible, so only screw with path if we didnt |
| 2320 | * find the key and see if we have stuff that matches |
| 2321 | */ |
| 2322 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2323 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2324 | if (path->slots[0] == 0) |
| 2325 | break; |
| 2326 | path->slots[0]--; |
| 2327 | } |
| 2328 | |
| 2329 | /* pull out the item */ |
| 2330 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2331 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2332 | |
| 2333 | /* make sure the item matches what we want */ |
| 2334 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2335 | break; |
| 2336 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2337 | break; |
| 2338 | |
| 2339 | /* release the path since we're done with it */ |
| 2340 | btrfs_release_path(root, path); |
| 2341 | |
| 2342 | /* |
| 2343 | * this is where we are basically btrfs_lookup, without the |
| 2344 | * crossing root thing. we store the inode number in the |
| 2345 | * offset of the orphan item. |
| 2346 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2347 | found_key.objectid = found_key.offset; |
| 2348 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2349 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2350 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2351 | if (IS_ERR(inode)) { |
| 2352 | ret = PTR_ERR(inode); |
| 2353 | goto out; |
| 2354 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2355 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2356 | /* |
| 2357 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2358 | * the proper thing when we hit it |
| 2359 | */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2360 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2361 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2362 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2363 | |
| 2364 | /* |
| 2365 | * if this is a bad inode, means we actually succeeded in |
| 2366 | * removing the inode, but not the orphan record, which means |
| 2367 | * we need to manually delete the orphan since iput will just |
| 2368 | * do a destroy_inode |
| 2369 | */ |
| 2370 | if (is_bad_inode(inode)) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2371 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2372 | if (IS_ERR(trans)) { |
| 2373 | ret = PTR_ERR(trans); |
| 2374 | goto out; |
| 2375 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2376 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2377 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2378 | iput(inode); |
| 2379 | continue; |
| 2380 | } |
| 2381 | |
| 2382 | /* if we have links, this was a truncate, lets do that */ |
| 2383 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 2384 | if (!S_ISREG(inode->i_mode)) { |
| 2385 | WARN_ON(1); |
| 2386 | iput(inode); |
| 2387 | continue; |
| 2388 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2389 | nr_truncate++; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2390 | ret = btrfs_truncate(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2391 | } else { |
| 2392 | nr_unlink++; |
| 2393 | } |
| 2394 | |
| 2395 | /* this will do delete_inode and everything for us */ |
| 2396 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2397 | if (ret) |
| 2398 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2399 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2400 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 2401 | |
| 2402 | if (root->orphan_block_rsv) |
| 2403 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 2404 | (u64)-1); |
| 2405 | |
| 2406 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
| 2407 | trans = btrfs_join_transaction(root, 1); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2408 | if (!IS_ERR(trans)) |
| 2409 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2410 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2411 | |
| 2412 | if (nr_unlink) |
| 2413 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2414 | if (nr_truncate) |
| 2415 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2416 | |
| 2417 | out: |
| 2418 | if (ret) |
| 2419 | printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret); |
| 2420 | btrfs_free_path(path); |
| 2421 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2422 | } |
| 2423 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2424 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2425 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2426 | * don't find any xattrs, we know there can't be any acls. |
| 2427 | * |
| 2428 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2429 | */ |
| 2430 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2431 | int slot, u64 objectid) |
| 2432 | { |
| 2433 | u32 nritems = btrfs_header_nritems(leaf); |
| 2434 | struct btrfs_key found_key; |
| 2435 | int scanned = 0; |
| 2436 | |
| 2437 | slot++; |
| 2438 | while (slot < nritems) { |
| 2439 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2440 | |
| 2441 | /* we found a different objectid, there must not be acls */ |
| 2442 | if (found_key.objectid != objectid) |
| 2443 | return 0; |
| 2444 | |
| 2445 | /* we found an xattr, assume we've got an acl */ |
| 2446 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2447 | return 1; |
| 2448 | |
| 2449 | /* |
| 2450 | * we found a key greater than an xattr key, there can't |
| 2451 | * be any acls later on |
| 2452 | */ |
| 2453 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2454 | return 0; |
| 2455 | |
| 2456 | slot++; |
| 2457 | scanned++; |
| 2458 | |
| 2459 | /* |
| 2460 | * it goes inode, inode backrefs, xattrs, extents, |
| 2461 | * so if there are a ton of hard links to an inode there can |
| 2462 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2463 | * this is just an optimization |
| 2464 | */ |
| 2465 | if (scanned >= 8) |
| 2466 | break; |
| 2467 | } |
| 2468 | /* we hit the end of the leaf before we found an xattr or |
| 2469 | * something larger than an xattr. We have to assume the inode |
| 2470 | * has acls |
| 2471 | */ |
| 2472 | return 1; |
| 2473 | } |
| 2474 | |
| 2475 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2476 | * read an inode from the btree into the in-memory inode |
| 2477 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2478 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2479 | { |
| 2480 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2481 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2482 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2483 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2484 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2485 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2486 | int maybe_acls; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2487 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2488 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2489 | int ret; |
| 2490 | |
| 2491 | path = btrfs_alloc_path(); |
| 2492 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2493 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2494 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2495 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2496 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2497 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2498 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2499 | leaf = path->nodes[0]; |
| 2500 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2501 | struct btrfs_inode_item); |
| 2502 | |
| 2503 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2504 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2505 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2506 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2507 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2508 | |
| 2509 | tspec = btrfs_inode_atime(inode_item); |
| 2510 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2511 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2512 | |
| 2513 | tspec = btrfs_inode_mtime(inode_item); |
| 2514 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2515 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2516 | |
| 2517 | tspec = btrfs_inode_ctime(inode_item); |
| 2518 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2519 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2520 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2521 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2522 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2523 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2524 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2525 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2526 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2527 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2528 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2529 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2530 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2531 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2532 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2533 | /* |
| 2534 | * try to precache a NULL acl entry for files that don't have |
| 2535 | * any xattrs or acls |
| 2536 | */ |
| 2537 | 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] | 2538 | if (!maybe_acls) |
| 2539 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2540 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2541 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2542 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2543 | btrfs_free_path(path); |
| 2544 | inode_item = NULL; |
| 2545 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2546 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2547 | case S_IFREG: |
| 2548 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2549 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2550 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2551 | inode->i_fop = &btrfs_file_operations; |
| 2552 | inode->i_op = &btrfs_file_inode_operations; |
| 2553 | break; |
| 2554 | case S_IFDIR: |
| 2555 | inode->i_fop = &btrfs_dir_file_operations; |
| 2556 | if (root == root->fs_info->tree_root) |
| 2557 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2558 | else |
| 2559 | inode->i_op = &btrfs_dir_inode_operations; |
| 2560 | break; |
| 2561 | case S_IFLNK: |
| 2562 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2563 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2564 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2565 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2566 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2567 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2568 | init_special_inode(inode, inode->i_mode, rdev); |
| 2569 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2570 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2571 | |
| 2572 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2573 | return; |
| 2574 | |
| 2575 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2576 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2577 | make_bad_inode(inode); |
| 2578 | } |
| 2579 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2580 | /* |
| 2581 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2582 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2583 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2584 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2585 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2586 | struct inode *inode) |
| 2587 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2588 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2589 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2590 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2591 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2592 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2593 | |
| 2594 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2595 | inode->i_atime.tv_sec); |
| 2596 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2597 | inode->i_atime.tv_nsec); |
| 2598 | |
| 2599 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2600 | inode->i_mtime.tv_sec); |
| 2601 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2602 | inode->i_mtime.tv_nsec); |
| 2603 | |
| 2604 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2605 | inode->i_ctime.tv_sec); |
| 2606 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2607 | inode->i_ctime.tv_nsec); |
| 2608 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2609 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2610 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2611 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2612 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2613 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2614 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2615 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2616 | } |
| 2617 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2618 | /* |
| 2619 | * copy everything in the in-memory inode into the btree. |
| 2620 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2621 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2622 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2623 | { |
| 2624 | struct btrfs_inode_item *inode_item; |
| 2625 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2626 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2627 | int ret; |
| 2628 | |
| 2629 | path = btrfs_alloc_path(); |
| 2630 | BUG_ON(!path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2631 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2632 | ret = btrfs_lookup_inode(trans, root, path, |
| 2633 | &BTRFS_I(inode)->location, 1); |
| 2634 | if (ret) { |
| 2635 | if (ret > 0) |
| 2636 | ret = -ENOENT; |
| 2637 | goto failed; |
| 2638 | } |
| 2639 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2640 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2641 | leaf = path->nodes[0]; |
| 2642 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2643 | struct btrfs_inode_item); |
| 2644 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2645 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2646 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2647 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2648 | ret = 0; |
| 2649 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2650 | btrfs_free_path(path); |
| 2651 | return ret; |
| 2652 | } |
| 2653 | |
| 2654 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2655 | /* |
| 2656 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2657 | * recovery code. It remove a link in a directory with a given name, and |
| 2658 | * also drops the back refs in the inode to the directory |
| 2659 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2660 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2661 | struct btrfs_root *root, |
| 2662 | struct inode *dir, struct inode *inode, |
| 2663 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2664 | { |
| 2665 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2666 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2667 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2668 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2669 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2670 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2671 | |
| 2672 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2673 | if (!path) { |
| 2674 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 2675 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2676 | } |
| 2677 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2678 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2679 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2680 | name, name_len, -1); |
| 2681 | if (IS_ERR(di)) { |
| 2682 | ret = PTR_ERR(di); |
| 2683 | goto err; |
| 2684 | } |
| 2685 | if (!di) { |
| 2686 | ret = -ENOENT; |
| 2687 | goto err; |
| 2688 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2689 | leaf = path->nodes[0]; |
| 2690 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2691 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2692 | if (ret) |
| 2693 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2694 | btrfs_release_path(root, path); |
| 2695 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2696 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2697 | inode->i_ino, |
| 2698 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2699 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2700 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2701 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2702 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2703 | goto err; |
| 2704 | } |
| 2705 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2706 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2707 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2708 | if (IS_ERR(di)) { |
| 2709 | ret = PTR_ERR(di); |
| 2710 | goto err; |
| 2711 | } |
| 2712 | if (!di) { |
| 2713 | ret = -ENOENT; |
| 2714 | goto err; |
| 2715 | } |
| 2716 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2717 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2718 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2719 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 2720 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2721 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2722 | |
| 2723 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2724 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 2725 | if (ret == -ENOENT) |
| 2726 | ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2727 | err: |
| 2728 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2729 | if (ret) |
| 2730 | goto out; |
| 2731 | |
| 2732 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2733 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2734 | btrfs_update_inode(trans, root, dir); |
| 2735 | btrfs_drop_nlink(inode); |
| 2736 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2737 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2738 | return ret; |
| 2739 | } |
| 2740 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2741 | /* helper to check if there is any shared block in the path */ |
| 2742 | static int check_path_shared(struct btrfs_root *root, |
| 2743 | struct btrfs_path *path) |
| 2744 | { |
| 2745 | struct extent_buffer *eb; |
| 2746 | int level; |
Dan Carpenter | 0e4dcbef | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 2747 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2748 | |
| 2749 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2750 | int ret; |
| 2751 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2752 | if (!path->nodes[level]) |
| 2753 | break; |
| 2754 | eb = path->nodes[level]; |
| 2755 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2756 | continue; |
| 2757 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2758 | &refs, NULL); |
| 2759 | if (refs > 1) |
| 2760 | return 1; |
| 2761 | } |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2762 | return 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | /* |
| 2766 | * helper to start transaction for unlink and rmdir. |
| 2767 | * |
| 2768 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2769 | * so in enospc case, we should make sure they will free space before |
| 2770 | * allowing them to use the global metadata reservation. |
| 2771 | */ |
| 2772 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2773 | struct dentry *dentry) |
| 2774 | { |
| 2775 | struct btrfs_trans_handle *trans; |
| 2776 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2777 | struct btrfs_path *path; |
| 2778 | struct btrfs_inode_ref *ref; |
| 2779 | struct btrfs_dir_item *di; |
| 2780 | struct inode *inode = dentry->d_inode; |
| 2781 | u64 index; |
| 2782 | int check_link = 1; |
| 2783 | int err = -ENOSPC; |
| 2784 | int ret; |
| 2785 | |
| 2786 | trans = btrfs_start_transaction(root, 10); |
| 2787 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 2788 | return trans; |
| 2789 | |
| 2790 | if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 2791 | return ERR_PTR(-ENOSPC); |
| 2792 | |
| 2793 | /* check if there is someone else holds reference */ |
| 2794 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 2795 | return ERR_PTR(-ENOSPC); |
| 2796 | |
| 2797 | if (atomic_read(&inode->i_count) > 2) |
| 2798 | return ERR_PTR(-ENOSPC); |
| 2799 | |
| 2800 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 2801 | return ERR_PTR(-ENOSPC); |
| 2802 | |
| 2803 | path = btrfs_alloc_path(); |
| 2804 | if (!path) { |
| 2805 | root->fs_info->enospc_unlink = 0; |
| 2806 | return ERR_PTR(-ENOMEM); |
| 2807 | } |
| 2808 | |
| 2809 | trans = btrfs_start_transaction(root, 0); |
| 2810 | if (IS_ERR(trans)) { |
| 2811 | btrfs_free_path(path); |
| 2812 | root->fs_info->enospc_unlink = 0; |
| 2813 | return trans; |
| 2814 | } |
| 2815 | |
| 2816 | path->skip_locking = 1; |
| 2817 | path->search_commit_root = 1; |
| 2818 | |
| 2819 | ret = btrfs_lookup_inode(trans, root, path, |
| 2820 | &BTRFS_I(dir)->location, 0); |
| 2821 | if (ret < 0) { |
| 2822 | err = ret; |
| 2823 | goto out; |
| 2824 | } |
| 2825 | if (ret == 0) { |
| 2826 | if (check_path_shared(root, path)) |
| 2827 | goto out; |
| 2828 | } else { |
| 2829 | check_link = 0; |
| 2830 | } |
| 2831 | btrfs_release_path(root, path); |
| 2832 | |
| 2833 | ret = btrfs_lookup_inode(trans, root, path, |
| 2834 | &BTRFS_I(inode)->location, 0); |
| 2835 | if (ret < 0) { |
| 2836 | err = ret; |
| 2837 | goto out; |
| 2838 | } |
| 2839 | if (ret == 0) { |
| 2840 | if (check_path_shared(root, path)) |
| 2841 | goto out; |
| 2842 | } else { |
| 2843 | check_link = 0; |
| 2844 | } |
| 2845 | btrfs_release_path(root, path); |
| 2846 | |
| 2847 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 2848 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 2849 | inode->i_ino, (u64)-1, 0); |
| 2850 | if (ret < 0) { |
| 2851 | err = ret; |
| 2852 | goto out; |
| 2853 | } |
| 2854 | BUG_ON(ret == 0); |
| 2855 | if (check_path_shared(root, path)) |
| 2856 | goto out; |
| 2857 | btrfs_release_path(root, path); |
| 2858 | } |
| 2859 | |
| 2860 | if (!check_link) { |
| 2861 | err = 0; |
| 2862 | goto out; |
| 2863 | } |
| 2864 | |
| 2865 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2866 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2867 | if (IS_ERR(di)) { |
| 2868 | err = PTR_ERR(di); |
| 2869 | goto out; |
| 2870 | } |
| 2871 | if (di) { |
| 2872 | if (check_path_shared(root, path)) |
| 2873 | goto out; |
| 2874 | } else { |
| 2875 | err = 0; |
| 2876 | goto out; |
| 2877 | } |
| 2878 | btrfs_release_path(root, path); |
| 2879 | |
| 2880 | ref = btrfs_lookup_inode_ref(trans, root, path, |
| 2881 | dentry->d_name.name, dentry->d_name.len, |
| 2882 | inode->i_ino, dir->i_ino, 0); |
| 2883 | if (IS_ERR(ref)) { |
| 2884 | err = PTR_ERR(ref); |
| 2885 | goto out; |
| 2886 | } |
| 2887 | BUG_ON(!ref); |
| 2888 | if (check_path_shared(root, path)) |
| 2889 | goto out; |
| 2890 | index = btrfs_inode_ref_index(path->nodes[0], ref); |
| 2891 | btrfs_release_path(root, path); |
| 2892 | |
| 2893 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index, |
| 2894 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2895 | if (IS_ERR(di)) { |
| 2896 | err = PTR_ERR(di); |
| 2897 | goto out; |
| 2898 | } |
| 2899 | BUG_ON(ret == -ENOENT); |
| 2900 | if (check_path_shared(root, path)) |
| 2901 | goto out; |
| 2902 | |
| 2903 | err = 0; |
| 2904 | out: |
| 2905 | btrfs_free_path(path); |
| 2906 | if (err) { |
| 2907 | btrfs_end_transaction(trans, root); |
| 2908 | root->fs_info->enospc_unlink = 0; |
| 2909 | return ERR_PTR(err); |
| 2910 | } |
| 2911 | |
| 2912 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 2913 | return trans; |
| 2914 | } |
| 2915 | |
| 2916 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 2917 | struct btrfs_root *root) |
| 2918 | { |
| 2919 | if (trans->block_rsv == &root->fs_info->global_block_rsv) { |
| 2920 | BUG_ON(!root->fs_info->enospc_unlink); |
| 2921 | root->fs_info->enospc_unlink = 0; |
| 2922 | } |
| 2923 | btrfs_end_transaction_throttle(trans, root); |
| 2924 | } |
| 2925 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2926 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2927 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2928 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2929 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2930 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2931 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2932 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2933 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2934 | trans = __unlink_start_trans(dir, dentry); |
| 2935 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2936 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2937 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2938 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2939 | |
| 2940 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2941 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2942 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2943 | dentry->d_name.name, dentry->d_name.len); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2944 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2945 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2946 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2947 | ret = btrfs_orphan_add(trans, inode); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2948 | BUG_ON(ret); |
| 2949 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2950 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2951 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2952 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2953 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2954 | return ret; |
| 2955 | } |
| 2956 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2957 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2958 | struct btrfs_root *root, |
| 2959 | struct inode *dir, u64 objectid, |
| 2960 | const char *name, int name_len) |
| 2961 | { |
| 2962 | struct btrfs_path *path; |
| 2963 | struct extent_buffer *leaf; |
| 2964 | struct btrfs_dir_item *di; |
| 2965 | struct btrfs_key key; |
| 2966 | u64 index; |
| 2967 | int ret; |
| 2968 | |
| 2969 | path = btrfs_alloc_path(); |
| 2970 | if (!path) |
| 2971 | return -ENOMEM; |
| 2972 | |
| 2973 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2974 | name, name_len, -1); |
| 2975 | BUG_ON(!di || IS_ERR(di)); |
| 2976 | |
| 2977 | leaf = path->nodes[0]; |
| 2978 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2979 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2980 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2981 | BUG_ON(ret); |
| 2982 | btrfs_release_path(root, path); |
| 2983 | |
| 2984 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2985 | objectid, root->root_key.objectid, |
| 2986 | dir->i_ino, &index, name, name_len); |
| 2987 | if (ret < 0) { |
| 2988 | BUG_ON(ret != -ENOENT); |
| 2989 | di = btrfs_search_dir_index_item(root, path, dir->i_ino, |
| 2990 | name, name_len); |
| 2991 | BUG_ON(!di || IS_ERR(di)); |
| 2992 | |
| 2993 | leaf = path->nodes[0]; |
| 2994 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2995 | btrfs_release_path(root, path); |
| 2996 | index = key.offset; |
| 2997 | } |
| 2998 | |
| 2999 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 3000 | index, name, name_len, -1); |
| 3001 | BUG_ON(!di || IS_ERR(di)); |
| 3002 | |
| 3003 | leaf = path->nodes[0]; |
| 3004 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3005 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3006 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 3007 | BUG_ON(ret); |
| 3008 | btrfs_release_path(root, path); |
| 3009 | |
| 3010 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 3011 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 3012 | ret = btrfs_update_inode(trans, root, dir); |
| 3013 | BUG_ON(ret); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3014 | |
| 3015 | btrfs_free_path(path); |
| 3016 | return 0; |
| 3017 | } |
| 3018 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3019 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3020 | { |
| 3021 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3022 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3023 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3024 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3025 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3026 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 3027 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3028 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3029 | return -ENOTEMPTY; |
| 3030 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3031 | trans = __unlink_start_trans(dir, dentry); |
| 3032 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3033 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3034 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3035 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3036 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3037 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 3038 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3039 | BTRFS_I(inode)->location.objectid, |
| 3040 | dentry->d_name.name, |
| 3041 | dentry->d_name.len); |
| 3042 | goto out; |
| 3043 | } |
| 3044 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3045 | err = btrfs_orphan_add(trans, inode); |
| 3046 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3047 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3048 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3049 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3050 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3051 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3052 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3053 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3054 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3055 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3056 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3057 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3058 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3059 | return err; |
| 3060 | } |
| 3061 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3062 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3063 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3064 | * when truncating bytes in a file, it is possible to avoid reading |
| 3065 | * the leaves that contain only checksum items. This can be the |
| 3066 | * majority of the IO required to delete a large file, but it must |
| 3067 | * be done carefully. |
| 3068 | * |
| 3069 | * The keys in the level just above the leaves are checked to make sure |
| 3070 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 3071 | * after the new size. |
| 3072 | * |
| 3073 | * Then the key for the next leaf is checked to make sure it also has |
| 3074 | * a checksum item for the same file. If it does, we know our target leaf |
| 3075 | * contains only checksum items, and it can be safely freed without reading |
| 3076 | * it. |
| 3077 | * |
| 3078 | * This is just an optimization targeted at large files. It may do |
| 3079 | * nothing. It will return 0 unless things went badly. |
| 3080 | */ |
| 3081 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 3082 | struct btrfs_root *root, |
| 3083 | struct btrfs_path *path, |
| 3084 | struct inode *inode, u64 new_size) |
| 3085 | { |
| 3086 | struct btrfs_key key; |
| 3087 | int ret; |
| 3088 | int nritems; |
| 3089 | struct btrfs_key found_key; |
| 3090 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3091 | struct btrfs_leaf_ref *ref; |
| 3092 | u64 leaf_gen; |
| 3093 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3094 | |
| 3095 | path->lowest_level = 1; |
| 3096 | key.objectid = inode->i_ino; |
| 3097 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 3098 | key.offset = new_size; |
| 3099 | again: |
| 3100 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3101 | if (ret < 0) |
| 3102 | goto out; |
| 3103 | |
| 3104 | if (path->nodes[1] == NULL) { |
| 3105 | ret = 0; |
| 3106 | goto out; |
| 3107 | } |
| 3108 | ret = 0; |
| 3109 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 3110 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 3111 | |
| 3112 | if (!nritems) |
| 3113 | goto out; |
| 3114 | |
| 3115 | if (path->slots[1] >= nritems) |
| 3116 | goto next_node; |
| 3117 | |
| 3118 | /* did we find a key greater than anything we want to delete? */ |
| 3119 | if (found_key.objectid > inode->i_ino || |
| 3120 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 3121 | goto out; |
| 3122 | |
| 3123 | /* we check the next key in the node to make sure the leave contains |
| 3124 | * only checksum items. This comparison doesn't work if our |
| 3125 | * leaf is the last one in the node |
| 3126 | */ |
| 3127 | if (path->slots[1] + 1 >= nritems) { |
| 3128 | next_node: |
| 3129 | /* search forward from the last key in the node, this |
| 3130 | * will bring us into the next node in the tree |
| 3131 | */ |
| 3132 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 3133 | |
| 3134 | /* unlikely, but we inc below, so check to be safe */ |
| 3135 | if (found_key.offset == (u64)-1) |
| 3136 | goto out; |
| 3137 | |
| 3138 | /* search_forward needs a path with locks held, do the |
| 3139 | * search again for the original key. It is possible |
| 3140 | * this will race with a balance and return a path that |
| 3141 | * we could modify, but this drop is just an optimization |
| 3142 | * and is allowed to miss some leaves. |
| 3143 | */ |
| 3144 | btrfs_release_path(root, path); |
| 3145 | found_key.offset++; |
| 3146 | |
| 3147 | /* setup a max key for search_forward */ |
| 3148 | other_key.offset = (u64)-1; |
| 3149 | other_key.type = key.type; |
| 3150 | other_key.objectid = key.objectid; |
| 3151 | |
| 3152 | path->keep_locks = 1; |
| 3153 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 3154 | path, 0, 0); |
| 3155 | path->keep_locks = 0; |
| 3156 | if (ret || found_key.objectid != key.objectid || |
| 3157 | found_key.type != key.type) { |
| 3158 | ret = 0; |
| 3159 | goto out; |
| 3160 | } |
| 3161 | |
| 3162 | key.offset = found_key.offset; |
| 3163 | btrfs_release_path(root, path); |
| 3164 | cond_resched(); |
| 3165 | goto again; |
| 3166 | } |
| 3167 | |
| 3168 | /* we know there's one more slot after us in the tree, |
| 3169 | * read that key so we can verify it is also a checksum item |
| 3170 | */ |
| 3171 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 3172 | |
| 3173 | if (found_key.objectid < inode->i_ino) |
| 3174 | goto next_key; |
| 3175 | |
| 3176 | if (found_key.type != key.type || found_key.offset < new_size) |
| 3177 | goto next_key; |
| 3178 | |
| 3179 | /* |
| 3180 | * if the key for the next leaf isn't a csum key from this objectid, |
| 3181 | * we can't be sure there aren't good items inside this leaf. |
| 3182 | * Bail out |
| 3183 | */ |
| 3184 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 3185 | goto out; |
| 3186 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3187 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 3188 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3189 | /* |
| 3190 | * it is safe to delete this leaf, it contains only |
| 3191 | * csum items from this inode at an offset >= new_size |
| 3192 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3193 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3194 | BUG_ON(ret); |
| 3195 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3196 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 3197 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 3198 | if (ref) { |
| 3199 | ref->root_gen = root->root_key.offset; |
| 3200 | ref->bytenr = leaf_start; |
| 3201 | ref->owner = 0; |
| 3202 | ref->generation = leaf_gen; |
| 3203 | ref->nritems = 0; |
| 3204 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3205 | btrfs_sort_leaf_ref(ref); |
| 3206 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3207 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 3208 | WARN_ON(ret); |
| 3209 | btrfs_free_leaf_ref(root, ref); |
| 3210 | } else { |
| 3211 | WARN_ON(1); |
| 3212 | } |
| 3213 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3214 | next_key: |
| 3215 | btrfs_release_path(root, path); |
| 3216 | |
| 3217 | if (other_key.objectid == inode->i_ino && |
| 3218 | other_key.type == key.type && other_key.offset > key.offset) { |
| 3219 | key.offset = other_key.offset; |
| 3220 | cond_resched(); |
| 3221 | goto again; |
| 3222 | } |
| 3223 | ret = 0; |
| 3224 | out: |
| 3225 | /* fixup any changes we've made to the path */ |
| 3226 | path->lowest_level = 0; |
| 3227 | path->keep_locks = 0; |
| 3228 | btrfs_release_path(root, path); |
| 3229 | return ret; |
| 3230 | } |
| 3231 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3232 | #endif |
| 3233 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3234 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3235 | * this can truncate away extent items, csum items and directory items. |
| 3236 | * 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] | 3237 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3238 | * |
| 3239 | * csum items that cross the new i_size are truncated to the new size |
| 3240 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3241 | * |
| 3242 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3243 | * 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] | 3244 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3245 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3246 | struct btrfs_root *root, |
| 3247 | struct inode *inode, |
| 3248 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3249 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3250 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3251 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3252 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3253 | struct btrfs_key key; |
| 3254 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3255 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3256 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3257 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3258 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3259 | u64 mask = root->sectorsize - 1; |
| 3260 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3261 | int found_extent; |
| 3262 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3263 | int pending_del_nr = 0; |
| 3264 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3265 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3266 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3267 | int ret; |
| 3268 | int err = 0; |
| 3269 | |
| 3270 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3271 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3272 | if (root->ref_cows || root == root->fs_info->tree_root) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3273 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3274 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3275 | path = btrfs_alloc_path(); |
| 3276 | BUG_ON(!path); |
Julia Lawall | 33c17ad | 2009-07-22 16:49:01 -0400 | [diff] [blame] | 3277 | path->reada = -1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3278 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3279 | key.objectid = inode->i_ino; |
| 3280 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3281 | key.type = (u8)-1; |
| 3282 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3283 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3284 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3285 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3286 | if (ret < 0) { |
| 3287 | err = ret; |
| 3288 | goto out; |
| 3289 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3290 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3291 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3292 | /* there are no items in the tree for us to truncate, we're |
| 3293 | * done |
| 3294 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3295 | if (path->slots[0] == 0) |
| 3296 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3297 | path->slots[0]--; |
| 3298 | } |
| 3299 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3300 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3301 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3302 | leaf = path->nodes[0]; |
| 3303 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3304 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3305 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3306 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3307 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3308 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3309 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3310 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3311 | break; |
| 3312 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3313 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3314 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3315 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3316 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3317 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3318 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 3319 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 3320 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 3321 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3322 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3323 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3324 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3325 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3326 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3327 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3328 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3329 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3330 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3331 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3332 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3333 | } else { |
| 3334 | if (item_end < new_size) |
| 3335 | break; |
| 3336 | if (found_key.offset >= new_size) |
| 3337 | del_item = 1; |
| 3338 | else |
| 3339 | del_item = 0; |
| 3340 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3341 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3342 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3343 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3344 | goto delete; |
| 3345 | |
| 3346 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3347 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3348 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3349 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3350 | u64 orig_num_bytes = |
| 3351 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3352 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3353 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3354 | extent_num_bytes = extent_num_bytes & |
| 3355 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3356 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3357 | extent_num_bytes); |
| 3358 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3359 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3360 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3361 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3362 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3363 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3364 | extent_num_bytes = |
| 3365 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3366 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3367 | extent_offset = found_key.offset - |
| 3368 | btrfs_file_extent_offset(leaf, fi); |
| 3369 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3370 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3371 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3372 | if (extent_start != 0) { |
| 3373 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3374 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3375 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3376 | } |
| 3377 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3378 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3379 | /* |
| 3380 | * we can't truncate inline items that have had |
| 3381 | * special encodings |
| 3382 | */ |
| 3383 | if (!del_item && |
| 3384 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3385 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3386 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3387 | u32 size = new_size - found_key.offset; |
| 3388 | |
| 3389 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3390 | inode_sub_bytes(inode, item_end + 1 - |
| 3391 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3392 | } |
| 3393 | size = |
| 3394 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3395 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3396 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3397 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3398 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3399 | inode_sub_bytes(inode, item_end + 1 - |
| 3400 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3401 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3402 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3403 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3404 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3405 | if (!pending_del_nr) { |
| 3406 | /* no pending yet, add ourselves */ |
| 3407 | pending_del_slot = path->slots[0]; |
| 3408 | pending_del_nr = 1; |
| 3409 | } else if (pending_del_nr && |
| 3410 | path->slots[0] + 1 == pending_del_slot) { |
| 3411 | /* hop on the pending chunk */ |
| 3412 | pending_del_nr++; |
| 3413 | pending_del_slot = path->slots[0]; |
| 3414 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3415 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3416 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3417 | } else { |
| 3418 | break; |
| 3419 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3420 | if (found_extent && (root->ref_cows || |
| 3421 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3422 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3423 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3424 | extent_num_bytes, 0, |
| 3425 | btrfs_header_owner(leaf), |
| 3426 | inode->i_ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3427 | BUG_ON(ret); |
| 3428 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3429 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3430 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3431 | break; |
| 3432 | |
| 3433 | if (path->slots[0] == 0 || |
| 3434 | path->slots[0] != pending_del_slot) { |
| 3435 | if (root->ref_cows) { |
| 3436 | err = -EAGAIN; |
| 3437 | goto out; |
| 3438 | } |
| 3439 | if (pending_del_nr) { |
| 3440 | ret = btrfs_del_items(trans, root, path, |
| 3441 | pending_del_slot, |
| 3442 | pending_del_nr); |
| 3443 | BUG_ON(ret); |
| 3444 | pending_del_nr = 0; |
| 3445 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3446 | btrfs_release_path(root, path); |
| 3447 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3448 | } else { |
| 3449 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3450 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3451 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3452 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3453 | if (pending_del_nr) { |
| 3454 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3455 | pending_del_nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3456 | BUG_ON(ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3457 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3458 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3459 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3460 | } |
| 3461 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3462 | /* |
| 3463 | * taken from block_truncate_page, but does cow as it zeros out |
| 3464 | * any bytes left in the last page in the file. |
| 3465 | */ |
| 3466 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3467 | { |
| 3468 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3469 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3470 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3471 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3472 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3473 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3474 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3475 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3476 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3477 | struct page *page; |
| 3478 | int ret = 0; |
| 3479 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3480 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3481 | |
| 3482 | if ((offset & (blocksize - 1)) == 0) |
| 3483 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3484 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3485 | if (ret) |
| 3486 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3487 | |
| 3488 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3489 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3490 | page = grab_cache_page(mapping, index); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3491 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3492 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3493 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3494 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3495 | |
| 3496 | page_start = page_offset(page); |
| 3497 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3498 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3499 | if (!PageUptodate(page)) { |
| 3500 | ret = btrfs_readpage(NULL, page); |
| 3501 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3502 | if (page->mapping != mapping) { |
| 3503 | unlock_page(page); |
| 3504 | page_cache_release(page); |
| 3505 | goto again; |
| 3506 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3507 | if (!PageUptodate(page)) { |
| 3508 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3509 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3510 | } |
| 3511 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3512 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3513 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3514 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 3515 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3516 | set_page_extent_mapped(page); |
| 3517 | |
| 3518 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3519 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3520 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3521 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3522 | unlock_page(page); |
| 3523 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3524 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3525 | btrfs_put_ordered_extent(ordered); |
| 3526 | goto again; |
| 3527 | } |
| 3528 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3529 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3530 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3531 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3532 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3533 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3534 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3535 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3536 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3537 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3538 | goto out_unlock; |
| 3539 | } |
| 3540 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3541 | ret = 0; |
| 3542 | if (offset != PAGE_CACHE_SIZE) { |
| 3543 | kaddr = kmap(page); |
| 3544 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3545 | flush_dcache_page(page); |
| 3546 | kunmap(page); |
| 3547 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3548 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3549 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3550 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3551 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3552 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3553 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3554 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3555 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3556 | unlock_page(page); |
| 3557 | page_cache_release(page); |
| 3558 | out: |
| 3559 | return ret; |
| 3560 | } |
| 3561 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 3562 | /* |
| 3563 | * This function puts in dummy file extents for the area we're creating a hole |
| 3564 | * for. So if we are truncating this file to a larger size we need to insert |
| 3565 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 3566 | * the range between oldsize and size |
| 3567 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3568 | 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] | 3569 | { |
| 3570 | struct btrfs_trans_handle *trans; |
| 3571 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3572 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3573 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3574 | struct extent_state *cached_state = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3575 | u64 mask = root->sectorsize - 1; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3576 | u64 hole_start = (oldsize + mask) & ~mask; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3577 | u64 block_end = (size + mask) & ~mask; |
| 3578 | u64 last_byte; |
| 3579 | u64 cur_offset; |
| 3580 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3581 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3582 | |
| 3583 | if (size <= hole_start) |
| 3584 | return 0; |
| 3585 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3586 | while (1) { |
| 3587 | struct btrfs_ordered_extent *ordered; |
| 3588 | btrfs_wait_ordered_range(inode, hole_start, |
| 3589 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3590 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
| 3591 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3592 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3593 | if (!ordered) |
| 3594 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3595 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3596 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3597 | btrfs_put_ordered_extent(ordered); |
| 3598 | } |
| 3599 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3600 | cur_offset = hole_start; |
| 3601 | while (1) { |
| 3602 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3603 | block_end - cur_offset, 0); |
| 3604 | BUG_ON(IS_ERR(em) || !em); |
| 3605 | last_byte = min(extent_map_end(em), block_end); |
| 3606 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3607 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3608 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3609 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3610 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3611 | trans = btrfs_start_transaction(root, 2); |
| 3612 | if (IS_ERR(trans)) { |
| 3613 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3614 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3615 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3616 | btrfs_set_trans_block_group(trans, inode); |
| 3617 | |
| 3618 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3619 | cur_offset + hole_size, |
| 3620 | &hint_byte, 1); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3621 | if (err) |
| 3622 | break; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3623 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3624 | err = btrfs_insert_file_extent(trans, root, |
| 3625 | inode->i_ino, cur_offset, 0, |
| 3626 | 0, hole_size, 0, hole_size, |
| 3627 | 0, 0, 0); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3628 | if (err) |
| 3629 | break; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3630 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3631 | btrfs_drop_extent_cache(inode, hole_start, |
| 3632 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3633 | |
| 3634 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3635 | } |
| 3636 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3637 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3638 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3639 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3640 | break; |
| 3641 | } |
| 3642 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3643 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3644 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3645 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3646 | return err; |
| 3647 | } |
| 3648 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3649 | static int btrfs_setsize(struct inode *inode, loff_t newsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3650 | { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3651 | loff_t oldsize = i_size_read(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3652 | int ret; |
| 3653 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3654 | if (newsize == oldsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3655 | return 0; |
| 3656 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3657 | if (newsize > oldsize) { |
| 3658 | i_size_write(inode, newsize); |
| 3659 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 3660 | truncate_pagecache(inode, oldsize, newsize); |
| 3661 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3662 | if (ret) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3663 | btrfs_setsize(inode, oldsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3664 | return ret; |
| 3665 | } |
| 3666 | |
Josef Bacik | 930f028 | 2011-03-04 14:41:41 -0500 | [diff] [blame] | 3667 | mark_inode_dirty(inode); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3668 | } else { |
| 3669 | |
| 3670 | /* |
| 3671 | * We're truncating a file that used to have good data down to |
| 3672 | * zero. Make sure it gets into the ordered flush list so that |
| 3673 | * any new writes get down to disk quickly. |
| 3674 | */ |
| 3675 | if (newsize == 0) |
| 3676 | BTRFS_I(inode)->ordered_data_close = 1; |
| 3677 | |
| 3678 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3679 | truncate_setsize(inode, newsize); |
| 3680 | ret = btrfs_truncate(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3683 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3686 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3687 | { |
| 3688 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3689 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3690 | int err; |
| 3691 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3692 | if (btrfs_root_readonly(root)) |
| 3693 | return -EROFS; |
| 3694 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3695 | err = inode_change_ok(inode, attr); |
| 3696 | if (err) |
| 3697 | return err; |
| 3698 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3699 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3700 | err = btrfs_setsize(inode, attr->ia_size); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3701 | if (err) |
| 3702 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3703 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3704 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3705 | if (attr->ia_valid) { |
| 3706 | setattr_copy(inode, attr); |
| 3707 | mark_inode_dirty(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3708 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3709 | if (attr->ia_valid & ATTR_MODE) |
| 3710 | err = btrfs_acl_chmod(inode); |
| 3711 | } |
| 3712 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3713 | return err; |
| 3714 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3715 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3716 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3717 | { |
| 3718 | struct btrfs_trans_handle *trans; |
| 3719 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3720 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3721 | int ret; |
| 3722 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame^] | 3723 | trace_btrfs_inode_evict(inode); |
| 3724 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3725 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3726 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
| 3727 | root == root->fs_info->tree_root)) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3728 | goto no_delete; |
| 3729 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3730 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3731 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3732 | goto no_delete; |
| 3733 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3734 | /* 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] | 3735 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3736 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3737 | if (root->fs_info->log_root_recovering) { |
| 3738 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3739 | goto no_delete; |
| 3740 | } |
| 3741 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3742 | if (inode->i_nlink > 0) { |
| 3743 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3744 | goto no_delete; |
| 3745 | } |
| 3746 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3747 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3748 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3749 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3750 | trans = btrfs_start_transaction(root, 0); |
| 3751 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3752 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3753 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3754 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3755 | ret = btrfs_block_rsv_check(trans, root, |
| 3756 | root->orphan_block_rsv, 0, 5); |
| 3757 | if (ret) { |
| 3758 | BUG_ON(ret != -EAGAIN); |
| 3759 | ret = btrfs_commit_transaction(trans, root); |
| 3760 | BUG_ON(ret); |
| 3761 | continue; |
| 3762 | } |
| 3763 | |
| 3764 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3765 | if (ret != -EAGAIN) |
| 3766 | break; |
| 3767 | |
| 3768 | nr = trans->blocks_used; |
| 3769 | btrfs_end_transaction(trans, root); |
| 3770 | trans = NULL; |
| 3771 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3772 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3773 | } |
| 3774 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3775 | if (ret == 0) { |
| 3776 | ret = btrfs_orphan_del(trans, inode); |
| 3777 | BUG_ON(ret); |
| 3778 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3779 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3780 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3781 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3782 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3783 | no_delete: |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3784 | end_writeback(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3785 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3786 | } |
| 3787 | |
| 3788 | /* |
| 3789 | * this returns the key found in the dir entry in the location pointer. |
| 3790 | * If no dir entries were found, location->objectid is 0. |
| 3791 | */ |
| 3792 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3793 | struct btrfs_key *location) |
| 3794 | { |
| 3795 | const char *name = dentry->d_name.name; |
| 3796 | int namelen = dentry->d_name.len; |
| 3797 | struct btrfs_dir_item *di; |
| 3798 | struct btrfs_path *path; |
| 3799 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3800 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3801 | |
| 3802 | path = btrfs_alloc_path(); |
| 3803 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3804 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3805 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 3806 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3807 | if (IS_ERR(di)) |
| 3808 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3809 | |
| 3810 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3811 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3812 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3813 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3814 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3815 | btrfs_free_path(path); |
| 3816 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3817 | out_err: |
| 3818 | location->objectid = 0; |
| 3819 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3820 | } |
| 3821 | |
| 3822 | /* |
| 3823 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3824 | * needs to be changed to reflect the root directory of the tree root. This |
| 3825 | * is kind of like crossing a mount point. |
| 3826 | */ |
| 3827 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3828 | struct inode *dir, |
| 3829 | struct dentry *dentry, |
| 3830 | struct btrfs_key *location, |
| 3831 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3832 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3833 | struct btrfs_path *path; |
| 3834 | struct btrfs_root *new_root; |
| 3835 | struct btrfs_root_ref *ref; |
| 3836 | struct extent_buffer *leaf; |
| 3837 | int ret; |
| 3838 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3839 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3840 | path = btrfs_alloc_path(); |
| 3841 | if (!path) { |
| 3842 | err = -ENOMEM; |
| 3843 | goto out; |
| 3844 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3845 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3846 | err = -ENOENT; |
| 3847 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3848 | BTRFS_I(dir)->root->root_key.objectid, |
| 3849 | location->objectid); |
| 3850 | if (ret) { |
| 3851 | if (ret < 0) |
| 3852 | err = ret; |
| 3853 | goto out; |
| 3854 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3855 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3856 | leaf = path->nodes[0]; |
| 3857 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 3858 | if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || |
| 3859 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3860 | goto out; |
| 3861 | |
| 3862 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3863 | (unsigned long)(ref + 1), |
| 3864 | dentry->d_name.len); |
| 3865 | if (ret) |
| 3866 | goto out; |
| 3867 | |
| 3868 | btrfs_release_path(root->fs_info->tree_root, path); |
| 3869 | |
| 3870 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3871 | if (IS_ERR(new_root)) { |
| 3872 | err = PTR_ERR(new_root); |
| 3873 | goto out; |
| 3874 | } |
| 3875 | |
| 3876 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3877 | err = -ENOENT; |
| 3878 | goto out; |
| 3879 | } |
| 3880 | |
| 3881 | *sub_root = new_root; |
| 3882 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3883 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3884 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3885 | err = 0; |
| 3886 | out: |
| 3887 | btrfs_free_path(path); |
| 3888 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3889 | } |
| 3890 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3891 | static void inode_tree_add(struct inode *inode) |
| 3892 | { |
| 3893 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3894 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3895 | struct rb_node **p; |
| 3896 | struct rb_node *parent; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3897 | again: |
| 3898 | p = &root->inode_tree.rb_node; |
| 3899 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3900 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 3901 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3902 | return; |
| 3903 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3904 | spin_lock(&root->inode_lock); |
| 3905 | while (*p) { |
| 3906 | parent = *p; |
| 3907 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3908 | |
| 3909 | if (inode->i_ino < entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3910 | p = &parent->rb_left; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3911 | else if (inode->i_ino > entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3912 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3913 | else { |
| 3914 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 3915 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3916 | rb_erase(parent, &root->inode_tree); |
| 3917 | RB_CLEAR_NODE(parent); |
| 3918 | spin_unlock(&root->inode_lock); |
| 3919 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3920 | } |
| 3921 | } |
| 3922 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3923 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3924 | spin_unlock(&root->inode_lock); |
| 3925 | } |
| 3926 | |
| 3927 | static void inode_tree_del(struct inode *inode) |
| 3928 | { |
| 3929 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3930 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3931 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3932 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3933 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3934 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3935 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3936 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3937 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3938 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3939 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3940 | /* |
| 3941 | * Free space cache has inodes in the tree root, but the tree root has a |
| 3942 | * root_refs of 0, so this could end up dropping the tree root as a |
| 3943 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 3944 | * make sure we don't drop it. |
| 3945 | */ |
| 3946 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 3947 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3948 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3949 | spin_lock(&root->inode_lock); |
| 3950 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3951 | spin_unlock(&root->inode_lock); |
| 3952 | if (empty) |
| 3953 | btrfs_add_dead_root(root); |
| 3954 | } |
| 3955 | } |
| 3956 | |
| 3957 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 3958 | { |
| 3959 | struct rb_node *node; |
| 3960 | struct rb_node *prev; |
| 3961 | struct btrfs_inode *entry; |
| 3962 | struct inode *inode; |
| 3963 | u64 objectid = 0; |
| 3964 | |
| 3965 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3966 | |
| 3967 | spin_lock(&root->inode_lock); |
| 3968 | again: |
| 3969 | node = root->inode_tree.rb_node; |
| 3970 | prev = NULL; |
| 3971 | while (node) { |
| 3972 | prev = node; |
| 3973 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3974 | |
| 3975 | if (objectid < entry->vfs_inode.i_ino) |
| 3976 | node = node->rb_left; |
| 3977 | else if (objectid > entry->vfs_inode.i_ino) |
| 3978 | node = node->rb_right; |
| 3979 | else |
| 3980 | break; |
| 3981 | } |
| 3982 | if (!node) { |
| 3983 | while (prev) { |
| 3984 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
| 3985 | if (objectid <= entry->vfs_inode.i_ino) { |
| 3986 | node = prev; |
| 3987 | break; |
| 3988 | } |
| 3989 | prev = rb_next(prev); |
| 3990 | } |
| 3991 | } |
| 3992 | while (node) { |
| 3993 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3994 | objectid = entry->vfs_inode.i_ino + 1; |
| 3995 | inode = igrab(&entry->vfs_inode); |
| 3996 | if (inode) { |
| 3997 | spin_unlock(&root->inode_lock); |
| 3998 | if (atomic_read(&inode->i_count) > 1) |
| 3999 | d_prune_aliases(inode); |
| 4000 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4001 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4002 | * the inode cache when its usage count |
| 4003 | * hits zero. |
| 4004 | */ |
| 4005 | iput(inode); |
| 4006 | cond_resched(); |
| 4007 | spin_lock(&root->inode_lock); |
| 4008 | goto again; |
| 4009 | } |
| 4010 | |
| 4011 | if (cond_resched_lock(&root->inode_lock)) |
| 4012 | goto again; |
| 4013 | |
| 4014 | node = rb_next(node); |
| 4015 | } |
| 4016 | spin_unlock(&root->inode_lock); |
| 4017 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4018 | } |
| 4019 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4020 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4021 | { |
| 4022 | struct btrfs_iget_args *args = p; |
| 4023 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4024 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4025 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4026 | return 0; |
| 4027 | } |
| 4028 | |
| 4029 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4030 | { |
| 4031 | struct btrfs_iget_args *args = opaque; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4032 | return args->ino == inode->i_ino && |
| 4033 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4034 | } |
| 4035 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4036 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4037 | u64 objectid, |
| 4038 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4039 | { |
| 4040 | struct inode *inode; |
| 4041 | struct btrfs_iget_args args; |
| 4042 | args.ino = objectid; |
| 4043 | args.root = root; |
| 4044 | |
| 4045 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 4046 | btrfs_init_locked_inode, |
| 4047 | (void *)&args); |
| 4048 | return inode; |
| 4049 | } |
| 4050 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4051 | /* Get an inode object given its location and corresponding root. |
| 4052 | * Returns in *is_new if the inode was read from disk |
| 4053 | */ |
| 4054 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4055 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4056 | { |
| 4057 | struct inode *inode; |
| 4058 | |
| 4059 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4060 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4061 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4062 | |
| 4063 | if (inode->i_state & I_NEW) { |
| 4064 | BTRFS_I(inode)->root = root; |
| 4065 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4066 | btrfs_read_locked_inode(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4067 | |
| 4068 | inode_tree_add(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4069 | unlock_new_inode(inode); |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4070 | if (new) |
| 4071 | *new = 1; |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | return inode; |
| 4075 | } |
| 4076 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4077 | static struct inode *new_simple_dir(struct super_block *s, |
| 4078 | struct btrfs_key *key, |
| 4079 | struct btrfs_root *root) |
| 4080 | { |
| 4081 | struct inode *inode = new_inode(s); |
| 4082 | |
| 4083 | if (!inode) |
| 4084 | return ERR_PTR(-ENOMEM); |
| 4085 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4086 | BTRFS_I(inode)->root = root; |
| 4087 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 4088 | BTRFS_I(inode)->dummy_inode = 1; |
| 4089 | |
| 4090 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 4091 | inode->i_op = &simple_dir_inode_operations; |
| 4092 | inode->i_fop = &simple_dir_operations; |
| 4093 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 4094 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 4095 | |
| 4096 | return inode; |
| 4097 | } |
| 4098 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4099 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4100 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4101 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4102 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4103 | struct btrfs_root *sub_root = root; |
| 4104 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4105 | int index; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4106 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4107 | |
| 4108 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 4109 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4110 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4111 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4112 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4113 | if (ret < 0) |
| 4114 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4115 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4116 | if (location.objectid == 0) |
| 4117 | return NULL; |
| 4118 | |
| 4119 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4120 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4121 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4122 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4123 | |
| 4124 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4125 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4126 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4127 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4128 | &location, &sub_root); |
| 4129 | if (ret < 0) { |
| 4130 | if (ret != -ENOENT) |
| 4131 | inode = ERR_PTR(ret); |
| 4132 | else |
| 4133 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4134 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4135 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4136 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4137 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4138 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 4139 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4140 | down_read(&root->fs_info->cleanup_work_sem); |
| 4141 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4142 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4143 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4144 | if (ret) |
| 4145 | inode = ERR_PTR(ret); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4148 | return inode; |
| 4149 | } |
| 4150 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 4151 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4152 | { |
| 4153 | struct btrfs_root *root; |
| 4154 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4155 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 4156 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4157 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4158 | if (dentry->d_inode) { |
| 4159 | root = BTRFS_I(dentry->d_inode)->root; |
| 4160 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4161 | return 1; |
| 4162 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4163 | return 0; |
| 4164 | } |
| 4165 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4166 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 4167 | struct nameidata *nd) |
| 4168 | { |
| 4169 | struct inode *inode; |
| 4170 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4171 | inode = btrfs_lookup_dentry(dir, dentry); |
| 4172 | if (IS_ERR(inode)) |
| 4173 | return ERR_CAST(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4174 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4175 | return d_splice_alias(inode, dentry); |
| 4176 | } |
| 4177 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4178 | static unsigned char btrfs_filetype_table[] = { |
| 4179 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 4180 | }; |
| 4181 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 4182 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 4183 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4184 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4185 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4186 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4187 | struct btrfs_item *item; |
| 4188 | struct btrfs_dir_item *di; |
| 4189 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4190 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4191 | struct btrfs_path *path; |
| 4192 | int ret; |
| 4193 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4194 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4195 | int slot; |
| 4196 | int advance; |
| 4197 | unsigned char d_type; |
| 4198 | int over = 0; |
| 4199 | u32 di_cur; |
| 4200 | u32 di_total; |
| 4201 | u32 di_len; |
| 4202 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4203 | char tmp_name[32]; |
| 4204 | char *name_ptr; |
| 4205 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4206 | |
| 4207 | /* FIXME, use a real flag for deciding about the key type */ |
| 4208 | if (root->fs_info->tree_root == root) |
| 4209 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4210 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4211 | /* special case for "." */ |
| 4212 | if (filp->f_pos == 0) { |
| 4213 | over = filldir(dirent, ".", 1, |
| 4214 | 1, inode->i_ino, |
| 4215 | DT_DIR); |
| 4216 | if (over) |
| 4217 | return 0; |
| 4218 | filp->f_pos = 1; |
| 4219 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4220 | /* special case for .., just use the back ref */ |
| 4221 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4222 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4223 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4224 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4225 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4226 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4227 | filp->f_pos = 2; |
| 4228 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4229 | path = btrfs_alloc_path(); |
| 4230 | path->reada = 2; |
| 4231 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4232 | btrfs_set_key_type(&key, key_type); |
| 4233 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4234 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4235 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4236 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4237 | if (ret < 0) |
| 4238 | goto err; |
| 4239 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4240 | |
| 4241 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4242 | leaf = path->nodes[0]; |
| 4243 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4244 | slot = path->slots[0]; |
| 4245 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4246 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4247 | ret = btrfs_next_leaf(root, path); |
| 4248 | if (ret) |
| 4249 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4250 | leaf = path->nodes[0]; |
| 4251 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4252 | slot = path->slots[0]; |
| 4253 | } else { |
| 4254 | slot++; |
| 4255 | path->slots[0]++; |
| 4256 | } |
| 4257 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4258 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4259 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4260 | item = btrfs_item_nr(leaf, slot); |
| 4261 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4262 | |
| 4263 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4264 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4265 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4266 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4267 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4268 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4269 | |
| 4270 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4271 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4272 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 4273 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4274 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4275 | |
| 4276 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4277 | struct btrfs_key location; |
| 4278 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 4279 | if (verify_dir_item(root, leaf, di)) |
| 4280 | break; |
| 4281 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4282 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4283 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4284 | name_ptr = tmp_name; |
| 4285 | } else { |
| 4286 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4287 | if (!name_ptr) { |
| 4288 | ret = -ENOMEM; |
| 4289 | goto err; |
| 4290 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4291 | } |
| 4292 | read_extent_buffer(leaf, name_ptr, |
| 4293 | (unsigned long)(di + 1), name_len); |
| 4294 | |
| 4295 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4296 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4297 | |
| 4298 | /* is this a reference to our own snapshot? If so |
| 4299 | * skip it |
| 4300 | */ |
| 4301 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4302 | location.objectid == root->root_key.objectid) { |
| 4303 | over = 0; |
| 4304 | goto skip; |
| 4305 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4306 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4307 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4308 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4309 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4310 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4311 | if (name_ptr != tmp_name) |
| 4312 | kfree(name_ptr); |
| 4313 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4314 | if (over) |
| 4315 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4316 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4317 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4318 | di_cur += di_len; |
| 4319 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4320 | } |
| 4321 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4322 | |
| 4323 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4324 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4325 | /* |
| 4326 | * 32-bit glibc will use getdents64, but then strtol - |
| 4327 | * so the last number we can serve is this. |
| 4328 | */ |
| 4329 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4330 | else |
| 4331 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4332 | nopos: |
| 4333 | ret = 0; |
| 4334 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4335 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4336 | return ret; |
| 4337 | } |
| 4338 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4339 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4340 | { |
| 4341 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4342 | struct btrfs_trans_handle *trans; |
| 4343 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4344 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4345 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4346 | if (BTRFS_I(inode)->dummy_inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4347 | return 0; |
| 4348 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4349 | smp_mb(); |
| 4350 | nolock = (root->fs_info->closing && root == root->fs_info->tree_root); |
| 4351 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4352 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4353 | if (nolock) |
| 4354 | trans = btrfs_join_transaction_nolock(root, 1); |
| 4355 | else |
| 4356 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4357 | if (IS_ERR(trans)) |
| 4358 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4359 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4360 | if (nolock) |
| 4361 | ret = btrfs_end_transaction_nolock(trans, root); |
| 4362 | else |
| 4363 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4364 | } |
| 4365 | return ret; |
| 4366 | } |
| 4367 | |
| 4368 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4369 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4370 | * inode changes. But, it is most likely to find the inode in cache. |
| 4371 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4372 | * to keep or drop this code. |
| 4373 | */ |
| 4374 | void btrfs_dirty_inode(struct inode *inode) |
| 4375 | { |
| 4376 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4377 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4378 | int ret; |
| 4379 | |
| 4380 | if (BTRFS_I(inode)->dummy_inode) |
| 4381 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4382 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4383 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4384 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4385 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4386 | |
| 4387 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4388 | if (ret && ret == -ENOSPC) { |
| 4389 | /* whoops, lets try again with the full transaction */ |
| 4390 | btrfs_end_transaction(trans, root); |
| 4391 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4392 | if (IS_ERR(trans)) { |
| 4393 | if (printk_ratelimit()) { |
| 4394 | printk(KERN_ERR "btrfs: fail to " |
| 4395 | "dirty inode %lu error %ld\n", |
| 4396 | inode->i_ino, PTR_ERR(trans)); |
| 4397 | } |
| 4398 | return; |
| 4399 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4400 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4401 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4402 | ret = btrfs_update_inode(trans, root, inode); |
| 4403 | if (ret) { |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4404 | if (printk_ratelimit()) { |
| 4405 | printk(KERN_ERR "btrfs: fail to " |
| 4406 | "dirty inode %lu error %d\n", |
| 4407 | inode->i_ino, ret); |
| 4408 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4409 | } |
| 4410 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4411 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4412 | } |
| 4413 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4414 | /* |
| 4415 | * find the highest existing sequence number in a directory |
| 4416 | * and then set the in-memory index_cnt variable to reflect |
| 4417 | * free sequence numbers |
| 4418 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4419 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4420 | { |
| 4421 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4422 | struct btrfs_key key, found_key; |
| 4423 | struct btrfs_path *path; |
| 4424 | struct extent_buffer *leaf; |
| 4425 | int ret; |
| 4426 | |
| 4427 | key.objectid = inode->i_ino; |
| 4428 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4429 | key.offset = (u64)-1; |
| 4430 | |
| 4431 | path = btrfs_alloc_path(); |
| 4432 | if (!path) |
| 4433 | return -ENOMEM; |
| 4434 | |
| 4435 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4436 | if (ret < 0) |
| 4437 | goto out; |
| 4438 | /* FIXME: we should be able to handle this */ |
| 4439 | if (ret == 0) |
| 4440 | goto out; |
| 4441 | ret = 0; |
| 4442 | |
| 4443 | /* |
| 4444 | * MAGIC NUMBER EXPLANATION: |
| 4445 | * since we search a directory based on f_pos we have to start at 2 |
| 4446 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4447 | * else has to start at 2 |
| 4448 | */ |
| 4449 | if (path->slots[0] == 0) { |
| 4450 | BTRFS_I(inode)->index_cnt = 2; |
| 4451 | goto out; |
| 4452 | } |
| 4453 | |
| 4454 | path->slots[0]--; |
| 4455 | |
| 4456 | leaf = path->nodes[0]; |
| 4457 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4458 | |
| 4459 | if (found_key.objectid != inode->i_ino || |
| 4460 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4461 | BTRFS_I(inode)->index_cnt = 2; |
| 4462 | goto out; |
| 4463 | } |
| 4464 | |
| 4465 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4466 | out: |
| 4467 | btrfs_free_path(path); |
| 4468 | return ret; |
| 4469 | } |
| 4470 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4471 | /* |
| 4472 | * helper to find a free sequence number in a given directory. This current |
| 4473 | * code is very simple, later versions will do smarter things in the btree |
| 4474 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4475 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4476 | { |
| 4477 | int ret = 0; |
| 4478 | |
| 4479 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 4480 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4481 | if (ret) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4482 | return ret; |
| 4483 | } |
| 4484 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4485 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4486 | BTRFS_I(dir)->index_cnt++; |
| 4487 | |
| 4488 | return ret; |
| 4489 | } |
| 4490 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4491 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4492 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4493 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4494 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4495 | u64 ref_objectid, u64 objectid, |
| 4496 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4497 | { |
| 4498 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4499 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4500 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4501 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4502 | struct btrfs_inode_ref *ref; |
| 4503 | struct btrfs_key key[2]; |
| 4504 | u32 sizes[2]; |
| 4505 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4506 | int ret; |
| 4507 | int owner; |
| 4508 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4509 | path = btrfs_alloc_path(); |
| 4510 | BUG_ON(!path); |
| 4511 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4512 | inode = new_inode(root->fs_info->sb); |
| 4513 | if (!inode) |
| 4514 | return ERR_PTR(-ENOMEM); |
| 4515 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4516 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame^] | 4517 | trace_btrfs_inode_request(dir); |
| 4518 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4519 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4520 | if (ret) { |
| 4521 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4522 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4523 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4524 | } |
| 4525 | /* |
| 4526 | * index_cnt is ignored for everything but a dir, |
| 4527 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4528 | * number |
| 4529 | */ |
| 4530 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4531 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4532 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 4533 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4534 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4535 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4536 | if (mode & S_IFDIR) |
| 4537 | owner = 0; |
| 4538 | else |
| 4539 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4540 | BTRFS_I(inode)->block_group = |
| 4541 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4542 | |
| 4543 | key[0].objectid = objectid; |
| 4544 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4545 | key[0].offset = 0; |
| 4546 | |
| 4547 | key[1].objectid = objectid; |
| 4548 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4549 | key[1].offset = ref_objectid; |
| 4550 | |
| 4551 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4552 | sizes[1] = name_len + sizeof(*ref); |
| 4553 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4554 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4555 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4556 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4557 | goto fail; |
| 4558 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 4559 | inode_init_owner(inode, dir, mode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4560 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4561 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4562 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4563 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4564 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4565 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4566 | |
| 4567 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4568 | struct btrfs_inode_ref); |
| 4569 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4570 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4571 | ptr = (unsigned long)(ref + 1); |
| 4572 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4573 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4574 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4575 | btrfs_free_path(path); |
| 4576 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4577 | location = &BTRFS_I(inode)->location; |
| 4578 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4579 | location->offset = 0; |
| 4580 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4581 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4582 | btrfs_inherit_iflags(inode, dir); |
| 4583 | |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4584 | if ((mode & S_IFREG)) { |
| 4585 | if (btrfs_test_opt(root, NODATASUM)) |
| 4586 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 4587 | if (btrfs_test_opt(root, NODATACOW)) |
| 4588 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4589 | } |
| 4590 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4591 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4592 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame^] | 4593 | |
| 4594 | trace_btrfs_inode_new(inode); |
| 4595 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4596 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4597 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4598 | if (dir) |
| 4599 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4600 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4601 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4602 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4603 | } |
| 4604 | |
| 4605 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4606 | { |
| 4607 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4608 | } |
| 4609 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4610 | /* |
| 4611 | * utility function to add 'inode' into 'parent_inode' with |
| 4612 | * a give name and a given sequence number. |
| 4613 | * if 'add_backref' is true, also insert a backref from the |
| 4614 | * inode to the parent directory. |
| 4615 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4616 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4617 | struct inode *parent_inode, struct inode *inode, |
| 4618 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4619 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4620 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4621 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4622 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4623 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4624 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4625 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4626 | } else { |
| 4627 | key.objectid = inode->i_ino; |
| 4628 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4629 | key.offset = 0; |
| 4630 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4631 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4632 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4633 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4634 | key.objectid, root->root_key.objectid, |
| 4635 | parent_inode->i_ino, |
| 4636 | index, name, name_len); |
| 4637 | } else if (add_backref) { |
| 4638 | ret = btrfs_insert_inode_ref(trans, root, |
| 4639 | name, name_len, inode->i_ino, |
| 4640 | parent_inode->i_ino, index); |
| 4641 | } |
| 4642 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4643 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4644 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4645 | parent_inode->i_ino, &key, |
| 4646 | btrfs_inode_type(inode), index); |
| 4647 | BUG_ON(ret); |
| 4648 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4649 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4650 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4651 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4652 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4653 | } |
| 4654 | return ret; |
| 4655 | } |
| 4656 | |
| 4657 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4658 | struct inode *dir, struct dentry *dentry, |
| 4659 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4660 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4661 | int err = btrfs_add_link(trans, dir, inode, |
| 4662 | dentry->d_name.name, dentry->d_name.len, |
| 4663 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4664 | if (!err) { |
| 4665 | d_instantiate(dentry, inode); |
| 4666 | return 0; |
| 4667 | } |
| 4668 | if (err > 0) |
| 4669 | err = -EEXIST; |
| 4670 | return err; |
| 4671 | } |
| 4672 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4673 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4674 | int mode, dev_t rdev) |
| 4675 | { |
| 4676 | struct btrfs_trans_handle *trans; |
| 4677 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4678 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4679 | int err; |
| 4680 | int drop_inode = 0; |
| 4681 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4682 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4683 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4684 | |
| 4685 | if (!new_valid_dev(rdev)) |
| 4686 | return -EINVAL; |
| 4687 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4688 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4689 | if (err) |
| 4690 | return err; |
| 4691 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4692 | /* |
| 4693 | * 2 for inode item and ref |
| 4694 | * 2 for dir items |
| 4695 | * 1 for xattr if selinux is on |
| 4696 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4697 | trans = btrfs_start_transaction(root, 5); |
| 4698 | if (IS_ERR(trans)) |
| 4699 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4700 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4701 | btrfs_set_trans_block_group(trans, dir); |
| 4702 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4703 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4704 | dentry->d_name.len, dir->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4705 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4706 | err = PTR_ERR(inode); |
| 4707 | if (IS_ERR(inode)) |
| 4708 | goto out_unlock; |
| 4709 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4710 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4711 | if (err) { |
| 4712 | drop_inode = 1; |
| 4713 | goto out_unlock; |
| 4714 | } |
| 4715 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4716 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4717 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4718 | if (err) |
| 4719 | drop_inode = 1; |
| 4720 | else { |
| 4721 | inode->i_op = &btrfs_special_inode_operations; |
| 4722 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4723 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4724 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4725 | btrfs_update_inode_block_group(trans, inode); |
| 4726 | btrfs_update_inode_block_group(trans, dir); |
| 4727 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4728 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4729 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4730 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4731 | if (drop_inode) { |
| 4732 | inode_dec_link_count(inode); |
| 4733 | iput(inode); |
| 4734 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4735 | return err; |
| 4736 | } |
| 4737 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4738 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4739 | int mode, struct nameidata *nd) |
| 4740 | { |
| 4741 | struct btrfs_trans_handle *trans; |
| 4742 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4743 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4744 | int drop_inode = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4745 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4746 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4747 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4748 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4749 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4750 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4751 | if (err) |
| 4752 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4753 | /* |
| 4754 | * 2 for inode item and ref |
| 4755 | * 2 for dir items |
| 4756 | * 1 for xattr if selinux is on |
| 4757 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4758 | trans = btrfs_start_transaction(root, 5); |
| 4759 | if (IS_ERR(trans)) |
| 4760 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4761 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4762 | btrfs_set_trans_block_group(trans, dir); |
| 4763 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4764 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4765 | dentry->d_name.len, dir->i_ino, objectid, |
| 4766 | BTRFS_I(dir)->block_group, mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4767 | err = PTR_ERR(inode); |
| 4768 | if (IS_ERR(inode)) |
| 4769 | goto out_unlock; |
| 4770 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4771 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4772 | if (err) { |
| 4773 | drop_inode = 1; |
| 4774 | goto out_unlock; |
| 4775 | } |
| 4776 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4777 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4778 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4779 | if (err) |
| 4780 | drop_inode = 1; |
| 4781 | else { |
| 4782 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4783 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4784 | inode->i_fop = &btrfs_file_operations; |
| 4785 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4786 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4787 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4788 | btrfs_update_inode_block_group(trans, inode); |
| 4789 | btrfs_update_inode_block_group(trans, dir); |
| 4790 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4791 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4792 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4793 | if (drop_inode) { |
| 4794 | inode_dec_link_count(inode); |
| 4795 | iput(inode); |
| 4796 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4797 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4798 | return err; |
| 4799 | } |
| 4800 | |
| 4801 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4802 | struct dentry *dentry) |
| 4803 | { |
| 4804 | struct btrfs_trans_handle *trans; |
| 4805 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4806 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4807 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4808 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4809 | int err; |
| 4810 | int drop_inode = 0; |
| 4811 | |
| 4812 | if (inode->i_nlink == 0) |
| 4813 | return -ENOENT; |
| 4814 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4815 | /* do not allow sys_link's with other subvols of the same device */ |
| 4816 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
| 4817 | return -EPERM; |
| 4818 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4819 | btrfs_inc_nlink(inode); |
Josef Bacik | bc1cbf1 | 2010-11-23 19:50:59 +0000 | [diff] [blame] | 4820 | inode->i_ctime = CURRENT_TIME; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4821 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4822 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4823 | if (err) |
| 4824 | goto fail; |
| 4825 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4826 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4827 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4828 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4829 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4830 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4831 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4832 | if (IS_ERR(trans)) { |
| 4833 | err = PTR_ERR(trans); |
| 4834 | goto fail; |
| 4835 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4836 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4837 | btrfs_set_trans_block_group(trans, dir); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 4838 | ihold(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4839 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4840 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4841 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4842 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4843 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4844 | } else { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4845 | struct dentry *parent = dget_parent(dentry); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4846 | btrfs_update_inode_block_group(trans, dir); |
| 4847 | err = btrfs_update_inode(trans, root, inode); |
| 4848 | BUG_ON(err); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4849 | btrfs_log_new_name(trans, inode, NULL, parent); |
| 4850 | dput(parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4851 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4852 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4853 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4854 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4855 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4856 | if (drop_inode) { |
| 4857 | inode_dec_link_count(inode); |
| 4858 | iput(inode); |
| 4859 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4860 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4861 | return err; |
| 4862 | } |
| 4863 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4864 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4865 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4866 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4867 | struct btrfs_trans_handle *trans; |
| 4868 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4869 | int err = 0; |
| 4870 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4871 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4872 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4873 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4874 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4875 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4876 | if (err) |
| 4877 | return err; |
| 4878 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4879 | /* |
| 4880 | * 2 items for inode and ref |
| 4881 | * 2 items for dir items |
| 4882 | * 1 for xattr if selinux is on |
| 4883 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4884 | trans = btrfs_start_transaction(root, 5); |
| 4885 | if (IS_ERR(trans)) |
| 4886 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4887 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4888 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4889 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4890 | dentry->d_name.len, dir->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4891 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 4892 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4893 | if (IS_ERR(inode)) { |
| 4894 | err = PTR_ERR(inode); |
| 4895 | goto out_fail; |
| 4896 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4897 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4898 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4899 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4900 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4901 | if (err) |
| 4902 | goto out_fail; |
| 4903 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4904 | inode->i_op = &btrfs_dir_inode_operations; |
| 4905 | inode->i_fop = &btrfs_dir_file_operations; |
| 4906 | btrfs_set_trans_block_group(trans, inode); |
| 4907 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4908 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4909 | err = btrfs_update_inode(trans, root, inode); |
| 4910 | if (err) |
| 4911 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4912 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4913 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 4914 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4915 | if (err) |
| 4916 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4917 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4918 | d_instantiate(dentry, inode); |
| 4919 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4920 | btrfs_update_inode_block_group(trans, inode); |
| 4921 | btrfs_update_inode_block_group(trans, dir); |
| 4922 | |
| 4923 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4924 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4925 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4926 | if (drop_on_err) |
| 4927 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4928 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4929 | return err; |
| 4930 | } |
| 4931 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4932 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4933 | * and an extent that you want to insert, deal with overlap and insert |
| 4934 | * the new extent into the tree. |
| 4935 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4936 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4937 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4938 | struct extent_map *em, |
| 4939 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4940 | { |
| 4941 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4942 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4943 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4944 | start_diff = map_start - em->start; |
| 4945 | em->start = map_start; |
| 4946 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4947 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4948 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4949 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4950 | em->block_len -= start_diff; |
| 4951 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4952 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4953 | } |
| 4954 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4955 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4956 | struct inode *inode, struct page *page, |
| 4957 | size_t pg_offset, u64 extent_offset, |
| 4958 | struct btrfs_file_extent_item *item) |
| 4959 | { |
| 4960 | int ret; |
| 4961 | struct extent_buffer *leaf = path->nodes[0]; |
| 4962 | char *tmp; |
| 4963 | size_t max_size; |
| 4964 | unsigned long inline_size; |
| 4965 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4966 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4967 | |
| 4968 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4969 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4970 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4971 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4972 | btrfs_item_nr(leaf, path->slots[0])); |
| 4973 | tmp = kmalloc(inline_size, GFP_NOFS); |
| 4974 | ptr = btrfs_file_extent_inline_start(item); |
| 4975 | |
| 4976 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4977 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 4978 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4979 | ret = btrfs_decompress(compress_type, tmp, page, |
| 4980 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4981 | if (ret) { |
| 4982 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 4983 | unsigned long copy_size = min_t(u64, |
| 4984 | PAGE_CACHE_SIZE - pg_offset, |
| 4985 | max_size - extent_offset); |
| 4986 | memset(kaddr + pg_offset, 0, copy_size); |
| 4987 | kunmap_atomic(kaddr, KM_USER0); |
| 4988 | } |
| 4989 | kfree(tmp); |
| 4990 | return 0; |
| 4991 | } |
| 4992 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4993 | /* |
| 4994 | * 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] | 4995 | * the ugly parts come from merging extents from the disk with the in-ram |
| 4996 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4997 | * where the in-ram extents might be locked pending data=ordered completion. |
| 4998 | * |
| 4999 | * This also copies inline extents directly into the page. |
| 5000 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5001 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5002 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5003 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5004 | int create) |
| 5005 | { |
| 5006 | int ret; |
| 5007 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5008 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5009 | u64 extent_start = 0; |
| 5010 | u64 extent_end = 0; |
| 5011 | u64 objectid = inode->i_ino; |
| 5012 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5013 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5014 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5015 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5016 | struct extent_buffer *leaf; |
| 5017 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5018 | struct extent_map *em = NULL; |
| 5019 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5020 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5021 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5022 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5023 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5024 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5025 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5026 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5027 | if (em) |
| 5028 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5029 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5030 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5031 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5032 | if (em->start > start || em->start + em->len <= start) |
| 5033 | free_extent_map(em); |
| 5034 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5035 | free_extent_map(em); |
| 5036 | else |
| 5037 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5038 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5039 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5040 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5041 | err = -ENOMEM; |
| 5042 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5043 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5044 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5045 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 5046 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5047 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5048 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5049 | |
| 5050 | if (!path) { |
| 5051 | path = btrfs_alloc_path(); |
| 5052 | BUG_ON(!path); |
| 5053 | } |
| 5054 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5055 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 5056 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5057 | if (ret < 0) { |
| 5058 | err = ret; |
| 5059 | goto out; |
| 5060 | } |
| 5061 | |
| 5062 | if (ret != 0) { |
| 5063 | if (path->slots[0] == 0) |
| 5064 | goto not_found; |
| 5065 | path->slots[0]--; |
| 5066 | } |
| 5067 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5068 | leaf = path->nodes[0]; |
| 5069 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5070 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5071 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5072 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5073 | found_type = btrfs_key_type(&found_key); |
| 5074 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5075 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 5076 | goto not_found; |
| 5077 | } |
| 5078 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5079 | found_type = btrfs_file_extent_type(leaf, item); |
| 5080 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5081 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5082 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5083 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5084 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5085 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5086 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5087 | size_t size; |
| 5088 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5089 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 5090 | ~((u64)root->sectorsize - 1); |
| 5091 | } |
| 5092 | |
| 5093 | if (start >= extent_end) { |
| 5094 | path->slots[0]++; |
| 5095 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 5096 | ret = btrfs_next_leaf(root, path); |
| 5097 | if (ret < 0) { |
| 5098 | err = ret; |
| 5099 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5100 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5101 | if (ret > 0) |
| 5102 | goto not_found; |
| 5103 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5104 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5105 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5106 | if (found_key.objectid != objectid || |
| 5107 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 5108 | goto not_found; |
| 5109 | if (start + len <= found_key.offset) |
| 5110 | goto not_found; |
| 5111 | em->start = start; |
| 5112 | em->len = found_key.offset - start; |
| 5113 | goto not_found_em; |
| 5114 | } |
| 5115 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5116 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5117 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5118 | em->start = extent_start; |
| 5119 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5120 | em->orig_start = extent_start - |
| 5121 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5122 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 5123 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5124 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5125 | goto insert; |
| 5126 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5127 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5128 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5129 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5130 | em->block_start = bytenr; |
| 5131 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 5132 | item); |
| 5133 | } else { |
| 5134 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 5135 | em->block_start = bytenr; |
| 5136 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5137 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 5138 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5139 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5140 | goto insert; |
| 5141 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5142 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5143 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5144 | size_t size; |
| 5145 | size_t extent_offset; |
| 5146 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5147 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5148 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5149 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5150 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5151 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5152 | goto out; |
| 5153 | } |
| 5154 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5155 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5156 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5157 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5158 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5159 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5160 | em->len = (copy_size + root->sectorsize - 1) & |
| 5161 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5162 | em->orig_start = EXTENT_MAP_INLINE; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5163 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5164 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5165 | em->compress_type = compress_type; |
| 5166 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5167 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5168 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5169 | if (btrfs_file_extent_compression(leaf, item) != |
| 5170 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5171 | ret = uncompress_inline(path, inode, page, |
| 5172 | pg_offset, |
| 5173 | extent_offset, item); |
| 5174 | BUG_ON(ret); |
| 5175 | } else { |
| 5176 | map = kmap(page); |
| 5177 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 5178 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 5179 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 5180 | memset(map + pg_offset + copy_size, 0, |
| 5181 | PAGE_CACHE_SIZE - pg_offset - |
| 5182 | copy_size); |
| 5183 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5184 | kunmap(page); |
| 5185 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5186 | flush_dcache_page(page); |
| 5187 | } else if (create && PageUptodate(page)) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5188 | WARN_ON(1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5189 | if (!trans) { |
| 5190 | kunmap(page); |
| 5191 | free_extent_map(em); |
| 5192 | em = NULL; |
| 5193 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 5194 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5195 | if (IS_ERR(trans)) |
| 5196 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5197 | goto again; |
| 5198 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5199 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5200 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5201 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5202 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5203 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5204 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5205 | set_extent_uptodate(io_tree, em->start, |
| 5206 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5207 | goto insert; |
| 5208 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5209 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5210 | WARN_ON(1); |
| 5211 | } |
| 5212 | not_found: |
| 5213 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5214 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5215 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5216 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5217 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5218 | insert: |
| 5219 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5220 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5221 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 5222 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 5223 | (unsigned long long)em->len, |
| 5224 | (unsigned long long)start, |
| 5225 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5226 | err = -EIO; |
| 5227 | goto out; |
| 5228 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5229 | |
| 5230 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5231 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5232 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5233 | /* it is possible that someone inserted the extent into the tree |
| 5234 | * while we had the lock dropped. It is also possible that |
| 5235 | * an overlapping map exists in the tree |
| 5236 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5237 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5238 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5239 | |
| 5240 | ret = 0; |
| 5241 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5242 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5243 | if (existing && (existing->start > start || |
| 5244 | existing->start + existing->len <= start)) { |
| 5245 | free_extent_map(existing); |
| 5246 | existing = NULL; |
| 5247 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5248 | if (!existing) { |
| 5249 | existing = lookup_extent_mapping(em_tree, em->start, |
| 5250 | em->len); |
| 5251 | if (existing) { |
| 5252 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5253 | em, start, |
| 5254 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5255 | free_extent_map(existing); |
| 5256 | if (err) { |
| 5257 | free_extent_map(em); |
| 5258 | em = NULL; |
| 5259 | } |
| 5260 | } else { |
| 5261 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5262 | free_extent_map(em); |
| 5263 | em = NULL; |
| 5264 | } |
| 5265 | } else { |
| 5266 | free_extent_map(em); |
| 5267 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5268 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5269 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5270 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5271 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5272 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame^] | 5273 | |
| 5274 | trace_btrfs_get_extent(root, em); |
| 5275 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5276 | if (path) |
| 5277 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5278 | if (trans) { |
| 5279 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5280 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5281 | err = ret; |
| 5282 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5283 | if (err) { |
| 5284 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5285 | return ERR_PTR(err); |
| 5286 | } |
| 5287 | return em; |
| 5288 | } |
| 5289 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5290 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 5291 | size_t pg_offset, u64 start, u64 len, |
| 5292 | int create) |
| 5293 | { |
| 5294 | struct extent_map *em; |
| 5295 | struct extent_map *hole_em = NULL; |
| 5296 | u64 range_start = start; |
| 5297 | u64 end; |
| 5298 | u64 found; |
| 5299 | u64 found_end; |
| 5300 | int err = 0; |
| 5301 | |
| 5302 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 5303 | if (IS_ERR(em)) |
| 5304 | return em; |
| 5305 | if (em) { |
| 5306 | /* |
| 5307 | * if our em maps to a hole, there might |
| 5308 | * actually be delalloc bytes behind it |
| 5309 | */ |
| 5310 | if (em->block_start != EXTENT_MAP_HOLE) |
| 5311 | return em; |
| 5312 | else |
| 5313 | hole_em = em; |
| 5314 | } |
| 5315 | |
| 5316 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 5317 | end = start + len; |
| 5318 | if (end < start) |
| 5319 | end = (u64)-1; |
| 5320 | else |
| 5321 | end -= 1; |
| 5322 | |
| 5323 | em = NULL; |
| 5324 | |
| 5325 | /* ok, we didn't find anything, lets look for delalloc */ |
| 5326 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 5327 | end, len, EXTENT_DELALLOC, 1); |
| 5328 | found_end = range_start + found; |
| 5329 | if (found_end < range_start) |
| 5330 | found_end = (u64)-1; |
| 5331 | |
| 5332 | /* |
| 5333 | * we didn't find anything useful, return |
| 5334 | * the original results from get_extent() |
| 5335 | */ |
| 5336 | if (range_start > end || found_end <= start) { |
| 5337 | em = hole_em; |
| 5338 | hole_em = NULL; |
| 5339 | goto out; |
| 5340 | } |
| 5341 | |
| 5342 | /* adjust the range_start to make sure it doesn't |
| 5343 | * go backwards from the start they passed in |
| 5344 | */ |
| 5345 | range_start = max(start,range_start); |
| 5346 | found = found_end - range_start; |
| 5347 | |
| 5348 | if (found > 0) { |
| 5349 | u64 hole_start = start; |
| 5350 | u64 hole_len = len; |
| 5351 | |
| 5352 | em = alloc_extent_map(GFP_NOFS); |
| 5353 | if (!em) { |
| 5354 | err = -ENOMEM; |
| 5355 | goto out; |
| 5356 | } |
| 5357 | /* |
| 5358 | * when btrfs_get_extent can't find anything it |
| 5359 | * returns one huge hole |
| 5360 | * |
| 5361 | * make sure what it found really fits our range, and |
| 5362 | * adjust to make sure it is based on the start from |
| 5363 | * the caller |
| 5364 | */ |
| 5365 | if (hole_em) { |
| 5366 | u64 calc_end = extent_map_end(hole_em); |
| 5367 | |
| 5368 | if (calc_end <= start || (hole_em->start > end)) { |
| 5369 | free_extent_map(hole_em); |
| 5370 | hole_em = NULL; |
| 5371 | } else { |
| 5372 | hole_start = max(hole_em->start, start); |
| 5373 | hole_len = calc_end - hole_start; |
| 5374 | } |
| 5375 | } |
| 5376 | em->bdev = NULL; |
| 5377 | if (hole_em && range_start > hole_start) { |
| 5378 | /* our hole starts before our delalloc, so we |
| 5379 | * have to return just the parts of the hole |
| 5380 | * that go until the delalloc starts |
| 5381 | */ |
| 5382 | em->len = min(hole_len, |
| 5383 | range_start - hole_start); |
| 5384 | em->start = hole_start; |
| 5385 | em->orig_start = hole_start; |
| 5386 | /* |
| 5387 | * don't adjust block start at all, |
| 5388 | * it is fixed at EXTENT_MAP_HOLE |
| 5389 | */ |
| 5390 | em->block_start = hole_em->block_start; |
| 5391 | em->block_len = hole_len; |
| 5392 | } else { |
| 5393 | em->start = range_start; |
| 5394 | em->len = found; |
| 5395 | em->orig_start = range_start; |
| 5396 | em->block_start = EXTENT_MAP_DELALLOC; |
| 5397 | em->block_len = found; |
| 5398 | } |
| 5399 | } else if (hole_em) { |
| 5400 | return hole_em; |
| 5401 | } |
| 5402 | out: |
| 5403 | |
| 5404 | free_extent_map(hole_em); |
| 5405 | if (err) { |
| 5406 | free_extent_map(em); |
| 5407 | return ERR_PTR(err); |
| 5408 | } |
| 5409 | return em; |
| 5410 | } |
| 5411 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5412 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 5413 | u64 start, u64 len) |
| 5414 | { |
| 5415 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5416 | struct btrfs_trans_handle *trans; |
| 5417 | struct extent_map *em; |
| 5418 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 5419 | struct btrfs_key ins; |
| 5420 | u64 alloc_hint; |
| 5421 | int ret; |
| 5422 | |
| 5423 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
| 5424 | |
| 5425 | trans = btrfs_join_transaction(root, 0); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5426 | if (IS_ERR(trans)) |
| 5427 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5428 | |
| 5429 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5430 | |
| 5431 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 5432 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
| 5433 | alloc_hint, (u64)-1, &ins, 1); |
| 5434 | if (ret) { |
| 5435 | em = ERR_PTR(ret); |
| 5436 | goto out; |
| 5437 | } |
| 5438 | |
| 5439 | em = alloc_extent_map(GFP_NOFS); |
| 5440 | if (!em) { |
| 5441 | em = ERR_PTR(-ENOMEM); |
| 5442 | goto out; |
| 5443 | } |
| 5444 | |
| 5445 | em->start = start; |
| 5446 | em->orig_start = em->start; |
| 5447 | em->len = ins.offset; |
| 5448 | |
| 5449 | em->block_start = ins.objectid; |
| 5450 | em->block_len = ins.offset; |
| 5451 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 5452 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5453 | |
| 5454 | while (1) { |
| 5455 | write_lock(&em_tree->lock); |
| 5456 | ret = add_extent_mapping(em_tree, em); |
| 5457 | write_unlock(&em_tree->lock); |
| 5458 | if (ret != -EEXIST) |
| 5459 | break; |
| 5460 | btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0); |
| 5461 | } |
| 5462 | |
| 5463 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 5464 | ins.offset, ins.offset, 0); |
| 5465 | if (ret) { |
| 5466 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 5467 | em = ERR_PTR(ret); |
| 5468 | } |
| 5469 | out: |
| 5470 | btrfs_end_transaction(trans, root); |
| 5471 | return em; |
| 5472 | } |
| 5473 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5474 | /* |
| 5475 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 5476 | * block must be cow'd |
| 5477 | */ |
| 5478 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 5479 | struct inode *inode, u64 offset, u64 len) |
| 5480 | { |
| 5481 | struct btrfs_path *path; |
| 5482 | int ret; |
| 5483 | struct extent_buffer *leaf; |
| 5484 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5485 | struct btrfs_file_extent_item *fi; |
| 5486 | struct btrfs_key key; |
| 5487 | u64 disk_bytenr; |
| 5488 | u64 backref_offset; |
| 5489 | u64 extent_end; |
| 5490 | u64 num_bytes; |
| 5491 | int slot; |
| 5492 | int found_type; |
| 5493 | |
| 5494 | path = btrfs_alloc_path(); |
| 5495 | if (!path) |
| 5496 | return -ENOMEM; |
| 5497 | |
| 5498 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 5499 | offset, 0); |
| 5500 | if (ret < 0) |
| 5501 | goto out; |
| 5502 | |
| 5503 | slot = path->slots[0]; |
| 5504 | if (ret == 1) { |
| 5505 | if (slot == 0) { |
| 5506 | /* can't find the item, must cow */ |
| 5507 | ret = 0; |
| 5508 | goto out; |
| 5509 | } |
| 5510 | slot--; |
| 5511 | } |
| 5512 | ret = 0; |
| 5513 | leaf = path->nodes[0]; |
| 5514 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5515 | if (key.objectid != inode->i_ino || |
| 5516 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5517 | /* not our file or wrong item type, must cow */ |
| 5518 | goto out; |
| 5519 | } |
| 5520 | |
| 5521 | if (key.offset > offset) { |
| 5522 | /* Wrong offset, must cow */ |
| 5523 | goto out; |
| 5524 | } |
| 5525 | |
| 5526 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5527 | found_type = btrfs_file_extent_type(leaf, fi); |
| 5528 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 5529 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 5530 | /* not a regular extent, must cow */ |
| 5531 | goto out; |
| 5532 | } |
| 5533 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5534 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 5535 | |
| 5536 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 5537 | if (extent_end < offset + len) { |
| 5538 | /* extent doesn't include our full range, must cow */ |
| 5539 | goto out; |
| 5540 | } |
| 5541 | |
| 5542 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 5543 | goto out; |
| 5544 | |
| 5545 | /* |
| 5546 | * look for other files referencing this extent, if we |
| 5547 | * find any we must cow |
| 5548 | */ |
| 5549 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
| 5550 | key.offset - backref_offset, disk_bytenr)) |
| 5551 | goto out; |
| 5552 | |
| 5553 | /* |
| 5554 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 5555 | * in this extent we are about to write. If there |
| 5556 | * are any csums in that range we have to cow in order |
| 5557 | * to keep the csums correct |
| 5558 | */ |
| 5559 | disk_bytenr += backref_offset; |
| 5560 | disk_bytenr += offset - key.offset; |
| 5561 | num_bytes = min(offset + len, extent_end) - offset; |
| 5562 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 5563 | goto out; |
| 5564 | /* |
| 5565 | * all of the above have passed, it is safe to overwrite this extent |
| 5566 | * without cow |
| 5567 | */ |
| 5568 | ret = 1; |
| 5569 | out: |
| 5570 | btrfs_free_path(path); |
| 5571 | return ret; |
| 5572 | } |
| 5573 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5574 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 5575 | struct buffer_head *bh_result, int create) |
| 5576 | { |
| 5577 | struct extent_map *em; |
| 5578 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5579 | u64 start = iblock << inode->i_blkbits; |
| 5580 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5581 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5582 | |
| 5583 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
| 5584 | if (IS_ERR(em)) |
| 5585 | return PTR_ERR(em); |
| 5586 | |
| 5587 | /* |
| 5588 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 5589 | * io. INLINE is special, and we could probably kludge it in here, but |
| 5590 | * it's still buffered so for safety lets just fall back to the generic |
| 5591 | * buffered path. |
| 5592 | * |
| 5593 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 5594 | * decompress it, so there will be buffering required no matter what we |
| 5595 | * do, so go ahead and fallback to buffered. |
| 5596 | * |
| 5597 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 5598 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 5599 | * the generic code. |
| 5600 | */ |
| 5601 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 5602 | em->block_start == EXTENT_MAP_INLINE) { |
| 5603 | free_extent_map(em); |
| 5604 | return -ENOTBLK; |
| 5605 | } |
| 5606 | |
| 5607 | /* Just a good old fashioned hole, return */ |
| 5608 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 5609 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5610 | free_extent_map(em); |
| 5611 | /* DIO will do one hole at a time, so just unlock a sector */ |
| 5612 | unlock_extent(&BTRFS_I(inode)->io_tree, start, |
| 5613 | start + root->sectorsize - 1, GFP_NOFS); |
| 5614 | return 0; |
| 5615 | } |
| 5616 | |
| 5617 | /* |
| 5618 | * We don't allocate a new extent in the following cases |
| 5619 | * |
| 5620 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 5621 | * existing extent. |
| 5622 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 5623 | * just use the extent. |
| 5624 | * |
| 5625 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5626 | if (!create) { |
| 5627 | len = em->len - (start - em->start); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5628 | goto map; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5629 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5630 | |
| 5631 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 5632 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 5633 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5634 | int type; |
| 5635 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5636 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5637 | |
| 5638 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5639 | type = BTRFS_ORDERED_PREALLOC; |
| 5640 | else |
| 5641 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5642 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5643 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5644 | |
| 5645 | /* |
| 5646 | * we're not going to log anything, but we do need |
| 5647 | * to make sure the current transaction stays open |
| 5648 | * while we look for nocow cross refs |
| 5649 | */ |
| 5650 | trans = btrfs_join_transaction(root, 0); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5651 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5652 | goto must_cow; |
| 5653 | |
| 5654 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
| 5655 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 5656 | block_start, len, len, type); |
| 5657 | btrfs_end_transaction(trans, root); |
| 5658 | if (ret) { |
| 5659 | free_extent_map(em); |
| 5660 | return ret; |
| 5661 | } |
| 5662 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5663 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5664 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5665 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5666 | must_cow: |
| 5667 | /* |
| 5668 | * this will cow the extent, reset the len in case we changed |
| 5669 | * it above |
| 5670 | */ |
| 5671 | len = bh_result->b_size; |
| 5672 | free_extent_map(em); |
| 5673 | em = btrfs_new_extent_direct(inode, start, len); |
| 5674 | if (IS_ERR(em)) |
| 5675 | return PTR_ERR(em); |
| 5676 | len = min(len, em->len - (start - em->start)); |
| 5677 | unlock: |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5678 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
| 5679 | EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1, |
| 5680 | 0, NULL, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5681 | map: |
| 5682 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 5683 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5684 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5685 | bh_result->b_bdev = em->bdev; |
| 5686 | set_buffer_mapped(bh_result); |
| 5687 | if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5688 | set_buffer_new(bh_result); |
| 5689 | |
| 5690 | free_extent_map(em); |
| 5691 | |
| 5692 | return 0; |
| 5693 | } |
| 5694 | |
| 5695 | struct btrfs_dio_private { |
| 5696 | struct inode *inode; |
| 5697 | u64 logical_offset; |
| 5698 | u64 disk_bytenr; |
| 5699 | u64 bytes; |
| 5700 | u32 *csums; |
| 5701 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5702 | |
| 5703 | /* number of bios pending for this dio */ |
| 5704 | atomic_t pending_bios; |
| 5705 | |
| 5706 | /* IO errors */ |
| 5707 | int errors; |
| 5708 | |
| 5709 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5710 | }; |
| 5711 | |
| 5712 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 5713 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5714 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5715 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 5716 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5717 | struct inode *inode = dip->inode; |
| 5718 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5719 | u64 start; |
| 5720 | u32 *private = dip->csums; |
| 5721 | |
| 5722 | start = dip->logical_offset; |
| 5723 | do { |
| 5724 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 5725 | struct page *page = bvec->bv_page; |
| 5726 | char *kaddr; |
| 5727 | u32 csum = ~(u32)0; |
| 5728 | unsigned long flags; |
| 5729 | |
| 5730 | local_irq_save(flags); |
| 5731 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 5732 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 5733 | csum, bvec->bv_len); |
| 5734 | btrfs_csum_final(csum, (char *)&csum); |
| 5735 | kunmap_atomic(kaddr, KM_IRQ0); |
| 5736 | local_irq_restore(flags); |
| 5737 | |
| 5738 | flush_dcache_page(bvec->bv_page); |
| 5739 | if (csum != *private) { |
| 5740 | printk(KERN_ERR "btrfs csum failed ino %lu off" |
| 5741 | " %llu csum %u private %u\n", |
| 5742 | inode->i_ino, (unsigned long long)start, |
| 5743 | csum, *private); |
| 5744 | err = -EIO; |
| 5745 | } |
| 5746 | } |
| 5747 | |
| 5748 | start += bvec->bv_len; |
| 5749 | private++; |
| 5750 | bvec++; |
| 5751 | } while (bvec <= bvec_end); |
| 5752 | |
| 5753 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
| 5754 | dip->logical_offset + dip->bytes - 1, GFP_NOFS); |
| 5755 | bio->bi_private = dip->private; |
| 5756 | |
| 5757 | kfree(dip->csums); |
| 5758 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 5759 | |
| 5760 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 5761 | if (err) |
| 5762 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5763 | dio_end_io(bio, err); |
| 5764 | } |
| 5765 | |
| 5766 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 5767 | { |
| 5768 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5769 | struct inode *inode = dip->inode; |
| 5770 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5771 | struct btrfs_trans_handle *trans; |
| 5772 | struct btrfs_ordered_extent *ordered = NULL; |
| 5773 | struct extent_state *cached_state = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5774 | u64 ordered_offset = dip->logical_offset; |
| 5775 | u64 ordered_bytes = dip->bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5776 | int ret; |
| 5777 | |
| 5778 | if (err) |
| 5779 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5780 | again: |
| 5781 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 5782 | &ordered_offset, |
| 5783 | ordered_bytes); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5784 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5785 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5786 | |
| 5787 | BUG_ON(!ordered); |
| 5788 | |
| 5789 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5790 | if (IS_ERR(trans)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5791 | err = -ENOMEM; |
| 5792 | goto out; |
| 5793 | } |
| 5794 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5795 | |
| 5796 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) { |
| 5797 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5798 | if (!ret) |
| 5799 | ret = btrfs_update_inode(trans, root, inode); |
| 5800 | err = ret; |
| 5801 | goto out; |
| 5802 | } |
| 5803 | |
| 5804 | lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5805 | ordered->file_offset + ordered->len - 1, 0, |
| 5806 | &cached_state, GFP_NOFS); |
| 5807 | |
| 5808 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) { |
| 5809 | ret = btrfs_mark_extent_written(trans, inode, |
| 5810 | ordered->file_offset, |
| 5811 | ordered->file_offset + |
| 5812 | ordered->len); |
| 5813 | if (ret) { |
| 5814 | err = ret; |
| 5815 | goto out_unlock; |
| 5816 | } |
| 5817 | } else { |
| 5818 | ret = insert_reserved_file_extent(trans, inode, |
| 5819 | ordered->file_offset, |
| 5820 | ordered->start, |
| 5821 | ordered->disk_len, |
| 5822 | ordered->len, |
| 5823 | ordered->len, |
| 5824 | 0, 0, 0, |
| 5825 | BTRFS_FILE_EXTENT_REG); |
| 5826 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 5827 | ordered->file_offset, ordered->len); |
| 5828 | if (ret) { |
| 5829 | err = ret; |
| 5830 | WARN_ON(1); |
| 5831 | goto out_unlock; |
| 5832 | } |
| 5833 | } |
| 5834 | |
| 5835 | add_pending_csums(trans, inode, ordered->file_offset, &ordered->list); |
| 5836 | btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5837 | btrfs_update_inode(trans, root, inode); |
| 5838 | out_unlock: |
| 5839 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5840 | ordered->file_offset + ordered->len - 1, |
| 5841 | &cached_state, GFP_NOFS); |
| 5842 | out: |
| 5843 | btrfs_delalloc_release_metadata(inode, ordered->len); |
| 5844 | btrfs_end_transaction(trans, root); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5845 | ordered_offset = ordered->file_offset + ordered->len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5846 | btrfs_put_ordered_extent(ordered); |
| 5847 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5848 | |
| 5849 | out_test: |
| 5850 | /* |
| 5851 | * our bio might span multiple ordered extents. If we haven't |
| 5852 | * completed the accounting for the whole dio, go back and try again |
| 5853 | */ |
| 5854 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 5855 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 5856 | ordered_offset; |
| 5857 | goto again; |
| 5858 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5859 | out_done: |
| 5860 | bio->bi_private = dip->private; |
| 5861 | |
| 5862 | kfree(dip->csums); |
| 5863 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 5864 | |
| 5865 | /* If we had an error make sure to clear the uptodate flag */ |
| 5866 | if (err) |
| 5867 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5868 | dio_end_io(bio, err); |
| 5869 | } |
| 5870 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5871 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 5872 | struct bio *bio, int mirror_num, |
| 5873 | unsigned long bio_flags, u64 offset) |
| 5874 | { |
| 5875 | int ret; |
| 5876 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5877 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
| 5878 | BUG_ON(ret); |
| 5879 | return 0; |
| 5880 | } |
| 5881 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5882 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 5883 | { |
| 5884 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5885 | |
| 5886 | if (err) { |
| 5887 | printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 5888 | "sector %#Lx len %u err no %d\n", |
| 5889 | dip->inode->i_ino, bio->bi_rw, |
| 5890 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5891 | dip->errors = 1; |
| 5892 | |
| 5893 | /* |
| 5894 | * before atomic variable goto zero, we must make sure |
| 5895 | * dip->errors is perceived to be set. |
| 5896 | */ |
| 5897 | smp_mb__before_atomic_dec(); |
| 5898 | } |
| 5899 | |
| 5900 | /* if there are more bios still pending for this dio, just exit */ |
| 5901 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 5902 | goto out; |
| 5903 | |
| 5904 | if (dip->errors) |
| 5905 | bio_io_error(dip->orig_bio); |
| 5906 | else { |
| 5907 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 5908 | bio_endio(dip->orig_bio, 0); |
| 5909 | } |
| 5910 | out: |
| 5911 | bio_put(bio); |
| 5912 | } |
| 5913 | |
| 5914 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 5915 | u64 first_sector, gfp_t gfp_flags) |
| 5916 | { |
| 5917 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 5918 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 5919 | } |
| 5920 | |
| 5921 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 5922 | int rw, u64 file_offset, int skip_sum, |
| 5923 | u32 *csums) |
| 5924 | { |
| 5925 | int write = rw & REQ_WRITE; |
| 5926 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5927 | int ret; |
| 5928 | |
| 5929 | bio_get(bio); |
| 5930 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 5931 | if (ret) |
| 5932 | goto err; |
| 5933 | |
| 5934 | if (write && !skip_sum) { |
| 5935 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 5936 | inode, rw, bio, 0, 0, |
| 5937 | file_offset, |
| 5938 | __btrfs_submit_bio_start_direct_io, |
| 5939 | __btrfs_submit_bio_done); |
| 5940 | goto err; |
| 5941 | } else if (!skip_sum) |
| 5942 | btrfs_lookup_bio_sums_dio(root, inode, bio, |
| 5943 | file_offset, csums); |
| 5944 | |
| 5945 | ret = btrfs_map_bio(root, rw, bio, 0, 1); |
| 5946 | err: |
| 5947 | bio_put(bio); |
| 5948 | return ret; |
| 5949 | } |
| 5950 | |
| 5951 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 5952 | int skip_sum) |
| 5953 | { |
| 5954 | struct inode *inode = dip->inode; |
| 5955 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5956 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5957 | struct bio *bio; |
| 5958 | struct bio *orig_bio = dip->orig_bio; |
| 5959 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 5960 | u64 start_sector = orig_bio->bi_sector; |
| 5961 | u64 file_offset = dip->logical_offset; |
| 5962 | u64 submit_len = 0; |
| 5963 | u64 map_length; |
| 5964 | int nr_pages = 0; |
| 5965 | u32 *csums = dip->csums; |
| 5966 | int ret = 0; |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 5967 | int write = rw & REQ_WRITE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5968 | |
| 5969 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 5970 | if (!bio) |
| 5971 | return -ENOMEM; |
| 5972 | bio->bi_private = dip; |
| 5973 | bio->bi_end_io = btrfs_end_dio_bio; |
| 5974 | atomic_inc(&dip->pending_bios); |
| 5975 | |
| 5976 | map_length = orig_bio->bi_size; |
| 5977 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 5978 | &map_length, NULL, 0); |
| 5979 | if (ret) { |
| 5980 | bio_put(bio); |
| 5981 | return -EIO; |
| 5982 | } |
| 5983 | |
| 5984 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 5985 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 5986 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5987 | bvec->bv_offset) < bvec->bv_len)) { |
| 5988 | /* |
| 5989 | * inc the count before we submit the bio so |
| 5990 | * we know the end IO handler won't happen before |
| 5991 | * we inc the count. Otherwise, the dip might get freed |
| 5992 | * before we're done setting it up |
| 5993 | */ |
| 5994 | atomic_inc(&dip->pending_bios); |
| 5995 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 5996 | file_offset, skip_sum, |
| 5997 | csums); |
| 5998 | if (ret) { |
| 5999 | bio_put(bio); |
| 6000 | atomic_dec(&dip->pending_bios); |
| 6001 | goto out_err; |
| 6002 | } |
| 6003 | |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 6004 | /* Write's use the ordered csums */ |
| 6005 | if (!write && !skip_sum) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6006 | csums = csums + nr_pages; |
| 6007 | start_sector += submit_len >> 9; |
| 6008 | file_offset += submit_len; |
| 6009 | |
| 6010 | submit_len = 0; |
| 6011 | nr_pages = 0; |
| 6012 | |
| 6013 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 6014 | start_sector, GFP_NOFS); |
| 6015 | if (!bio) |
| 6016 | goto out_err; |
| 6017 | bio->bi_private = dip; |
| 6018 | bio->bi_end_io = btrfs_end_dio_bio; |
| 6019 | |
| 6020 | map_length = orig_bio->bi_size; |
| 6021 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 6022 | &map_length, NULL, 0); |
| 6023 | if (ret) { |
| 6024 | bio_put(bio); |
| 6025 | goto out_err; |
| 6026 | } |
| 6027 | } else { |
| 6028 | submit_len += bvec->bv_len; |
| 6029 | nr_pages ++; |
| 6030 | bvec++; |
| 6031 | } |
| 6032 | } |
| 6033 | |
| 6034 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
| 6035 | csums); |
| 6036 | if (!ret) |
| 6037 | return 0; |
| 6038 | |
| 6039 | bio_put(bio); |
| 6040 | out_err: |
| 6041 | dip->errors = 1; |
| 6042 | /* |
| 6043 | * before atomic variable goto zero, we must |
| 6044 | * make sure dip->errors is perceived to be set. |
| 6045 | */ |
| 6046 | smp_mb__before_atomic_dec(); |
| 6047 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 6048 | bio_io_error(dip->orig_bio); |
| 6049 | |
| 6050 | /* bio_end_io() will handle error, so we needn't return it */ |
| 6051 | return 0; |
| 6052 | } |
| 6053 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6054 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 6055 | loff_t file_offset) |
| 6056 | { |
| 6057 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6058 | struct btrfs_dio_private *dip; |
| 6059 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6060 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 6061 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6062 | int ret = 0; |
| 6063 | |
| 6064 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 6065 | |
| 6066 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 6067 | if (!dip) { |
| 6068 | ret = -ENOMEM; |
| 6069 | goto free_ordered; |
| 6070 | } |
| 6071 | dip->csums = NULL; |
| 6072 | |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 6073 | /* Write's use the ordered csum stuff, so we don't need dip->csums */ |
| 6074 | if (!write && !skip_sum) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6075 | dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); |
| 6076 | if (!dip->csums) { |
Daniel J Blueman | b4966b7 | 2011-03-09 16:46:42 +0000 | [diff] [blame] | 6077 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6078 | ret = -ENOMEM; |
| 6079 | goto free_ordered; |
| 6080 | } |
| 6081 | } |
| 6082 | |
| 6083 | dip->private = bio->bi_private; |
| 6084 | dip->inode = inode; |
| 6085 | dip->logical_offset = file_offset; |
| 6086 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6087 | dip->bytes = 0; |
| 6088 | do { |
| 6089 | dip->bytes += bvec->bv_len; |
| 6090 | bvec++; |
| 6091 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 6092 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6093 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6094 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6095 | dip->errors = 0; |
| 6096 | dip->orig_bio = bio; |
| 6097 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6098 | |
| 6099 | if (write) |
| 6100 | bio->bi_end_io = btrfs_endio_direct_write; |
| 6101 | else |
| 6102 | bio->bi_end_io = btrfs_endio_direct_read; |
| 6103 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6104 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 6105 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6106 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6107 | free_ordered: |
| 6108 | /* |
| 6109 | * If this is a write, we need to clean up the reserved space and kill |
| 6110 | * the ordered extent. |
| 6111 | */ |
| 6112 | if (write) { |
| 6113 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 6114 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6115 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 6116 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 6117 | btrfs_free_reserved_extent(root, ordered->start, |
| 6118 | ordered->disk_len); |
| 6119 | btrfs_put_ordered_extent(ordered); |
| 6120 | btrfs_put_ordered_extent(ordered); |
| 6121 | } |
| 6122 | bio_endio(bio, ret); |
| 6123 | } |
| 6124 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6125 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 6126 | const struct iovec *iov, loff_t offset, |
| 6127 | unsigned long nr_segs) |
| 6128 | { |
| 6129 | int seg; |
| 6130 | size_t size; |
| 6131 | unsigned long addr; |
| 6132 | unsigned blocksize_mask = root->sectorsize - 1; |
| 6133 | ssize_t retval = -EINVAL; |
| 6134 | loff_t end = offset; |
| 6135 | |
| 6136 | if (offset & blocksize_mask) |
| 6137 | goto out; |
| 6138 | |
| 6139 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 6140 | for (seg = 0; seg < nr_segs; seg++) { |
| 6141 | addr = (unsigned long)iov[seg].iov_base; |
| 6142 | size = iov[seg].iov_len; |
| 6143 | end += size; |
| 6144 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
| 6145 | goto out; |
| 6146 | } |
| 6147 | retval = 0; |
| 6148 | out: |
| 6149 | return retval; |
| 6150 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6151 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 6152 | const struct iovec *iov, loff_t offset, |
| 6153 | unsigned long nr_segs) |
| 6154 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6155 | struct file *file = iocb->ki_filp; |
| 6156 | struct inode *inode = file->f_mapping->host; |
| 6157 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6158 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6159 | u64 lockstart, lockend; |
| 6160 | ssize_t ret; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6161 | int writing = rw & WRITE; |
| 6162 | int write_bits = 0; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6163 | size_t count = iov_length(iov, nr_segs); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6164 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6165 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
| 6166 | offset, nr_segs)) { |
| 6167 | return 0; |
| 6168 | } |
| 6169 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6170 | lockstart = offset; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6171 | lockend = offset + count - 1; |
| 6172 | |
| 6173 | if (writing) { |
| 6174 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 6175 | if (ret) |
| 6176 | goto out; |
| 6177 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6178 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6179 | while (1) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6180 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6181 | 0, &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6182 | /* |
| 6183 | * We're concerned with the entire range that we're going to be |
| 6184 | * doing DIO to, so we need to make sure theres no ordered |
| 6185 | * extents in this range. |
| 6186 | */ |
| 6187 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6188 | lockend - lockstart + 1); |
| 6189 | if (!ordered) |
| 6190 | break; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6191 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6192 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6193 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6194 | btrfs_put_ordered_extent(ordered); |
| 6195 | cond_resched(); |
| 6196 | } |
| 6197 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6198 | /* |
| 6199 | * we don't use btrfs_set_extent_delalloc because we don't want |
| 6200 | * the dirty or uptodate bits |
| 6201 | */ |
| 6202 | if (writing) { |
| 6203 | write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING; |
| 6204 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6205 | EXTENT_DELALLOC, 0, NULL, &cached_state, |
| 6206 | GFP_NOFS); |
| 6207 | if (ret) { |
| 6208 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6209 | lockend, EXTENT_LOCKED | write_bits, |
| 6210 | 1, 0, &cached_state, GFP_NOFS); |
| 6211 | goto out; |
| 6212 | } |
| 6213 | } |
| 6214 | |
| 6215 | free_extent_state(cached_state); |
| 6216 | cached_state = NULL; |
| 6217 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6218 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 6219 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 6220 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
| 6221 | btrfs_submit_direct, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6222 | |
| 6223 | if (ret < 0 && ret != -EIOCBQUEUED) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6224 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset, |
| 6225 | offset + iov_length(iov, nr_segs) - 1, |
| 6226 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6227 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6228 | } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) { |
| 6229 | /* |
| 6230 | * We're falling back to buffered, unlock the section we didn't |
| 6231 | * do IO on. |
| 6232 | */ |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6233 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret, |
| 6234 | offset + iov_length(iov, nr_segs) - 1, |
| 6235 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6236 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6237 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6238 | out: |
| 6239 | free_extent_state(cached_state); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6240 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6241 | } |
| 6242 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6243 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 6244 | __u64 start, __u64 len) |
| 6245 | { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6246 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6247 | } |
| 6248 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6249 | int btrfs_readpage(struct file *file, struct page *page) |
| 6250 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6251 | struct extent_io_tree *tree; |
| 6252 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6253 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6254 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6255 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6256 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 6257 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6258 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6259 | |
| 6260 | |
| 6261 | if (current->flags & PF_MEMALLOC) { |
| 6262 | redirty_page_for_writepage(wbc, page); |
| 6263 | unlock_page(page); |
| 6264 | return 0; |
| 6265 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6266 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6267 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 6268 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6269 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6270 | int btrfs_writepages(struct address_space *mapping, |
| 6271 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6272 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6273 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6274 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6275 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6276 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 6277 | } |
| 6278 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6279 | static int |
| 6280 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 6281 | struct list_head *pages, unsigned nr_pages) |
| 6282 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6283 | struct extent_io_tree *tree; |
| 6284 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6285 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 6286 | btrfs_get_extent); |
| 6287 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6288 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6289 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6290 | struct extent_io_tree *tree; |
| 6291 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6292 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6293 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6294 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 6295 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6296 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6297 | if (ret == 1) { |
| 6298 | ClearPagePrivate(page); |
| 6299 | set_page_private(page, 0); |
| 6300 | page_cache_release(page); |
| 6301 | } |
| 6302 | return ret; |
| 6303 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6304 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6305 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 6306 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 6307 | if (PageWriteback(page) || PageDirty(page)) |
| 6308 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 6309 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6310 | } |
| 6311 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6312 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 6313 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6314 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6315 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6316 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6317 | u64 page_start = page_offset(page); |
| 6318 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6319 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6320 | |
| 6321 | /* |
| 6322 | * we have the page locked, so new writeback can't start, |
| 6323 | * and the dirty bit won't be cleared while we are here. |
| 6324 | * |
| 6325 | * Wait for IO on this page so that we can safely clear |
| 6326 | * the PagePrivate2 bit and do ordered accounting |
| 6327 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6328 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6329 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6330 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6331 | if (offset) { |
| 6332 | btrfs_releasepage(page, GFP_NOFS); |
| 6333 | return; |
| 6334 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6335 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6336 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6337 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 6338 | page_offset(page)); |
| 6339 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6340 | /* |
| 6341 | * IO on this page will never be started, so we need |
| 6342 | * to account for any ordered extents now |
| 6343 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6344 | clear_extent_bit(tree, page_start, page_end, |
| 6345 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6346 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6347 | &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6348 | /* |
| 6349 | * whoever cleared the private bit is responsible |
| 6350 | * for the finish_ordered_io |
| 6351 | */ |
| 6352 | if (TestClearPagePrivate2(page)) { |
| 6353 | btrfs_finish_ordered_io(page->mapping->host, |
| 6354 | page_start, page_end); |
| 6355 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6356 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6357 | cached_state = NULL; |
| 6358 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6359 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6360 | } |
| 6361 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6362 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6363 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6364 | __btrfs_releasepage(page, GFP_NOFS); |
| 6365 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6366 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6367 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6368 | ClearPagePrivate(page); |
| 6369 | set_page_private(page, 0); |
| 6370 | page_cache_release(page); |
| 6371 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6372 | } |
| 6373 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6374 | /* |
| 6375 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 6376 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 6377 | * be careful to check for EOF conditions here. We set the page up correctly |
| 6378 | * for a written page which means we get ENOSPC checking when writing into |
| 6379 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 6380 | * support these features. |
| 6381 | * |
| 6382 | * We are not allowed to take the i_mutex here so we have to play games to |
| 6383 | * protect against truncate races as the page could now be beyond EOF. Because |
| 6384 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 6385 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 6386 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 6387 | * unlock the page. |
| 6388 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6389 | 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] | 6390 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6391 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 6392 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6393 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6394 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 6395 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6396 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6397 | char *kaddr; |
| 6398 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6399 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6400 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6401 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6402 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6403 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6404 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6405 | if (ret) { |
| 6406 | if (ret == -ENOMEM) |
| 6407 | ret = VM_FAULT_OOM; |
| 6408 | else /* -ENOSPC, -EIO, etc */ |
| 6409 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6410 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6411 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6412 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6413 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6414 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6415 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6416 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6417 | page_start = page_offset(page); |
| 6418 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6419 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6420 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6421 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6422 | /* page got truncated out from underneath us */ |
| 6423 | goto out_unlock; |
| 6424 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6425 | wait_on_page_writeback(page); |
| 6426 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6427 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 6428 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6429 | set_page_extent_mapped(page); |
| 6430 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6431 | /* |
| 6432 | * we can't set the delalloc bits if there are pending ordered |
| 6433 | * extents. Drop our locks and wait for them to finish |
| 6434 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6435 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6436 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6437 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6438 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6439 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6440 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6441 | btrfs_put_ordered_extent(ordered); |
| 6442 | goto again; |
| 6443 | } |
| 6444 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6445 | /* |
| 6446 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 6447 | * if it was already dirty, so for space accounting reasons we need to |
| 6448 | * clear any delalloc bits for the range we are fixing to save. There |
| 6449 | * is probably a better way to do this, but for now keep consistent with |
| 6450 | * prepare_pages in the normal write path. |
| 6451 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6452 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6453 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6454 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6455 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6456 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 6457 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6458 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6459 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6460 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6461 | ret = VM_FAULT_SIGBUS; |
| 6462 | goto out_unlock; |
| 6463 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6464 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6465 | |
| 6466 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6467 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6468 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6469 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6470 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6471 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6472 | if (zero_start != PAGE_CACHE_SIZE) { |
| 6473 | kaddr = kmap(page); |
| 6474 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 6475 | flush_dcache_page(page); |
| 6476 | kunmap(page); |
| 6477 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6478 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6479 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6480 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6481 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6482 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 6483 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 6484 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6485 | 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] | 6486 | |
| 6487 | out_unlock: |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6488 | if (!ret) |
| 6489 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6490 | unlock_page(page); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6491 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6492 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6493 | return ret; |
| 6494 | } |
| 6495 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6496 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6497 | { |
| 6498 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6499 | int ret; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6500 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6501 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6502 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6503 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6504 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 6505 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 6506 | if (ret) |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6507 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6508 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6509 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6510 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6511 | |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6512 | trans = btrfs_start_transaction(root, 5); |
| 6513 | if (IS_ERR(trans)) |
| 6514 | return PTR_ERR(trans); |
| 6515 | |
| 6516 | btrfs_set_trans_block_group(trans, inode); |
| 6517 | |
| 6518 | ret = btrfs_orphan_add(trans, inode); |
| 6519 | if (ret) { |
| 6520 | btrfs_end_transaction(trans, root); |
| 6521 | return ret; |
| 6522 | } |
| 6523 | |
| 6524 | nr = trans->blocks_used; |
| 6525 | btrfs_end_transaction(trans, root); |
| 6526 | btrfs_btree_balance_dirty(root, nr); |
| 6527 | |
| 6528 | /* Now start a transaction for the truncate */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6529 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6530 | if (IS_ERR(trans)) |
| 6531 | return PTR_ERR(trans); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6532 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6533 | trans->block_rsv = root->orphan_block_rsv; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6534 | |
| 6535 | /* |
| 6536 | * setattr is responsible for setting the ordered_data_close flag, |
| 6537 | * but that is only tested during the last file release. That |
| 6538 | * could happen well after the next commit, leaving a great big |
| 6539 | * window where new writes may get lost if someone chooses to write |
| 6540 | * to this file after truncating to zero |
| 6541 | * |
| 6542 | * The inode doesn't have any dirty data here, and so if we commit |
| 6543 | * this is a noop. If someone immediately starts writing to the inode |
| 6544 | * it is very likely we'll catch some of their writes in this |
| 6545 | * transaction, and the commit will find this file on the ordered |
| 6546 | * data list with good things to send down. |
| 6547 | * |
| 6548 | * This is a best effort solution, there is still a window where |
| 6549 | * using truncate to replace the contents of the file will |
| 6550 | * end up with a zero length file after a crash. |
| 6551 | */ |
| 6552 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 6553 | btrfs_add_ordered_operation(trans, root, inode); |
| 6554 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6555 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6556 | if (!trans) { |
| 6557 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6558 | if (IS_ERR(trans)) |
| 6559 | return PTR_ERR(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6560 | btrfs_set_trans_block_group(trans, inode); |
| 6561 | trans->block_rsv = root->orphan_block_rsv; |
| 6562 | } |
| 6563 | |
| 6564 | ret = btrfs_block_rsv_check(trans, root, |
| 6565 | root->orphan_block_rsv, 0, 5); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6566 | if (ret == -EAGAIN) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6567 | ret = btrfs_commit_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6568 | if (ret) |
| 6569 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6570 | trans = NULL; |
| 6571 | continue; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6572 | } else if (ret) { |
| 6573 | err = ret; |
| 6574 | break; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6575 | } |
| 6576 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6577 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 6578 | inode->i_size, |
| 6579 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6580 | if (ret != -EAGAIN) { |
| 6581 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6582 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6583 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6584 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6585 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6586 | if (ret) { |
| 6587 | err = ret; |
| 6588 | break; |
| 6589 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6590 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6591 | nr = trans->blocks_used; |
| 6592 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6593 | trans = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6594 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6595 | } |
| 6596 | |
| 6597 | if (ret == 0 && inode->i_nlink > 0) { |
| 6598 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6599 | if (ret) |
| 6600 | err = ret; |
Josef Bacik | ded5db9 | 2011-03-04 14:09:46 -0500 | [diff] [blame] | 6601 | } else if (ret && inode->i_nlink > 0) { |
| 6602 | /* |
| 6603 | * Failed to do the truncate, remove us from the in memory |
| 6604 | * orphan list. |
| 6605 | */ |
| 6606 | ret = btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6607 | } |
| 6608 | |
| 6609 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6610 | if (ret && !err) |
| 6611 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6612 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6613 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6614 | ret = btrfs_end_transaction_throttle(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6615 | if (ret && !err) |
| 6616 | err = ret; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6617 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6618 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6619 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6620 | } |
| 6621 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6622 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6623 | * create a new subvolume directory/inode (helper for the ioctl). |
| 6624 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6625 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6626 | struct btrfs_root *new_root, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6627 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6628 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6629 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6630 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6631 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6632 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6633 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6634 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6635 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 6636 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6637 | inode->i_op = &btrfs_dir_inode_operations; |
| 6638 | inode->i_fop = &btrfs_dir_file_operations; |
| 6639 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6640 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6641 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6642 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6643 | err = btrfs_update_inode(trans, new_root, inode); |
| 6644 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6645 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6646 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6647 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6648 | } |
| 6649 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6650 | /* helper function for file defrag and space balancing. This |
| 6651 | * forces readahead on a given range of bytes in an inode |
| 6652 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6653 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6654 | struct file_ra_state *ra, struct file *file, |
| 6655 | pgoff_t offset, pgoff_t last_index) |
| 6656 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6657 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6658 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6659 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 6660 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6661 | } |
| 6662 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6663 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 6664 | { |
| 6665 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6666 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6667 | |
| 6668 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 6669 | if (!ei) |
| 6670 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6671 | |
| 6672 | ei->root = NULL; |
| 6673 | ei->space_info = NULL; |
| 6674 | ei->generation = 0; |
| 6675 | ei->sequence = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 6676 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6677 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6678 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6679 | ei->delalloc_bytes = 0; |
| 6680 | ei->reserved_bytes = 0; |
| 6681 | ei->disk_i_size = 0; |
| 6682 | ei->flags = 0; |
| 6683 | ei->index_cnt = (u64)-1; |
| 6684 | ei->last_unlink_trans = 0; |
| 6685 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6686 | atomic_set(&ei->outstanding_extents, 0); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 6687 | atomic_set(&ei->reserved_extents, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6688 | |
| 6689 | ei->ordered_data_close = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6690 | ei->orphan_meta_reserved = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6691 | ei->dummy_inode = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6692 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6693 | |
| 6694 | inode = &ei->vfs_inode; |
| 6695 | extent_map_tree_init(&ei->extent_tree, GFP_NOFS); |
| 6696 | extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS); |
| 6697 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS); |
| 6698 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6699 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6700 | INIT_LIST_HEAD(&ei->i_orphan); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6701 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6702 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6703 | RB_CLEAR_NODE(&ei->rb_node); |
| 6704 | |
| 6705 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6706 | } |
| 6707 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6708 | static void btrfs_i_callback(struct rcu_head *head) |
| 6709 | { |
| 6710 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 6711 | INIT_LIST_HEAD(&inode->i_dentry); |
| 6712 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 6713 | } |
| 6714 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6715 | void btrfs_destroy_inode(struct inode *inode) |
| 6716 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6717 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6718 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6719 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6720 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 6721 | WARN_ON(inode->i_data.nrpages); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6722 | WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents)); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 6723 | WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6724 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6725 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6726 | * This can happen where we create an inode, but somebody else also |
| 6727 | * created the same inode and we need to destroy the one we already |
| 6728 | * created. |
| 6729 | */ |
| 6730 | if (!root) |
| 6731 | goto free; |
| 6732 | |
| 6733 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6734 | * Make sure we're properly removed from the ordered operation |
| 6735 | * lists. |
| 6736 | */ |
| 6737 | smp_mb(); |
| 6738 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 6739 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 6740 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 6741 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 6742 | } |
| 6743 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6744 | if (root == root->fs_info->tree_root) { |
| 6745 | struct btrfs_block_group_cache *block_group; |
| 6746 | |
| 6747 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 6748 | BTRFS_I(inode)->block_group); |
| 6749 | if (block_group && block_group->inode == inode) { |
| 6750 | spin_lock(&block_group->lock); |
| 6751 | block_group->inode = NULL; |
| 6752 | spin_unlock(&block_group->lock); |
| 6753 | btrfs_put_block_group(block_group); |
| 6754 | } else if (block_group) { |
| 6755 | btrfs_put_block_group(block_group); |
| 6756 | } |
| 6757 | } |
| 6758 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6759 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6760 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6761 | printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", |
| 6762 | inode->i_ino); |
| 6763 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6764 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6765 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6766 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6767 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6768 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 6769 | if (!ordered) |
| 6770 | break; |
| 6771 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6772 | printk(KERN_ERR "btrfs found ordered " |
| 6773 | "extent %llu %llu on inode cleanup\n", |
| 6774 | (unsigned long long)ordered->file_offset, |
| 6775 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6776 | btrfs_remove_ordered_extent(inode, ordered); |
| 6777 | btrfs_put_ordered_extent(ordered); |
| 6778 | btrfs_put_ordered_extent(ordered); |
| 6779 | } |
| 6780 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6781 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6782 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6783 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6784 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6785 | } |
| 6786 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6787 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6788 | { |
| 6789 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6790 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6791 | if (btrfs_root_refs(&root->root_item) == 0 && |
| 6792 | root != root->fs_info->tree_root) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6793 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6794 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6795 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6796 | } |
| 6797 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 6798 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6799 | { |
| 6800 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 6801 | |
| 6802 | inode_init_once(&ei->vfs_inode); |
| 6803 | } |
| 6804 | |
| 6805 | void btrfs_destroy_cachep(void) |
| 6806 | { |
| 6807 | if (btrfs_inode_cachep) |
| 6808 | kmem_cache_destroy(btrfs_inode_cachep); |
| 6809 | if (btrfs_trans_handle_cachep) |
| 6810 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 6811 | if (btrfs_transaction_cachep) |
| 6812 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6813 | if (btrfs_path_cachep) |
| 6814 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 6815 | if (btrfs_free_space_cachep) |
| 6816 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6817 | } |
| 6818 | |
| 6819 | int btrfs_init_cachep(void) |
| 6820 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6821 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 6822 | sizeof(struct btrfs_inode), 0, |
| 6823 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6824 | if (!btrfs_inode_cachep) |
| 6825 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6826 | |
| 6827 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 6828 | sizeof(struct btrfs_trans_handle), 0, |
| 6829 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6830 | if (!btrfs_trans_handle_cachep) |
| 6831 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6832 | |
| 6833 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 6834 | sizeof(struct btrfs_transaction), 0, |
| 6835 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6836 | if (!btrfs_transaction_cachep) |
| 6837 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6838 | |
| 6839 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 6840 | sizeof(struct btrfs_path), 0, |
| 6841 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6842 | if (!btrfs_path_cachep) |
| 6843 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6844 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 6845 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache", |
| 6846 | sizeof(struct btrfs_free_space), 0, |
| 6847 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 6848 | if (!btrfs_free_space_cachep) |
| 6849 | goto fail; |
| 6850 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6851 | return 0; |
| 6852 | fail: |
| 6853 | btrfs_destroy_cachep(); |
| 6854 | return -ENOMEM; |
| 6855 | } |
| 6856 | |
| 6857 | static int btrfs_getattr(struct vfsmount *mnt, |
| 6858 | struct dentry *dentry, struct kstat *stat) |
| 6859 | { |
| 6860 | struct inode *inode = dentry->d_inode; |
| 6861 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6862 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 6863 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6864 | stat->blocks = (inode_get_bytes(inode) + |
| 6865 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6866 | return 0; |
| 6867 | } |
| 6868 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6869 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 6870 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6871 | { |
| 6872 | struct btrfs_trans_handle *trans; |
| 6873 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6874 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6875 | struct inode *new_inode = new_dentry->d_inode; |
| 6876 | struct inode *old_inode = old_dentry->d_inode; |
| 6877 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6878 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6879 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6880 | int ret; |
| 6881 | |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6882 | if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 6883 | return -EPERM; |
| 6884 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6885 | /* we only allow rename subvolume link between subvolumes */ |
| 6886 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6887 | return -EXDEV; |
| 6888 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6889 | if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 6890 | (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) |
| 6891 | return -ENOTEMPTY; |
| 6892 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6893 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6894 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6895 | return -ENOTEMPTY; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6896 | /* |
| 6897 | * we're using rename to replace one file with another. |
| 6898 | * and the replacement file is large. Start IO on it now so |
| 6899 | * we don't add too much work to the end of the transaction |
| 6900 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 6901 | 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] | 6902 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 6903 | filemap_flush(old_inode->i_mapping); |
| 6904 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6905 | /* close the racy window with snapshot create/destroy ioctl */ |
| 6906 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 6907 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6908 | /* |
| 6909 | * We want to reserve the absolute worst case amount of items. So if |
| 6910 | * both inodes are subvols and we need to unlink them then that would |
| 6911 | * require 4 item modifications, but if they are both normal inodes it |
| 6912 | * would require 5 item modifications, so we'll assume their normal |
| 6913 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 6914 | * should cover the worst case number of items we'll modify. |
| 6915 | */ |
| 6916 | trans = btrfs_start_transaction(root, 20); |
| 6917 | if (IS_ERR(trans)) |
| 6918 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6919 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6920 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6921 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6922 | if (dest != root) |
| 6923 | btrfs_record_root_in_trans(trans, dest); |
| 6924 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6925 | ret = btrfs_set_inode_index(new_dir, &index); |
| 6926 | if (ret) |
| 6927 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6928 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6929 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6930 | /* force full log commit if subvolume involved. */ |
| 6931 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 6932 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6933 | ret = btrfs_insert_inode_ref(trans, dest, |
| 6934 | new_dentry->d_name.name, |
| 6935 | new_dentry->d_name.len, |
| 6936 | old_inode->i_ino, |
| 6937 | new_dir->i_ino, index); |
| 6938 | if (ret) |
| 6939 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6940 | /* |
| 6941 | * this is an ugly little race, but the rename is required |
| 6942 | * to make sure that if we crash, the inode is either at the |
| 6943 | * old name or the new one. pinning the log transaction lets |
| 6944 | * us make sure we don't allow a log commit to come in after |
| 6945 | * we unlink the name but before we add the new name back in. |
| 6946 | */ |
| 6947 | btrfs_pin_log_trans(root); |
| 6948 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6949 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6950 | * make sure the inode gets flushed if it is replacing |
| 6951 | * something. |
| 6952 | */ |
| 6953 | if (new_inode && new_inode->i_size && |
| 6954 | old_inode && S_ISREG(old_inode->i_mode)) { |
| 6955 | btrfs_add_ordered_operation(trans, root, old_inode); |
| 6956 | } |
| 6957 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6958 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 6959 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 6960 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6961 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6962 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 6963 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 6964 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6965 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6966 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 6967 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 6968 | old_dentry->d_name.name, |
| 6969 | old_dentry->d_name.len); |
| 6970 | } else { |
| 6971 | btrfs_inc_nlink(old_dentry->d_inode); |
| 6972 | ret = btrfs_unlink_inode(trans, root, old_dir, |
| 6973 | old_dentry->d_inode, |
| 6974 | old_dentry->d_name.name, |
| 6975 | old_dentry->d_name.len); |
| 6976 | } |
| 6977 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6978 | |
| 6979 | if (new_inode) { |
| 6980 | new_inode->i_ctime = CURRENT_TIME; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6981 | if (unlikely(new_inode->i_ino == |
| 6982 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 6983 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 6984 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 6985 | root_objectid, |
| 6986 | new_dentry->d_name.name, |
| 6987 | new_dentry->d_name.len); |
| 6988 | BUG_ON(new_inode->i_nlink == 0); |
| 6989 | } else { |
| 6990 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 6991 | new_dentry->d_inode, |
| 6992 | new_dentry->d_name.name, |
| 6993 | new_dentry->d_name.len); |
| 6994 | } |
| 6995 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6996 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6997 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6998 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6999 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7000 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7001 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7002 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 7003 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7004 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7005 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7006 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7007 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 7008 | struct dentry *parent = dget_parent(new_dentry); |
| 7009 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
| 7010 | dput(parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7011 | btrfs_end_log_trans(root); |
| 7012 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7013 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 7014 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7015 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7016 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 7017 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7018 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7019 | return ret; |
| 7020 | } |
| 7021 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7022 | /* |
| 7023 | * some fairly slow code that needs optimization. This walks the list |
| 7024 | * of all the inodes with pending delalloc and forces them to disk. |
| 7025 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7026 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7027 | { |
| 7028 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 7029 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7030 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7031 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7032 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 7033 | return -EROFS; |
| 7034 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7035 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7036 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7037 | binode = list_entry(head->next, struct btrfs_inode, |
| 7038 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7039 | inode = igrab(&binode->vfs_inode); |
| 7040 | if (!inode) |
| 7041 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7042 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7043 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7044 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7045 | if (delay_iput) |
| 7046 | btrfs_add_delayed_iput(inode); |
| 7047 | else |
| 7048 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7049 | } |
| 7050 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7051 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7052 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7053 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7054 | |
| 7055 | /* the filemap_flush will queue IO into the worker threads, but |
| 7056 | * we have to make sure the IO is actually started and that |
| 7057 | * ordered extents get created before we return |
| 7058 | */ |
| 7059 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7060 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7061 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7062 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7063 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 7064 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7065 | } |
| 7066 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7067 | return 0; |
| 7068 | } |
| 7069 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 7070 | int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput, |
| 7071 | int sync) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7072 | { |
| 7073 | struct btrfs_inode *binode; |
| 7074 | struct inode *inode = NULL; |
| 7075 | |
| 7076 | spin_lock(&root->fs_info->delalloc_lock); |
| 7077 | while (!list_empty(&root->fs_info->delalloc_inodes)) { |
| 7078 | binode = list_entry(root->fs_info->delalloc_inodes.next, |
| 7079 | struct btrfs_inode, delalloc_inodes); |
| 7080 | inode = igrab(&binode->vfs_inode); |
| 7081 | if (inode) { |
| 7082 | list_move_tail(&binode->delalloc_inodes, |
| 7083 | &root->fs_info->delalloc_inodes); |
| 7084 | break; |
| 7085 | } |
| 7086 | |
| 7087 | list_del_init(&binode->delalloc_inodes); |
| 7088 | cond_resched_lock(&root->fs_info->delalloc_lock); |
| 7089 | } |
| 7090 | spin_unlock(&root->fs_info->delalloc_lock); |
| 7091 | |
| 7092 | if (inode) { |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 7093 | if (sync) { |
| 7094 | filemap_write_and_wait(inode->i_mapping); |
| 7095 | /* |
| 7096 | * We have to do this because compression doesn't |
| 7097 | * actually set PG_writeback until it submits the pages |
| 7098 | * for IO, which happens in an async thread, so we could |
| 7099 | * race and not actually wait for any writeback pages |
| 7100 | * because they've not been submitted yet. Technically |
| 7101 | * this could still be the case for the ordered stuff |
| 7102 | * since the async thread may not have started to do its |
| 7103 | * work yet. If this becomes the case then we need to |
| 7104 | * figure out a way to make sure that in writepage we |
| 7105 | * wait for any async pages to be submitted before |
| 7106 | * returning so that fdatawait does what its supposed to |
| 7107 | * do. |
| 7108 | */ |
| 7109 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 7110 | } else { |
| 7111 | filemap_flush(inode->i_mapping); |
| 7112 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7113 | if (delay_iput) |
| 7114 | btrfs_add_delayed_iput(inode); |
| 7115 | else |
| 7116 | iput(inode); |
| 7117 | return 1; |
| 7118 | } |
| 7119 | return 0; |
| 7120 | } |
| 7121 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7122 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 7123 | const char *symname) |
| 7124 | { |
| 7125 | struct btrfs_trans_handle *trans; |
| 7126 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 7127 | struct btrfs_path *path; |
| 7128 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7129 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7130 | int err; |
| 7131 | int drop_inode = 0; |
| 7132 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7133 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7134 | int name_len; |
| 7135 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7136 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7137 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7138 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7139 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7140 | |
| 7141 | name_len = strlen(symname) + 1; |
| 7142 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 7143 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7144 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7145 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 7146 | if (err) |
| 7147 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7148 | /* |
| 7149 | * 2 items for inode item and ref |
| 7150 | * 2 items for dir items |
| 7151 | * 1 item for xattr if selinux is on |
| 7152 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7153 | trans = btrfs_start_transaction(root, 5); |
| 7154 | if (IS_ERR(trans)) |
| 7155 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7156 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7157 | btrfs_set_trans_block_group(trans, dir); |
| 7158 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7159 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 7160 | dentry->d_name.len, dir->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7161 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 7162 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7163 | err = PTR_ERR(inode); |
| 7164 | if (IS_ERR(inode)) |
| 7165 | goto out_unlock; |
| 7166 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 7167 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7168 | if (err) { |
| 7169 | drop_inode = 1; |
| 7170 | goto out_unlock; |
| 7171 | } |
| 7172 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7173 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 7174 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7175 | if (err) |
| 7176 | drop_inode = 1; |
| 7177 | else { |
| 7178 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7179 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7180 | inode->i_fop = &btrfs_file_operations; |
| 7181 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7182 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7183 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7184 | btrfs_update_inode_block_group(trans, inode); |
| 7185 | btrfs_update_inode_block_group(trans, dir); |
| 7186 | if (drop_inode) |
| 7187 | goto out_unlock; |
| 7188 | |
| 7189 | path = btrfs_alloc_path(); |
| 7190 | BUG_ON(!path); |
| 7191 | key.objectid = inode->i_ino; |
| 7192 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7193 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 7194 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 7195 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 7196 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7197 | if (err) { |
| 7198 | drop_inode = 1; |
| 7199 | goto out_unlock; |
| 7200 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7201 | leaf = path->nodes[0]; |
| 7202 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 7203 | struct btrfs_file_extent_item); |
| 7204 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 7205 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7206 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7207 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 7208 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 7209 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 7210 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 7211 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7212 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7213 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 7214 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7215 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7216 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7217 | inode->i_op = &btrfs_symlink_inode_operations; |
| 7218 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7219 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7220 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7221 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7222 | err = btrfs_update_inode(trans, root, inode); |
| 7223 | if (err) |
| 7224 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7225 | |
| 7226 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7227 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 7228 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7229 | if (drop_inode) { |
| 7230 | inode_dec_link_count(inode); |
| 7231 | iput(inode); |
| 7232 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7233 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7234 | return err; |
| 7235 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7236 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7237 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7238 | u64 start, u64 num_bytes, u64 min_size, |
| 7239 | loff_t actual_len, u64 *alloc_hint, |
| 7240 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7241 | { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7242 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7243 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7244 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7245 | u64 i_size; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7246 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7247 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7248 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7249 | if (trans) |
| 7250 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7251 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7252 | if (own_trans) { |
| 7253 | trans = btrfs_start_transaction(root, 3); |
| 7254 | if (IS_ERR(trans)) { |
| 7255 | ret = PTR_ERR(trans); |
| 7256 | break; |
| 7257 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7258 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7259 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7260 | ret = btrfs_reserve_extent(trans, root, num_bytes, min_size, |
| 7261 | 0, *alloc_hint, (u64)-1, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7262 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7263 | if (own_trans) |
| 7264 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7265 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7266 | } |
| 7267 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7268 | ret = insert_reserved_file_extent(trans, inode, |
| 7269 | cur_offset, ins.objectid, |
| 7270 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 7271 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7272 | BTRFS_FILE_EXTENT_PREALLOC); |
| 7273 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 7274 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7275 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7276 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7277 | num_bytes -= ins.offset; |
| 7278 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7279 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7280 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7281 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7282 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7283 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7284 | (actual_len > inode->i_size) && |
| 7285 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7286 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7287 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7288 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7289 | i_size = cur_offset; |
| 7290 | i_size_write(inode, i_size); |
| 7291 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7292 | } |
| 7293 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7294 | ret = btrfs_update_inode(trans, root, inode); |
| 7295 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7296 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7297 | if (own_trans) |
| 7298 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7299 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7300 | return ret; |
| 7301 | } |
| 7302 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7303 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7304 | u64 start, u64 num_bytes, u64 min_size, |
| 7305 | loff_t actual_len, u64 *alloc_hint) |
| 7306 | { |
| 7307 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7308 | min_size, actual_len, alloc_hint, |
| 7309 | NULL); |
| 7310 | } |
| 7311 | |
| 7312 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 7313 | struct btrfs_trans_handle *trans, int mode, |
| 7314 | u64 start, u64 num_bytes, u64 min_size, |
| 7315 | loff_t actual_len, u64 *alloc_hint) |
| 7316 | { |
| 7317 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7318 | min_size, actual_len, alloc_hint, trans); |
| 7319 | } |
| 7320 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7321 | static int btrfs_set_page_dirty(struct page *page) |
| 7322 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7323 | return __set_page_dirty_nobuffers(page); |
| 7324 | } |
| 7325 | |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 7326 | static int btrfs_permission(struct inode *inode, int mask, unsigned int flags) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7327 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7328 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7329 | |
| 7330 | if (btrfs_root_readonly(root) && (mask & MAY_WRITE)) |
| 7331 | return -EROFS; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7332 | if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7333 | return -EACCES; |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 7334 | return generic_permission(inode, mask, flags, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7335 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7336 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7337 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7338 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7339 | .lookup = btrfs_lookup, |
| 7340 | .create = btrfs_create, |
| 7341 | .unlink = btrfs_unlink, |
| 7342 | .link = btrfs_link, |
| 7343 | .mkdir = btrfs_mkdir, |
| 7344 | .rmdir = btrfs_rmdir, |
| 7345 | .rename = btrfs_rename, |
| 7346 | .symlink = btrfs_symlink, |
| 7347 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7348 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7349 | .setxattr = btrfs_setxattr, |
| 7350 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7351 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7352 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7353 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7354 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7355 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7356 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7357 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7358 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7359 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 7360 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7361 | .llseek = generic_file_llseek, |
| 7362 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 7363 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7364 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7365 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7366 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7367 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 7368 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7369 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7370 | }; |
| 7371 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7372 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7373 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 7374 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7375 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7376 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7377 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7378 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 7379 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 7380 | .set_bit_hook = btrfs_set_bit_hook, |
| 7381 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7382 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 7383 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7384 | }; |
| 7385 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 7386 | /* |
| 7387 | * btrfs doesn't support the bmap operation because swapfiles |
| 7388 | * use bmap to make a mapping of extents in the file. They assume |
| 7389 | * these extents won't change over the life of the file and they |
| 7390 | * use the bmap result to do IO directly to the drive. |
| 7391 | * |
| 7392 | * the btrfs bmap call would return logical addresses that aren't |
| 7393 | * suitable for IO and they also will change frequently as COW |
| 7394 | * operations happen. So, swapfile + btrfs == corruption. |
| 7395 | * |
| 7396 | * For now we're avoiding this by dropping bmap. |
| 7397 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7398 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7399 | .readpage = btrfs_readpage, |
| 7400 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7401 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7402 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7403 | .sync_page = block_sync_page, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7404 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7405 | .invalidatepage = btrfs_invalidatepage, |
| 7406 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7407 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 7408 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7409 | }; |
| 7410 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7411 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7412 | .readpage = btrfs_readpage, |
| 7413 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 7414 | .invalidatepage = btrfs_invalidatepage, |
| 7415 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7416 | }; |
| 7417 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7418 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7419 | .getattr = btrfs_getattr, |
| 7420 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7421 | .setxattr = btrfs_setxattr, |
| 7422 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7423 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7424 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7425 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7426 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7427 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7428 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7429 | .getattr = btrfs_getattr, |
| 7430 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7431 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7432 | .setxattr = btrfs_setxattr, |
| 7433 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7434 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7435 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7436 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7437 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7438 | .readlink = generic_readlink, |
| 7439 | .follow_link = page_follow_link_light, |
| 7440 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 7441 | .getattr = btrfs_getattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7442 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 7443 | .setxattr = btrfs_setxattr, |
| 7444 | .getxattr = btrfs_getxattr, |
| 7445 | .listxattr = btrfs_listxattr, |
| 7446 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7447 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7448 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 7449 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7450 | .d_delete = btrfs_dentry_delete, |
| 7451 | }; |