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" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 54 | #include "inode-map.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 55 | |
| 56 | struct btrfs_iget_args { |
| 57 | u64 ino; |
| 58 | struct btrfs_root *root; |
| 59 | }; |
| 60 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 61 | static const struct inode_operations btrfs_dir_inode_operations; |
| 62 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 63 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 64 | static const struct inode_operations btrfs_special_inode_operations; |
| 65 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 66 | static const struct address_space_operations btrfs_aops; |
| 67 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 68 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 69 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 70 | |
| 71 | static struct kmem_cache *btrfs_inode_cachep; |
| 72 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 73 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 74 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 75 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 76 | |
| 77 | #define S_SHIFT 12 |
| 78 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 79 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 80 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 81 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 82 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 83 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 84 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 85 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 86 | }; |
| 87 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 88 | static int btrfs_setsize(struct inode *inode, loff_t newsize); |
| 89 | static int btrfs_truncate(struct inode *inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 90 | 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] | 91 | static noinline int cow_file_range(struct inode *inode, |
| 92 | struct page *locked_page, |
| 93 | u64 start, u64 end, int *page_started, |
| 94 | unsigned long *nr_written, int unlock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 95 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 96 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 97 | struct inode *inode, struct inode *dir, |
| 98 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 99 | { |
| 100 | int err; |
| 101 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 102 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 103 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 104 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 105 | return err; |
| 106 | } |
| 107 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 108 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 109 | * this does all the hard work for inserting an inline extent into |
| 110 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 111 | * no overlapping inline items exist in the btree |
| 112 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 113 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 114 | struct btrfs_root *root, struct inode *inode, |
| 115 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 116 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 117 | struct page **compressed_pages) |
| 118 | { |
| 119 | struct btrfs_key key; |
| 120 | struct btrfs_path *path; |
| 121 | struct extent_buffer *leaf; |
| 122 | struct page *page = NULL; |
| 123 | char *kaddr; |
| 124 | unsigned long ptr; |
| 125 | struct btrfs_file_extent_item *ei; |
| 126 | int err = 0; |
| 127 | int ret; |
| 128 | size_t cur_size = size; |
| 129 | size_t datasize; |
| 130 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 131 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 132 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 133 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 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 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 142 | key.objectid = btrfs_ino(inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 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, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 223 | size_t compressed_size, int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 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, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 256 | compress_type, compressed_pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 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); |
Yoshinori Sano | dac97e51 | 2011-02-15 12:01:42 +0000 | [diff] [blame] | 293 | BUG_ON(!async_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 294 | async_extent->start = start; |
| 295 | async_extent->ram_size = ram_size; |
| 296 | async_extent->compressed_size = compressed_size; |
| 297 | async_extent->pages = pages; |
| 298 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 299 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 300 | list_add_tail(&async_extent->list, &cow->extents); |
| 301 | return 0; |
| 302 | } |
| 303 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 304 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 305 | * we create compressed extents in two phases. The first |
| 306 | * phase compresses a range of pages that have already been |
| 307 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 308 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 309 | * This is done inside an ordered work queue, and the compression |
| 310 | * is spread across many cpus. The actual IO submission is step |
| 311 | * two, and the ordered work queue takes care of making sure that |
| 312 | * happens in the same order things were put onto the queue by |
| 313 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 314 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 315 | * If this code finds it can't get good compression, it puts an |
| 316 | * entry onto the work queue to write the uncompressed bytes. This |
| 317 | * makes sure that both compressed inodes and uncompressed inodes |
| 318 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 319 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 320 | static noinline int compress_file_range(struct inode *inode, |
| 321 | struct page *locked_page, |
| 322 | u64 start, u64 end, |
| 323 | struct async_cow *async_cow, |
| 324 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 325 | { |
| 326 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 327 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 328 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 329 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 330 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 331 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 332 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 333 | struct page **pages = NULL; |
| 334 | unsigned long nr_pages; |
| 335 | unsigned long nr_pages_ret = 0; |
| 336 | unsigned long total_compressed = 0; |
| 337 | unsigned long total_in = 0; |
| 338 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 339 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 340 | int i; |
| 341 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 342 | int compress_type = root->fs_info->compress_type; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 343 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 344 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 345 | again: |
| 346 | will_compress = 0; |
| 347 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 348 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 349 | |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 350 | /* |
| 351 | * we don't want to send crud past the end of i_size through |
| 352 | * compression, that's just a waste of CPU time. So, if the |
| 353 | * end of the file is before the start of our current |
| 354 | * requested range of bytes, we bail out to the uncompressed |
| 355 | * cleanup code that can deal with all of this. |
| 356 | * |
| 357 | * It isn't really the fastest way to fix things, but this is a |
| 358 | * very uncommon corner. |
| 359 | */ |
| 360 | if (actual_end <= start) |
| 361 | goto cleanup_and_bail_uncompressed; |
| 362 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 363 | total_compressed = actual_end - start; |
| 364 | |
| 365 | /* we want to make sure that amount of ram required to uncompress |
| 366 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 367 | * of a compressed extent to 128k. This is a crucial number |
| 368 | * because it also controls how easily we can spread reads across |
| 369 | * cpus for decompression. |
| 370 | * |
| 371 | * We also want to make sure the amount of IO required to do |
| 372 | * a random read is reasonably small, so we limit the size of |
| 373 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 374 | */ |
| 375 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 376 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 377 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 378 | total_in = 0; |
| 379 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 380 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 381 | /* |
| 382 | * we do compression for mount -o compress and when the |
| 383 | * inode has not been flagged as nocompress. This flag can |
| 384 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 385 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 386 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 387 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 388 | (BTRFS_I(inode)->force_compress) || |
| 389 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 390 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 391 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Yoshinori Sano | dac97e51 | 2011-02-15 12:01:42 +0000 | [diff] [blame] | 392 | BUG_ON(!pages); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 393 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 394 | if (BTRFS_I(inode)->force_compress) |
| 395 | compress_type = BTRFS_I(inode)->force_compress; |
| 396 | |
| 397 | ret = btrfs_compress_pages(compress_type, |
| 398 | inode->i_mapping, start, |
| 399 | total_compressed, pages, |
| 400 | nr_pages, &nr_pages_ret, |
| 401 | &total_in, |
| 402 | &total_compressed, |
| 403 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 404 | |
| 405 | if (!ret) { |
| 406 | unsigned long offset = total_compressed & |
| 407 | (PAGE_CACHE_SIZE - 1); |
| 408 | struct page *page = pages[nr_pages_ret - 1]; |
| 409 | char *kaddr; |
| 410 | |
| 411 | /* zero the tail end of the last page, we might be |
| 412 | * sending it down to disk |
| 413 | */ |
| 414 | if (offset) { |
| 415 | kaddr = kmap_atomic(page, KM_USER0); |
| 416 | memset(kaddr + offset, 0, |
| 417 | PAGE_CACHE_SIZE - offset); |
| 418 | kunmap_atomic(kaddr, KM_USER0); |
| 419 | } |
| 420 | will_compress = 1; |
| 421 | } |
| 422 | } |
| 423 | if (start == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 424 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 425 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 426 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 427 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 428 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 429 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 430 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 431 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 432 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 433 | */ |
| 434 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 435 | start, end, 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 436 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 437 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 438 | ret = cow_file_range_inline(trans, root, inode, |
| 439 | start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 440 | total_compressed, |
| 441 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 442 | } |
| 443 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 444 | /* |
| 445 | * inline extent creation worked, we don't need |
| 446 | * to create any more async work items. Unlock |
| 447 | * and free up our temp pages. |
| 448 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 449 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 450 | &BTRFS_I(inode)->io_tree, |
| 451 | start, end, NULL, |
| 452 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 453 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 454 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 455 | |
| 456 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 457 | goto free_pages_out; |
| 458 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 459 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | if (will_compress) { |
| 463 | /* |
| 464 | * we aren't doing an inline extent round the compressed size |
| 465 | * up to a block size boundary so the allocator does sane |
| 466 | * things |
| 467 | */ |
| 468 | total_compressed = (total_compressed + blocksize - 1) & |
| 469 | ~(blocksize - 1); |
| 470 | |
| 471 | /* |
| 472 | * one last check to make sure the compression is really a |
| 473 | * win, compare the page count read with the blocks on disk |
| 474 | */ |
| 475 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 476 | ~(PAGE_CACHE_SIZE - 1); |
| 477 | if (total_compressed >= total_in) { |
| 478 | will_compress = 0; |
| 479 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 480 | num_bytes = total_in; |
| 481 | } |
| 482 | } |
| 483 | if (!will_compress && pages) { |
| 484 | /* |
| 485 | * the compression code ran but failed to make things smaller, |
| 486 | * free any pages it allocated and our page pointer array |
| 487 | */ |
| 488 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 489 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 490 | page_cache_release(pages[i]); |
| 491 | } |
| 492 | kfree(pages); |
| 493 | pages = NULL; |
| 494 | total_compressed = 0; |
| 495 | nr_pages_ret = 0; |
| 496 | |
| 497 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 498 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 499 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 500 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 501 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 502 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 503 | if (will_compress) { |
| 504 | *num_added += 1; |
| 505 | |
| 506 | /* the async work queues will take care of doing actual |
| 507 | * allocation on disk for these compressed pages, |
| 508 | * and will submit them to the elevator. |
| 509 | */ |
| 510 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 511 | total_compressed, pages, nr_pages_ret, |
| 512 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 513 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 514 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 515 | start += num_bytes; |
| 516 | pages = NULL; |
| 517 | cond_resched(); |
| 518 | goto again; |
| 519 | } |
| 520 | } else { |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 521 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 522 | /* |
| 523 | * No compression, but we still need to write the pages in |
| 524 | * the file we've been given so far. redirty the locked |
| 525 | * page if it corresponds to our extent and set things up |
| 526 | * for the async work queue to run cow_file_range to do |
| 527 | * the normal delalloc dance |
| 528 | */ |
| 529 | if (page_offset(locked_page) >= start && |
| 530 | page_offset(locked_page) <= end) { |
| 531 | __set_page_dirty_nobuffers(locked_page); |
| 532 | /* unlocked later on in the async handlers */ |
| 533 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 534 | add_async_extent(async_cow, start, end - start + 1, |
| 535 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 536 | *num_added += 1; |
| 537 | } |
| 538 | |
| 539 | out: |
| 540 | return 0; |
| 541 | |
| 542 | free_pages_out: |
| 543 | for (i = 0; i < nr_pages_ret; i++) { |
| 544 | WARN_ON(pages[i]->mapping); |
| 545 | page_cache_release(pages[i]); |
| 546 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 547 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 548 | |
| 549 | goto out; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * phase two of compressed writeback. This is the ordered portion |
| 554 | * of the code, which only gets called in the order the work was |
| 555 | * queued. We walk all the async extents created by compress_file_range |
| 556 | * and send them down to the disk. |
| 557 | */ |
| 558 | static noinline int submit_compressed_extents(struct inode *inode, |
| 559 | struct async_cow *async_cow) |
| 560 | { |
| 561 | struct async_extent *async_extent; |
| 562 | u64 alloc_hint = 0; |
| 563 | struct btrfs_trans_handle *trans; |
| 564 | struct btrfs_key ins; |
| 565 | struct extent_map *em; |
| 566 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 567 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 568 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 569 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 570 | |
| 571 | if (list_empty(&async_cow->extents)) |
| 572 | return 0; |
| 573 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 574 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 575 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 576 | async_extent = list_entry(async_cow->extents.next, |
| 577 | struct async_extent, list); |
| 578 | list_del(&async_extent->list); |
| 579 | |
| 580 | io_tree = &BTRFS_I(inode)->io_tree; |
| 581 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 582 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 583 | /* did the compression code fall back to uncompressed IO? */ |
| 584 | if (!async_extent->pages) { |
| 585 | int page_started = 0; |
| 586 | unsigned long nr_written = 0; |
| 587 | |
| 588 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 589 | async_extent->start + |
| 590 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 591 | |
| 592 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 593 | ret = cow_file_range(inode, async_cow->locked_page, |
| 594 | async_extent->start, |
| 595 | async_extent->start + |
| 596 | async_extent->ram_size - 1, |
| 597 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 598 | |
| 599 | /* |
| 600 | * if page_started, cow_file_range inserted an |
| 601 | * inline extent and took care of all the unlocking |
| 602 | * and IO for us. Otherwise, we need to submit |
| 603 | * all those pages down to the drive. |
| 604 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 605 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 606 | extent_write_locked_range(io_tree, |
| 607 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 608 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 609 | async_extent->ram_size - 1, |
| 610 | btrfs_get_extent, |
| 611 | WB_SYNC_ALL); |
| 612 | kfree(async_extent); |
| 613 | cond_resched(); |
| 614 | continue; |
| 615 | } |
| 616 | |
| 617 | lock_extent(io_tree, async_extent->start, |
| 618 | async_extent->start + async_extent->ram_size - 1, |
| 619 | GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 620 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 621 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 622 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 623 | ret = btrfs_reserve_extent(trans, root, |
| 624 | async_extent->compressed_size, |
| 625 | async_extent->compressed_size, |
| 626 | 0, alloc_hint, |
| 627 | (u64)-1, &ins, 1); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 628 | btrfs_end_transaction(trans, root); |
| 629 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 630 | if (ret) { |
| 631 | int i; |
| 632 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 633 | WARN_ON(async_extent->pages[i]->mapping); |
| 634 | page_cache_release(async_extent->pages[i]); |
| 635 | } |
| 636 | kfree(async_extent->pages); |
| 637 | async_extent->nr_pages = 0; |
| 638 | async_extent->pages = NULL; |
| 639 | unlock_extent(io_tree, async_extent->start, |
| 640 | async_extent->start + |
| 641 | async_extent->ram_size - 1, GFP_NOFS); |
| 642 | goto retry; |
| 643 | } |
| 644 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 645 | /* |
| 646 | * here we're doing allocation and writeback of the |
| 647 | * compressed pages |
| 648 | */ |
| 649 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 650 | async_extent->start + |
| 651 | async_extent->ram_size - 1, 0); |
| 652 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 653 | em = alloc_extent_map(GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 654 | BUG_ON(!em); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 655 | em->start = async_extent->start; |
| 656 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 657 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 658 | |
| 659 | em->block_start = ins.objectid; |
| 660 | em->block_len = ins.offset; |
| 661 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 662 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 663 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 664 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 665 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 666 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 667 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 668 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 669 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 670 | if (ret != -EEXIST) { |
| 671 | free_extent_map(em); |
| 672 | break; |
| 673 | } |
| 674 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 675 | async_extent->start + |
| 676 | async_extent->ram_size - 1, 0); |
| 677 | } |
| 678 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 679 | ret = btrfs_add_ordered_extent_compress(inode, |
| 680 | async_extent->start, |
| 681 | ins.objectid, |
| 682 | async_extent->ram_size, |
| 683 | ins.offset, |
| 684 | BTRFS_ORDERED_COMPRESSED, |
| 685 | async_extent->compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 686 | BUG_ON(ret); |
| 687 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 688 | /* |
| 689 | * clear dirty, set writeback and unlock the pages. |
| 690 | */ |
| 691 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 692 | &BTRFS_I(inode)->io_tree, |
| 693 | async_extent->start, |
| 694 | async_extent->start + |
| 695 | async_extent->ram_size - 1, |
| 696 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 697 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 698 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 699 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 700 | |
| 701 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 702 | async_extent->start, |
| 703 | async_extent->ram_size, |
| 704 | ins.objectid, |
| 705 | ins.offset, async_extent->pages, |
| 706 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 707 | |
| 708 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 709 | alloc_hint = ins.objectid + ins.offset; |
| 710 | kfree(async_extent); |
| 711 | cond_resched(); |
| 712 | } |
| 713 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 714 | return 0; |
| 715 | } |
| 716 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 717 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 718 | u64 num_bytes) |
| 719 | { |
| 720 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 721 | struct extent_map *em; |
| 722 | u64 alloc_hint = 0; |
| 723 | |
| 724 | read_lock(&em_tree->lock); |
| 725 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 726 | if (em) { |
| 727 | /* |
| 728 | * if block start isn't an actual block number then find the |
| 729 | * first block in this inode and use that as a hint. If that |
| 730 | * block is also bogus then just don't worry about it. |
| 731 | */ |
| 732 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 733 | free_extent_map(em); |
| 734 | em = search_extent_mapping(em_tree, 0, 0); |
| 735 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 736 | alloc_hint = em->block_start; |
| 737 | if (em) |
| 738 | free_extent_map(em); |
| 739 | } else { |
| 740 | alloc_hint = em->block_start; |
| 741 | free_extent_map(em); |
| 742 | } |
| 743 | } |
| 744 | read_unlock(&em_tree->lock); |
| 745 | |
| 746 | return alloc_hint; |
| 747 | } |
| 748 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 749 | static inline bool is_free_space_inode(struct btrfs_root *root, |
| 750 | struct inode *inode) |
| 751 | { |
| 752 | if (root == root->fs_info->tree_root || |
| 753 | BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) |
| 754 | return true; |
| 755 | return false; |
| 756 | } |
| 757 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 758 | /* |
| 759 | * when extent_io.c finds a delayed allocation range in the file, |
| 760 | * the call backs end up in this code. The basic idea is to |
| 761 | * allocate extents on disk for the range, and create ordered data structs |
| 762 | * in ram to track those extents. |
| 763 | * |
| 764 | * locked_page is the page that writepage had locked already. We use |
| 765 | * it to make sure we don't do extra locks or unlocks. |
| 766 | * |
| 767 | * *page_started is set to one if we unlock locked_page and do everything |
| 768 | * required to start IO on it. It may be clean and already done with |
| 769 | * IO when we return. |
| 770 | */ |
| 771 | static noinline int cow_file_range(struct inode *inode, |
| 772 | struct page *locked_page, |
| 773 | u64 start, u64 end, int *page_started, |
| 774 | unsigned long *nr_written, |
| 775 | int unlock) |
| 776 | { |
| 777 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 778 | struct btrfs_trans_handle *trans; |
| 779 | u64 alloc_hint = 0; |
| 780 | u64 num_bytes; |
| 781 | unsigned long ram_size; |
| 782 | u64 disk_num_bytes; |
| 783 | u64 cur_alloc_size; |
| 784 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 785 | struct btrfs_key ins; |
| 786 | struct extent_map *em; |
| 787 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 788 | int ret = 0; |
| 789 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 790 | BUG_ON(is_free_space_inode(root, inode)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 791 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 792 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 793 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 794 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 795 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 796 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 797 | num_bytes = max(blocksize, num_bytes); |
| 798 | disk_num_bytes = num_bytes; |
| 799 | ret = 0; |
| 800 | |
| 801 | if (start == 0) { |
| 802 | /* lets try to make an inline extent */ |
| 803 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 804 | start, end, 0, 0, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 805 | if (ret == 0) { |
| 806 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 807 | &BTRFS_I(inode)->io_tree, |
| 808 | start, end, NULL, |
| 809 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 810 | EXTENT_CLEAR_UNLOCK | |
| 811 | EXTENT_CLEAR_DELALLOC | |
| 812 | EXTENT_CLEAR_DIRTY | |
| 813 | EXTENT_SET_WRITEBACK | |
| 814 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 815 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 816 | *nr_written = *nr_written + |
| 817 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 818 | *page_started = 1; |
| 819 | ret = 0; |
| 820 | goto out; |
| 821 | } |
| 822 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 823 | |
| 824 | BUG_ON(disk_num_bytes > |
| 825 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 826 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 827 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 828 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 829 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 830 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 831 | unsigned long op; |
| 832 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 833 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 834 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 835 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 836 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 837 | BUG_ON(ret); |
| 838 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 839 | em = alloc_extent_map(GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 840 | BUG_ON(!em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 841 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 842 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 843 | ram_size = ins.offset; |
| 844 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 845 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 846 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 847 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 848 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 849 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 850 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 851 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 852 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 853 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 854 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 855 | if (ret != -EEXIST) { |
| 856 | free_extent_map(em); |
| 857 | break; |
| 858 | } |
| 859 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 860 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 861 | } |
| 862 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 863 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 864 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 865 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 866 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 867 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 868 | if (root->root_key.objectid == |
| 869 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 870 | ret = btrfs_reloc_clone_csums(inode, start, |
| 871 | cur_alloc_size); |
| 872 | BUG_ON(ret); |
| 873 | } |
| 874 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 875 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 876 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 877 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 878 | /* we're not doing compressed IO, don't unlock the first |
| 879 | * page (which the caller expects to stay locked), don't |
| 880 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 881 | * |
| 882 | * Do set the Private2 bit so we know this page was properly |
| 883 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 884 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 885 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 886 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 887 | EXTENT_SET_PRIVATE2; |
| 888 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 889 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 890 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 891 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 892 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 893 | num_bytes -= cur_alloc_size; |
| 894 | alloc_hint = ins.objectid + ins.offset; |
| 895 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 896 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 897 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 898 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 899 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 900 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 901 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 902 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 903 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 904 | /* |
| 905 | * work queue call back to started compression on a file and pages |
| 906 | */ |
| 907 | static noinline void async_cow_start(struct btrfs_work *work) |
| 908 | { |
| 909 | struct async_cow *async_cow; |
| 910 | int num_added = 0; |
| 911 | async_cow = container_of(work, struct async_cow, work); |
| 912 | |
| 913 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 914 | async_cow->start, async_cow->end, async_cow, |
| 915 | &num_added); |
| 916 | if (num_added == 0) |
| 917 | async_cow->inode = NULL; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * work queue call back to submit previously compressed pages |
| 922 | */ |
| 923 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 924 | { |
| 925 | struct async_cow *async_cow; |
| 926 | struct btrfs_root *root; |
| 927 | unsigned long nr_pages; |
| 928 | |
| 929 | async_cow = container_of(work, struct async_cow, work); |
| 930 | |
| 931 | root = async_cow->root; |
| 932 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 933 | PAGE_CACHE_SHIFT; |
| 934 | |
| 935 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 936 | |
| 937 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 938 | 5 * 1042 * 1024 && |
| 939 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 940 | wake_up(&root->fs_info->async_submit_wait); |
| 941 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 942 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 943 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 944 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 945 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 946 | static noinline void async_cow_free(struct btrfs_work *work) |
| 947 | { |
| 948 | struct async_cow *async_cow; |
| 949 | async_cow = container_of(work, struct async_cow, work); |
| 950 | kfree(async_cow); |
| 951 | } |
| 952 | |
| 953 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 954 | u64 start, u64 end, int *page_started, |
| 955 | unsigned long *nr_written) |
| 956 | { |
| 957 | struct async_cow *async_cow; |
| 958 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 959 | unsigned long nr_pages; |
| 960 | u64 cur_end; |
| 961 | int limit = 10 * 1024 * 1042; |
| 962 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 963 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 964 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 965 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 966 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 967 | BUG_ON(!async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 968 | async_cow->inode = inode; |
| 969 | async_cow->root = root; |
| 970 | async_cow->locked_page = locked_page; |
| 971 | async_cow->start = start; |
| 972 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 973 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 974 | cur_end = end; |
| 975 | else |
| 976 | cur_end = min(end, start + 512 * 1024 - 1); |
| 977 | |
| 978 | async_cow->end = cur_end; |
| 979 | INIT_LIST_HEAD(&async_cow->extents); |
| 980 | |
| 981 | async_cow->work.func = async_cow_start; |
| 982 | async_cow->work.ordered_func = async_cow_submit; |
| 983 | async_cow->work.ordered_free = async_cow_free; |
| 984 | async_cow->work.flags = 0; |
| 985 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 986 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 987 | PAGE_CACHE_SHIFT; |
| 988 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 989 | |
| 990 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 991 | &async_cow->work); |
| 992 | |
| 993 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 994 | wait_event(root->fs_info->async_submit_wait, |
| 995 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 996 | limit)); |
| 997 | } |
| 998 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 999 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1000 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1001 | wait_event(root->fs_info->async_submit_wait, |
| 1002 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1003 | 0)); |
| 1004 | } |
| 1005 | |
| 1006 | *nr_written += nr_pages; |
| 1007 | start = cur_end + 1; |
| 1008 | } |
| 1009 | *page_started = 1; |
| 1010 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1011 | } |
| 1012 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1013 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1014 | u64 bytenr, u64 num_bytes) |
| 1015 | { |
| 1016 | int ret; |
| 1017 | struct btrfs_ordered_sum *sums; |
| 1018 | LIST_HEAD(list); |
| 1019 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1020 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
| 1021 | bytenr + num_bytes - 1, &list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1022 | if (ret == 0 && list_empty(&list)) |
| 1023 | return 0; |
| 1024 | |
| 1025 | while (!list_empty(&list)) { |
| 1026 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1027 | list_del(&sums->list); |
| 1028 | kfree(sums); |
| 1029 | } |
| 1030 | return 1; |
| 1031 | } |
| 1032 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1033 | /* |
| 1034 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1035 | * of the extents that exist in the file, and COWs the file as required. |
| 1036 | * |
| 1037 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1038 | * blocks on disk |
| 1039 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1040 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1041 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1042 | u64 start, u64 end, int *page_started, int force, |
| 1043 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1044 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1045 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1046 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1047 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1048 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1049 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1050 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1051 | u64 cow_start; |
| 1052 | u64 cur_offset; |
| 1053 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1054 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1055 | u64 disk_bytenr; |
| 1056 | u64 num_bytes; |
| 1057 | int extent_type; |
| 1058 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1059 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1060 | int nocow; |
| 1061 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1062 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1063 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1064 | |
| 1065 | path = btrfs_alloc_path(); |
| 1066 | BUG_ON(!path); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1067 | |
| 1068 | nolock = is_free_space_inode(root, inode); |
| 1069 | |
| 1070 | if (nolock) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1071 | trans = btrfs_join_transaction_nolock(root, 1); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1072 | else |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1073 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1074 | BUG_ON(IS_ERR(trans)); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1075 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1076 | cow_start = (u64)-1; |
| 1077 | cur_offset = start; |
| 1078 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1079 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1080 | cur_offset, 0); |
| 1081 | BUG_ON(ret < 0); |
| 1082 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1083 | leaf = path->nodes[0]; |
| 1084 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1085 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1086 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1087 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1088 | path->slots[0]--; |
| 1089 | } |
| 1090 | check_prev = 0; |
| 1091 | next_slot: |
| 1092 | leaf = path->nodes[0]; |
| 1093 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1094 | ret = btrfs_next_leaf(root, path); |
| 1095 | if (ret < 0) |
| 1096 | BUG_ON(1); |
| 1097 | if (ret > 0) |
| 1098 | break; |
| 1099 | leaf = path->nodes[0]; |
| 1100 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1101 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1102 | nocow = 0; |
| 1103 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1104 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1105 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1106 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1107 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1108 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1109 | found_key.offset > end) |
| 1110 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1111 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1112 | if (found_key.offset > cur_offset) { |
| 1113 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1114 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1115 | goto out_check; |
| 1116 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1117 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1118 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1119 | struct btrfs_file_extent_item); |
| 1120 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1121 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1122 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1123 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1124 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1125 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1126 | extent_end = found_key.offset + |
| 1127 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1128 | if (extent_end <= start) { |
| 1129 | path->slots[0]++; |
| 1130 | goto next_slot; |
| 1131 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1132 | if (disk_bytenr == 0) |
| 1133 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1134 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1135 | btrfs_file_extent_encryption(leaf, fi) || |
| 1136 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1137 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1138 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1139 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1140 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1141 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1142 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1143 | found_key.offset - |
| 1144 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1145 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1146 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1147 | disk_bytenr += cur_offset - found_key.offset; |
| 1148 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1149 | /* |
| 1150 | * force cow if csum exists in the range. |
| 1151 | * this ensure that csum for a given extent are |
| 1152 | * either valid or do not exist. |
| 1153 | */ |
| 1154 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1155 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1156 | nocow = 1; |
| 1157 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1158 | extent_end = found_key.offset + |
| 1159 | btrfs_file_extent_inline_len(leaf, fi); |
| 1160 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1161 | } else { |
| 1162 | BUG_ON(1); |
| 1163 | } |
| 1164 | out_check: |
| 1165 | if (extent_end <= start) { |
| 1166 | path->slots[0]++; |
| 1167 | goto next_slot; |
| 1168 | } |
| 1169 | if (!nocow) { |
| 1170 | if (cow_start == (u64)-1) |
| 1171 | cow_start = cur_offset; |
| 1172 | cur_offset = extent_end; |
| 1173 | if (cur_offset > end) |
| 1174 | break; |
| 1175 | path->slots[0]++; |
| 1176 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1177 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1178 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1179 | btrfs_release_path(root, path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1180 | if (cow_start != (u64)-1) { |
| 1181 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1182 | found_key.offset - 1, page_started, |
| 1183 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1184 | BUG_ON(ret); |
| 1185 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1186 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1187 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1188 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1189 | struct extent_map *em; |
| 1190 | struct extent_map_tree *em_tree; |
| 1191 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 1192 | em = alloc_extent_map(GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 1193 | BUG_ON(!em); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1194 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1195 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1196 | em->len = num_bytes; |
| 1197 | em->block_len = num_bytes; |
| 1198 | em->block_start = disk_bytenr; |
| 1199 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1200 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1201 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1202 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1203 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1204 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1205 | if (ret != -EEXIST) { |
| 1206 | free_extent_map(em); |
| 1207 | break; |
| 1208 | } |
| 1209 | btrfs_drop_extent_cache(inode, em->start, |
| 1210 | em->start + em->len - 1, 0); |
| 1211 | } |
| 1212 | type = BTRFS_ORDERED_PREALLOC; |
| 1213 | } else { |
| 1214 | type = BTRFS_ORDERED_NOCOW; |
| 1215 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1216 | |
| 1217 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1218 | num_bytes, num_bytes, type); |
| 1219 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1220 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1221 | if (root->root_key.objectid == |
| 1222 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1223 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1224 | num_bytes); |
| 1225 | BUG_ON(ret); |
| 1226 | } |
| 1227 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1228 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1229 | cur_offset, cur_offset + num_bytes - 1, |
| 1230 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1231 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1232 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1233 | cur_offset = extent_end; |
| 1234 | if (cur_offset > end) |
| 1235 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1236 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1237 | btrfs_release_path(root, path); |
| 1238 | |
| 1239 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1240 | cow_start = cur_offset; |
| 1241 | if (cow_start != (u64)-1) { |
| 1242 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1243 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1244 | BUG_ON(ret); |
| 1245 | } |
| 1246 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1247 | if (nolock) { |
| 1248 | ret = btrfs_end_transaction_nolock(trans, root); |
| 1249 | BUG_ON(ret); |
| 1250 | } else { |
| 1251 | ret = btrfs_end_transaction(trans, root); |
| 1252 | BUG_ON(ret); |
| 1253 | } |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1254 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1255 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1256 | } |
| 1257 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1258 | /* |
| 1259 | * extent_io.c call back to do delayed allocation processing |
| 1260 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1261 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1262 | u64 start, u64 end, int *page_started, |
| 1263 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1264 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1265 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1266 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1267 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1268 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1269 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1270 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1271 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1272 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1273 | page_started, 0, nr_written); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1274 | else if (!btrfs_test_opt(root, COMPRESS) && |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 1275 | !(BTRFS_I(inode)->force_compress) && |
| 1276 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1277 | ret = cow_file_range(inode, locked_page, start, end, |
| 1278 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1279 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1280 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1281 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1282 | return ret; |
| 1283 | } |
| 1284 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1285 | static int btrfs_split_extent_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1286 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1287 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1288 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1289 | if (!(orig->state & EXTENT_DELALLOC)) |
| 1290 | return 0; |
| 1291 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1292 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1298 | * extents so we can keep track of new extents that are just merged onto old |
| 1299 | * extents, such as when we are doing sequential writes, so we can properly |
| 1300 | * account for the metadata space we'll need. |
| 1301 | */ |
| 1302 | static int btrfs_merge_extent_hook(struct inode *inode, |
| 1303 | struct extent_state *new, |
| 1304 | struct extent_state *other) |
| 1305 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1306 | /* not delalloc, ignore it */ |
| 1307 | if (!(other->state & EXTENT_DELALLOC)) |
| 1308 | return 0; |
| 1309 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1310 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1311 | return 0; |
| 1312 | } |
| 1313 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1314 | /* |
| 1315 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1316 | * bytes in this file, and to maintain the list of inodes that |
| 1317 | * have pending delalloc work to be done. |
| 1318 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1319 | static int btrfs_set_bit_hook(struct inode *inode, |
| 1320 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1321 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1322 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1323 | /* |
| 1324 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1325 | * but in this case, we are only testeing for the DELALLOC |
| 1326 | * bit, which is only set or cleared with irqs on |
| 1327 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1328 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1329 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1330 | u64 len = state->end + 1 - state->start; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1331 | bool do_list = !is_free_space_inode(root, inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1332 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1333 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1334 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1335 | else |
| 1336 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1337 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1338 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1339 | BTRFS_I(inode)->delalloc_bytes += len; |
| 1340 | root->fs_info->delalloc_bytes += len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1341 | if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1342 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1343 | &root->fs_info->delalloc_inodes); |
| 1344 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1345 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1346 | } |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1350 | /* |
| 1351 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1352 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1353 | static int btrfs_clear_bit_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1354 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1355 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1356 | /* |
| 1357 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1358 | * but in this case, we are only testeing for the DELALLOC |
| 1359 | * bit, which is only set or cleared with irqs on |
| 1360 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1361 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1362 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1363 | u64 len = state->end + 1 - state->start; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1364 | bool do_list = !is_free_space_inode(root, inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1365 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1366 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1367 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1368 | else if (!(*bits & EXTENT_DO_ACCOUNTING)) |
| 1369 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
| 1370 | |
| 1371 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1372 | btrfs_delalloc_release_metadata(inode, len); |
| 1373 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1374 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 1375 | && do_list) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1376 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1377 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1378 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1379 | root->fs_info->delalloc_bytes -= len; |
| 1380 | BTRFS_I(inode)->delalloc_bytes -= len; |
| 1381 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1382 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1383 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1384 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1385 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1386 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1387 | } |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1391 | /* |
| 1392 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1393 | * we don't create bios that span stripes or chunks |
| 1394 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1395 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1396 | size_t size, struct bio *bio, |
| 1397 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1398 | { |
| 1399 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1400 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1401 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1402 | u64 length = 0; |
| 1403 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1404 | int ret; |
| 1405 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1406 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1407 | return 0; |
| 1408 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1409 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1410 | map_tree = &root->fs_info->mapping_tree; |
| 1411 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1412 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1413 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1414 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1415 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1416 | return 1; |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 1417 | return ret; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1418 | } |
| 1419 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1420 | /* |
| 1421 | * in order to insert checksums into the metadata in large chunks, |
| 1422 | * we wait until bio submission time. All the pages in the bio are |
| 1423 | * checksummed and sums are attached onto the ordered extent record. |
| 1424 | * |
| 1425 | * At IO completion time the cums attached on the ordered extent record |
| 1426 | * are inserted into the btree |
| 1427 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1428 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1429 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1430 | unsigned long bio_flags, |
| 1431 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1432 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1433 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1434 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1435 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1436 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1437 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1438 | return 0; |
| 1439 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1440 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1441 | /* |
| 1442 | * in order to insert checksums into the metadata in large chunks, |
| 1443 | * we wait until bio submission time. All the pages in the bio are |
| 1444 | * checksummed and sums are attached onto the ordered extent record. |
| 1445 | * |
| 1446 | * At IO completion time the cums attached on the ordered extent record |
| 1447 | * are inserted into the btree |
| 1448 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1449 | 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] | 1450 | int mirror_num, unsigned long bio_flags, |
| 1451 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1452 | { |
| 1453 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1454 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1455 | } |
| 1456 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1457 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1458 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1459 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1460 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1461 | 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] | 1462 | int mirror_num, unsigned long bio_flags, |
| 1463 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1464 | { |
| 1465 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1466 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1467 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1468 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1469 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1470 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1471 | if (is_free_space_inode(root, inode)) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1472 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2); |
| 1473 | else |
| 1474 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1475 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1476 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1477 | if (!(rw & REQ_WRITE)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1478 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1479 | return btrfs_submit_compressed_read(inode, bio, |
| 1480 | mirror_num, bio_flags); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1481 | } else if (!skip_sum) { |
| 1482 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1483 | if (ret) |
| 1484 | return ret; |
| 1485 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1486 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1487 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1488 | /* csum items have already been cloned */ |
| 1489 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1490 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1491 | /* we're doing a write, do the async checksumming */ |
| 1492 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1493 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1494 | bio_flags, bio_offset, |
| 1495 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1496 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1497 | } |
| 1498 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1499 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1500 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1501 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1502 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1503 | /* |
| 1504 | * given a list of ordered sums record them in the inode. This happens |
| 1505 | * at IO completion time based on sums calculated at bio submission time. |
| 1506 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1507 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1508 | struct inode *inode, u64 file_offset, |
| 1509 | struct list_head *list) |
| 1510 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1511 | struct btrfs_ordered_sum *sum; |
| 1512 | |
| 1513 | btrfs_set_trans_block_group(trans, inode); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1514 | |
| 1515 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1516 | btrfs_csum_file_blocks(trans, |
| 1517 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1518 | } |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1522 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1523 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1524 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1525 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1526 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1527 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1528 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1529 | } |
| 1530 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1531 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1532 | struct btrfs_writepage_fixup { |
| 1533 | struct page *page; |
| 1534 | struct btrfs_work work; |
| 1535 | }; |
| 1536 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1537 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1538 | { |
| 1539 | struct btrfs_writepage_fixup *fixup; |
| 1540 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1541 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1542 | struct page *page; |
| 1543 | struct inode *inode; |
| 1544 | u64 page_start; |
| 1545 | u64 page_end; |
| 1546 | |
| 1547 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1548 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1549 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1550 | lock_page(page); |
| 1551 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1552 | ClearPageChecked(page); |
| 1553 | goto out_page; |
| 1554 | } |
| 1555 | |
| 1556 | inode = page->mapping->host; |
| 1557 | page_start = page_offset(page); |
| 1558 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1559 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1560 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
| 1561 | &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1562 | |
| 1563 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1564 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1565 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1566 | |
| 1567 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1568 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1569 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1570 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1571 | unlock_page(page); |
| 1572 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1573 | goto again; |
| 1574 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1575 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1576 | BUG(); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1577 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1578 | ClearPageChecked(page); |
| 1579 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1580 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1581 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1582 | out_page: |
| 1583 | unlock_page(page); |
| 1584 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1585 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | /* |
| 1589 | * There are a few paths in the higher layers of the kernel that directly |
| 1590 | * set the page dirty bit without asking the filesystem if it is a |
| 1591 | * good idea. This causes problems because we want to make sure COW |
| 1592 | * properly happens and the data=ordered rules are followed. |
| 1593 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1594 | * 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] | 1595 | * hasn't been properly setup for IO. We kick off an async process |
| 1596 | * to fix it up. The async helper will wait for ordered extents, set |
| 1597 | * the delalloc bit and make it safe to write the page. |
| 1598 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1599 | 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] | 1600 | { |
| 1601 | struct inode *inode = page->mapping->host; |
| 1602 | struct btrfs_writepage_fixup *fixup; |
| 1603 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1604 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1605 | /* this page is properly in the ordered list */ |
| 1606 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1607 | return 0; |
| 1608 | |
| 1609 | if (PageChecked(page)) |
| 1610 | return -EAGAIN; |
| 1611 | |
| 1612 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1613 | if (!fixup) |
| 1614 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1615 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1616 | SetPageChecked(page); |
| 1617 | page_cache_get(page); |
| 1618 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1619 | fixup->page = page; |
| 1620 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1621 | return -EAGAIN; |
| 1622 | } |
| 1623 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1624 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1625 | struct inode *inode, u64 file_pos, |
| 1626 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1627 | u64 num_bytes, u64 ram_bytes, |
| 1628 | u8 compression, u8 encryption, |
| 1629 | u16 other_encoding, int extent_type) |
| 1630 | { |
| 1631 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1632 | struct btrfs_file_extent_item *fi; |
| 1633 | struct btrfs_path *path; |
| 1634 | struct extent_buffer *leaf; |
| 1635 | struct btrfs_key ins; |
| 1636 | u64 hint; |
| 1637 | int ret; |
| 1638 | |
| 1639 | path = btrfs_alloc_path(); |
| 1640 | BUG_ON(!path); |
| 1641 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1642 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1643 | |
| 1644 | /* |
| 1645 | * we may be replacing one extent in the tree with another. |
| 1646 | * The new extent is pinned in the extent map, and we don't want |
| 1647 | * to drop it from the cache until it is completely in the btree. |
| 1648 | * |
| 1649 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1650 | * the caller is expected to unpin it and allow it to be merged |
| 1651 | * with the others. |
| 1652 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1653 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1654 | &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1655 | BUG_ON(ret); |
| 1656 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1657 | ins.objectid = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1658 | ins.offset = file_pos; |
| 1659 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1660 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1661 | BUG_ON(ret); |
| 1662 | leaf = path->nodes[0]; |
| 1663 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1664 | struct btrfs_file_extent_item); |
| 1665 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1666 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1667 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1668 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1669 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1670 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1671 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1672 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1673 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1674 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1675 | |
| 1676 | btrfs_unlock_up_safe(path, 1); |
| 1677 | btrfs_set_lock_blocking(leaf); |
| 1678 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1679 | btrfs_mark_buffer_dirty(leaf); |
| 1680 | |
| 1681 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1682 | |
| 1683 | ins.objectid = disk_bytenr; |
| 1684 | ins.offset = disk_num_bytes; |
| 1685 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1686 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1687 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1688 | btrfs_ino(inode), file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1689 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1690 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1691 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1692 | return 0; |
| 1693 | } |
| 1694 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1695 | /* |
| 1696 | * helper function for btrfs_finish_ordered_io, this |
| 1697 | * just reads in some of the csum leaves to prime them into ram |
| 1698 | * before we start the transaction. It limits the amount of btree |
| 1699 | * reads required while inside the transaction. |
| 1700 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1701 | /* as ordered data IO finishes, this gets called so we can finish |
| 1702 | * an ordered extent if the range of bytes in the file it covers are |
| 1703 | * fully written. |
| 1704 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1705 | 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] | 1706 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1707 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1708 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1709 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1710 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1711 | struct extent_state *cached_state = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1712 | int compress_type = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1713 | int ret; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1714 | bool nolock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1715 | |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 1716 | ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 1717 | end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1718 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1719 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1720 | BUG_ON(!ordered_extent); |
Josef Bacik | efd049f | 2010-02-02 20:50:10 +0000 | [diff] [blame] | 1721 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1722 | nolock = is_free_space_inode(root, inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1723 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1724 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
| 1725 | BUG_ON(!list_empty(&ordered_extent->list)); |
| 1726 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1727 | if (!ret) { |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1728 | if (nolock) |
| 1729 | trans = btrfs_join_transaction_nolock(root, 1); |
| 1730 | else |
| 1731 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1732 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1733 | btrfs_set_trans_block_group(trans, inode); |
| 1734 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1735 | ret = btrfs_update_inode(trans, root, inode); |
| 1736 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1737 | } |
| 1738 | goto out; |
| 1739 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1740 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1741 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 1742 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1743 | 0, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1744 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1745 | if (nolock) |
| 1746 | trans = btrfs_join_transaction_nolock(root, 1); |
| 1747 | else |
| 1748 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1749 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1750 | btrfs_set_trans_block_group(trans, inode); |
| 1751 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1752 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1753 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1754 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1755 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1756 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1757 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1758 | ordered_extent->file_offset, |
| 1759 | ordered_extent->file_offset + |
| 1760 | ordered_extent->len); |
| 1761 | BUG_ON(ret); |
| 1762 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 1763 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1764 | ret = insert_reserved_file_extent(trans, inode, |
| 1765 | ordered_extent->file_offset, |
| 1766 | ordered_extent->start, |
| 1767 | ordered_extent->disk_len, |
| 1768 | ordered_extent->len, |
| 1769 | ordered_extent->len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1770 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1771 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1772 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1773 | ordered_extent->file_offset, |
| 1774 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1775 | BUG_ON(ret); |
| 1776 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1777 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 1778 | ordered_extent->file_offset + |
| 1779 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
| 1780 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1781 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1782 | &ordered_extent->list); |
| 1783 | |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 1784 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1785 | if (!ret) { |
| 1786 | ret = btrfs_update_inode(trans, root, inode); |
| 1787 | BUG_ON(ret); |
| 1788 | } |
| 1789 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1790 | out: |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1791 | if (nolock) { |
| 1792 | if (trans) |
| 1793 | btrfs_end_transaction_nolock(trans, root); |
| 1794 | } else { |
| 1795 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
| 1796 | if (trans) |
| 1797 | btrfs_end_transaction(trans, root); |
| 1798 | } |
| 1799 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1800 | /* once for us */ |
| 1801 | btrfs_put_ordered_extent(ordered_extent); |
| 1802 | /* once for the tree */ |
| 1803 | btrfs_put_ordered_extent(ordered_extent); |
| 1804 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1805 | return 0; |
| 1806 | } |
| 1807 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1808 | 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] | 1809 | struct extent_state *state, int uptodate) |
| 1810 | { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1811 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 1812 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1813 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1814 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1815 | } |
| 1816 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1817 | /* |
| 1818 | * When IO fails, either with EIO or csum verification fails, we |
| 1819 | * try other mirrors that might have a good copy of the data. This |
| 1820 | * io_failure_record is used to record state as we go through all the |
| 1821 | * mirrors. If another mirror has good data, the page is set up to date |
| 1822 | * and things continue. If a good mirror can't be found, the original |
| 1823 | * bio end_io callback is called to indicate things have failed. |
| 1824 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1825 | struct io_failure_record { |
| 1826 | struct page *page; |
| 1827 | u64 start; |
| 1828 | u64 len; |
| 1829 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1830 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1831 | int last_mirror; |
| 1832 | }; |
| 1833 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1834 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1835 | struct page *page, u64 start, u64 end, |
| 1836 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1837 | { |
| 1838 | struct io_failure_record *failrec = NULL; |
| 1839 | u64 private; |
| 1840 | struct extent_map *em; |
| 1841 | struct inode *inode = page->mapping->host; |
| 1842 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1843 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1844 | struct bio *bio; |
| 1845 | int num_copies; |
| 1846 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1847 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1848 | u64 logical; |
| 1849 | |
| 1850 | ret = get_state_private(failure_tree, start, &private); |
| 1851 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1852 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1853 | if (!failrec) |
| 1854 | return -ENOMEM; |
| 1855 | failrec->start = start; |
| 1856 | failrec->len = end - start + 1; |
| 1857 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1858 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1859 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1860 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1861 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1862 | if (em->start > start || em->start + em->len < start) { |
| 1863 | free_extent_map(em); |
| 1864 | em = NULL; |
| 1865 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1866 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1867 | |
| 1868 | if (!em || IS_ERR(em)) { |
| 1869 | kfree(failrec); |
| 1870 | return -EIO; |
| 1871 | } |
| 1872 | logical = start - em->start; |
| 1873 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1874 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1875 | logical = em->block_start; |
| 1876 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1877 | extent_set_compress_type(&failrec->bio_flags, |
| 1878 | em->compress_type); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1879 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1880 | failrec->logical = logical; |
| 1881 | free_extent_map(em); |
| 1882 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1883 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1884 | set_state_private(failure_tree, start, |
| 1885 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1886 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1887 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1888 | } |
| 1889 | num_copies = btrfs_num_copies( |
| 1890 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1891 | failrec->logical, failrec->len); |
| 1892 | failrec->last_mirror++; |
| 1893 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1894 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1895 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1896 | failrec->start, |
| 1897 | EXTENT_LOCKED); |
| 1898 | if (state && state->start != failrec->start) |
| 1899 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1900 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1901 | } |
| 1902 | if (!state || failrec->last_mirror > num_copies) { |
| 1903 | set_state_private(failure_tree, failrec->start, 0); |
| 1904 | clear_extent_bits(failure_tree, failrec->start, |
| 1905 | failrec->start + failrec->len - 1, |
| 1906 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1907 | kfree(failrec); |
| 1908 | return -EIO; |
| 1909 | } |
| 1910 | bio = bio_alloc(GFP_NOFS, 1); |
| 1911 | bio->bi_private = state; |
| 1912 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1913 | bio->bi_sector = failrec->logical >> 9; |
| 1914 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1915 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1916 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1917 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1918 | if (failed_bio->bi_rw & REQ_WRITE) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1919 | rw = WRITE; |
| 1920 | else |
| 1921 | rw = READ; |
| 1922 | |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1923 | ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1924 | failrec->last_mirror, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1925 | failrec->bio_flags, 0); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1926 | return ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1927 | } |
| 1928 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1929 | /* |
| 1930 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1931 | * to see if we need to process or clean up an io_failure_record |
| 1932 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1933 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1934 | { |
| 1935 | u64 private; |
| 1936 | u64 private_failure; |
| 1937 | struct io_failure_record *failure; |
| 1938 | int ret; |
| 1939 | |
| 1940 | private = 0; |
| 1941 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1942 | (u64)-1, 1, EXTENT_DIRTY, 0)) { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1943 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1944 | start, &private_failure); |
| 1945 | if (ret == 0) { |
| 1946 | failure = (struct io_failure_record *)(unsigned long) |
| 1947 | private_failure; |
| 1948 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1949 | failure->start, 0); |
| 1950 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1951 | failure->start, |
| 1952 | failure->start + failure->len - 1, |
| 1953 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1954 | GFP_NOFS); |
| 1955 | kfree(failure); |
| 1956 | } |
| 1957 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1958 | return 0; |
| 1959 | } |
| 1960 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1961 | /* |
| 1962 | * when reads are done, we need to check csums to verify the data is correct |
| 1963 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1964 | * the io_failure_record routines to find good copies |
| 1965 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1966 | 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] | 1967 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1968 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1969 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1970 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1971 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1972 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1973 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1974 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1975 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1976 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1977 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1978 | if (PageChecked(page)) { |
| 1979 | ClearPageChecked(page); |
| 1980 | goto good; |
| 1981 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1982 | |
| 1983 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1984 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1985 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1986 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1987 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1988 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1989 | GFP_NOFS); |
| 1990 | return 0; |
| 1991 | } |
| 1992 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1993 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1994 | private = state->private; |
| 1995 | ret = 0; |
| 1996 | } else { |
| 1997 | ret = get_state_private(io_tree, start, &private); |
| 1998 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1999 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2000 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2001 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2002 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2003 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 2004 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2005 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2006 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2007 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2008 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2009 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2010 | /* if the io failure tree for this inode is non-empty, |
| 2011 | * check to see if we've recovered from a failed IO |
| 2012 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2013 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2014 | return 0; |
| 2015 | |
| 2016 | zeroit: |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 2017 | if (printk_ratelimit()) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2018 | printk(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u " |
| 2019 | "private %llu\n", |
| 2020 | (unsigned long long)btrfs_ino(page->mapping->host), |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 2021 | (unsigned long long)start, csum, |
| 2022 | (unsigned long long)private); |
| 2023 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2024 | memset(kaddr + offset, 1, end - start + 1); |
| 2025 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2026 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2027 | if (private == 0) |
| 2028 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2029 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2030 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2031 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2032 | struct delayed_iput { |
| 2033 | struct list_head list; |
| 2034 | struct inode *inode; |
| 2035 | }; |
| 2036 | |
| 2037 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2038 | { |
| 2039 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2040 | struct delayed_iput *delayed; |
| 2041 | |
| 2042 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2043 | return; |
| 2044 | |
| 2045 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2046 | delayed->inode = inode; |
| 2047 | |
| 2048 | spin_lock(&fs_info->delayed_iput_lock); |
| 2049 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2050 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2051 | } |
| 2052 | |
| 2053 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2054 | { |
| 2055 | LIST_HEAD(list); |
| 2056 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2057 | struct delayed_iput *delayed; |
| 2058 | int empty; |
| 2059 | |
| 2060 | spin_lock(&fs_info->delayed_iput_lock); |
| 2061 | empty = list_empty(&fs_info->delayed_iputs); |
| 2062 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2063 | if (empty) |
| 2064 | return; |
| 2065 | |
| 2066 | down_read(&root->fs_info->cleanup_work_sem); |
| 2067 | spin_lock(&fs_info->delayed_iput_lock); |
| 2068 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2069 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2070 | |
| 2071 | while (!list_empty(&list)) { |
| 2072 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2073 | list_del(&delayed->list); |
| 2074 | iput(delayed->inode); |
| 2075 | kfree(delayed); |
| 2076 | } |
| 2077 | up_read(&root->fs_info->cleanup_work_sem); |
| 2078 | } |
| 2079 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2080 | /* |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2081 | * calculate extra metadata reservation when snapshotting a subvolume |
| 2082 | * contains orphan files. |
| 2083 | */ |
| 2084 | void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans, |
| 2085 | struct btrfs_pending_snapshot *pending, |
| 2086 | u64 *bytes_to_reserve) |
| 2087 | { |
| 2088 | struct btrfs_root *root; |
| 2089 | struct btrfs_block_rsv *block_rsv; |
| 2090 | u64 num_bytes; |
| 2091 | int index; |
| 2092 | |
| 2093 | root = pending->root; |
| 2094 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2095 | return; |
| 2096 | |
| 2097 | block_rsv = root->orphan_block_rsv; |
| 2098 | |
| 2099 | /* orphan block reservation for the snapshot */ |
| 2100 | num_bytes = block_rsv->size; |
| 2101 | |
| 2102 | /* |
| 2103 | * after the snapshot is created, COWing tree blocks may use more |
| 2104 | * space than it frees. So we should make sure there is enough |
| 2105 | * reserved space. |
| 2106 | */ |
| 2107 | index = trans->transid & 0x1; |
| 2108 | if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) { |
| 2109 | num_bytes += block_rsv->size - |
| 2110 | (block_rsv->reserved + block_rsv->freed[index]); |
| 2111 | } |
| 2112 | |
| 2113 | *bytes_to_reserve += num_bytes; |
| 2114 | } |
| 2115 | |
| 2116 | void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans, |
| 2117 | struct btrfs_pending_snapshot *pending) |
| 2118 | { |
| 2119 | struct btrfs_root *root = pending->root; |
| 2120 | struct btrfs_root *snap = pending->snap; |
| 2121 | struct btrfs_block_rsv *block_rsv; |
| 2122 | u64 num_bytes; |
| 2123 | int index; |
| 2124 | int ret; |
| 2125 | |
| 2126 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2127 | return; |
| 2128 | |
| 2129 | /* refill source subvolume's orphan block reservation */ |
| 2130 | block_rsv = root->orphan_block_rsv; |
| 2131 | index = trans->transid & 0x1; |
| 2132 | if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) { |
| 2133 | num_bytes = block_rsv->size - |
| 2134 | (block_rsv->reserved + block_rsv->freed[index]); |
| 2135 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2136 | root->orphan_block_rsv, |
| 2137 | num_bytes); |
| 2138 | BUG_ON(ret); |
| 2139 | } |
| 2140 | |
| 2141 | /* setup orphan block reservation for the snapshot */ |
| 2142 | block_rsv = btrfs_alloc_block_rsv(snap); |
| 2143 | BUG_ON(!block_rsv); |
| 2144 | |
| 2145 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2146 | snap->orphan_block_rsv = block_rsv; |
| 2147 | |
| 2148 | num_bytes = root->orphan_block_rsv->size; |
| 2149 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2150 | block_rsv, num_bytes); |
| 2151 | BUG_ON(ret); |
| 2152 | |
| 2153 | #if 0 |
| 2154 | /* insert orphan item for the snapshot */ |
| 2155 | WARN_ON(!root->orphan_item_inserted); |
| 2156 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2157 | snap->root_key.objectid); |
| 2158 | BUG_ON(ret); |
| 2159 | snap->orphan_item_inserted = 1; |
| 2160 | #endif |
| 2161 | } |
| 2162 | |
| 2163 | enum btrfs_orphan_cleanup_state { |
| 2164 | ORPHAN_CLEANUP_STARTED = 1, |
| 2165 | ORPHAN_CLEANUP_DONE = 2, |
| 2166 | }; |
| 2167 | |
| 2168 | /* |
| 2169 | * This is called in transaction commmit time. If there are no orphan |
| 2170 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2171 | * structure. |
| 2172 | */ |
| 2173 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2174 | struct btrfs_root *root) |
| 2175 | { |
| 2176 | int ret; |
| 2177 | |
| 2178 | if (!list_empty(&root->orphan_list) || |
| 2179 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2180 | return; |
| 2181 | |
| 2182 | if (root->orphan_item_inserted && |
| 2183 | btrfs_root_refs(&root->root_item) > 0) { |
| 2184 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2185 | root->root_key.objectid); |
| 2186 | BUG_ON(ret); |
| 2187 | root->orphan_item_inserted = 0; |
| 2188 | } |
| 2189 | |
| 2190 | if (root->orphan_block_rsv) { |
| 2191 | WARN_ON(root->orphan_block_rsv->size > 0); |
| 2192 | btrfs_free_block_rsv(root, root->orphan_block_rsv); |
| 2193 | root->orphan_block_rsv = NULL; |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2198 | * This creates an orphan entry for the given inode in case something goes |
| 2199 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2200 | * |
| 2201 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2202 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2203 | */ |
| 2204 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2205 | { |
| 2206 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2207 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2208 | int reserve = 0; |
| 2209 | int insert = 0; |
| 2210 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2211 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2212 | if (!root->orphan_block_rsv) { |
| 2213 | block_rsv = btrfs_alloc_block_rsv(root); |
| 2214 | BUG_ON(!block_rsv); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2215 | } |
| 2216 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2217 | spin_lock(&root->orphan_lock); |
| 2218 | if (!root->orphan_block_rsv) { |
| 2219 | root->orphan_block_rsv = block_rsv; |
| 2220 | } else if (block_rsv) { |
| 2221 | btrfs_free_block_rsv(root, block_rsv); |
| 2222 | block_rsv = NULL; |
| 2223 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2224 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2225 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2226 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2227 | #if 0 |
| 2228 | /* |
| 2229 | * For proper ENOSPC handling, we should do orphan |
| 2230 | * cleanup when mounting. But this introduces backward |
| 2231 | * compatibility issue. |
| 2232 | */ |
| 2233 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2234 | insert = 2; |
| 2235 | else |
| 2236 | insert = 1; |
| 2237 | #endif |
| 2238 | insert = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2239 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2240 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2241 | if (!BTRFS_I(inode)->orphan_meta_reserved) { |
| 2242 | BTRFS_I(inode)->orphan_meta_reserved = 1; |
| 2243 | reserve = 1; |
| 2244 | } |
| 2245 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2246 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2247 | if (block_rsv) |
| 2248 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2249 | |
| 2250 | /* grab metadata reservation from transaction handle */ |
| 2251 | if (reserve) { |
| 2252 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
| 2253 | BUG_ON(ret); |
| 2254 | } |
| 2255 | |
| 2256 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2257 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2258 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2259 | BUG_ON(ret); |
| 2260 | } |
| 2261 | |
| 2262 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2263 | if (insert >= 2) { |
| 2264 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2265 | root->root_key.objectid); |
| 2266 | BUG_ON(ret); |
| 2267 | } |
| 2268 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | /* |
| 2272 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2273 | * item for this particular inode. |
| 2274 | */ |
| 2275 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2276 | { |
| 2277 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2278 | int delete_item = 0; |
| 2279 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2280 | int ret = 0; |
| 2281 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2282 | spin_lock(&root->orphan_lock); |
| 2283 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2284 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2285 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2286 | } |
| 2287 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2288 | if (BTRFS_I(inode)->orphan_meta_reserved) { |
| 2289 | BTRFS_I(inode)->orphan_meta_reserved = 0; |
| 2290 | release_rsv = 1; |
| 2291 | } |
| 2292 | spin_unlock(&root->orphan_lock); |
| 2293 | |
| 2294 | if (trans && delete_item) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2295 | ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode)); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2296 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2297 | } |
| 2298 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2299 | if (release_rsv) |
| 2300 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2301 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2302 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | /* |
| 2306 | * this cleans up any orphans that may be left on the list from the last use |
| 2307 | * of this root. |
| 2308 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2309 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2310 | { |
| 2311 | struct btrfs_path *path; |
| 2312 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2313 | struct btrfs_key key, found_key; |
| 2314 | struct btrfs_trans_handle *trans; |
| 2315 | struct inode *inode; |
| 2316 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2317 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2318 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2319 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2320 | |
| 2321 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2322 | if (!path) { |
| 2323 | ret = -ENOMEM; |
| 2324 | goto out; |
| 2325 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2326 | path->reada = -1; |
| 2327 | |
| 2328 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2329 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2330 | key.offset = (u64)-1; |
| 2331 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2332 | while (1) { |
| 2333 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2334 | if (ret < 0) |
| 2335 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2336 | |
| 2337 | /* |
| 2338 | * if ret == 0 means we found what we were searching for, which |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2339 | * is weird, but possible, so only screw with path if we didn't |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2340 | * find the key and see if we have stuff that matches |
| 2341 | */ |
| 2342 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2343 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2344 | if (path->slots[0] == 0) |
| 2345 | break; |
| 2346 | path->slots[0]--; |
| 2347 | } |
| 2348 | |
| 2349 | /* pull out the item */ |
| 2350 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2351 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2352 | |
| 2353 | /* make sure the item matches what we want */ |
| 2354 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2355 | break; |
| 2356 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2357 | break; |
| 2358 | |
| 2359 | /* release the path since we're done with it */ |
| 2360 | btrfs_release_path(root, path); |
| 2361 | |
| 2362 | /* |
| 2363 | * this is where we are basically btrfs_lookup, without the |
| 2364 | * crossing root thing. we store the inode number in the |
| 2365 | * offset of the orphan item. |
| 2366 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2367 | found_key.objectid = found_key.offset; |
| 2368 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2369 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2370 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2371 | if (IS_ERR(inode)) { |
| 2372 | ret = PTR_ERR(inode); |
| 2373 | goto out; |
| 2374 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2375 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2376 | /* |
| 2377 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2378 | * the proper thing when we hit it |
| 2379 | */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2380 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2381 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2382 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2383 | |
| 2384 | /* |
| 2385 | * if this is a bad inode, means we actually succeeded in |
| 2386 | * removing the inode, but not the orphan record, which means |
| 2387 | * we need to manually delete the orphan since iput will just |
| 2388 | * do a destroy_inode |
| 2389 | */ |
| 2390 | if (is_bad_inode(inode)) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2391 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2392 | if (IS_ERR(trans)) { |
| 2393 | ret = PTR_ERR(trans); |
| 2394 | goto out; |
| 2395 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2396 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2397 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2398 | iput(inode); |
| 2399 | continue; |
| 2400 | } |
| 2401 | |
| 2402 | /* if we have links, this was a truncate, lets do that */ |
| 2403 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 2404 | if (!S_ISREG(inode->i_mode)) { |
| 2405 | WARN_ON(1); |
| 2406 | iput(inode); |
| 2407 | continue; |
| 2408 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2409 | nr_truncate++; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2410 | ret = btrfs_truncate(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2411 | } else { |
| 2412 | nr_unlink++; |
| 2413 | } |
| 2414 | |
| 2415 | /* this will do delete_inode and everything for us */ |
| 2416 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2417 | if (ret) |
| 2418 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2419 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2420 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 2421 | |
| 2422 | if (root->orphan_block_rsv) |
| 2423 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 2424 | (u64)-1); |
| 2425 | |
| 2426 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
| 2427 | trans = btrfs_join_transaction(root, 1); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2428 | if (!IS_ERR(trans)) |
| 2429 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2430 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2431 | |
| 2432 | if (nr_unlink) |
| 2433 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2434 | if (nr_truncate) |
| 2435 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2436 | |
| 2437 | out: |
| 2438 | if (ret) |
| 2439 | printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret); |
| 2440 | btrfs_free_path(path); |
| 2441 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2442 | } |
| 2443 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2444 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2445 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2446 | * don't find any xattrs, we know there can't be any acls. |
| 2447 | * |
| 2448 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2449 | */ |
| 2450 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2451 | int slot, u64 objectid) |
| 2452 | { |
| 2453 | u32 nritems = btrfs_header_nritems(leaf); |
| 2454 | struct btrfs_key found_key; |
| 2455 | int scanned = 0; |
| 2456 | |
| 2457 | slot++; |
| 2458 | while (slot < nritems) { |
| 2459 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2460 | |
| 2461 | /* we found a different objectid, there must not be acls */ |
| 2462 | if (found_key.objectid != objectid) |
| 2463 | return 0; |
| 2464 | |
| 2465 | /* we found an xattr, assume we've got an acl */ |
| 2466 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2467 | return 1; |
| 2468 | |
| 2469 | /* |
| 2470 | * we found a key greater than an xattr key, there can't |
| 2471 | * be any acls later on |
| 2472 | */ |
| 2473 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2474 | return 0; |
| 2475 | |
| 2476 | slot++; |
| 2477 | scanned++; |
| 2478 | |
| 2479 | /* |
| 2480 | * it goes inode, inode backrefs, xattrs, extents, |
| 2481 | * so if there are a ton of hard links to an inode there can |
| 2482 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2483 | * this is just an optimization |
| 2484 | */ |
| 2485 | if (scanned >= 8) |
| 2486 | break; |
| 2487 | } |
| 2488 | /* we hit the end of the leaf before we found an xattr or |
| 2489 | * something larger than an xattr. We have to assume the inode |
| 2490 | * has acls |
| 2491 | */ |
| 2492 | return 1; |
| 2493 | } |
| 2494 | |
| 2495 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2496 | * read an inode from the btree into the in-memory inode |
| 2497 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2498 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2499 | { |
| 2500 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2501 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2502 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2503 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2504 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2505 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2506 | int maybe_acls; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2507 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2508 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2509 | int ret; |
| 2510 | |
| 2511 | path = btrfs_alloc_path(); |
| 2512 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2513 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2514 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2515 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2516 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2517 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2518 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2519 | leaf = path->nodes[0]; |
| 2520 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2521 | struct btrfs_inode_item); |
| 2522 | |
| 2523 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2524 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2525 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2526 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2527 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2528 | |
| 2529 | tspec = btrfs_inode_atime(inode_item); |
| 2530 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2531 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2532 | |
| 2533 | tspec = btrfs_inode_mtime(inode_item); |
| 2534 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2535 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2536 | |
| 2537 | tspec = btrfs_inode_ctime(inode_item); |
| 2538 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2539 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2540 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2541 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2542 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2543 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2544 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2545 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2546 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2547 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2548 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2549 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2550 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2551 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2552 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2553 | /* |
| 2554 | * try to precache a NULL acl entry for files that don't have |
| 2555 | * any xattrs or acls |
| 2556 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2557 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
| 2558 | btrfs_ino(inode)); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 2559 | if (!maybe_acls) |
| 2560 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2561 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2562 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2563 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2564 | btrfs_free_path(path); |
| 2565 | inode_item = NULL; |
| 2566 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2567 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2568 | case S_IFREG: |
| 2569 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2570 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2571 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2572 | inode->i_fop = &btrfs_file_operations; |
| 2573 | inode->i_op = &btrfs_file_inode_operations; |
| 2574 | break; |
| 2575 | case S_IFDIR: |
| 2576 | inode->i_fop = &btrfs_dir_file_operations; |
| 2577 | if (root == root->fs_info->tree_root) |
| 2578 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2579 | else |
| 2580 | inode->i_op = &btrfs_dir_inode_operations; |
| 2581 | break; |
| 2582 | case S_IFLNK: |
| 2583 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2584 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2585 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2586 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2587 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2588 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2589 | init_special_inode(inode, inode->i_mode, rdev); |
| 2590 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2591 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2592 | |
| 2593 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2594 | return; |
| 2595 | |
| 2596 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2597 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2598 | make_bad_inode(inode); |
| 2599 | } |
| 2600 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2601 | /* |
| 2602 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2603 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2604 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2605 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2606 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2607 | struct inode *inode) |
| 2608 | { |
Josef Bacik | 12ddb96 | 2011-04-05 13:02:27 -0400 | [diff] [blame] | 2609 | if (!leaf->map_token) |
| 2610 | map_private_extent_buffer(leaf, (unsigned long)item, |
| 2611 | sizeof(struct btrfs_inode_item), |
| 2612 | &leaf->map_token, &leaf->kaddr, |
| 2613 | &leaf->map_start, &leaf->map_len, |
| 2614 | KM_USER1); |
| 2615 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2616 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2617 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2618 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2619 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2620 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2621 | |
| 2622 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2623 | inode->i_atime.tv_sec); |
| 2624 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2625 | inode->i_atime.tv_nsec); |
| 2626 | |
| 2627 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2628 | inode->i_mtime.tv_sec); |
| 2629 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2630 | inode->i_mtime.tv_nsec); |
| 2631 | |
| 2632 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2633 | inode->i_ctime.tv_sec); |
| 2634 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2635 | inode->i_ctime.tv_nsec); |
| 2636 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2637 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2638 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2639 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2640 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2641 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2642 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2643 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Josef Bacik | 12ddb96 | 2011-04-05 13:02:27 -0400 | [diff] [blame] | 2644 | |
| 2645 | if (leaf->map_token) { |
| 2646 | unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); |
| 2647 | leaf->map_token = NULL; |
| 2648 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2649 | } |
| 2650 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2651 | /* |
| 2652 | * copy everything in the in-memory inode into the btree. |
| 2653 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2654 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2655 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2656 | { |
| 2657 | struct btrfs_inode_item *inode_item; |
| 2658 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2659 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2660 | int ret; |
| 2661 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2662 | /* |
| 2663 | * If root is tree root, it means this inode is used to |
| 2664 | * store free space information. And these inodes are updated |
| 2665 | * when committing the transaction, so they needn't delaye to |
| 2666 | * be updated, or deadlock will occured. |
| 2667 | */ |
Chris Mason | dcc6d07 | 2011-05-22 07:07:01 -0400 | [diff] [blame] | 2668 | if (!is_free_space_inode(root, inode)) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2669 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 2670 | if (!ret) |
| 2671 | btrfs_set_inode_last_trans(trans, inode); |
| 2672 | return ret; |
| 2673 | } |
| 2674 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2675 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2676 | if (!path) |
| 2677 | return -ENOMEM; |
| 2678 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2679 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2680 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 2681 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2682 | if (ret) { |
| 2683 | if (ret > 0) |
| 2684 | ret = -ENOENT; |
| 2685 | goto failed; |
| 2686 | } |
| 2687 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2688 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2689 | leaf = path->nodes[0]; |
| 2690 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2691 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2692 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2693 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2694 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2695 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2696 | ret = 0; |
| 2697 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2698 | btrfs_free_path(path); |
| 2699 | return ret; |
| 2700 | } |
| 2701 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2702 | /* |
| 2703 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2704 | * recovery code. It remove a link in a directory with a given name, and |
| 2705 | * also drops the back refs in the inode to the directory |
| 2706 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2707 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2708 | struct btrfs_root *root, |
| 2709 | struct inode *dir, struct inode *inode, |
| 2710 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2711 | { |
| 2712 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2713 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2714 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2715 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2716 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2717 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2718 | u64 ino = btrfs_ino(inode); |
| 2719 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2720 | |
| 2721 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2722 | if (!path) { |
| 2723 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 2724 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2725 | } |
| 2726 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2727 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2728 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2729 | name, name_len, -1); |
| 2730 | if (IS_ERR(di)) { |
| 2731 | ret = PTR_ERR(di); |
| 2732 | goto err; |
| 2733 | } |
| 2734 | if (!di) { |
| 2735 | ret = -ENOENT; |
| 2736 | goto err; |
| 2737 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2738 | leaf = path->nodes[0]; |
| 2739 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2740 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2741 | if (ret) |
| 2742 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2743 | btrfs_release_path(root, path); |
| 2744 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2745 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 2746 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2747 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2748 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2749 | "inode %llu parent %llu\n", name_len, name, |
| 2750 | (unsigned long long)ino, (unsigned long long)dir_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2751 | goto err; |
| 2752 | } |
| 2753 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2754 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
| 2755 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2756 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2757 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2758 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2759 | inode, dir_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2760 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2761 | |
| 2762 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2763 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 2764 | if (ret == -ENOENT) |
| 2765 | ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2766 | err: |
| 2767 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2768 | if (ret) |
| 2769 | goto out; |
| 2770 | |
| 2771 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2772 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2773 | btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2774 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2775 | return ret; |
| 2776 | } |
| 2777 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2778 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2779 | struct btrfs_root *root, |
| 2780 | struct inode *dir, struct inode *inode, |
| 2781 | const char *name, int name_len) |
| 2782 | { |
| 2783 | int ret; |
| 2784 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 2785 | if (!ret) { |
| 2786 | btrfs_drop_nlink(inode); |
| 2787 | ret = btrfs_update_inode(trans, root, inode); |
| 2788 | } |
| 2789 | return ret; |
| 2790 | } |
| 2791 | |
| 2792 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2793 | /* helper to check if there is any shared block in the path */ |
| 2794 | static int check_path_shared(struct btrfs_root *root, |
| 2795 | struct btrfs_path *path) |
| 2796 | { |
| 2797 | struct extent_buffer *eb; |
| 2798 | int level; |
Dan Carpenter | 0e4dcbef | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 2799 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2800 | |
| 2801 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2802 | int ret; |
| 2803 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2804 | if (!path->nodes[level]) |
| 2805 | break; |
| 2806 | eb = path->nodes[level]; |
| 2807 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2808 | continue; |
| 2809 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2810 | &refs, NULL); |
| 2811 | if (refs > 1) |
| 2812 | return 1; |
| 2813 | } |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2814 | return 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | /* |
| 2818 | * helper to start transaction for unlink and rmdir. |
| 2819 | * |
| 2820 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2821 | * so in enospc case, we should make sure they will free space before |
| 2822 | * allowing them to use the global metadata reservation. |
| 2823 | */ |
| 2824 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2825 | struct dentry *dentry) |
| 2826 | { |
| 2827 | struct btrfs_trans_handle *trans; |
| 2828 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2829 | struct btrfs_path *path; |
| 2830 | struct btrfs_inode_ref *ref; |
| 2831 | struct btrfs_dir_item *di; |
| 2832 | struct inode *inode = dentry->d_inode; |
| 2833 | u64 index; |
| 2834 | int check_link = 1; |
| 2835 | int err = -ENOSPC; |
| 2836 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2837 | u64 ino = btrfs_ino(inode); |
| 2838 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2839 | |
| 2840 | trans = btrfs_start_transaction(root, 10); |
| 2841 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 2842 | return trans; |
| 2843 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2844 | if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2845 | return ERR_PTR(-ENOSPC); |
| 2846 | |
| 2847 | /* check if there is someone else holds reference */ |
| 2848 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 2849 | return ERR_PTR(-ENOSPC); |
| 2850 | |
| 2851 | if (atomic_read(&inode->i_count) > 2) |
| 2852 | return ERR_PTR(-ENOSPC); |
| 2853 | |
| 2854 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 2855 | return ERR_PTR(-ENOSPC); |
| 2856 | |
| 2857 | path = btrfs_alloc_path(); |
| 2858 | if (!path) { |
| 2859 | root->fs_info->enospc_unlink = 0; |
| 2860 | return ERR_PTR(-ENOMEM); |
| 2861 | } |
| 2862 | |
| 2863 | trans = btrfs_start_transaction(root, 0); |
| 2864 | if (IS_ERR(trans)) { |
| 2865 | btrfs_free_path(path); |
| 2866 | root->fs_info->enospc_unlink = 0; |
| 2867 | return trans; |
| 2868 | } |
| 2869 | |
| 2870 | path->skip_locking = 1; |
| 2871 | path->search_commit_root = 1; |
| 2872 | |
| 2873 | ret = btrfs_lookup_inode(trans, root, path, |
| 2874 | &BTRFS_I(dir)->location, 0); |
| 2875 | if (ret < 0) { |
| 2876 | err = ret; |
| 2877 | goto out; |
| 2878 | } |
| 2879 | if (ret == 0) { |
| 2880 | if (check_path_shared(root, path)) |
| 2881 | goto out; |
| 2882 | } else { |
| 2883 | check_link = 0; |
| 2884 | } |
| 2885 | btrfs_release_path(root, path); |
| 2886 | |
| 2887 | ret = btrfs_lookup_inode(trans, root, path, |
| 2888 | &BTRFS_I(inode)->location, 0); |
| 2889 | if (ret < 0) { |
| 2890 | err = ret; |
| 2891 | goto out; |
| 2892 | } |
| 2893 | if (ret == 0) { |
| 2894 | if (check_path_shared(root, path)) |
| 2895 | goto out; |
| 2896 | } else { |
| 2897 | check_link = 0; |
| 2898 | } |
| 2899 | btrfs_release_path(root, path); |
| 2900 | |
| 2901 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 2902 | ret = btrfs_lookup_file_extent(trans, root, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2903 | ino, (u64)-1, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2904 | if (ret < 0) { |
| 2905 | err = ret; |
| 2906 | goto out; |
| 2907 | } |
| 2908 | BUG_ON(ret == 0); |
| 2909 | if (check_path_shared(root, path)) |
| 2910 | goto out; |
| 2911 | btrfs_release_path(root, path); |
| 2912 | } |
| 2913 | |
| 2914 | if (!check_link) { |
| 2915 | err = 0; |
| 2916 | goto out; |
| 2917 | } |
| 2918 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2919 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2920 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2921 | if (IS_ERR(di)) { |
| 2922 | err = PTR_ERR(di); |
| 2923 | goto out; |
| 2924 | } |
| 2925 | if (di) { |
| 2926 | if (check_path_shared(root, path)) |
| 2927 | goto out; |
| 2928 | } else { |
| 2929 | err = 0; |
| 2930 | goto out; |
| 2931 | } |
| 2932 | btrfs_release_path(root, path); |
| 2933 | |
| 2934 | ref = btrfs_lookup_inode_ref(trans, root, path, |
| 2935 | dentry->d_name.name, dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2936 | ino, dir_ino, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2937 | if (IS_ERR(ref)) { |
| 2938 | err = PTR_ERR(ref); |
| 2939 | goto out; |
| 2940 | } |
| 2941 | BUG_ON(!ref); |
| 2942 | if (check_path_shared(root, path)) |
| 2943 | goto out; |
| 2944 | index = btrfs_inode_ref_index(path->nodes[0], ref); |
| 2945 | btrfs_release_path(root, path); |
| 2946 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2947 | /* |
| 2948 | * This is a commit root search, if we can lookup inode item and other |
| 2949 | * relative items in the commit root, it means the transaction of |
| 2950 | * dir/file creation has been committed, and the dir index item that we |
| 2951 | * delay to insert has also been inserted into the commit root. So |
| 2952 | * we needn't worry about the delayed insertion of the dir index item |
| 2953 | * here. |
| 2954 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2955 | di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2956 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2957 | if (IS_ERR(di)) { |
| 2958 | err = PTR_ERR(di); |
| 2959 | goto out; |
| 2960 | } |
| 2961 | BUG_ON(ret == -ENOENT); |
| 2962 | if (check_path_shared(root, path)) |
| 2963 | goto out; |
| 2964 | |
| 2965 | err = 0; |
| 2966 | out: |
| 2967 | btrfs_free_path(path); |
| 2968 | if (err) { |
| 2969 | btrfs_end_transaction(trans, root); |
| 2970 | root->fs_info->enospc_unlink = 0; |
| 2971 | return ERR_PTR(err); |
| 2972 | } |
| 2973 | |
| 2974 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 2975 | return trans; |
| 2976 | } |
| 2977 | |
| 2978 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 2979 | struct btrfs_root *root) |
| 2980 | { |
| 2981 | if (trans->block_rsv == &root->fs_info->global_block_rsv) { |
| 2982 | BUG_ON(!root->fs_info->enospc_unlink); |
| 2983 | root->fs_info->enospc_unlink = 0; |
| 2984 | } |
| 2985 | btrfs_end_transaction_throttle(trans, root); |
| 2986 | } |
| 2987 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2988 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2989 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2990 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2991 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2992 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2993 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2994 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2995 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2996 | trans = __unlink_start_trans(dir, dentry); |
| 2997 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2998 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2999 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3000 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3001 | |
| 3002 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3003 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3004 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3005 | dentry->d_name.name, dentry->d_name.len); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3006 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3007 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3008 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3009 | ret = btrfs_orphan_add(trans, inode); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3010 | BUG_ON(ret); |
| 3011 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3012 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3013 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3014 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3015 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3016 | return ret; |
| 3017 | } |
| 3018 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3019 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3020 | struct btrfs_root *root, |
| 3021 | struct inode *dir, u64 objectid, |
| 3022 | const char *name, int name_len) |
| 3023 | { |
| 3024 | struct btrfs_path *path; |
| 3025 | struct extent_buffer *leaf; |
| 3026 | struct btrfs_dir_item *di; |
| 3027 | struct btrfs_key key; |
| 3028 | u64 index; |
| 3029 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3030 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3031 | |
| 3032 | path = btrfs_alloc_path(); |
| 3033 | if (!path) |
| 3034 | return -ENOMEM; |
| 3035 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3036 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3037 | name, name_len, -1); |
| 3038 | BUG_ON(!di || IS_ERR(di)); |
| 3039 | |
| 3040 | leaf = path->nodes[0]; |
| 3041 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3042 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3043 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 3044 | BUG_ON(ret); |
| 3045 | btrfs_release_path(root, path); |
| 3046 | |
| 3047 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3048 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3049 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3050 | if (ret < 0) { |
| 3051 | BUG_ON(ret != -ENOENT); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3052 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3053 | name, name_len); |
| 3054 | BUG_ON(!di || IS_ERR(di)); |
| 3055 | |
| 3056 | leaf = path->nodes[0]; |
| 3057 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 3058 | btrfs_release_path(root, path); |
| 3059 | index = key.offset; |
| 3060 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3061 | btrfs_release_path(root, path); |
| 3062 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3063 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
| 3064 | BUG_ON(ret); |
| 3065 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3066 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 3067 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 3068 | ret = btrfs_update_inode(trans, root, dir); |
| 3069 | BUG_ON(ret); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3070 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3071 | return 0; |
| 3072 | } |
| 3073 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3074 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3075 | { |
| 3076 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3077 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3078 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3079 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3080 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3081 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 3082 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3083 | btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3084 | return -ENOTEMPTY; |
| 3085 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3086 | trans = __unlink_start_trans(dir, dentry); |
| 3087 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3088 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3089 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3090 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3091 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3092 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3093 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3094 | BTRFS_I(inode)->location.objectid, |
| 3095 | dentry->d_name.name, |
| 3096 | dentry->d_name.len); |
| 3097 | goto out; |
| 3098 | } |
| 3099 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3100 | err = btrfs_orphan_add(trans, inode); |
| 3101 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3102 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3103 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3104 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3105 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3106 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3107 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3108 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3109 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3110 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3111 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3112 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3113 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3114 | return err; |
| 3115 | } |
| 3116 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3117 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3118 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3119 | * when truncating bytes in a file, it is possible to avoid reading |
| 3120 | * the leaves that contain only checksum items. This can be the |
| 3121 | * majority of the IO required to delete a large file, but it must |
| 3122 | * be done carefully. |
| 3123 | * |
| 3124 | * The keys in the level just above the leaves are checked to make sure |
| 3125 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 3126 | * after the new size. |
| 3127 | * |
| 3128 | * Then the key for the next leaf is checked to make sure it also has |
| 3129 | * a checksum item for the same file. If it does, we know our target leaf |
| 3130 | * contains only checksum items, and it can be safely freed without reading |
| 3131 | * it. |
| 3132 | * |
| 3133 | * This is just an optimization targeted at large files. It may do |
| 3134 | * nothing. It will return 0 unless things went badly. |
| 3135 | */ |
| 3136 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 3137 | struct btrfs_root *root, |
| 3138 | struct btrfs_path *path, |
| 3139 | struct inode *inode, u64 new_size) |
| 3140 | { |
| 3141 | struct btrfs_key key; |
| 3142 | int ret; |
| 3143 | int nritems; |
| 3144 | struct btrfs_key found_key; |
| 3145 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3146 | struct btrfs_leaf_ref *ref; |
| 3147 | u64 leaf_gen; |
| 3148 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3149 | |
| 3150 | path->lowest_level = 1; |
| 3151 | key.objectid = inode->i_ino; |
| 3152 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 3153 | key.offset = new_size; |
| 3154 | again: |
| 3155 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3156 | if (ret < 0) |
| 3157 | goto out; |
| 3158 | |
| 3159 | if (path->nodes[1] == NULL) { |
| 3160 | ret = 0; |
| 3161 | goto out; |
| 3162 | } |
| 3163 | ret = 0; |
| 3164 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 3165 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 3166 | |
| 3167 | if (!nritems) |
| 3168 | goto out; |
| 3169 | |
| 3170 | if (path->slots[1] >= nritems) |
| 3171 | goto next_node; |
| 3172 | |
| 3173 | /* did we find a key greater than anything we want to delete? */ |
| 3174 | if (found_key.objectid > inode->i_ino || |
| 3175 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 3176 | goto out; |
| 3177 | |
| 3178 | /* we check the next key in the node to make sure the leave contains |
| 3179 | * only checksum items. This comparison doesn't work if our |
| 3180 | * leaf is the last one in the node |
| 3181 | */ |
| 3182 | if (path->slots[1] + 1 >= nritems) { |
| 3183 | next_node: |
| 3184 | /* search forward from the last key in the node, this |
| 3185 | * will bring us into the next node in the tree |
| 3186 | */ |
| 3187 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 3188 | |
| 3189 | /* unlikely, but we inc below, so check to be safe */ |
| 3190 | if (found_key.offset == (u64)-1) |
| 3191 | goto out; |
| 3192 | |
| 3193 | /* search_forward needs a path with locks held, do the |
| 3194 | * search again for the original key. It is possible |
| 3195 | * this will race with a balance and return a path that |
| 3196 | * we could modify, but this drop is just an optimization |
| 3197 | * and is allowed to miss some leaves. |
| 3198 | */ |
| 3199 | btrfs_release_path(root, path); |
| 3200 | found_key.offset++; |
| 3201 | |
| 3202 | /* setup a max key for search_forward */ |
| 3203 | other_key.offset = (u64)-1; |
| 3204 | other_key.type = key.type; |
| 3205 | other_key.objectid = key.objectid; |
| 3206 | |
| 3207 | path->keep_locks = 1; |
| 3208 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 3209 | path, 0, 0); |
| 3210 | path->keep_locks = 0; |
| 3211 | if (ret || found_key.objectid != key.objectid || |
| 3212 | found_key.type != key.type) { |
| 3213 | ret = 0; |
| 3214 | goto out; |
| 3215 | } |
| 3216 | |
| 3217 | key.offset = found_key.offset; |
| 3218 | btrfs_release_path(root, path); |
| 3219 | cond_resched(); |
| 3220 | goto again; |
| 3221 | } |
| 3222 | |
| 3223 | /* we know there's one more slot after us in the tree, |
| 3224 | * read that key so we can verify it is also a checksum item |
| 3225 | */ |
| 3226 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 3227 | |
| 3228 | if (found_key.objectid < inode->i_ino) |
| 3229 | goto next_key; |
| 3230 | |
| 3231 | if (found_key.type != key.type || found_key.offset < new_size) |
| 3232 | goto next_key; |
| 3233 | |
| 3234 | /* |
| 3235 | * if the key for the next leaf isn't a csum key from this objectid, |
| 3236 | * we can't be sure there aren't good items inside this leaf. |
| 3237 | * Bail out |
| 3238 | */ |
| 3239 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 3240 | goto out; |
| 3241 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3242 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 3243 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3244 | /* |
| 3245 | * it is safe to delete this leaf, it contains only |
| 3246 | * csum items from this inode at an offset >= new_size |
| 3247 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3248 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3249 | BUG_ON(ret); |
| 3250 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3251 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 3252 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 3253 | if (ref) { |
| 3254 | ref->root_gen = root->root_key.offset; |
| 3255 | ref->bytenr = leaf_start; |
| 3256 | ref->owner = 0; |
| 3257 | ref->generation = leaf_gen; |
| 3258 | ref->nritems = 0; |
| 3259 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3260 | btrfs_sort_leaf_ref(ref); |
| 3261 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3262 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 3263 | WARN_ON(ret); |
| 3264 | btrfs_free_leaf_ref(root, ref); |
| 3265 | } else { |
| 3266 | WARN_ON(1); |
| 3267 | } |
| 3268 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3269 | next_key: |
| 3270 | btrfs_release_path(root, path); |
| 3271 | |
| 3272 | if (other_key.objectid == inode->i_ino && |
| 3273 | other_key.type == key.type && other_key.offset > key.offset) { |
| 3274 | key.offset = other_key.offset; |
| 3275 | cond_resched(); |
| 3276 | goto again; |
| 3277 | } |
| 3278 | ret = 0; |
| 3279 | out: |
| 3280 | /* fixup any changes we've made to the path */ |
| 3281 | path->lowest_level = 0; |
| 3282 | path->keep_locks = 0; |
| 3283 | btrfs_release_path(root, path); |
| 3284 | return ret; |
| 3285 | } |
| 3286 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3287 | #endif |
| 3288 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3289 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3290 | * this can truncate away extent items, csum items and directory items. |
| 3291 | * 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] | 3292 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3293 | * |
| 3294 | * csum items that cross the new i_size are truncated to the new size |
| 3295 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3296 | * |
| 3297 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3298 | * 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] | 3299 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3300 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3301 | struct btrfs_root *root, |
| 3302 | struct inode *inode, |
| 3303 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3304 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3305 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3306 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3307 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3308 | struct btrfs_key key; |
| 3309 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3310 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3311 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3312 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3313 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3314 | u64 mask = root->sectorsize - 1; |
| 3315 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3316 | int found_extent; |
| 3317 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3318 | int pending_del_nr = 0; |
| 3319 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3320 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3321 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3322 | int ret; |
| 3323 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3324 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3325 | |
| 3326 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3327 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3328 | if (root->ref_cows || root == root->fs_info->tree_root) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3329 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3330 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3331 | /* |
| 3332 | * This function is also used to drop the items in the log tree before |
| 3333 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 3334 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 3335 | * items. |
| 3336 | */ |
| 3337 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 3338 | btrfs_kill_delayed_inode_items(inode); |
| 3339 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3340 | path = btrfs_alloc_path(); |
| 3341 | BUG_ON(!path); |
Julia Lawall | 33c17ad | 2009-07-22 16:49:01 -0400 | [diff] [blame] | 3342 | path->reada = -1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3343 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3344 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3345 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3346 | key.type = (u8)-1; |
| 3347 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3348 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3349 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3350 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3351 | if (ret < 0) { |
| 3352 | err = ret; |
| 3353 | goto out; |
| 3354 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3355 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3356 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3357 | /* there are no items in the tree for us to truncate, we're |
| 3358 | * done |
| 3359 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3360 | if (path->slots[0] == 0) |
| 3361 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3362 | path->slots[0]--; |
| 3363 | } |
| 3364 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3365 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3366 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3367 | leaf = path->nodes[0]; |
| 3368 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3369 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3370 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3371 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3372 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3373 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3374 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3375 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3376 | break; |
| 3377 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3378 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3379 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3380 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3381 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3382 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3383 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 3384 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 3385 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 3386 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3387 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3388 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3389 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3390 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3391 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3392 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3393 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3394 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3395 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3396 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3397 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3398 | } else { |
| 3399 | if (item_end < new_size) |
| 3400 | break; |
| 3401 | if (found_key.offset >= new_size) |
| 3402 | del_item = 1; |
| 3403 | else |
| 3404 | del_item = 0; |
| 3405 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3406 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3407 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3408 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3409 | goto delete; |
| 3410 | |
| 3411 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3412 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3413 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3414 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3415 | u64 orig_num_bytes = |
| 3416 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3417 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3418 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3419 | extent_num_bytes = extent_num_bytes & |
| 3420 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3421 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3422 | extent_num_bytes); |
| 3423 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3424 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3425 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3426 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3427 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3428 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3429 | extent_num_bytes = |
| 3430 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3431 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3432 | extent_offset = found_key.offset - |
| 3433 | btrfs_file_extent_offset(leaf, fi); |
| 3434 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3435 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3436 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3437 | if (extent_start != 0) { |
| 3438 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3439 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3440 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3441 | } |
| 3442 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3443 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3444 | /* |
| 3445 | * we can't truncate inline items that have had |
| 3446 | * special encodings |
| 3447 | */ |
| 3448 | if (!del_item && |
| 3449 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3450 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3451 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3452 | u32 size = new_size - found_key.offset; |
| 3453 | |
| 3454 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3455 | inode_sub_bytes(inode, item_end + 1 - |
| 3456 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3457 | } |
| 3458 | size = |
| 3459 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3460 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3461 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3462 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3463 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3464 | inode_sub_bytes(inode, item_end + 1 - |
| 3465 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3466 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3467 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3468 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3469 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3470 | if (!pending_del_nr) { |
| 3471 | /* no pending yet, add ourselves */ |
| 3472 | pending_del_slot = path->slots[0]; |
| 3473 | pending_del_nr = 1; |
| 3474 | } else if (pending_del_nr && |
| 3475 | path->slots[0] + 1 == pending_del_slot) { |
| 3476 | /* hop on the pending chunk */ |
| 3477 | pending_del_nr++; |
| 3478 | pending_del_slot = path->slots[0]; |
| 3479 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3480 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3481 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3482 | } else { |
| 3483 | break; |
| 3484 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3485 | if (found_extent && (root->ref_cows || |
| 3486 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3487 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3488 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3489 | extent_num_bytes, 0, |
| 3490 | btrfs_header_owner(leaf), |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3491 | ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3492 | BUG_ON(ret); |
| 3493 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3494 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3495 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3496 | break; |
| 3497 | |
| 3498 | if (path->slots[0] == 0 || |
| 3499 | path->slots[0] != pending_del_slot) { |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 3500 | if (root->ref_cows && |
| 3501 | BTRFS_I(inode)->location.objectid != |
| 3502 | BTRFS_FREE_INO_OBJECTID) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3503 | err = -EAGAIN; |
| 3504 | goto out; |
| 3505 | } |
| 3506 | if (pending_del_nr) { |
| 3507 | ret = btrfs_del_items(trans, root, path, |
| 3508 | pending_del_slot, |
| 3509 | pending_del_nr); |
| 3510 | BUG_ON(ret); |
| 3511 | pending_del_nr = 0; |
| 3512 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3513 | btrfs_release_path(root, path); |
| 3514 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3515 | } else { |
| 3516 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3517 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3518 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3519 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3520 | if (pending_del_nr) { |
| 3521 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3522 | pending_del_nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3523 | BUG_ON(ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3524 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3525 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3526 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3527 | } |
| 3528 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3529 | /* |
| 3530 | * taken from block_truncate_page, but does cow as it zeros out |
| 3531 | * any bytes left in the last page in the file. |
| 3532 | */ |
| 3533 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3534 | { |
| 3535 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3536 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3537 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3538 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3539 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3540 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3541 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3542 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3543 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3544 | struct page *page; |
| 3545 | int ret = 0; |
| 3546 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3547 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3548 | |
| 3549 | if ((offset & (blocksize - 1)) == 0) |
| 3550 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3551 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3552 | if (ret) |
| 3553 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3554 | |
| 3555 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3556 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3557 | page = grab_cache_page(mapping, index); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3558 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3559 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3560 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3561 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3562 | |
| 3563 | page_start = page_offset(page); |
| 3564 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3565 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3566 | if (!PageUptodate(page)) { |
| 3567 | ret = btrfs_readpage(NULL, page); |
| 3568 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3569 | if (page->mapping != mapping) { |
| 3570 | unlock_page(page); |
| 3571 | page_cache_release(page); |
| 3572 | goto again; |
| 3573 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3574 | if (!PageUptodate(page)) { |
| 3575 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3576 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3577 | } |
| 3578 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3579 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3580 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3581 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 3582 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3583 | set_page_extent_mapped(page); |
| 3584 | |
| 3585 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3586 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3587 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3588 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3589 | unlock_page(page); |
| 3590 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3591 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3592 | btrfs_put_ordered_extent(ordered); |
| 3593 | goto again; |
| 3594 | } |
| 3595 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3596 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3597 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3598 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3599 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3600 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3601 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3602 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3603 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3604 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3605 | goto out_unlock; |
| 3606 | } |
| 3607 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3608 | ret = 0; |
| 3609 | if (offset != PAGE_CACHE_SIZE) { |
| 3610 | kaddr = kmap(page); |
| 3611 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3612 | flush_dcache_page(page); |
| 3613 | kunmap(page); |
| 3614 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3615 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3616 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3617 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3618 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3619 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3620 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3621 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3622 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3623 | unlock_page(page); |
| 3624 | page_cache_release(page); |
| 3625 | out: |
| 3626 | return ret; |
| 3627 | } |
| 3628 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 3629 | /* |
| 3630 | * This function puts in dummy file extents for the area we're creating a hole |
| 3631 | * for. So if we are truncating this file to a larger size we need to insert |
| 3632 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 3633 | * the range between oldsize and size |
| 3634 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3635 | 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] | 3636 | { |
| 3637 | struct btrfs_trans_handle *trans; |
| 3638 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3639 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3640 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3641 | struct extent_state *cached_state = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3642 | u64 mask = root->sectorsize - 1; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3643 | u64 hole_start = (oldsize + mask) & ~mask; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3644 | u64 block_end = (size + mask) & ~mask; |
| 3645 | u64 last_byte; |
| 3646 | u64 cur_offset; |
| 3647 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3648 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3649 | |
| 3650 | if (size <= hole_start) |
| 3651 | return 0; |
| 3652 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3653 | while (1) { |
| 3654 | struct btrfs_ordered_extent *ordered; |
| 3655 | btrfs_wait_ordered_range(inode, hole_start, |
| 3656 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3657 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
| 3658 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3659 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3660 | if (!ordered) |
| 3661 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3662 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3663 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3664 | btrfs_put_ordered_extent(ordered); |
| 3665 | } |
| 3666 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3667 | cur_offset = hole_start; |
| 3668 | while (1) { |
| 3669 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3670 | block_end - cur_offset, 0); |
| 3671 | BUG_ON(IS_ERR(em) || !em); |
| 3672 | last_byte = min(extent_map_end(em), block_end); |
| 3673 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3674 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3675 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3676 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3677 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3678 | trans = btrfs_start_transaction(root, 2); |
| 3679 | if (IS_ERR(trans)) { |
| 3680 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3681 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3682 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3683 | btrfs_set_trans_block_group(trans, inode); |
| 3684 | |
| 3685 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3686 | cur_offset + hole_size, |
| 3687 | &hint_byte, 1); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3688 | if (err) |
| 3689 | break; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3690 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3691 | err = btrfs_insert_file_extent(trans, root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3692 | btrfs_ino(inode), cur_offset, 0, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3693 | 0, hole_size, 0, hole_size, |
| 3694 | 0, 0, 0); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3695 | if (err) |
| 3696 | break; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3697 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3698 | btrfs_drop_extent_cache(inode, hole_start, |
| 3699 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3700 | |
| 3701 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3702 | } |
| 3703 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3704 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3705 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3706 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3707 | break; |
| 3708 | } |
| 3709 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3710 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3711 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3712 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3713 | return err; |
| 3714 | } |
| 3715 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3716 | static int btrfs_setsize(struct inode *inode, loff_t newsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3717 | { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3718 | loff_t oldsize = i_size_read(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3719 | int ret; |
| 3720 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3721 | if (newsize == oldsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3722 | return 0; |
| 3723 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3724 | if (newsize > oldsize) { |
| 3725 | i_size_write(inode, newsize); |
| 3726 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 3727 | truncate_pagecache(inode, oldsize, newsize); |
| 3728 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3729 | if (ret) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3730 | btrfs_setsize(inode, oldsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3731 | return ret; |
| 3732 | } |
| 3733 | |
Josef Bacik | 930f028 | 2011-03-04 14:41:41 -0500 | [diff] [blame] | 3734 | mark_inode_dirty(inode); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3735 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3736 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3737 | /* |
| 3738 | * We're truncating a file that used to have good data down to |
| 3739 | * zero. Make sure it gets into the ordered flush list so that |
| 3740 | * any new writes get down to disk quickly. |
| 3741 | */ |
| 3742 | if (newsize == 0) |
| 3743 | BTRFS_I(inode)->ordered_data_close = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3744 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3745 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3746 | truncate_setsize(inode, newsize); |
| 3747 | ret = btrfs_truncate(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3750 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3753 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3754 | { |
| 3755 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3756 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3757 | int err; |
| 3758 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3759 | if (btrfs_root_readonly(root)) |
| 3760 | return -EROFS; |
| 3761 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3762 | err = inode_change_ok(inode, attr); |
| 3763 | if (err) |
| 3764 | return err; |
| 3765 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3766 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3767 | err = btrfs_setsize(inode, attr->ia_size); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3768 | if (err) |
| 3769 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3770 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3771 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3772 | if (attr->ia_valid) { |
| 3773 | setattr_copy(inode, attr); |
| 3774 | mark_inode_dirty(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3775 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3776 | if (attr->ia_valid & ATTR_MODE) |
| 3777 | err = btrfs_acl_chmod(inode); |
| 3778 | } |
| 3779 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3780 | return err; |
| 3781 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3782 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3783 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3784 | { |
| 3785 | struct btrfs_trans_handle *trans; |
| 3786 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3787 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3788 | int ret; |
| 3789 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3790 | trace_btrfs_inode_evict(inode); |
| 3791 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3792 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3793 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 3794 | is_free_space_inode(root, inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3795 | goto no_delete; |
| 3796 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3797 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3798 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3799 | goto no_delete; |
| 3800 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3801 | /* 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] | 3802 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3803 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3804 | if (root->fs_info->log_root_recovering) { |
| 3805 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3806 | goto no_delete; |
| 3807 | } |
| 3808 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3809 | if (inode->i_nlink > 0) { |
| 3810 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3811 | goto no_delete; |
| 3812 | } |
| 3813 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3814 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3815 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3816 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3817 | trans = btrfs_start_transaction(root, 0); |
| 3818 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3819 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3820 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3821 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3822 | ret = btrfs_block_rsv_check(trans, root, |
| 3823 | root->orphan_block_rsv, 0, 5); |
| 3824 | if (ret) { |
| 3825 | BUG_ON(ret != -EAGAIN); |
| 3826 | ret = btrfs_commit_transaction(trans, root); |
| 3827 | BUG_ON(ret); |
| 3828 | continue; |
| 3829 | } |
| 3830 | |
| 3831 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3832 | if (ret != -EAGAIN) |
| 3833 | break; |
| 3834 | |
| 3835 | nr = trans->blocks_used; |
| 3836 | btrfs_end_transaction(trans, root); |
| 3837 | trans = NULL; |
| 3838 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3839 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3840 | } |
| 3841 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3842 | if (ret == 0) { |
| 3843 | ret = btrfs_orphan_del(trans, inode); |
| 3844 | BUG_ON(ret); |
| 3845 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3846 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3847 | if (!(root == root->fs_info->tree_root || |
| 3848 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3849 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3850 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3851 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3852 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3853 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3854 | no_delete: |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3855 | end_writeback(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3856 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | /* |
| 3860 | * this returns the key found in the dir entry in the location pointer. |
| 3861 | * If no dir entries were found, location->objectid is 0. |
| 3862 | */ |
| 3863 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3864 | struct btrfs_key *location) |
| 3865 | { |
| 3866 | const char *name = dentry->d_name.name; |
| 3867 | int namelen = dentry->d_name.len; |
| 3868 | struct btrfs_dir_item *di; |
| 3869 | struct btrfs_path *path; |
| 3870 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3871 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3872 | |
| 3873 | path = btrfs_alloc_path(); |
| 3874 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3875 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3876 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3877 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3878 | if (IS_ERR(di)) |
| 3879 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3880 | |
| 3881 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3882 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3883 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3884 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3885 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3886 | btrfs_free_path(path); |
| 3887 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3888 | out_err: |
| 3889 | location->objectid = 0; |
| 3890 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | /* |
| 3894 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3895 | * needs to be changed to reflect the root directory of the tree root. This |
| 3896 | * is kind of like crossing a mount point. |
| 3897 | */ |
| 3898 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3899 | struct inode *dir, |
| 3900 | struct dentry *dentry, |
| 3901 | struct btrfs_key *location, |
| 3902 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3903 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3904 | struct btrfs_path *path; |
| 3905 | struct btrfs_root *new_root; |
| 3906 | struct btrfs_root_ref *ref; |
| 3907 | struct extent_buffer *leaf; |
| 3908 | int ret; |
| 3909 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3910 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3911 | path = btrfs_alloc_path(); |
| 3912 | if (!path) { |
| 3913 | err = -ENOMEM; |
| 3914 | goto out; |
| 3915 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3916 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3917 | err = -ENOENT; |
| 3918 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3919 | BTRFS_I(dir)->root->root_key.objectid, |
| 3920 | location->objectid); |
| 3921 | if (ret) { |
| 3922 | if (ret < 0) |
| 3923 | err = ret; |
| 3924 | goto out; |
| 3925 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3926 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3927 | leaf = path->nodes[0]; |
| 3928 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3929 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3930 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3931 | goto out; |
| 3932 | |
| 3933 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3934 | (unsigned long)(ref + 1), |
| 3935 | dentry->d_name.len); |
| 3936 | if (ret) |
| 3937 | goto out; |
| 3938 | |
| 3939 | btrfs_release_path(root->fs_info->tree_root, path); |
| 3940 | |
| 3941 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3942 | if (IS_ERR(new_root)) { |
| 3943 | err = PTR_ERR(new_root); |
| 3944 | goto out; |
| 3945 | } |
| 3946 | |
| 3947 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3948 | err = -ENOENT; |
| 3949 | goto out; |
| 3950 | } |
| 3951 | |
| 3952 | *sub_root = new_root; |
| 3953 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3954 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3955 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3956 | err = 0; |
| 3957 | out: |
| 3958 | btrfs_free_path(path); |
| 3959 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3960 | } |
| 3961 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3962 | static void inode_tree_add(struct inode *inode) |
| 3963 | { |
| 3964 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3965 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3966 | struct rb_node **p; |
| 3967 | struct rb_node *parent; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3968 | u64 ino = btrfs_ino(inode); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3969 | again: |
| 3970 | p = &root->inode_tree.rb_node; |
| 3971 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3972 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 3973 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3974 | return; |
| 3975 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3976 | spin_lock(&root->inode_lock); |
| 3977 | while (*p) { |
| 3978 | parent = *p; |
| 3979 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3980 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3981 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3982 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3983 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3984 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3985 | else { |
| 3986 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 3987 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3988 | rb_erase(parent, &root->inode_tree); |
| 3989 | RB_CLEAR_NODE(parent); |
| 3990 | spin_unlock(&root->inode_lock); |
| 3991 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3992 | } |
| 3993 | } |
| 3994 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3995 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3996 | spin_unlock(&root->inode_lock); |
| 3997 | } |
| 3998 | |
| 3999 | static void inode_tree_del(struct inode *inode) |
| 4000 | { |
| 4001 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4002 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4003 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4004 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4005 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4006 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4007 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4008 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4009 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4010 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4011 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4012 | /* |
| 4013 | * Free space cache has inodes in the tree root, but the tree root has a |
| 4014 | * root_refs of 0, so this could end up dropping the tree root as a |
| 4015 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 4016 | * make sure we don't drop it. |
| 4017 | */ |
| 4018 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 4019 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4020 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 4021 | spin_lock(&root->inode_lock); |
| 4022 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 4023 | spin_unlock(&root->inode_lock); |
| 4024 | if (empty) |
| 4025 | btrfs_add_dead_root(root); |
| 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 4030 | { |
| 4031 | struct rb_node *node; |
| 4032 | struct rb_node *prev; |
| 4033 | struct btrfs_inode *entry; |
| 4034 | struct inode *inode; |
| 4035 | u64 objectid = 0; |
| 4036 | |
| 4037 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4038 | |
| 4039 | spin_lock(&root->inode_lock); |
| 4040 | again: |
| 4041 | node = root->inode_tree.rb_node; |
| 4042 | prev = NULL; |
| 4043 | while (node) { |
| 4044 | prev = node; |
| 4045 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4046 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4047 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4048 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4049 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4050 | node = node->rb_right; |
| 4051 | else |
| 4052 | break; |
| 4053 | } |
| 4054 | if (!node) { |
| 4055 | while (prev) { |
| 4056 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4057 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4058 | node = prev; |
| 4059 | break; |
| 4060 | } |
| 4061 | prev = rb_next(prev); |
| 4062 | } |
| 4063 | } |
| 4064 | while (node) { |
| 4065 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4066 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4067 | inode = igrab(&entry->vfs_inode); |
| 4068 | if (inode) { |
| 4069 | spin_unlock(&root->inode_lock); |
| 4070 | if (atomic_read(&inode->i_count) > 1) |
| 4071 | d_prune_aliases(inode); |
| 4072 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4073 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4074 | * the inode cache when its usage count |
| 4075 | * hits zero. |
| 4076 | */ |
| 4077 | iput(inode); |
| 4078 | cond_resched(); |
| 4079 | spin_lock(&root->inode_lock); |
| 4080 | goto again; |
| 4081 | } |
| 4082 | |
| 4083 | if (cond_resched_lock(&root->inode_lock)) |
| 4084 | goto again; |
| 4085 | |
| 4086 | node = rb_next(node); |
| 4087 | } |
| 4088 | spin_unlock(&root->inode_lock); |
| 4089 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4090 | } |
| 4091 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4092 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4093 | { |
| 4094 | struct btrfs_iget_args *args = p; |
| 4095 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4096 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4097 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4098 | return 0; |
| 4099 | } |
| 4100 | |
| 4101 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4102 | { |
| 4103 | struct btrfs_iget_args *args = opaque; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4104 | return args->ino == btrfs_ino(inode) && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4105 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4106 | } |
| 4107 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4108 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4109 | u64 objectid, |
| 4110 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4111 | { |
| 4112 | struct inode *inode; |
| 4113 | struct btrfs_iget_args args; |
| 4114 | args.ino = objectid; |
| 4115 | args.root = root; |
| 4116 | |
| 4117 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 4118 | btrfs_init_locked_inode, |
| 4119 | (void *)&args); |
| 4120 | return inode; |
| 4121 | } |
| 4122 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4123 | /* Get an inode object given its location and corresponding root. |
| 4124 | * Returns in *is_new if the inode was read from disk |
| 4125 | */ |
| 4126 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4127 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4128 | { |
| 4129 | struct inode *inode; |
| 4130 | |
| 4131 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4132 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4133 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4134 | |
| 4135 | if (inode->i_state & I_NEW) { |
| 4136 | BTRFS_I(inode)->root = root; |
| 4137 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4138 | btrfs_read_locked_inode(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4139 | inode_tree_add(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4140 | unlock_new_inode(inode); |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4141 | if (new) |
| 4142 | *new = 1; |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | return inode; |
| 4146 | } |
| 4147 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4148 | static struct inode *new_simple_dir(struct super_block *s, |
| 4149 | struct btrfs_key *key, |
| 4150 | struct btrfs_root *root) |
| 4151 | { |
| 4152 | struct inode *inode = new_inode(s); |
| 4153 | |
| 4154 | if (!inode) |
| 4155 | return ERR_PTR(-ENOMEM); |
| 4156 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4157 | BTRFS_I(inode)->root = root; |
| 4158 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 4159 | BTRFS_I(inode)->dummy_inode = 1; |
| 4160 | |
| 4161 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 4162 | inode->i_op = &simple_dir_inode_operations; |
| 4163 | inode->i_fop = &simple_dir_operations; |
| 4164 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 4165 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 4166 | |
| 4167 | return inode; |
| 4168 | } |
| 4169 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4170 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4171 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4172 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4173 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4174 | struct btrfs_root *sub_root = root; |
| 4175 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4176 | int index; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4177 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4178 | |
| 4179 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 4180 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4181 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4182 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4183 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4184 | if (ret < 0) |
| 4185 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4186 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4187 | if (location.objectid == 0) |
| 4188 | return NULL; |
| 4189 | |
| 4190 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4191 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4192 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4193 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4194 | |
| 4195 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4196 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4197 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4198 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4199 | &location, &sub_root); |
| 4200 | if (ret < 0) { |
| 4201 | if (ret != -ENOENT) |
| 4202 | inode = ERR_PTR(ret); |
| 4203 | else |
| 4204 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4205 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4206 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4207 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4208 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4209 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 4210 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4211 | down_read(&root->fs_info->cleanup_work_sem); |
| 4212 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4213 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4214 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4215 | if (ret) |
| 4216 | inode = ERR_PTR(ret); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4219 | return inode; |
| 4220 | } |
| 4221 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 4222 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4223 | { |
| 4224 | struct btrfs_root *root; |
| 4225 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4226 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 4227 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4228 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4229 | if (dentry->d_inode) { |
| 4230 | root = BTRFS_I(dentry->d_inode)->root; |
| 4231 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4232 | return 1; |
| 4233 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4234 | return 0; |
| 4235 | } |
| 4236 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4237 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 4238 | struct nameidata *nd) |
| 4239 | { |
| 4240 | struct inode *inode; |
| 4241 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4242 | inode = btrfs_lookup_dentry(dir, dentry); |
| 4243 | if (IS_ERR(inode)) |
| 4244 | return ERR_CAST(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4245 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4246 | return d_splice_alias(inode, dentry); |
| 4247 | } |
| 4248 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4249 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4250 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 4251 | }; |
| 4252 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 4253 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 4254 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4255 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4256 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4257 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4258 | struct btrfs_item *item; |
| 4259 | struct btrfs_dir_item *di; |
| 4260 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4261 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4262 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4263 | struct list_head ins_list; |
| 4264 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4265 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4266 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4267 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4268 | unsigned char d_type; |
| 4269 | int over = 0; |
| 4270 | u32 di_cur; |
| 4271 | u32 di_total; |
| 4272 | u32 di_len; |
| 4273 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4274 | char tmp_name[32]; |
| 4275 | char *name_ptr; |
| 4276 | int name_len; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4277 | int is_curr = 0; /* filp->f_pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4278 | |
| 4279 | /* FIXME, use a real flag for deciding about the key type */ |
| 4280 | if (root->fs_info->tree_root == root) |
| 4281 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4282 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4283 | /* special case for "." */ |
| 4284 | if (filp->f_pos == 0) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4285 | over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4286 | if (over) |
| 4287 | return 0; |
| 4288 | filp->f_pos = 1; |
| 4289 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4290 | /* special case for .., just use the back ref */ |
| 4291 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4292 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4293 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4294 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4295 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4296 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4297 | filp->f_pos = 2; |
| 4298 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4299 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4300 | if (!path) |
| 4301 | return -ENOMEM; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4302 | path->reada = 2; |
| 4303 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4304 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4305 | INIT_LIST_HEAD(&ins_list); |
| 4306 | INIT_LIST_HEAD(&del_list); |
| 4307 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 4308 | } |
| 4309 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4310 | btrfs_set_key_type(&key, key_type); |
| 4311 | key.offset = filp->f_pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4312 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4313 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4314 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4315 | if (ret < 0) |
| 4316 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4317 | |
| 4318 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4319 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4320 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4321 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4322 | ret = btrfs_next_leaf(root, path); |
| 4323 | if (ret < 0) |
| 4324 | goto err; |
| 4325 | else if (ret > 0) |
| 4326 | break; |
| 4327 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4328 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4329 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4330 | item = btrfs_item_nr(leaf, slot); |
| 4331 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4332 | |
| 4333 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4334 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4335 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4336 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4337 | if (found_key.offset < filp->f_pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4338 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4339 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 4340 | btrfs_should_delete_dir_index(&del_list, |
| 4341 | found_key.offset)) |
| 4342 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4343 | |
| 4344 | filp->f_pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4345 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4346 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4347 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 4348 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4349 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4350 | |
| 4351 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4352 | struct btrfs_key location; |
| 4353 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 4354 | if (verify_dir_item(root, leaf, di)) |
| 4355 | break; |
| 4356 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4357 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4358 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4359 | name_ptr = tmp_name; |
| 4360 | } else { |
| 4361 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4362 | if (!name_ptr) { |
| 4363 | ret = -ENOMEM; |
| 4364 | goto err; |
| 4365 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4366 | } |
| 4367 | read_extent_buffer(leaf, name_ptr, |
| 4368 | (unsigned long)(di + 1), name_len); |
| 4369 | |
| 4370 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4371 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4372 | |
| 4373 | /* is this a reference to our own snapshot? If so |
| 4374 | * skip it |
| 4375 | */ |
| 4376 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4377 | location.objectid == root->root_key.objectid) { |
| 4378 | over = 0; |
| 4379 | goto skip; |
| 4380 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4381 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4382 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4383 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4384 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4385 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4386 | if (name_ptr != tmp_name) |
| 4387 | kfree(name_ptr); |
| 4388 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4389 | if (over) |
| 4390 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4391 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4392 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4393 | di_cur += di_len; |
| 4394 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4395 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4396 | next: |
| 4397 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4398 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4399 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4400 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4401 | if (is_curr) |
| 4402 | filp->f_pos++; |
| 4403 | ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir, |
| 4404 | &ins_list); |
| 4405 | if (ret) |
| 4406 | goto nopos; |
| 4407 | } |
| 4408 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4409 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4410 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4411 | /* |
| 4412 | * 32-bit glibc will use getdents64, but then strtol - |
| 4413 | * so the last number we can serve is this. |
| 4414 | */ |
| 4415 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4416 | else |
| 4417 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4418 | nopos: |
| 4419 | ret = 0; |
| 4420 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4421 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 4422 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4423 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4424 | return ret; |
| 4425 | } |
| 4426 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4427 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4428 | { |
| 4429 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4430 | struct btrfs_trans_handle *trans; |
| 4431 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4432 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4433 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4434 | if (BTRFS_I(inode)->dummy_inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4435 | return 0; |
| 4436 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4437 | smp_mb(); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 4438 | if (root->fs_info->closing && is_free_space_inode(root, inode)) |
| 4439 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4440 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4441 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4442 | if (nolock) |
| 4443 | trans = btrfs_join_transaction_nolock(root, 1); |
| 4444 | else |
| 4445 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4446 | if (IS_ERR(trans)) |
| 4447 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4448 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4449 | if (nolock) |
| 4450 | ret = btrfs_end_transaction_nolock(trans, root); |
| 4451 | else |
| 4452 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4453 | } |
| 4454 | return ret; |
| 4455 | } |
| 4456 | |
| 4457 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4458 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4459 | * inode changes. But, it is most likely to find the inode in cache. |
| 4460 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4461 | * to keep or drop this code. |
| 4462 | */ |
| 4463 | void btrfs_dirty_inode(struct inode *inode) |
| 4464 | { |
| 4465 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4466 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4467 | int ret; |
| 4468 | |
| 4469 | if (BTRFS_I(inode)->dummy_inode) |
| 4470 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4471 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4472 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4473 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4474 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4475 | |
| 4476 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4477 | if (ret && ret == -ENOSPC) { |
| 4478 | /* whoops, lets try again with the full transaction */ |
| 4479 | btrfs_end_transaction(trans, root); |
| 4480 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4481 | if (IS_ERR(trans)) { |
| 4482 | if (printk_ratelimit()) { |
| 4483 | printk(KERN_ERR "btrfs: fail to " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4484 | "dirty inode %llu error %ld\n", |
| 4485 | (unsigned long long)btrfs_ino(inode), |
| 4486 | PTR_ERR(trans)); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4487 | } |
| 4488 | return; |
| 4489 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4490 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4491 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4492 | ret = btrfs_update_inode(trans, root, inode); |
| 4493 | if (ret) { |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4494 | if (printk_ratelimit()) { |
| 4495 | printk(KERN_ERR "btrfs: fail to " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4496 | "dirty inode %llu error %d\n", |
| 4497 | (unsigned long long)btrfs_ino(inode), |
| 4498 | ret); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4499 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4500 | } |
| 4501 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4502 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4503 | if (BTRFS_I(inode)->delayed_node) |
| 4504 | btrfs_balance_delayed_items(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4505 | } |
| 4506 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4507 | /* |
| 4508 | * find the highest existing sequence number in a directory |
| 4509 | * and then set the in-memory index_cnt variable to reflect |
| 4510 | * free sequence numbers |
| 4511 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4512 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4513 | { |
| 4514 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4515 | struct btrfs_key key, found_key; |
| 4516 | struct btrfs_path *path; |
| 4517 | struct extent_buffer *leaf; |
| 4518 | int ret; |
| 4519 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4520 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4521 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4522 | key.offset = (u64)-1; |
| 4523 | |
| 4524 | path = btrfs_alloc_path(); |
| 4525 | if (!path) |
| 4526 | return -ENOMEM; |
| 4527 | |
| 4528 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4529 | if (ret < 0) |
| 4530 | goto out; |
| 4531 | /* FIXME: we should be able to handle this */ |
| 4532 | if (ret == 0) |
| 4533 | goto out; |
| 4534 | ret = 0; |
| 4535 | |
| 4536 | /* |
| 4537 | * MAGIC NUMBER EXPLANATION: |
| 4538 | * since we search a directory based on f_pos we have to start at 2 |
| 4539 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4540 | * else has to start at 2 |
| 4541 | */ |
| 4542 | if (path->slots[0] == 0) { |
| 4543 | BTRFS_I(inode)->index_cnt = 2; |
| 4544 | goto out; |
| 4545 | } |
| 4546 | |
| 4547 | path->slots[0]--; |
| 4548 | |
| 4549 | leaf = path->nodes[0]; |
| 4550 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4551 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4552 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4553 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4554 | BTRFS_I(inode)->index_cnt = 2; |
| 4555 | goto out; |
| 4556 | } |
| 4557 | |
| 4558 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4559 | out: |
| 4560 | btrfs_free_path(path); |
| 4561 | return ret; |
| 4562 | } |
| 4563 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4564 | /* |
| 4565 | * helper to find a free sequence number in a given directory. This current |
| 4566 | * code is very simple, later versions will do smarter things in the btree |
| 4567 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4568 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4569 | { |
| 4570 | int ret = 0; |
| 4571 | |
| 4572 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4573 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 4574 | if (ret) { |
| 4575 | ret = btrfs_set_inode_index_count(dir); |
| 4576 | if (ret) |
| 4577 | return ret; |
| 4578 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4579 | } |
| 4580 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4581 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4582 | BTRFS_I(dir)->index_cnt++; |
| 4583 | |
| 4584 | return ret; |
| 4585 | } |
| 4586 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4587 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4588 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4589 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4590 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4591 | u64 ref_objectid, u64 objectid, |
| 4592 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4593 | { |
| 4594 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4595 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4596 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4597 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4598 | struct btrfs_inode_ref *ref; |
| 4599 | struct btrfs_key key[2]; |
| 4600 | u32 sizes[2]; |
| 4601 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4602 | int ret; |
| 4603 | int owner; |
| 4604 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4605 | path = btrfs_alloc_path(); |
| 4606 | BUG_ON(!path); |
| 4607 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4608 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4609 | if (!inode) { |
| 4610 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4611 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4612 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4613 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4614 | /* |
| 4615 | * we have to initialize this early, so we can reclaim the inode |
| 4616 | * number if we fail afterwards in this function. |
| 4617 | */ |
| 4618 | inode->i_ino = objectid; |
| 4619 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4620 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4621 | trace_btrfs_inode_request(dir); |
| 4622 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4623 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4624 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4625 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4626 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4627 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4628 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4629 | } |
| 4630 | /* |
| 4631 | * index_cnt is ignored for everything but a dir, |
| 4632 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4633 | * number |
| 4634 | */ |
| 4635 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4636 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4637 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 4638 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4639 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4640 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4641 | if (mode & S_IFDIR) |
| 4642 | owner = 0; |
| 4643 | else |
| 4644 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4645 | BTRFS_I(inode)->block_group = |
| 4646 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4647 | |
| 4648 | key[0].objectid = objectid; |
| 4649 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4650 | key[0].offset = 0; |
| 4651 | |
| 4652 | key[1].objectid = objectid; |
| 4653 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4654 | key[1].offset = ref_objectid; |
| 4655 | |
| 4656 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4657 | sizes[1] = name_len + sizeof(*ref); |
| 4658 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4659 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4660 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4661 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4662 | goto fail; |
| 4663 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 4664 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4665 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4666 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4667 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4668 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4669 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4670 | |
| 4671 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4672 | struct btrfs_inode_ref); |
| 4673 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4674 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4675 | ptr = (unsigned long)(ref + 1); |
| 4676 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4677 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4678 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4679 | btrfs_free_path(path); |
| 4680 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4681 | location = &BTRFS_I(inode)->location; |
| 4682 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4683 | location->offset = 0; |
| 4684 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4685 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4686 | btrfs_inherit_iflags(inode, dir); |
| 4687 | |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4688 | if ((mode & S_IFREG)) { |
| 4689 | if (btrfs_test_opt(root, NODATASUM)) |
| 4690 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 4691 | if (btrfs_test_opt(root, NODATACOW) || |
| 4692 | (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4693 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4694 | } |
| 4695 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4696 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4697 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4698 | |
| 4699 | trace_btrfs_inode_new(inode); |
| 4700 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4701 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4702 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4703 | if (dir) |
| 4704 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4705 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4706 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4707 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4711 | { |
| 4712 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4713 | } |
| 4714 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4715 | /* |
| 4716 | * utility function to add 'inode' into 'parent_inode' with |
| 4717 | * a give name and a given sequence number. |
| 4718 | * if 'add_backref' is true, also insert a backref from the |
| 4719 | * inode to the parent directory. |
| 4720 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4721 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4722 | struct inode *parent_inode, struct inode *inode, |
| 4723 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4724 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4725 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4726 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4727 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4728 | u64 ino = btrfs_ino(inode); |
| 4729 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4730 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4731 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4732 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4733 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4734 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4735 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4736 | key.offset = 0; |
| 4737 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4738 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4739 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4740 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4741 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4742 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4743 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4744 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 4745 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4746 | } |
| 4747 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4748 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4749 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4750 | parent_inode, &key, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4751 | btrfs_inode_type(inode), index); |
| 4752 | BUG_ON(ret); |
| 4753 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4754 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4755 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4756 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4757 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4758 | } |
| 4759 | return ret; |
| 4760 | } |
| 4761 | |
| 4762 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4763 | struct inode *dir, struct dentry *dentry, |
| 4764 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4765 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4766 | int err = btrfs_add_link(trans, dir, inode, |
| 4767 | dentry->d_name.name, dentry->d_name.len, |
| 4768 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4769 | if (!err) { |
| 4770 | d_instantiate(dentry, inode); |
| 4771 | return 0; |
| 4772 | } |
| 4773 | if (err > 0) |
| 4774 | err = -EEXIST; |
| 4775 | return err; |
| 4776 | } |
| 4777 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4778 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4779 | int mode, dev_t rdev) |
| 4780 | { |
| 4781 | struct btrfs_trans_handle *trans; |
| 4782 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4783 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4784 | int err; |
| 4785 | int drop_inode = 0; |
| 4786 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4787 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4788 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4789 | |
| 4790 | if (!new_valid_dev(rdev)) |
| 4791 | return -EINVAL; |
| 4792 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4793 | /* |
| 4794 | * 2 for inode item and ref |
| 4795 | * 2 for dir items |
| 4796 | * 1 for xattr if selinux is on |
| 4797 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4798 | trans = btrfs_start_transaction(root, 5); |
| 4799 | if (IS_ERR(trans)) |
| 4800 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4801 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4802 | btrfs_set_trans_block_group(trans, dir); |
| 4803 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4804 | err = btrfs_find_free_ino(root, &objectid); |
| 4805 | if (err) |
| 4806 | goto out_unlock; |
| 4807 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4808 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4809 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4810 | BTRFS_I(dir)->block_group, mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4811 | if (IS_ERR(inode)) { |
| 4812 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4813 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4814 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4815 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4816 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4817 | if (err) { |
| 4818 | drop_inode = 1; |
| 4819 | goto out_unlock; |
| 4820 | } |
| 4821 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4822 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4823 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4824 | if (err) |
| 4825 | drop_inode = 1; |
| 4826 | else { |
| 4827 | inode->i_op = &btrfs_special_inode_operations; |
| 4828 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4829 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4830 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4831 | btrfs_update_inode_block_group(trans, inode); |
| 4832 | btrfs_update_inode_block_group(trans, dir); |
| 4833 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4834 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4835 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4836 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4837 | if (drop_inode) { |
| 4838 | inode_dec_link_count(inode); |
| 4839 | iput(inode); |
| 4840 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4841 | return err; |
| 4842 | } |
| 4843 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4844 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4845 | int mode, struct nameidata *nd) |
| 4846 | { |
| 4847 | struct btrfs_trans_handle *trans; |
| 4848 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4849 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4850 | int drop_inode = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4851 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4852 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4853 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4854 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4855 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4856 | /* |
| 4857 | * 2 for inode item and ref |
| 4858 | * 2 for dir items |
| 4859 | * 1 for xattr if selinux is on |
| 4860 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4861 | trans = btrfs_start_transaction(root, 5); |
| 4862 | if (IS_ERR(trans)) |
| 4863 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4864 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4865 | btrfs_set_trans_block_group(trans, dir); |
| 4866 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4867 | err = btrfs_find_free_ino(root, &objectid); |
| 4868 | if (err) |
| 4869 | goto out_unlock; |
| 4870 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4871 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4872 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4873 | BTRFS_I(dir)->block_group, mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4874 | if (IS_ERR(inode)) { |
| 4875 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4876 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4877 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4878 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4879 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4880 | if (err) { |
| 4881 | drop_inode = 1; |
| 4882 | goto out_unlock; |
| 4883 | } |
| 4884 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4885 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4886 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4887 | if (err) |
| 4888 | drop_inode = 1; |
| 4889 | else { |
| 4890 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4891 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4892 | inode->i_fop = &btrfs_file_operations; |
| 4893 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4894 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4895 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4896 | btrfs_update_inode_block_group(trans, inode); |
| 4897 | btrfs_update_inode_block_group(trans, dir); |
| 4898 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4899 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4900 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4901 | if (drop_inode) { |
| 4902 | inode_dec_link_count(inode); |
| 4903 | iput(inode); |
| 4904 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4905 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4906 | return err; |
| 4907 | } |
| 4908 | |
| 4909 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4910 | struct dentry *dentry) |
| 4911 | { |
| 4912 | struct btrfs_trans_handle *trans; |
| 4913 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4914 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4915 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4916 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4917 | int err; |
| 4918 | int drop_inode = 0; |
| 4919 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4920 | /* do not allow sys_link's with other subvols of the same device */ |
| 4921 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 4922 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4923 | |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 4924 | if (inode->i_nlink == ~0U) |
| 4925 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4926 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4927 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4928 | if (err) |
| 4929 | goto fail; |
| 4930 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4931 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4932 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4933 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4934 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4935 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4936 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4937 | if (IS_ERR(trans)) { |
| 4938 | err = PTR_ERR(trans); |
| 4939 | goto fail; |
| 4940 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4941 | |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 4942 | btrfs_inc_nlink(inode); |
| 4943 | inode->i_ctime = CURRENT_TIME; |
| 4944 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4945 | btrfs_set_trans_block_group(trans, dir); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 4946 | ihold(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4947 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4948 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4949 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4950 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4951 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4952 | } else { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4953 | struct dentry *parent = dget_parent(dentry); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4954 | btrfs_update_inode_block_group(trans, dir); |
| 4955 | err = btrfs_update_inode(trans, root, inode); |
| 4956 | BUG_ON(err); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4957 | btrfs_log_new_name(trans, inode, NULL, parent); |
| 4958 | dput(parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4959 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4960 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4961 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4962 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4963 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4964 | if (drop_inode) { |
| 4965 | inode_dec_link_count(inode); |
| 4966 | iput(inode); |
| 4967 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4968 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4969 | return err; |
| 4970 | } |
| 4971 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4972 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4973 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4974 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4975 | struct btrfs_trans_handle *trans; |
| 4976 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4977 | int err = 0; |
| 4978 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4979 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4980 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4981 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4982 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4983 | /* |
| 4984 | * 2 items for inode and ref |
| 4985 | * 2 items for dir items |
| 4986 | * 1 for xattr if selinux is on |
| 4987 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4988 | trans = btrfs_start_transaction(root, 5); |
| 4989 | if (IS_ERR(trans)) |
| 4990 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4991 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4992 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4993 | err = btrfs_find_free_ino(root, &objectid); |
| 4994 | if (err) |
| 4995 | goto out_fail; |
| 4996 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4997 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4998 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4999 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 5000 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5001 | if (IS_ERR(inode)) { |
| 5002 | err = PTR_ERR(inode); |
| 5003 | goto out_fail; |
| 5004 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5005 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5006 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5007 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5008 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5009 | if (err) |
| 5010 | goto out_fail; |
| 5011 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5012 | inode->i_op = &btrfs_dir_inode_operations; |
| 5013 | inode->i_fop = &btrfs_dir_file_operations; |
| 5014 | btrfs_set_trans_block_group(trans, inode); |
| 5015 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5016 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5017 | err = btrfs_update_inode(trans, root, inode); |
| 5018 | if (err) |
| 5019 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5020 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5021 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 5022 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5023 | if (err) |
| 5024 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5025 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5026 | d_instantiate(dentry, inode); |
| 5027 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5028 | btrfs_update_inode_block_group(trans, inode); |
| 5029 | btrfs_update_inode_block_group(trans, dir); |
| 5030 | |
| 5031 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5032 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 5033 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5034 | if (drop_on_err) |
| 5035 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5036 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5037 | return err; |
| 5038 | } |
| 5039 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5040 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 5041 | * and an extent that you want to insert, deal with overlap and insert |
| 5042 | * the new extent into the tree. |
| 5043 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5044 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 5045 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5046 | struct extent_map *em, |
| 5047 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5048 | { |
| 5049 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5050 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5051 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 5052 | start_diff = map_start - em->start; |
| 5053 | em->start = map_start; |
| 5054 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5055 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 5056 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5057 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5058 | em->block_len -= start_diff; |
| 5059 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5060 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5061 | } |
| 5062 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5063 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 5064 | struct inode *inode, struct page *page, |
| 5065 | size_t pg_offset, u64 extent_offset, |
| 5066 | struct btrfs_file_extent_item *item) |
| 5067 | { |
| 5068 | int ret; |
| 5069 | struct extent_buffer *leaf = path->nodes[0]; |
| 5070 | char *tmp; |
| 5071 | size_t max_size; |
| 5072 | unsigned long inline_size; |
| 5073 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5074 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5075 | |
| 5076 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5077 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5078 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 5079 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 5080 | btrfs_item_nr(leaf, path->slots[0])); |
| 5081 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 5082 | if (!tmp) |
| 5083 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5084 | ptr = btrfs_file_extent_inline_start(item); |
| 5085 | |
| 5086 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 5087 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 5088 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5089 | ret = btrfs_decompress(compress_type, tmp, page, |
| 5090 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5091 | if (ret) { |
| 5092 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 5093 | unsigned long copy_size = min_t(u64, |
| 5094 | PAGE_CACHE_SIZE - pg_offset, |
| 5095 | max_size - extent_offset); |
| 5096 | memset(kaddr + pg_offset, 0, copy_size); |
| 5097 | kunmap_atomic(kaddr, KM_USER0); |
| 5098 | } |
| 5099 | kfree(tmp); |
| 5100 | return 0; |
| 5101 | } |
| 5102 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5103 | /* |
| 5104 | * 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] | 5105 | * the ugly parts come from merging extents from the disk with the in-ram |
| 5106 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5107 | * where the in-ram extents might be locked pending data=ordered completion. |
| 5108 | * |
| 5109 | * This also copies inline extents directly into the page. |
| 5110 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5111 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5112 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5113 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5114 | int create) |
| 5115 | { |
| 5116 | int ret; |
| 5117 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5118 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5119 | u64 extent_start = 0; |
| 5120 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5121 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5122 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5123 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5124 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5125 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5126 | struct extent_buffer *leaf; |
| 5127 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5128 | struct extent_map *em = NULL; |
| 5129 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5130 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5131 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5132 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5133 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5134 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5135 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5136 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5137 | if (em) |
| 5138 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5139 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5140 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5141 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5142 | if (em->start > start || em->start + em->len <= start) |
| 5143 | free_extent_map(em); |
| 5144 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5145 | free_extent_map(em); |
| 5146 | else |
| 5147 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5148 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5149 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5150 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5151 | err = -ENOMEM; |
| 5152 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5153 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5154 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5155 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 5156 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5157 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5158 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5159 | |
| 5160 | if (!path) { |
| 5161 | path = btrfs_alloc_path(); |
| 5162 | BUG_ON(!path); |
| 5163 | } |
| 5164 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5165 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 5166 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5167 | if (ret < 0) { |
| 5168 | err = ret; |
| 5169 | goto out; |
| 5170 | } |
| 5171 | |
| 5172 | if (ret != 0) { |
| 5173 | if (path->slots[0] == 0) |
| 5174 | goto not_found; |
| 5175 | path->slots[0]--; |
| 5176 | } |
| 5177 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5178 | leaf = path->nodes[0]; |
| 5179 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5180 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5181 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5182 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5183 | found_type = btrfs_key_type(&found_key); |
| 5184 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5185 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 5186 | goto not_found; |
| 5187 | } |
| 5188 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5189 | found_type = btrfs_file_extent_type(leaf, item); |
| 5190 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5191 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5192 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5193 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5194 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5195 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5196 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5197 | size_t size; |
| 5198 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5199 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 5200 | ~((u64)root->sectorsize - 1); |
| 5201 | } |
| 5202 | |
| 5203 | if (start >= extent_end) { |
| 5204 | path->slots[0]++; |
| 5205 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 5206 | ret = btrfs_next_leaf(root, path); |
| 5207 | if (ret < 0) { |
| 5208 | err = ret; |
| 5209 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5210 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5211 | if (ret > 0) |
| 5212 | goto not_found; |
| 5213 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5214 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5215 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5216 | if (found_key.objectid != objectid || |
| 5217 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 5218 | goto not_found; |
| 5219 | if (start + len <= found_key.offset) |
| 5220 | goto not_found; |
| 5221 | em->start = start; |
| 5222 | em->len = found_key.offset - start; |
| 5223 | goto not_found_em; |
| 5224 | } |
| 5225 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5226 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5227 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5228 | em->start = extent_start; |
| 5229 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5230 | em->orig_start = extent_start - |
| 5231 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5232 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 5233 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5234 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5235 | goto insert; |
| 5236 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5237 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5238 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5239 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5240 | em->block_start = bytenr; |
| 5241 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 5242 | item); |
| 5243 | } else { |
| 5244 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 5245 | em->block_start = bytenr; |
| 5246 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5247 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 5248 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5249 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5250 | goto insert; |
| 5251 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5252 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5253 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5254 | size_t size; |
| 5255 | size_t extent_offset; |
| 5256 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5257 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5258 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5259 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5260 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5261 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5262 | goto out; |
| 5263 | } |
| 5264 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5265 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5266 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5267 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5268 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5269 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5270 | em->len = (copy_size + root->sectorsize - 1) & |
| 5271 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5272 | em->orig_start = EXTENT_MAP_INLINE; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5273 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5274 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5275 | em->compress_type = compress_type; |
| 5276 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5277 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5278 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5279 | if (btrfs_file_extent_compression(leaf, item) != |
| 5280 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5281 | ret = uncompress_inline(path, inode, page, |
| 5282 | pg_offset, |
| 5283 | extent_offset, item); |
| 5284 | BUG_ON(ret); |
| 5285 | } else { |
| 5286 | map = kmap(page); |
| 5287 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 5288 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 5289 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 5290 | memset(map + pg_offset + copy_size, 0, |
| 5291 | PAGE_CACHE_SIZE - pg_offset - |
| 5292 | copy_size); |
| 5293 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5294 | kunmap(page); |
| 5295 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5296 | flush_dcache_page(page); |
| 5297 | } else if (create && PageUptodate(page)) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5298 | WARN_ON(1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5299 | if (!trans) { |
| 5300 | kunmap(page); |
| 5301 | free_extent_map(em); |
| 5302 | em = NULL; |
| 5303 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 5304 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5305 | if (IS_ERR(trans)) |
| 5306 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5307 | goto again; |
| 5308 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5309 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5310 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5311 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5312 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5313 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5314 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5315 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 5316 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5317 | goto insert; |
| 5318 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5319 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5320 | WARN_ON(1); |
| 5321 | } |
| 5322 | not_found: |
| 5323 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5324 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5325 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5326 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5327 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5328 | insert: |
| 5329 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5330 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5331 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 5332 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 5333 | (unsigned long long)em->len, |
| 5334 | (unsigned long long)start, |
| 5335 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5336 | err = -EIO; |
| 5337 | goto out; |
| 5338 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5339 | |
| 5340 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5341 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5342 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5343 | /* it is possible that someone inserted the extent into the tree |
| 5344 | * while we had the lock dropped. It is also possible that |
| 5345 | * an overlapping map exists in the tree |
| 5346 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5347 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5348 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5349 | |
| 5350 | ret = 0; |
| 5351 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5352 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5353 | if (existing && (existing->start > start || |
| 5354 | existing->start + existing->len <= start)) { |
| 5355 | free_extent_map(existing); |
| 5356 | existing = NULL; |
| 5357 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5358 | if (!existing) { |
| 5359 | existing = lookup_extent_mapping(em_tree, em->start, |
| 5360 | em->len); |
| 5361 | if (existing) { |
| 5362 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5363 | em, start, |
| 5364 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5365 | free_extent_map(existing); |
| 5366 | if (err) { |
| 5367 | free_extent_map(em); |
| 5368 | em = NULL; |
| 5369 | } |
| 5370 | } else { |
| 5371 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5372 | free_extent_map(em); |
| 5373 | em = NULL; |
| 5374 | } |
| 5375 | } else { |
| 5376 | free_extent_map(em); |
| 5377 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5378 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5379 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5380 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5381 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5382 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5383 | |
| 5384 | trace_btrfs_get_extent(root, em); |
| 5385 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5386 | if (path) |
| 5387 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5388 | if (trans) { |
| 5389 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5390 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5391 | err = ret; |
| 5392 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5393 | if (err) { |
| 5394 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5395 | return ERR_PTR(err); |
| 5396 | } |
| 5397 | return em; |
| 5398 | } |
| 5399 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5400 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 5401 | size_t pg_offset, u64 start, u64 len, |
| 5402 | int create) |
| 5403 | { |
| 5404 | struct extent_map *em; |
| 5405 | struct extent_map *hole_em = NULL; |
| 5406 | u64 range_start = start; |
| 5407 | u64 end; |
| 5408 | u64 found; |
| 5409 | u64 found_end; |
| 5410 | int err = 0; |
| 5411 | |
| 5412 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 5413 | if (IS_ERR(em)) |
| 5414 | return em; |
| 5415 | if (em) { |
| 5416 | /* |
| 5417 | * if our em maps to a hole, there might |
| 5418 | * actually be delalloc bytes behind it |
| 5419 | */ |
| 5420 | if (em->block_start != EXTENT_MAP_HOLE) |
| 5421 | return em; |
| 5422 | else |
| 5423 | hole_em = em; |
| 5424 | } |
| 5425 | |
| 5426 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 5427 | end = start + len; |
| 5428 | if (end < start) |
| 5429 | end = (u64)-1; |
| 5430 | else |
| 5431 | end -= 1; |
| 5432 | |
| 5433 | em = NULL; |
| 5434 | |
| 5435 | /* ok, we didn't find anything, lets look for delalloc */ |
| 5436 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 5437 | end, len, EXTENT_DELALLOC, 1); |
| 5438 | found_end = range_start + found; |
| 5439 | if (found_end < range_start) |
| 5440 | found_end = (u64)-1; |
| 5441 | |
| 5442 | /* |
| 5443 | * we didn't find anything useful, return |
| 5444 | * the original results from get_extent() |
| 5445 | */ |
| 5446 | if (range_start > end || found_end <= start) { |
| 5447 | em = hole_em; |
| 5448 | hole_em = NULL; |
| 5449 | goto out; |
| 5450 | } |
| 5451 | |
| 5452 | /* adjust the range_start to make sure it doesn't |
| 5453 | * go backwards from the start they passed in |
| 5454 | */ |
| 5455 | range_start = max(start,range_start); |
| 5456 | found = found_end - range_start; |
| 5457 | |
| 5458 | if (found > 0) { |
| 5459 | u64 hole_start = start; |
| 5460 | u64 hole_len = len; |
| 5461 | |
| 5462 | em = alloc_extent_map(GFP_NOFS); |
| 5463 | if (!em) { |
| 5464 | err = -ENOMEM; |
| 5465 | goto out; |
| 5466 | } |
| 5467 | /* |
| 5468 | * when btrfs_get_extent can't find anything it |
| 5469 | * returns one huge hole |
| 5470 | * |
| 5471 | * make sure what it found really fits our range, and |
| 5472 | * adjust to make sure it is based on the start from |
| 5473 | * the caller |
| 5474 | */ |
| 5475 | if (hole_em) { |
| 5476 | u64 calc_end = extent_map_end(hole_em); |
| 5477 | |
| 5478 | if (calc_end <= start || (hole_em->start > end)) { |
| 5479 | free_extent_map(hole_em); |
| 5480 | hole_em = NULL; |
| 5481 | } else { |
| 5482 | hole_start = max(hole_em->start, start); |
| 5483 | hole_len = calc_end - hole_start; |
| 5484 | } |
| 5485 | } |
| 5486 | em->bdev = NULL; |
| 5487 | if (hole_em && range_start > hole_start) { |
| 5488 | /* our hole starts before our delalloc, so we |
| 5489 | * have to return just the parts of the hole |
| 5490 | * that go until the delalloc starts |
| 5491 | */ |
| 5492 | em->len = min(hole_len, |
| 5493 | range_start - hole_start); |
| 5494 | em->start = hole_start; |
| 5495 | em->orig_start = hole_start; |
| 5496 | /* |
| 5497 | * don't adjust block start at all, |
| 5498 | * it is fixed at EXTENT_MAP_HOLE |
| 5499 | */ |
| 5500 | em->block_start = hole_em->block_start; |
| 5501 | em->block_len = hole_len; |
| 5502 | } else { |
| 5503 | em->start = range_start; |
| 5504 | em->len = found; |
| 5505 | em->orig_start = range_start; |
| 5506 | em->block_start = EXTENT_MAP_DELALLOC; |
| 5507 | em->block_len = found; |
| 5508 | } |
| 5509 | } else if (hole_em) { |
| 5510 | return hole_em; |
| 5511 | } |
| 5512 | out: |
| 5513 | |
| 5514 | free_extent_map(hole_em); |
| 5515 | if (err) { |
| 5516 | free_extent_map(em); |
| 5517 | return ERR_PTR(err); |
| 5518 | } |
| 5519 | return em; |
| 5520 | } |
| 5521 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5522 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5523 | struct extent_map *em, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5524 | u64 start, u64 len) |
| 5525 | { |
| 5526 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5527 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5528 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 5529 | struct btrfs_key ins; |
| 5530 | u64 alloc_hint; |
| 5531 | int ret; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5532 | bool insert = false; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5533 | |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5534 | /* |
| 5535 | * Ok if the extent map we looked up is a hole and is for the exact |
| 5536 | * range we want, there is no reason to allocate a new one, however if |
| 5537 | * it is not right then we need to free this one and drop the cache for |
| 5538 | * our range. |
| 5539 | */ |
| 5540 | if (em->block_start != EXTENT_MAP_HOLE || em->start != start || |
| 5541 | em->len != len) { |
| 5542 | free_extent_map(em); |
| 5543 | em = NULL; |
| 5544 | insert = true; |
| 5545 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
| 5546 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5547 | |
| 5548 | trans = btrfs_join_transaction(root, 0); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5549 | if (IS_ERR(trans)) |
| 5550 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5551 | |
| 5552 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5553 | |
| 5554 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 5555 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
| 5556 | alloc_hint, (u64)-1, &ins, 1); |
| 5557 | if (ret) { |
| 5558 | em = ERR_PTR(ret); |
| 5559 | goto out; |
| 5560 | } |
| 5561 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5562 | if (!em) { |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5563 | em = alloc_extent_map(GFP_NOFS); |
| 5564 | if (!em) { |
| 5565 | em = ERR_PTR(-ENOMEM); |
| 5566 | goto out; |
| 5567 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5568 | } |
| 5569 | |
| 5570 | em->start = start; |
| 5571 | em->orig_start = em->start; |
| 5572 | em->len = ins.offset; |
| 5573 | |
| 5574 | em->block_start = ins.objectid; |
| 5575 | em->block_len = ins.offset; |
| 5576 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5577 | |
| 5578 | /* |
| 5579 | * We need to do this because if we're using the original em we searched |
| 5580 | * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that. |
| 5581 | */ |
| 5582 | em->flags = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5583 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5584 | |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5585 | while (insert) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5586 | write_lock(&em_tree->lock); |
| 5587 | ret = add_extent_mapping(em_tree, em); |
| 5588 | write_unlock(&em_tree->lock); |
| 5589 | if (ret != -EEXIST) |
| 5590 | break; |
| 5591 | btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0); |
| 5592 | } |
| 5593 | |
| 5594 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 5595 | ins.offset, ins.offset, 0); |
| 5596 | if (ret) { |
| 5597 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 5598 | em = ERR_PTR(ret); |
| 5599 | } |
| 5600 | out: |
| 5601 | btrfs_end_transaction(trans, root); |
| 5602 | return em; |
| 5603 | } |
| 5604 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5605 | /* |
| 5606 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 5607 | * block must be cow'd |
| 5608 | */ |
| 5609 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 5610 | struct inode *inode, u64 offset, u64 len) |
| 5611 | { |
| 5612 | struct btrfs_path *path; |
| 5613 | int ret; |
| 5614 | struct extent_buffer *leaf; |
| 5615 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5616 | struct btrfs_file_extent_item *fi; |
| 5617 | struct btrfs_key key; |
| 5618 | u64 disk_bytenr; |
| 5619 | u64 backref_offset; |
| 5620 | u64 extent_end; |
| 5621 | u64 num_bytes; |
| 5622 | int slot; |
| 5623 | int found_type; |
| 5624 | |
| 5625 | path = btrfs_alloc_path(); |
| 5626 | if (!path) |
| 5627 | return -ENOMEM; |
| 5628 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5629 | ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5630 | offset, 0); |
| 5631 | if (ret < 0) |
| 5632 | goto out; |
| 5633 | |
| 5634 | slot = path->slots[0]; |
| 5635 | if (ret == 1) { |
| 5636 | if (slot == 0) { |
| 5637 | /* can't find the item, must cow */ |
| 5638 | ret = 0; |
| 5639 | goto out; |
| 5640 | } |
| 5641 | slot--; |
| 5642 | } |
| 5643 | ret = 0; |
| 5644 | leaf = path->nodes[0]; |
| 5645 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5646 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5647 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5648 | /* not our file or wrong item type, must cow */ |
| 5649 | goto out; |
| 5650 | } |
| 5651 | |
| 5652 | if (key.offset > offset) { |
| 5653 | /* Wrong offset, must cow */ |
| 5654 | goto out; |
| 5655 | } |
| 5656 | |
| 5657 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5658 | found_type = btrfs_file_extent_type(leaf, fi); |
| 5659 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 5660 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 5661 | /* not a regular extent, must cow */ |
| 5662 | goto out; |
| 5663 | } |
| 5664 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5665 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 5666 | |
| 5667 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 5668 | if (extent_end < offset + len) { |
| 5669 | /* extent doesn't include our full range, must cow */ |
| 5670 | goto out; |
| 5671 | } |
| 5672 | |
| 5673 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 5674 | goto out; |
| 5675 | |
| 5676 | /* |
| 5677 | * look for other files referencing this extent, if we |
| 5678 | * find any we must cow |
| 5679 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5680 | if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5681 | key.offset - backref_offset, disk_bytenr)) |
| 5682 | goto out; |
| 5683 | |
| 5684 | /* |
| 5685 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 5686 | * in this extent we are about to write. If there |
| 5687 | * are any csums in that range we have to cow in order |
| 5688 | * to keep the csums correct |
| 5689 | */ |
| 5690 | disk_bytenr += backref_offset; |
| 5691 | disk_bytenr += offset - key.offset; |
| 5692 | num_bytes = min(offset + len, extent_end) - offset; |
| 5693 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 5694 | goto out; |
| 5695 | /* |
| 5696 | * all of the above have passed, it is safe to overwrite this extent |
| 5697 | * without cow |
| 5698 | */ |
| 5699 | ret = 1; |
| 5700 | out: |
| 5701 | btrfs_free_path(path); |
| 5702 | return ret; |
| 5703 | } |
| 5704 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5705 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 5706 | struct buffer_head *bh_result, int create) |
| 5707 | { |
| 5708 | struct extent_map *em; |
| 5709 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5710 | u64 start = iblock << inode->i_blkbits; |
| 5711 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5712 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5713 | |
| 5714 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
| 5715 | if (IS_ERR(em)) |
| 5716 | return PTR_ERR(em); |
| 5717 | |
| 5718 | /* |
| 5719 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 5720 | * io. INLINE is special, and we could probably kludge it in here, but |
| 5721 | * it's still buffered so for safety lets just fall back to the generic |
| 5722 | * buffered path. |
| 5723 | * |
| 5724 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 5725 | * decompress it, so there will be buffering required no matter what we |
| 5726 | * do, so go ahead and fallback to buffered. |
| 5727 | * |
| 5728 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 5729 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 5730 | * the generic code. |
| 5731 | */ |
| 5732 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 5733 | em->block_start == EXTENT_MAP_INLINE) { |
| 5734 | free_extent_map(em); |
| 5735 | return -ENOTBLK; |
| 5736 | } |
| 5737 | |
| 5738 | /* Just a good old fashioned hole, return */ |
| 5739 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 5740 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5741 | free_extent_map(em); |
| 5742 | /* DIO will do one hole at a time, so just unlock a sector */ |
| 5743 | unlock_extent(&BTRFS_I(inode)->io_tree, start, |
| 5744 | start + root->sectorsize - 1, GFP_NOFS); |
| 5745 | return 0; |
| 5746 | } |
| 5747 | |
| 5748 | /* |
| 5749 | * We don't allocate a new extent in the following cases |
| 5750 | * |
| 5751 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 5752 | * existing extent. |
| 5753 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 5754 | * just use the extent. |
| 5755 | * |
| 5756 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5757 | if (!create) { |
| 5758 | len = em->len - (start - em->start); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5759 | goto map; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5760 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5761 | |
| 5762 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 5763 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 5764 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5765 | int type; |
| 5766 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5767 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5768 | |
| 5769 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5770 | type = BTRFS_ORDERED_PREALLOC; |
| 5771 | else |
| 5772 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5773 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5774 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5775 | |
| 5776 | /* |
| 5777 | * we're not going to log anything, but we do need |
| 5778 | * to make sure the current transaction stays open |
| 5779 | * while we look for nocow cross refs |
| 5780 | */ |
| 5781 | trans = btrfs_join_transaction(root, 0); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5782 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5783 | goto must_cow; |
| 5784 | |
| 5785 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
| 5786 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 5787 | block_start, len, len, type); |
| 5788 | btrfs_end_transaction(trans, root); |
| 5789 | if (ret) { |
| 5790 | free_extent_map(em); |
| 5791 | return ret; |
| 5792 | } |
| 5793 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5794 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5795 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5796 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5797 | must_cow: |
| 5798 | /* |
| 5799 | * this will cow the extent, reset the len in case we changed |
| 5800 | * it above |
| 5801 | */ |
| 5802 | len = bh_result->b_size; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5803 | em = btrfs_new_extent_direct(inode, em, start, len); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5804 | if (IS_ERR(em)) |
| 5805 | return PTR_ERR(em); |
| 5806 | len = min(len, em->len - (start - em->start)); |
| 5807 | unlock: |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5808 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
| 5809 | EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1, |
| 5810 | 0, NULL, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5811 | map: |
| 5812 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 5813 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5814 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5815 | bh_result->b_bdev = em->bdev; |
| 5816 | set_buffer_mapped(bh_result); |
| 5817 | if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5818 | set_buffer_new(bh_result); |
| 5819 | |
| 5820 | free_extent_map(em); |
| 5821 | |
| 5822 | return 0; |
| 5823 | } |
| 5824 | |
| 5825 | struct btrfs_dio_private { |
| 5826 | struct inode *inode; |
| 5827 | u64 logical_offset; |
| 5828 | u64 disk_bytenr; |
| 5829 | u64 bytes; |
| 5830 | u32 *csums; |
| 5831 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5832 | |
| 5833 | /* number of bios pending for this dio */ |
| 5834 | atomic_t pending_bios; |
| 5835 | |
| 5836 | /* IO errors */ |
| 5837 | int errors; |
| 5838 | |
| 5839 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5840 | }; |
| 5841 | |
| 5842 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 5843 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5844 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5845 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 5846 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5847 | struct inode *inode = dip->inode; |
| 5848 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5849 | u64 start; |
| 5850 | u32 *private = dip->csums; |
| 5851 | |
| 5852 | start = dip->logical_offset; |
| 5853 | do { |
| 5854 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 5855 | struct page *page = bvec->bv_page; |
| 5856 | char *kaddr; |
| 5857 | u32 csum = ~(u32)0; |
| 5858 | unsigned long flags; |
| 5859 | |
| 5860 | local_irq_save(flags); |
| 5861 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 5862 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 5863 | csum, bvec->bv_len); |
| 5864 | btrfs_csum_final(csum, (char *)&csum); |
| 5865 | kunmap_atomic(kaddr, KM_IRQ0); |
| 5866 | local_irq_restore(flags); |
| 5867 | |
| 5868 | flush_dcache_page(bvec->bv_page); |
| 5869 | if (csum != *private) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5870 | printk(KERN_ERR "btrfs csum failed ino %llu off" |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5871 | " %llu csum %u private %u\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5872 | (unsigned long long)btrfs_ino(inode), |
| 5873 | (unsigned long long)start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5874 | csum, *private); |
| 5875 | err = -EIO; |
| 5876 | } |
| 5877 | } |
| 5878 | |
| 5879 | start += bvec->bv_len; |
| 5880 | private++; |
| 5881 | bvec++; |
| 5882 | } while (bvec <= bvec_end); |
| 5883 | |
| 5884 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
| 5885 | dip->logical_offset + dip->bytes - 1, GFP_NOFS); |
| 5886 | bio->bi_private = dip->private; |
| 5887 | |
| 5888 | kfree(dip->csums); |
| 5889 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 5890 | |
| 5891 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 5892 | if (err) |
| 5893 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5894 | dio_end_io(bio, err); |
| 5895 | } |
| 5896 | |
| 5897 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 5898 | { |
| 5899 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5900 | struct inode *inode = dip->inode; |
| 5901 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5902 | struct btrfs_trans_handle *trans; |
| 5903 | struct btrfs_ordered_extent *ordered = NULL; |
| 5904 | struct extent_state *cached_state = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5905 | u64 ordered_offset = dip->logical_offset; |
| 5906 | u64 ordered_bytes = dip->bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5907 | int ret; |
| 5908 | |
| 5909 | if (err) |
| 5910 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5911 | again: |
| 5912 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 5913 | &ordered_offset, |
| 5914 | ordered_bytes); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5915 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5916 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5917 | |
| 5918 | BUG_ON(!ordered); |
| 5919 | |
| 5920 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5921 | if (IS_ERR(trans)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5922 | err = -ENOMEM; |
| 5923 | goto out; |
| 5924 | } |
| 5925 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5926 | |
| 5927 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) { |
| 5928 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5929 | if (!ret) |
| 5930 | ret = btrfs_update_inode(trans, root, inode); |
| 5931 | err = ret; |
| 5932 | goto out; |
| 5933 | } |
| 5934 | |
| 5935 | lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5936 | ordered->file_offset + ordered->len - 1, 0, |
| 5937 | &cached_state, GFP_NOFS); |
| 5938 | |
| 5939 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) { |
| 5940 | ret = btrfs_mark_extent_written(trans, inode, |
| 5941 | ordered->file_offset, |
| 5942 | ordered->file_offset + |
| 5943 | ordered->len); |
| 5944 | if (ret) { |
| 5945 | err = ret; |
| 5946 | goto out_unlock; |
| 5947 | } |
| 5948 | } else { |
| 5949 | ret = insert_reserved_file_extent(trans, inode, |
| 5950 | ordered->file_offset, |
| 5951 | ordered->start, |
| 5952 | ordered->disk_len, |
| 5953 | ordered->len, |
| 5954 | ordered->len, |
| 5955 | 0, 0, 0, |
| 5956 | BTRFS_FILE_EXTENT_REG); |
| 5957 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 5958 | ordered->file_offset, ordered->len); |
| 5959 | if (ret) { |
| 5960 | err = ret; |
| 5961 | WARN_ON(1); |
| 5962 | goto out_unlock; |
| 5963 | } |
| 5964 | } |
| 5965 | |
| 5966 | add_pending_csums(trans, inode, ordered->file_offset, &ordered->list); |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 5967 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5968 | if (!ret) |
| 5969 | btrfs_update_inode(trans, root, inode); |
| 5970 | ret = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5971 | out_unlock: |
| 5972 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5973 | ordered->file_offset + ordered->len - 1, |
| 5974 | &cached_state, GFP_NOFS); |
| 5975 | out: |
| 5976 | btrfs_delalloc_release_metadata(inode, ordered->len); |
| 5977 | btrfs_end_transaction(trans, root); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5978 | ordered_offset = ordered->file_offset + ordered->len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5979 | btrfs_put_ordered_extent(ordered); |
| 5980 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5981 | |
| 5982 | out_test: |
| 5983 | /* |
| 5984 | * our bio might span multiple ordered extents. If we haven't |
| 5985 | * completed the accounting for the whole dio, go back and try again |
| 5986 | */ |
| 5987 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 5988 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 5989 | ordered_offset; |
| 5990 | goto again; |
| 5991 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5992 | out_done: |
| 5993 | bio->bi_private = dip->private; |
| 5994 | |
| 5995 | kfree(dip->csums); |
| 5996 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 5997 | |
| 5998 | /* If we had an error make sure to clear the uptodate flag */ |
| 5999 | if (err) |
| 6000 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6001 | dio_end_io(bio, err); |
| 6002 | } |
| 6003 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6004 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 6005 | struct bio *bio, int mirror_num, |
| 6006 | unsigned long bio_flags, u64 offset) |
| 6007 | { |
| 6008 | int ret; |
| 6009 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6010 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
| 6011 | BUG_ON(ret); |
| 6012 | return 0; |
| 6013 | } |
| 6014 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6015 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 6016 | { |
| 6017 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6018 | |
| 6019 | if (err) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6020 | printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6021 | "sector %#Lx len %u err no %d\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6022 | (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw, |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6023 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6024 | dip->errors = 1; |
| 6025 | |
| 6026 | /* |
| 6027 | * before atomic variable goto zero, we must make sure |
| 6028 | * dip->errors is perceived to be set. |
| 6029 | */ |
| 6030 | smp_mb__before_atomic_dec(); |
| 6031 | } |
| 6032 | |
| 6033 | /* if there are more bios still pending for this dio, just exit */ |
| 6034 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 6035 | goto out; |
| 6036 | |
| 6037 | if (dip->errors) |
| 6038 | bio_io_error(dip->orig_bio); |
| 6039 | else { |
| 6040 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 6041 | bio_endio(dip->orig_bio, 0); |
| 6042 | } |
| 6043 | out: |
| 6044 | bio_put(bio); |
| 6045 | } |
| 6046 | |
| 6047 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 6048 | u64 first_sector, gfp_t gfp_flags) |
| 6049 | { |
| 6050 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 6051 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 6052 | } |
| 6053 | |
| 6054 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 6055 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6056 | u32 *csums, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6057 | { |
| 6058 | int write = rw & REQ_WRITE; |
| 6059 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6060 | int ret; |
| 6061 | |
| 6062 | bio_get(bio); |
| 6063 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 6064 | if (ret) |
| 6065 | goto err; |
| 6066 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6067 | if (skip_sum) |
| 6068 | goto map; |
| 6069 | |
| 6070 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6071 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 6072 | inode, rw, bio, 0, 0, |
| 6073 | file_offset, |
| 6074 | __btrfs_submit_bio_start_direct_io, |
| 6075 | __btrfs_submit_bio_done); |
| 6076 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6077 | } else if (write) { |
| 6078 | /* |
| 6079 | * If we aren't doing async submit, calculate the csum of the |
| 6080 | * bio now. |
| 6081 | */ |
| 6082 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 6083 | if (ret) |
| 6084 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 6085 | } else if (!skip_sum) { |
| 6086 | ret = btrfs_lookup_bio_sums_dio(root, inode, bio, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6087 | file_offset, csums); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 6088 | if (ret) |
| 6089 | goto err; |
| 6090 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6091 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6092 | map: |
| 6093 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6094 | err: |
| 6095 | bio_put(bio); |
| 6096 | return ret; |
| 6097 | } |
| 6098 | |
| 6099 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 6100 | int skip_sum) |
| 6101 | { |
| 6102 | struct inode *inode = dip->inode; |
| 6103 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6104 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 6105 | struct bio *bio; |
| 6106 | struct bio *orig_bio = dip->orig_bio; |
| 6107 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 6108 | u64 start_sector = orig_bio->bi_sector; |
| 6109 | u64 file_offset = dip->logical_offset; |
| 6110 | u64 submit_len = 0; |
| 6111 | u64 map_length; |
| 6112 | int nr_pages = 0; |
| 6113 | u32 *csums = dip->csums; |
| 6114 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6115 | int async_submit = 0; |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 6116 | int write = rw & REQ_WRITE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6117 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6118 | map_length = orig_bio->bi_size; |
| 6119 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 6120 | &map_length, NULL, 0); |
| 6121 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6122 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6123 | return -EIO; |
| 6124 | } |
| 6125 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6126 | if (map_length >= orig_bio->bi_size) { |
| 6127 | bio = orig_bio; |
| 6128 | goto submit; |
| 6129 | } |
| 6130 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6131 | async_submit = 1; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6132 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 6133 | if (!bio) |
| 6134 | return -ENOMEM; |
| 6135 | bio->bi_private = dip; |
| 6136 | bio->bi_end_io = btrfs_end_dio_bio; |
| 6137 | atomic_inc(&dip->pending_bios); |
| 6138 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6139 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 6140 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 6141 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 6142 | bvec->bv_offset) < bvec->bv_len)) { |
| 6143 | /* |
| 6144 | * inc the count before we submit the bio so |
| 6145 | * we know the end IO handler won't happen before |
| 6146 | * we inc the count. Otherwise, the dip might get freed |
| 6147 | * before we're done setting it up |
| 6148 | */ |
| 6149 | atomic_inc(&dip->pending_bios); |
| 6150 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 6151 | file_offset, skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6152 | csums, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6153 | if (ret) { |
| 6154 | bio_put(bio); |
| 6155 | atomic_dec(&dip->pending_bios); |
| 6156 | goto out_err; |
| 6157 | } |
| 6158 | |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 6159 | /* Write's use the ordered csums */ |
| 6160 | if (!write && !skip_sum) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6161 | csums = csums + nr_pages; |
| 6162 | start_sector += submit_len >> 9; |
| 6163 | file_offset += submit_len; |
| 6164 | |
| 6165 | submit_len = 0; |
| 6166 | nr_pages = 0; |
| 6167 | |
| 6168 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 6169 | start_sector, GFP_NOFS); |
| 6170 | if (!bio) |
| 6171 | goto out_err; |
| 6172 | bio->bi_private = dip; |
| 6173 | bio->bi_end_io = btrfs_end_dio_bio; |
| 6174 | |
| 6175 | map_length = orig_bio->bi_size; |
| 6176 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 6177 | &map_length, NULL, 0); |
| 6178 | if (ret) { |
| 6179 | bio_put(bio); |
| 6180 | goto out_err; |
| 6181 | } |
| 6182 | } else { |
| 6183 | submit_len += bvec->bv_len; |
| 6184 | nr_pages ++; |
| 6185 | bvec++; |
| 6186 | } |
| 6187 | } |
| 6188 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6189 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6190 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6191 | csums, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6192 | if (!ret) |
| 6193 | return 0; |
| 6194 | |
| 6195 | bio_put(bio); |
| 6196 | out_err: |
| 6197 | dip->errors = 1; |
| 6198 | /* |
| 6199 | * before atomic variable goto zero, we must |
| 6200 | * make sure dip->errors is perceived to be set. |
| 6201 | */ |
| 6202 | smp_mb__before_atomic_dec(); |
| 6203 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 6204 | bio_io_error(dip->orig_bio); |
| 6205 | |
| 6206 | /* bio_end_io() will handle error, so we needn't return it */ |
| 6207 | return 0; |
| 6208 | } |
| 6209 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6210 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 6211 | loff_t file_offset) |
| 6212 | { |
| 6213 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6214 | struct btrfs_dio_private *dip; |
| 6215 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6216 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 6217 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6218 | int ret = 0; |
| 6219 | |
| 6220 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 6221 | |
| 6222 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 6223 | if (!dip) { |
| 6224 | ret = -ENOMEM; |
| 6225 | goto free_ordered; |
| 6226 | } |
| 6227 | dip->csums = NULL; |
| 6228 | |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 6229 | /* Write's use the ordered csum stuff, so we don't need dip->csums */ |
| 6230 | if (!write && !skip_sum) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6231 | dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); |
| 6232 | if (!dip->csums) { |
Daniel J Blueman | b4966b7 | 2011-03-09 16:46:42 +0000 | [diff] [blame] | 6233 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6234 | ret = -ENOMEM; |
| 6235 | goto free_ordered; |
| 6236 | } |
| 6237 | } |
| 6238 | |
| 6239 | dip->private = bio->bi_private; |
| 6240 | dip->inode = inode; |
| 6241 | dip->logical_offset = file_offset; |
| 6242 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6243 | dip->bytes = 0; |
| 6244 | do { |
| 6245 | dip->bytes += bvec->bv_len; |
| 6246 | bvec++; |
| 6247 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 6248 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6249 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6250 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6251 | dip->errors = 0; |
| 6252 | dip->orig_bio = bio; |
| 6253 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6254 | |
| 6255 | if (write) |
| 6256 | bio->bi_end_io = btrfs_endio_direct_write; |
| 6257 | else |
| 6258 | bio->bi_end_io = btrfs_endio_direct_read; |
| 6259 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6260 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 6261 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6262 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6263 | free_ordered: |
| 6264 | /* |
| 6265 | * If this is a write, we need to clean up the reserved space and kill |
| 6266 | * the ordered extent. |
| 6267 | */ |
| 6268 | if (write) { |
| 6269 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 6270 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6271 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 6272 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 6273 | btrfs_free_reserved_extent(root, ordered->start, |
| 6274 | ordered->disk_len); |
| 6275 | btrfs_put_ordered_extent(ordered); |
| 6276 | btrfs_put_ordered_extent(ordered); |
| 6277 | } |
| 6278 | bio_endio(bio, ret); |
| 6279 | } |
| 6280 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6281 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 6282 | const struct iovec *iov, loff_t offset, |
| 6283 | unsigned long nr_segs) |
| 6284 | { |
| 6285 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6286 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6287 | size_t size; |
| 6288 | unsigned long addr; |
| 6289 | unsigned blocksize_mask = root->sectorsize - 1; |
| 6290 | ssize_t retval = -EINVAL; |
| 6291 | loff_t end = offset; |
| 6292 | |
| 6293 | if (offset & blocksize_mask) |
| 6294 | goto out; |
| 6295 | |
| 6296 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 6297 | for (seg = 0; seg < nr_segs; seg++) { |
| 6298 | addr = (unsigned long)iov[seg].iov_base; |
| 6299 | size = iov[seg].iov_len; |
| 6300 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6301 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6302 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6303 | |
| 6304 | /* If this is a write we don't need to check anymore */ |
| 6305 | if (rw & WRITE) |
| 6306 | continue; |
| 6307 | |
| 6308 | /* |
| 6309 | * Check to make sure we don't have duplicate iov_base's in this |
| 6310 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 6311 | * when reading back. |
| 6312 | */ |
| 6313 | for (i = seg + 1; i < nr_segs; i++) { |
| 6314 | if (iov[seg].iov_base == iov[i].iov_base) |
| 6315 | goto out; |
| 6316 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6317 | } |
| 6318 | retval = 0; |
| 6319 | out: |
| 6320 | return retval; |
| 6321 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6322 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 6323 | const struct iovec *iov, loff_t offset, |
| 6324 | unsigned long nr_segs) |
| 6325 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6326 | struct file *file = iocb->ki_filp; |
| 6327 | struct inode *inode = file->f_mapping->host; |
| 6328 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6329 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6330 | u64 lockstart, lockend; |
| 6331 | ssize_t ret; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6332 | int writing = rw & WRITE; |
| 6333 | int write_bits = 0; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6334 | size_t count = iov_length(iov, nr_segs); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6335 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6336 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
| 6337 | offset, nr_segs)) { |
| 6338 | return 0; |
| 6339 | } |
| 6340 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6341 | lockstart = offset; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6342 | lockend = offset + count - 1; |
| 6343 | |
| 6344 | if (writing) { |
| 6345 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 6346 | if (ret) |
| 6347 | goto out; |
| 6348 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6349 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6350 | while (1) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6351 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6352 | 0, &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6353 | /* |
| 6354 | * We're concerned with the entire range that we're going to be |
| 6355 | * doing DIO to, so we need to make sure theres no ordered |
| 6356 | * extents in this range. |
| 6357 | */ |
| 6358 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6359 | lockend - lockstart + 1); |
| 6360 | if (!ordered) |
| 6361 | break; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6362 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6363 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6364 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6365 | btrfs_put_ordered_extent(ordered); |
| 6366 | cond_resched(); |
| 6367 | } |
| 6368 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6369 | /* |
| 6370 | * we don't use btrfs_set_extent_delalloc because we don't want |
| 6371 | * the dirty or uptodate bits |
| 6372 | */ |
| 6373 | if (writing) { |
| 6374 | write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING; |
| 6375 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6376 | EXTENT_DELALLOC, 0, NULL, &cached_state, |
| 6377 | GFP_NOFS); |
| 6378 | if (ret) { |
| 6379 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6380 | lockend, EXTENT_LOCKED | write_bits, |
| 6381 | 1, 0, &cached_state, GFP_NOFS); |
| 6382 | goto out; |
| 6383 | } |
| 6384 | } |
| 6385 | |
| 6386 | free_extent_state(cached_state); |
| 6387 | cached_state = NULL; |
| 6388 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6389 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 6390 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 6391 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
| 6392 | btrfs_submit_direct, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6393 | |
| 6394 | if (ret < 0 && ret != -EIOCBQUEUED) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6395 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset, |
| 6396 | offset + iov_length(iov, nr_segs) - 1, |
| 6397 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6398 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6399 | } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) { |
| 6400 | /* |
| 6401 | * We're falling back to buffered, unlock the section we didn't |
| 6402 | * do IO on. |
| 6403 | */ |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6404 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret, |
| 6405 | offset + iov_length(iov, nr_segs) - 1, |
| 6406 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6407 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6408 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6409 | out: |
| 6410 | free_extent_state(cached_state); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6411 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6412 | } |
| 6413 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6414 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 6415 | __u64 start, __u64 len) |
| 6416 | { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6417 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6418 | } |
| 6419 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6420 | int btrfs_readpage(struct file *file, struct page *page) |
| 6421 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6422 | struct extent_io_tree *tree; |
| 6423 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6424 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6425 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6426 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6427 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 6428 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6429 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6430 | |
| 6431 | |
| 6432 | if (current->flags & PF_MEMALLOC) { |
| 6433 | redirty_page_for_writepage(wbc, page); |
| 6434 | unlock_page(page); |
| 6435 | return 0; |
| 6436 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6437 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6438 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 6439 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6440 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6441 | int btrfs_writepages(struct address_space *mapping, |
| 6442 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6443 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6444 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6445 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6446 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6447 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 6448 | } |
| 6449 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6450 | static int |
| 6451 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 6452 | struct list_head *pages, unsigned nr_pages) |
| 6453 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6454 | struct extent_io_tree *tree; |
| 6455 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6456 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 6457 | btrfs_get_extent); |
| 6458 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6459 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6460 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6461 | struct extent_io_tree *tree; |
| 6462 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6463 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6464 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6465 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 6466 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6467 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6468 | if (ret == 1) { |
| 6469 | ClearPagePrivate(page); |
| 6470 | set_page_private(page, 0); |
| 6471 | page_cache_release(page); |
| 6472 | } |
| 6473 | return ret; |
| 6474 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6475 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6476 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 6477 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 6478 | if (PageWriteback(page) || PageDirty(page)) |
| 6479 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 6480 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6481 | } |
| 6482 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6483 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 6484 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6485 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6486 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6487 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6488 | u64 page_start = page_offset(page); |
| 6489 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6490 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6491 | |
| 6492 | /* |
| 6493 | * we have the page locked, so new writeback can't start, |
| 6494 | * and the dirty bit won't be cleared while we are here. |
| 6495 | * |
| 6496 | * Wait for IO on this page so that we can safely clear |
| 6497 | * the PagePrivate2 bit and do ordered accounting |
| 6498 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6499 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6500 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6501 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6502 | if (offset) { |
| 6503 | btrfs_releasepage(page, GFP_NOFS); |
| 6504 | return; |
| 6505 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6506 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6507 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6508 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 6509 | page_offset(page)); |
| 6510 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6511 | /* |
| 6512 | * IO on this page will never be started, so we need |
| 6513 | * to account for any ordered extents now |
| 6514 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6515 | clear_extent_bit(tree, page_start, page_end, |
| 6516 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6517 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6518 | &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6519 | /* |
| 6520 | * whoever cleared the private bit is responsible |
| 6521 | * for the finish_ordered_io |
| 6522 | */ |
| 6523 | if (TestClearPagePrivate2(page)) { |
| 6524 | btrfs_finish_ordered_io(page->mapping->host, |
| 6525 | page_start, page_end); |
| 6526 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6527 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6528 | cached_state = NULL; |
| 6529 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6530 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6531 | } |
| 6532 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6533 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6534 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6535 | __btrfs_releasepage(page, GFP_NOFS); |
| 6536 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6537 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6538 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6539 | ClearPagePrivate(page); |
| 6540 | set_page_private(page, 0); |
| 6541 | page_cache_release(page); |
| 6542 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6543 | } |
| 6544 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6545 | /* |
| 6546 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 6547 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 6548 | * be careful to check for EOF conditions here. We set the page up correctly |
| 6549 | * for a written page which means we get ENOSPC checking when writing into |
| 6550 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 6551 | * support these features. |
| 6552 | * |
| 6553 | * We are not allowed to take the i_mutex here so we have to play games to |
| 6554 | * protect against truncate races as the page could now be beyond EOF. Because |
| 6555 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 6556 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 6557 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 6558 | * unlock the page. |
| 6559 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6560 | 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] | 6561 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6562 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 6563 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6564 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6565 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 6566 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6567 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6568 | char *kaddr; |
| 6569 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6570 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6571 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6572 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6573 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6574 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6575 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6576 | if (ret) { |
| 6577 | if (ret == -ENOMEM) |
| 6578 | ret = VM_FAULT_OOM; |
| 6579 | else /* -ENOSPC, -EIO, etc */ |
| 6580 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6581 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6582 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6583 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6584 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6585 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6586 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6587 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6588 | page_start = page_offset(page); |
| 6589 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6590 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6591 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6592 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6593 | /* page got truncated out from underneath us */ |
| 6594 | goto out_unlock; |
| 6595 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6596 | wait_on_page_writeback(page); |
| 6597 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6598 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 6599 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6600 | set_page_extent_mapped(page); |
| 6601 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6602 | /* |
| 6603 | * we can't set the delalloc bits if there are pending ordered |
| 6604 | * extents. Drop our locks and wait for them to finish |
| 6605 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6606 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6607 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6608 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6609 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6610 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6611 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6612 | btrfs_put_ordered_extent(ordered); |
| 6613 | goto again; |
| 6614 | } |
| 6615 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6616 | /* |
| 6617 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 6618 | * if it was already dirty, so for space accounting reasons we need to |
| 6619 | * clear any delalloc bits for the range we are fixing to save. There |
| 6620 | * is probably a better way to do this, but for now keep consistent with |
| 6621 | * prepare_pages in the normal write path. |
| 6622 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6623 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6624 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6625 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6626 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6627 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 6628 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6629 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6630 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6631 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6632 | ret = VM_FAULT_SIGBUS; |
| 6633 | goto out_unlock; |
| 6634 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6635 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6636 | |
| 6637 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6638 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6639 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6640 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6641 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6642 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6643 | if (zero_start != PAGE_CACHE_SIZE) { |
| 6644 | kaddr = kmap(page); |
| 6645 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 6646 | flush_dcache_page(page); |
| 6647 | kunmap(page); |
| 6648 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6649 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6650 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6651 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6652 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6653 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 6654 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 6655 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6656 | 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] | 6657 | |
| 6658 | out_unlock: |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6659 | if (!ret) |
| 6660 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6661 | unlock_page(page); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6662 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6663 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6664 | return ret; |
| 6665 | } |
| 6666 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6667 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6668 | { |
| 6669 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6670 | int ret; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6671 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6672 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6673 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6674 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6675 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 6676 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 6677 | if (ret) |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6678 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6679 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6680 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6681 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6682 | |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6683 | trans = btrfs_start_transaction(root, 5); |
| 6684 | if (IS_ERR(trans)) |
| 6685 | return PTR_ERR(trans); |
| 6686 | |
| 6687 | btrfs_set_trans_block_group(trans, inode); |
| 6688 | |
| 6689 | ret = btrfs_orphan_add(trans, inode); |
| 6690 | if (ret) { |
| 6691 | btrfs_end_transaction(trans, root); |
| 6692 | return ret; |
| 6693 | } |
| 6694 | |
| 6695 | nr = trans->blocks_used; |
| 6696 | btrfs_end_transaction(trans, root); |
| 6697 | btrfs_btree_balance_dirty(root, nr); |
| 6698 | |
| 6699 | /* Now start a transaction for the truncate */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6700 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6701 | if (IS_ERR(trans)) |
| 6702 | return PTR_ERR(trans); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6703 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6704 | trans->block_rsv = root->orphan_block_rsv; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6705 | |
| 6706 | /* |
| 6707 | * setattr is responsible for setting the ordered_data_close flag, |
| 6708 | * but that is only tested during the last file release. That |
| 6709 | * could happen well after the next commit, leaving a great big |
| 6710 | * window where new writes may get lost if someone chooses to write |
| 6711 | * to this file after truncating to zero |
| 6712 | * |
| 6713 | * The inode doesn't have any dirty data here, and so if we commit |
| 6714 | * this is a noop. If someone immediately starts writing to the inode |
| 6715 | * it is very likely we'll catch some of their writes in this |
| 6716 | * transaction, and the commit will find this file on the ordered |
| 6717 | * data list with good things to send down. |
| 6718 | * |
| 6719 | * This is a best effort solution, there is still a window where |
| 6720 | * using truncate to replace the contents of the file will |
| 6721 | * end up with a zero length file after a crash. |
| 6722 | */ |
| 6723 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 6724 | btrfs_add_ordered_operation(trans, root, inode); |
| 6725 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6726 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6727 | if (!trans) { |
| 6728 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6729 | if (IS_ERR(trans)) |
| 6730 | return PTR_ERR(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6731 | btrfs_set_trans_block_group(trans, inode); |
| 6732 | trans->block_rsv = root->orphan_block_rsv; |
| 6733 | } |
| 6734 | |
| 6735 | ret = btrfs_block_rsv_check(trans, root, |
| 6736 | root->orphan_block_rsv, 0, 5); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6737 | if (ret == -EAGAIN) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6738 | ret = btrfs_commit_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6739 | if (ret) |
| 6740 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6741 | trans = NULL; |
| 6742 | continue; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6743 | } else if (ret) { |
| 6744 | err = ret; |
| 6745 | break; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6746 | } |
| 6747 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6748 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 6749 | inode->i_size, |
| 6750 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6751 | if (ret != -EAGAIN) { |
| 6752 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6753 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6754 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6755 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6756 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6757 | if (ret) { |
| 6758 | err = ret; |
| 6759 | break; |
| 6760 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6761 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6762 | nr = trans->blocks_used; |
| 6763 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6764 | trans = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6765 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6766 | } |
| 6767 | |
| 6768 | if (ret == 0 && inode->i_nlink > 0) { |
| 6769 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6770 | if (ret) |
| 6771 | err = ret; |
Josef Bacik | ded5db9 | 2011-03-04 14:09:46 -0500 | [diff] [blame] | 6772 | } else if (ret && inode->i_nlink > 0) { |
| 6773 | /* |
| 6774 | * Failed to do the truncate, remove us from the in memory |
| 6775 | * orphan list. |
| 6776 | */ |
| 6777 | ret = btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6778 | } |
| 6779 | |
| 6780 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6781 | if (ret && !err) |
| 6782 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6783 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6784 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6785 | ret = btrfs_end_transaction_throttle(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6786 | if (ret && !err) |
| 6787 | err = ret; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6788 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6789 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6790 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6791 | } |
| 6792 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6793 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6794 | * create a new subvolume directory/inode (helper for the ioctl). |
| 6795 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6796 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6797 | struct btrfs_root *new_root, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6798 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6799 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6800 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6801 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6802 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6803 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6804 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6805 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6806 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 6807 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6808 | inode->i_op = &btrfs_dir_inode_operations; |
| 6809 | inode->i_fop = &btrfs_dir_file_operations; |
| 6810 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6811 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6812 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6813 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6814 | err = btrfs_update_inode(trans, new_root, inode); |
| 6815 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6816 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6817 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6818 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6819 | } |
| 6820 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6821 | /* helper function for file defrag and space balancing. This |
| 6822 | * forces readahead on a given range of bytes in an inode |
| 6823 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6824 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6825 | struct file_ra_state *ra, struct file *file, |
| 6826 | pgoff_t offset, pgoff_t last_index) |
| 6827 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6828 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6829 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6830 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 6831 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6832 | } |
| 6833 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6834 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 6835 | { |
| 6836 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6837 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6838 | |
| 6839 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 6840 | if (!ei) |
| 6841 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6842 | |
| 6843 | ei->root = NULL; |
| 6844 | ei->space_info = NULL; |
| 6845 | ei->generation = 0; |
| 6846 | ei->sequence = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 6847 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6848 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6849 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6850 | ei->delalloc_bytes = 0; |
| 6851 | ei->reserved_bytes = 0; |
| 6852 | ei->disk_i_size = 0; |
| 6853 | ei->flags = 0; |
| 6854 | ei->index_cnt = (u64)-1; |
| 6855 | ei->last_unlink_trans = 0; |
| 6856 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6857 | atomic_set(&ei->outstanding_extents, 0); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 6858 | atomic_set(&ei->reserved_extents, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6859 | |
| 6860 | ei->ordered_data_close = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6861 | ei->orphan_meta_reserved = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6862 | ei->dummy_inode = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6863 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6864 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6865 | ei->delayed_node = NULL; |
| 6866 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6867 | inode = &ei->vfs_inode; |
| 6868 | extent_map_tree_init(&ei->extent_tree, GFP_NOFS); |
| 6869 | extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS); |
| 6870 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS); |
| 6871 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6872 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6873 | INIT_LIST_HEAD(&ei->i_orphan); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6874 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6875 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6876 | RB_CLEAR_NODE(&ei->rb_node); |
| 6877 | |
| 6878 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6879 | } |
| 6880 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6881 | static void btrfs_i_callback(struct rcu_head *head) |
| 6882 | { |
| 6883 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 6884 | INIT_LIST_HEAD(&inode->i_dentry); |
| 6885 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 6886 | } |
| 6887 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6888 | void btrfs_destroy_inode(struct inode *inode) |
| 6889 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6890 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6891 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6892 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6893 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 6894 | WARN_ON(inode->i_data.nrpages); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6895 | WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents)); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 6896 | WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6897 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6898 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6899 | * This can happen where we create an inode, but somebody else also |
| 6900 | * created the same inode and we need to destroy the one we already |
| 6901 | * created. |
| 6902 | */ |
| 6903 | if (!root) |
| 6904 | goto free; |
| 6905 | |
| 6906 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6907 | * Make sure we're properly removed from the ordered operation |
| 6908 | * lists. |
| 6909 | */ |
| 6910 | smp_mb(); |
| 6911 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 6912 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 6913 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 6914 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 6915 | } |
| 6916 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6917 | if (root == root->fs_info->tree_root) { |
| 6918 | struct btrfs_block_group_cache *block_group; |
| 6919 | |
| 6920 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 6921 | BTRFS_I(inode)->block_group); |
| 6922 | if (block_group && block_group->inode == inode) { |
| 6923 | spin_lock(&block_group->lock); |
| 6924 | block_group->inode = NULL; |
| 6925 | spin_unlock(&block_group->lock); |
| 6926 | btrfs_put_block_group(block_group); |
| 6927 | } else if (block_group) { |
| 6928 | btrfs_put_block_group(block_group); |
| 6929 | } |
| 6930 | } |
| 6931 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6932 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6933 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6934 | printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n", |
| 6935 | (unsigned long long)btrfs_ino(inode)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6936 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6937 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6938 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6939 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6940 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6941 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 6942 | if (!ordered) |
| 6943 | break; |
| 6944 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6945 | printk(KERN_ERR "btrfs found ordered " |
| 6946 | "extent %llu %llu on inode cleanup\n", |
| 6947 | (unsigned long long)ordered->file_offset, |
| 6948 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6949 | btrfs_remove_ordered_extent(inode, ordered); |
| 6950 | btrfs_put_ordered_extent(ordered); |
| 6951 | btrfs_put_ordered_extent(ordered); |
| 6952 | } |
| 6953 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6954 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6955 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6956 | free: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6957 | btrfs_remove_delayed_node(inode); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6958 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6959 | } |
| 6960 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6961 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6962 | { |
| 6963 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6964 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6965 | if (btrfs_root_refs(&root->root_item) == 0 && |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 6966 | !is_free_space_inode(root, inode)) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6967 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6968 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6969 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6970 | } |
| 6971 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 6972 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6973 | { |
| 6974 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 6975 | |
| 6976 | inode_init_once(&ei->vfs_inode); |
| 6977 | } |
| 6978 | |
| 6979 | void btrfs_destroy_cachep(void) |
| 6980 | { |
| 6981 | if (btrfs_inode_cachep) |
| 6982 | kmem_cache_destroy(btrfs_inode_cachep); |
| 6983 | if (btrfs_trans_handle_cachep) |
| 6984 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 6985 | if (btrfs_transaction_cachep) |
| 6986 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6987 | if (btrfs_path_cachep) |
| 6988 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 6989 | if (btrfs_free_space_cachep) |
| 6990 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6991 | } |
| 6992 | |
| 6993 | int btrfs_init_cachep(void) |
| 6994 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6995 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 6996 | sizeof(struct btrfs_inode), 0, |
| 6997 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6998 | if (!btrfs_inode_cachep) |
| 6999 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7000 | |
| 7001 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 7002 | sizeof(struct btrfs_trans_handle), 0, |
| 7003 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7004 | if (!btrfs_trans_handle_cachep) |
| 7005 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7006 | |
| 7007 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 7008 | sizeof(struct btrfs_transaction), 0, |
| 7009 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7010 | if (!btrfs_transaction_cachep) |
| 7011 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7012 | |
| 7013 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 7014 | sizeof(struct btrfs_path), 0, |
| 7015 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7016 | if (!btrfs_path_cachep) |
| 7017 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7018 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 7019 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache", |
| 7020 | sizeof(struct btrfs_free_space), 0, |
| 7021 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 7022 | if (!btrfs_free_space_cachep) |
| 7023 | goto fail; |
| 7024 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7025 | return 0; |
| 7026 | fail: |
| 7027 | btrfs_destroy_cachep(); |
| 7028 | return -ENOMEM; |
| 7029 | } |
| 7030 | |
| 7031 | static int btrfs_getattr(struct vfsmount *mnt, |
| 7032 | struct dentry *dentry, struct kstat *stat) |
| 7033 | { |
| 7034 | struct inode *inode = dentry->d_inode; |
| 7035 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7036 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 7037 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 7038 | stat->blocks = (inode_get_bytes(inode) + |
| 7039 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7040 | return 0; |
| 7041 | } |
| 7042 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7043 | /* |
| 7044 | * If a file is moved, it will inherit the cow and compression flags of the new |
| 7045 | * directory. |
| 7046 | */ |
| 7047 | static void fixup_inode_flags(struct inode *dir, struct inode *inode) |
| 7048 | { |
| 7049 | struct btrfs_inode *b_dir = BTRFS_I(dir); |
| 7050 | struct btrfs_inode *b_inode = BTRFS_I(inode); |
| 7051 | |
| 7052 | if (b_dir->flags & BTRFS_INODE_NODATACOW) |
| 7053 | b_inode->flags |= BTRFS_INODE_NODATACOW; |
| 7054 | else |
| 7055 | b_inode->flags &= ~BTRFS_INODE_NODATACOW; |
| 7056 | |
| 7057 | if (b_dir->flags & BTRFS_INODE_COMPRESS) |
| 7058 | b_inode->flags |= BTRFS_INODE_COMPRESS; |
| 7059 | else |
| 7060 | b_inode->flags &= ~BTRFS_INODE_COMPRESS; |
| 7061 | } |
| 7062 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7063 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 7064 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7065 | { |
| 7066 | struct btrfs_trans_handle *trans; |
| 7067 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7068 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7069 | struct inode *new_inode = new_dentry->d_inode; |
| 7070 | struct inode *old_inode = old_dentry->d_inode; |
| 7071 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7072 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7073 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7074 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7075 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7076 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7077 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7078 | return -EPERM; |
| 7079 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7080 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7081 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7082 | return -EXDEV; |
| 7083 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7084 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 7085 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7086 | return -ENOTEMPTY; |
| 7087 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7088 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7089 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7090 | return -ENOTEMPTY; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7091 | /* |
| 7092 | * we're using rename to replace one file with another. |
| 7093 | * and the replacement file is large. Start IO on it now so |
| 7094 | * we don't add too much work to the end of the transaction |
| 7095 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 7096 | 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] | 7097 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 7098 | filemap_flush(old_inode->i_mapping); |
| 7099 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7100 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7101 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7102 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7103 | /* |
| 7104 | * We want to reserve the absolute worst case amount of items. So if |
| 7105 | * both inodes are subvols and we need to unlink them then that would |
| 7106 | * require 4 item modifications, but if they are both normal inodes it |
| 7107 | * would require 5 item modifications, so we'll assume their normal |
| 7108 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 7109 | * should cover the worst case number of items we'll modify. |
| 7110 | */ |
| 7111 | trans = btrfs_start_transaction(root, 20); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 7112 | if (IS_ERR(trans)) { |
| 7113 | ret = PTR_ERR(trans); |
| 7114 | goto out_notrans; |
| 7115 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7116 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7117 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7118 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7119 | if (dest != root) |
| 7120 | btrfs_record_root_in_trans(trans, dest); |
| 7121 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7122 | ret = btrfs_set_inode_index(new_dir, &index); |
| 7123 | if (ret) |
| 7124 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7125 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7126 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7127 | /* force full log commit if subvolume involved. */ |
| 7128 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 7129 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7130 | ret = btrfs_insert_inode_ref(trans, dest, |
| 7131 | new_dentry->d_name.name, |
| 7132 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7133 | old_ino, |
| 7134 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7135 | if (ret) |
| 7136 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7137 | /* |
| 7138 | * this is an ugly little race, but the rename is required |
| 7139 | * to make sure that if we crash, the inode is either at the |
| 7140 | * old name or the new one. pinning the log transaction lets |
| 7141 | * us make sure we don't allow a log commit to come in after |
| 7142 | * we unlink the name but before we add the new name back in. |
| 7143 | */ |
| 7144 | btrfs_pin_log_trans(root); |
| 7145 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7146 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7147 | * make sure the inode gets flushed if it is replacing |
| 7148 | * something. |
| 7149 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7150 | if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode)) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7151 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7152 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7153 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 7154 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 7155 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7156 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7157 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 7158 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 7159 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7160 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7161 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 7162 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 7163 | old_dentry->d_name.name, |
| 7164 | old_dentry->d_name.len); |
| 7165 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 7166 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 7167 | old_dentry->d_inode, |
| 7168 | old_dentry->d_name.name, |
| 7169 | old_dentry->d_name.len); |
| 7170 | if (!ret) |
| 7171 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7172 | } |
| 7173 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7174 | |
| 7175 | if (new_inode) { |
| 7176 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7177 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7178 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 7179 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 7180 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 7181 | root_objectid, |
| 7182 | new_dentry->d_name.name, |
| 7183 | new_dentry->d_name.len); |
| 7184 | BUG_ON(new_inode->i_nlink == 0); |
| 7185 | } else { |
| 7186 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 7187 | new_dentry->d_inode, |
| 7188 | new_dentry->d_name.name, |
| 7189 | new_dentry->d_name.len); |
| 7190 | } |
| 7191 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7192 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7193 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7194 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7195 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7196 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7197 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7198 | fixup_inode_flags(new_dir, old_inode); |
| 7199 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7200 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 7201 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7202 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7203 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7204 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7205 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 7206 | struct dentry *parent = dget_parent(new_dentry); |
| 7207 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
| 7208 | dput(parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7209 | btrfs_end_log_trans(root); |
| 7210 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7211 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 7212 | btrfs_end_transaction_throttle(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 7213 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7214 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7215 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7216 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7217 | return ret; |
| 7218 | } |
| 7219 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7220 | /* |
| 7221 | * some fairly slow code that needs optimization. This walks the list |
| 7222 | * of all the inodes with pending delalloc and forces them to disk. |
| 7223 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7224 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7225 | { |
| 7226 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 7227 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7228 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7229 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7230 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 7231 | return -EROFS; |
| 7232 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7233 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7234 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7235 | binode = list_entry(head->next, struct btrfs_inode, |
| 7236 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7237 | inode = igrab(&binode->vfs_inode); |
| 7238 | if (!inode) |
| 7239 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7240 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7241 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7242 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7243 | if (delay_iput) |
| 7244 | btrfs_add_delayed_iput(inode); |
| 7245 | else |
| 7246 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7247 | } |
| 7248 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7249 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7250 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7251 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7252 | |
| 7253 | /* the filemap_flush will queue IO into the worker threads, but |
| 7254 | * we have to make sure the IO is actually started and that |
| 7255 | * ordered extents get created before we return |
| 7256 | */ |
| 7257 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7258 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7259 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7260 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7261 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 7262 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7263 | } |
| 7264 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7265 | return 0; |
| 7266 | } |
| 7267 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 7268 | int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput, |
| 7269 | int sync) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7270 | { |
| 7271 | struct btrfs_inode *binode; |
| 7272 | struct inode *inode = NULL; |
| 7273 | |
| 7274 | spin_lock(&root->fs_info->delalloc_lock); |
| 7275 | while (!list_empty(&root->fs_info->delalloc_inodes)) { |
| 7276 | binode = list_entry(root->fs_info->delalloc_inodes.next, |
| 7277 | struct btrfs_inode, delalloc_inodes); |
| 7278 | inode = igrab(&binode->vfs_inode); |
| 7279 | if (inode) { |
| 7280 | list_move_tail(&binode->delalloc_inodes, |
| 7281 | &root->fs_info->delalloc_inodes); |
| 7282 | break; |
| 7283 | } |
| 7284 | |
| 7285 | list_del_init(&binode->delalloc_inodes); |
| 7286 | cond_resched_lock(&root->fs_info->delalloc_lock); |
| 7287 | } |
| 7288 | spin_unlock(&root->fs_info->delalloc_lock); |
| 7289 | |
| 7290 | if (inode) { |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 7291 | if (sync) { |
| 7292 | filemap_write_and_wait(inode->i_mapping); |
| 7293 | /* |
| 7294 | * We have to do this because compression doesn't |
| 7295 | * actually set PG_writeback until it submits the pages |
| 7296 | * for IO, which happens in an async thread, so we could |
| 7297 | * race and not actually wait for any writeback pages |
| 7298 | * because they've not been submitted yet. Technically |
| 7299 | * this could still be the case for the ordered stuff |
| 7300 | * since the async thread may not have started to do its |
| 7301 | * work yet. If this becomes the case then we need to |
| 7302 | * figure out a way to make sure that in writepage we |
| 7303 | * wait for any async pages to be submitted before |
| 7304 | * returning so that fdatawait does what its supposed to |
| 7305 | * do. |
| 7306 | */ |
| 7307 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 7308 | } else { |
| 7309 | filemap_flush(inode->i_mapping); |
| 7310 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7311 | if (delay_iput) |
| 7312 | btrfs_add_delayed_iput(inode); |
| 7313 | else |
| 7314 | iput(inode); |
| 7315 | return 1; |
| 7316 | } |
| 7317 | return 0; |
| 7318 | } |
| 7319 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7320 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 7321 | const char *symname) |
| 7322 | { |
| 7323 | struct btrfs_trans_handle *trans; |
| 7324 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 7325 | struct btrfs_path *path; |
| 7326 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7327 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7328 | int err; |
| 7329 | int drop_inode = 0; |
| 7330 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7331 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7332 | int name_len; |
| 7333 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7334 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7335 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7336 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7337 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7338 | |
| 7339 | name_len = strlen(symname) + 1; |
| 7340 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 7341 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7342 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7343 | /* |
| 7344 | * 2 items for inode item and ref |
| 7345 | * 2 items for dir items |
| 7346 | * 1 item for xattr if selinux is on |
| 7347 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7348 | trans = btrfs_start_transaction(root, 5); |
| 7349 | if (IS_ERR(trans)) |
| 7350 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7351 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7352 | btrfs_set_trans_block_group(trans, dir); |
| 7353 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 7354 | err = btrfs_find_free_ino(root, &objectid); |
| 7355 | if (err) |
| 7356 | goto out_unlock; |
| 7357 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7358 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7359 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7360 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 7361 | &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7362 | if (IS_ERR(inode)) { |
| 7363 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7364 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7365 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7366 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 7367 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7368 | if (err) { |
| 7369 | drop_inode = 1; |
| 7370 | goto out_unlock; |
| 7371 | } |
| 7372 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7373 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 7374 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7375 | if (err) |
| 7376 | drop_inode = 1; |
| 7377 | else { |
| 7378 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7379 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7380 | inode->i_fop = &btrfs_file_operations; |
| 7381 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7382 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7383 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7384 | btrfs_update_inode_block_group(trans, inode); |
| 7385 | btrfs_update_inode_block_group(trans, dir); |
| 7386 | if (drop_inode) |
| 7387 | goto out_unlock; |
| 7388 | |
| 7389 | path = btrfs_alloc_path(); |
| 7390 | BUG_ON(!path); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7391 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7392 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7393 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 7394 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 7395 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 7396 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7397 | if (err) { |
| 7398 | drop_inode = 1; |
| 7399 | goto out_unlock; |
| 7400 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7401 | leaf = path->nodes[0]; |
| 7402 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 7403 | struct btrfs_file_extent_item); |
| 7404 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 7405 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7406 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7407 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 7408 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 7409 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 7410 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 7411 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7412 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7413 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 7414 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7415 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7416 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7417 | inode->i_op = &btrfs_symlink_inode_operations; |
| 7418 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7419 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7420 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7421 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7422 | err = btrfs_update_inode(trans, root, inode); |
| 7423 | if (err) |
| 7424 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7425 | |
| 7426 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7427 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 7428 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7429 | if (drop_inode) { |
| 7430 | inode_dec_link_count(inode); |
| 7431 | iput(inode); |
| 7432 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7433 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7434 | return err; |
| 7435 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7436 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7437 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7438 | u64 start, u64 num_bytes, u64 min_size, |
| 7439 | loff_t actual_len, u64 *alloc_hint, |
| 7440 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7441 | { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7442 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7443 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7444 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7445 | u64 i_size; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7446 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7447 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7448 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7449 | if (trans) |
| 7450 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7451 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7452 | if (own_trans) { |
| 7453 | trans = btrfs_start_transaction(root, 3); |
| 7454 | if (IS_ERR(trans)) { |
| 7455 | ret = PTR_ERR(trans); |
| 7456 | break; |
| 7457 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7458 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7459 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7460 | ret = btrfs_reserve_extent(trans, root, num_bytes, min_size, |
| 7461 | 0, *alloc_hint, (u64)-1, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7462 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7463 | if (own_trans) |
| 7464 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7465 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7466 | } |
| 7467 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7468 | ret = insert_reserved_file_extent(trans, inode, |
| 7469 | cur_offset, ins.objectid, |
| 7470 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 7471 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7472 | BTRFS_FILE_EXTENT_PREALLOC); |
| 7473 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 7474 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7475 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7476 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7477 | num_bytes -= ins.offset; |
| 7478 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7479 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7480 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7481 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7482 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7483 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7484 | (actual_len > inode->i_size) && |
| 7485 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7486 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7487 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7488 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7489 | i_size = cur_offset; |
| 7490 | i_size_write(inode, i_size); |
| 7491 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7492 | } |
| 7493 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7494 | ret = btrfs_update_inode(trans, root, inode); |
| 7495 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7496 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7497 | if (own_trans) |
| 7498 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7499 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7500 | return ret; |
| 7501 | } |
| 7502 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7503 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7504 | u64 start, u64 num_bytes, u64 min_size, |
| 7505 | loff_t actual_len, u64 *alloc_hint) |
| 7506 | { |
| 7507 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7508 | min_size, actual_len, alloc_hint, |
| 7509 | NULL); |
| 7510 | } |
| 7511 | |
| 7512 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 7513 | struct btrfs_trans_handle *trans, int mode, |
| 7514 | u64 start, u64 num_bytes, u64 min_size, |
| 7515 | loff_t actual_len, u64 *alloc_hint) |
| 7516 | { |
| 7517 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7518 | min_size, actual_len, alloc_hint, trans); |
| 7519 | } |
| 7520 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7521 | static int btrfs_set_page_dirty(struct page *page) |
| 7522 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7523 | return __set_page_dirty_nobuffers(page); |
| 7524 | } |
| 7525 | |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 7526 | static int btrfs_permission(struct inode *inode, int mask, unsigned int flags) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7527 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7528 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7529 | |
| 7530 | if (btrfs_root_readonly(root) && (mask & MAY_WRITE)) |
| 7531 | return -EROFS; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7532 | if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7533 | return -EACCES; |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 7534 | return generic_permission(inode, mask, flags, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7535 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7536 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7537 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7538 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7539 | .lookup = btrfs_lookup, |
| 7540 | .create = btrfs_create, |
| 7541 | .unlink = btrfs_unlink, |
| 7542 | .link = btrfs_link, |
| 7543 | .mkdir = btrfs_mkdir, |
| 7544 | .rmdir = btrfs_rmdir, |
| 7545 | .rename = btrfs_rename, |
| 7546 | .symlink = btrfs_symlink, |
| 7547 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7548 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7549 | .setxattr = btrfs_setxattr, |
| 7550 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7551 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7552 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7553 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7554 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7555 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7556 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7557 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7558 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7559 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 7560 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7561 | .llseek = generic_file_llseek, |
| 7562 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 7563 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7564 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7565 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7566 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7567 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 7568 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7569 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7570 | }; |
| 7571 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7572 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7573 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 7574 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7575 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7576 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7577 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7578 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 7579 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 7580 | .set_bit_hook = btrfs_set_bit_hook, |
| 7581 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7582 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 7583 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7584 | }; |
| 7585 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 7586 | /* |
| 7587 | * btrfs doesn't support the bmap operation because swapfiles |
| 7588 | * use bmap to make a mapping of extents in the file. They assume |
| 7589 | * these extents won't change over the life of the file and they |
| 7590 | * use the bmap result to do IO directly to the drive. |
| 7591 | * |
| 7592 | * the btrfs bmap call would return logical addresses that aren't |
| 7593 | * suitable for IO and they also will change frequently as COW |
| 7594 | * operations happen. So, swapfile + btrfs == corruption. |
| 7595 | * |
| 7596 | * For now we're avoiding this by dropping bmap. |
| 7597 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7598 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7599 | .readpage = btrfs_readpage, |
| 7600 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7601 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7602 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7603 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7604 | .invalidatepage = btrfs_invalidatepage, |
| 7605 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7606 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 7607 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7608 | }; |
| 7609 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7610 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7611 | .readpage = btrfs_readpage, |
| 7612 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 7613 | .invalidatepage = btrfs_invalidatepage, |
| 7614 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7615 | }; |
| 7616 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7617 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7618 | .getattr = btrfs_getattr, |
| 7619 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7620 | .setxattr = btrfs_setxattr, |
| 7621 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7622 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7623 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7624 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7625 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7626 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7627 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7628 | .getattr = btrfs_getattr, |
| 7629 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7630 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7631 | .setxattr = btrfs_setxattr, |
| 7632 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7633 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7634 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7635 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7636 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7637 | .readlink = generic_readlink, |
| 7638 | .follow_link = page_follow_link_light, |
| 7639 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 7640 | .getattr = btrfs_getattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7641 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 7642 | .setxattr = btrfs_setxattr, |
| 7643 | .getxattr = btrfs_getxattr, |
| 7644 | .listxattr = btrfs_listxattr, |
| 7645 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7646 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7647 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 7648 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7649 | .d_delete = btrfs_dentry_delete, |
| 7650 | }; |