Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 20 | #include <linux/bio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 22 | #include <linux/file.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 23 | #include <linux/fs.h> |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/string.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 29 | #include <linux/backing-dev.h> |
| 30 | #include <linux/mpage.h> |
| 31 | #include <linux/swap.h> |
| 32 | #include <linux/writeback.h> |
| 33 | #include <linux/statfs.h> |
| 34 | #include <linux/compat.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 35 | #include <linux/bit_spinlock.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 36 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 37 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 38 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 40 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 41 | #include "ctree.h" |
| 42 | #include "disk-io.h" |
| 43 | #include "transaction.h" |
| 44 | #include "btrfs_inode.h" |
| 45 | #include "ioctl.h" |
| 46 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 47 | #include "volumes.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 48 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 49 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 50 | #include "tree-log.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 51 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 52 | #include "locking.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 53 | |
| 54 | struct btrfs_iget_args { |
| 55 | u64 ino; |
| 56 | struct btrfs_root *root; |
| 57 | }; |
| 58 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 59 | static const struct inode_operations btrfs_dir_inode_operations; |
| 60 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 61 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 62 | static const struct inode_operations btrfs_special_inode_operations; |
| 63 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 64 | static const struct address_space_operations btrfs_aops; |
| 65 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 66 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 67 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 68 | |
| 69 | static struct kmem_cache *btrfs_inode_cachep; |
| 70 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 71 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 72 | struct kmem_cache *btrfs_path_cachep; |
| 73 | |
| 74 | #define S_SHIFT 12 |
| 75 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 76 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 77 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 78 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 79 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 80 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 81 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 82 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 83 | }; |
| 84 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 85 | static void btrfs_truncate(struct inode *inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 86 | static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 87 | static noinline int cow_file_range(struct inode *inode, |
| 88 | struct page *locked_page, |
| 89 | u64 start, u64 end, int *page_started, |
| 90 | unsigned long *nr_written, int unlock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 91 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 92 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
| 93 | struct inode *inode, struct inode *dir) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 94 | { |
| 95 | int err; |
| 96 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 97 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 98 | if (!err) |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 99 | err = btrfs_xattr_security_init(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 100 | return err; |
| 101 | } |
| 102 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 103 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 104 | * this does all the hard work for inserting an inline extent into |
| 105 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 106 | * no overlapping inline items exist in the btree |
| 107 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 108 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 109 | struct btrfs_root *root, struct inode *inode, |
| 110 | u64 start, size_t size, size_t compressed_size, |
| 111 | struct page **compressed_pages) |
| 112 | { |
| 113 | struct btrfs_key key; |
| 114 | struct btrfs_path *path; |
| 115 | struct extent_buffer *leaf; |
| 116 | struct page *page = NULL; |
| 117 | char *kaddr; |
| 118 | unsigned long ptr; |
| 119 | struct btrfs_file_extent_item *ei; |
| 120 | int err = 0; |
| 121 | int ret; |
| 122 | size_t cur_size = size; |
| 123 | size_t datasize; |
| 124 | unsigned long offset; |
| 125 | int use_compress = 0; |
| 126 | |
| 127 | if (compressed_size && compressed_pages) { |
| 128 | use_compress = 1; |
| 129 | cur_size = compressed_size; |
| 130 | } |
| 131 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 132 | path = btrfs_alloc_path(); |
| 133 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 134 | return -ENOMEM; |
| 135 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 136 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 137 | btrfs_set_trans_block_group(trans, inode); |
| 138 | |
| 139 | key.objectid = inode->i_ino; |
| 140 | key.offset = start; |
| 141 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 142 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 143 | |
| 144 | inode_add_bytes(inode, size); |
| 145 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 146 | datasize); |
| 147 | BUG_ON(ret); |
| 148 | if (ret) { |
| 149 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 150 | goto fail; |
| 151 | } |
| 152 | leaf = path->nodes[0]; |
| 153 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 154 | struct btrfs_file_extent_item); |
| 155 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 156 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 157 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 158 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 159 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 160 | ptr = btrfs_file_extent_inline_start(ei); |
| 161 | |
| 162 | if (use_compress) { |
| 163 | struct page *cpage; |
| 164 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 165 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 167 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 168 | PAGE_CACHE_SIZE); |
| 169 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 170 | kaddr = kmap_atomic(cpage, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 172 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 173 | |
| 174 | i++; |
| 175 | ptr += cur_size; |
| 176 | compressed_size -= cur_size; |
| 177 | } |
| 178 | btrfs_set_file_extent_compression(leaf, ei, |
| 179 | BTRFS_COMPRESS_ZLIB); |
| 180 | } else { |
| 181 | page = find_get_page(inode->i_mapping, |
| 182 | start >> PAGE_CACHE_SHIFT); |
| 183 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 184 | kaddr = kmap_atomic(page, KM_USER0); |
| 185 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 186 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
| 187 | kunmap_atomic(kaddr, KM_USER0); |
| 188 | page_cache_release(page); |
| 189 | } |
| 190 | btrfs_mark_buffer_dirty(leaf); |
| 191 | btrfs_free_path(path); |
| 192 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 193 | /* |
| 194 | * we're an inline extent, so nobody can |
| 195 | * extend the file past i_size without locking |
| 196 | * a page we already have locked. |
| 197 | * |
| 198 | * We must do any isize and inode updates |
| 199 | * before we unlock the pages. Otherwise we |
| 200 | * could end up racing with unlink. |
| 201 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 202 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
| 203 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 204 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 205 | return 0; |
| 206 | fail: |
| 207 | btrfs_free_path(path); |
| 208 | return err; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /* |
| 213 | * conditionally insert an inline extent into the file. This |
| 214 | * does the checks required to make sure the data is small enough |
| 215 | * to fit as an inline extent. |
| 216 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 217 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 218 | struct btrfs_root *root, |
| 219 | struct inode *inode, u64 start, u64 end, |
| 220 | size_t compressed_size, |
| 221 | struct page **compressed_pages) |
| 222 | { |
| 223 | u64 isize = i_size_read(inode); |
| 224 | u64 actual_end = min(end + 1, isize); |
| 225 | u64 inline_len = actual_end - start; |
| 226 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 227 | ~((u64)root->sectorsize - 1); |
| 228 | u64 hint_byte; |
| 229 | u64 data_len = inline_len; |
| 230 | int ret; |
| 231 | |
| 232 | if (compressed_size) |
| 233 | data_len = compressed_size; |
| 234 | |
| 235 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 236 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 238 | (!compressed_size && |
| 239 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 240 | end + 1 < isize || |
| 241 | data_len > root->fs_info->max_inline) { |
| 242 | return 1; |
| 243 | } |
| 244 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 245 | ret = btrfs_drop_extents(trans, inode, start, aligned_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 246 | &hint_byte, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 247 | BUG_ON(ret); |
| 248 | |
| 249 | if (isize > actual_end) |
| 250 | inline_len = min_t(u64, isize, actual_end); |
| 251 | ret = insert_inline_extent(trans, root, inode, start, |
| 252 | inline_len, compressed_size, |
| 253 | compressed_pages); |
| 254 | BUG_ON(ret); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 255 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 256 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 260 | struct async_extent { |
| 261 | u64 start; |
| 262 | u64 ram_size; |
| 263 | u64 compressed_size; |
| 264 | struct page **pages; |
| 265 | unsigned long nr_pages; |
| 266 | struct list_head list; |
| 267 | }; |
| 268 | |
| 269 | struct async_cow { |
| 270 | struct inode *inode; |
| 271 | struct btrfs_root *root; |
| 272 | struct page *locked_page; |
| 273 | u64 start; |
| 274 | u64 end; |
| 275 | struct list_head extents; |
| 276 | struct btrfs_work work; |
| 277 | }; |
| 278 | |
| 279 | static noinline int add_async_extent(struct async_cow *cow, |
| 280 | u64 start, u64 ram_size, |
| 281 | u64 compressed_size, |
| 282 | struct page **pages, |
| 283 | unsigned long nr_pages) |
| 284 | { |
| 285 | struct async_extent *async_extent; |
| 286 | |
| 287 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
| 288 | async_extent->start = start; |
| 289 | async_extent->ram_size = ram_size; |
| 290 | async_extent->compressed_size = compressed_size; |
| 291 | async_extent->pages = pages; |
| 292 | async_extent->nr_pages = nr_pages; |
| 293 | list_add_tail(&async_extent->list, &cow->extents); |
| 294 | return 0; |
| 295 | } |
| 296 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 297 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 298 | * we create compressed extents in two phases. The first |
| 299 | * phase compresses a range of pages that have already been |
| 300 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 301 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 302 | * This is done inside an ordered work queue, and the compression |
| 303 | * is spread across many cpus. The actual IO submission is step |
| 304 | * two, and the ordered work queue takes care of making sure that |
| 305 | * happens in the same order things were put onto the queue by |
| 306 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 307 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 308 | * If this code finds it can't get good compression, it puts an |
| 309 | * entry onto the work queue to write the uncompressed bytes. This |
| 310 | * makes sure that both compressed inodes and uncompressed inodes |
| 311 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 312 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 313 | static noinline int compress_file_range(struct inode *inode, |
| 314 | struct page *locked_page, |
| 315 | u64 start, u64 end, |
| 316 | struct async_cow *async_cow, |
| 317 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 318 | { |
| 319 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 320 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 321 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 322 | u64 orig_start; |
| 323 | u64 disk_num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 324 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 325 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 326 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 327 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 328 | struct page **pages = NULL; |
| 329 | unsigned long nr_pages; |
| 330 | unsigned long nr_pages_ret = 0; |
| 331 | unsigned long total_compressed = 0; |
| 332 | unsigned long total_in = 0; |
| 333 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 334 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 335 | int i; |
| 336 | int will_compress; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 337 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 338 | orig_start = start; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 339 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 340 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 341 | again: |
| 342 | will_compress = 0; |
| 343 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 344 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 345 | |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 346 | /* |
| 347 | * we don't want to send crud past the end of i_size through |
| 348 | * compression, that's just a waste of CPU time. So, if the |
| 349 | * end of the file is before the start of our current |
| 350 | * requested range of bytes, we bail out to the uncompressed |
| 351 | * cleanup code that can deal with all of this. |
| 352 | * |
| 353 | * It isn't really the fastest way to fix things, but this is a |
| 354 | * very uncommon corner. |
| 355 | */ |
| 356 | if (actual_end <= start) |
| 357 | goto cleanup_and_bail_uncompressed; |
| 358 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 359 | total_compressed = actual_end - start; |
| 360 | |
| 361 | /* we want to make sure that amount of ram required to uncompress |
| 362 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 363 | * of a compressed extent to 128k. This is a crucial number |
| 364 | * because it also controls how easily we can spread reads across |
| 365 | * cpus for decompression. |
| 366 | * |
| 367 | * We also want to make sure the amount of IO required to do |
| 368 | * a random read is reasonably small, so we limit the size of |
| 369 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 370 | */ |
| 371 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 372 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 373 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 374 | disk_num_bytes = num_bytes; |
| 375 | total_in = 0; |
| 376 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 377 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 378 | /* |
| 379 | * we do compression for mount -o compress and when the |
| 380 | * inode has not been flagged as nocompress. This flag can |
| 381 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 382 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 383 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 384 | (btrfs_test_opt(root, COMPRESS) || |
| 385 | (BTRFS_I(inode)->force_compress))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 386 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 387 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 388 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 389 | ret = btrfs_zlib_compress_pages(inode->i_mapping, start, |
| 390 | total_compressed, pages, |
| 391 | nr_pages, &nr_pages_ret, |
| 392 | &total_in, |
| 393 | &total_compressed, |
| 394 | max_compressed); |
| 395 | |
| 396 | if (!ret) { |
| 397 | unsigned long offset = total_compressed & |
| 398 | (PAGE_CACHE_SIZE - 1); |
| 399 | struct page *page = pages[nr_pages_ret - 1]; |
| 400 | char *kaddr; |
| 401 | |
| 402 | /* zero the tail end of the last page, we might be |
| 403 | * sending it down to disk |
| 404 | */ |
| 405 | if (offset) { |
| 406 | kaddr = kmap_atomic(page, KM_USER0); |
| 407 | memset(kaddr + offset, 0, |
| 408 | PAGE_CACHE_SIZE - offset); |
| 409 | kunmap_atomic(kaddr, KM_USER0); |
| 410 | } |
| 411 | will_compress = 1; |
| 412 | } |
| 413 | } |
| 414 | if (start == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 415 | trans = btrfs_join_transaction(root, 1); |
| 416 | BUG_ON(!trans); |
| 417 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 418 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 419 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 420 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 421 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 422 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 423 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 424 | */ |
| 425 | ret = cow_file_range_inline(trans, root, inode, |
| 426 | start, end, 0, NULL); |
| 427 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 428 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 429 | ret = cow_file_range_inline(trans, root, inode, |
| 430 | start, end, |
| 431 | total_compressed, pages); |
| 432 | } |
| 433 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 434 | /* |
| 435 | * inline extent creation worked, we don't need |
| 436 | * to create any more async work items. Unlock |
| 437 | * and free up our temp pages. |
| 438 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 439 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 440 | &BTRFS_I(inode)->io_tree, |
| 441 | start, end, NULL, |
| 442 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 443 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 444 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 445 | |
| 446 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 447 | goto free_pages_out; |
| 448 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 449 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | if (will_compress) { |
| 453 | /* |
| 454 | * we aren't doing an inline extent round the compressed size |
| 455 | * up to a block size boundary so the allocator does sane |
| 456 | * things |
| 457 | */ |
| 458 | total_compressed = (total_compressed + blocksize - 1) & |
| 459 | ~(blocksize - 1); |
| 460 | |
| 461 | /* |
| 462 | * one last check to make sure the compression is really a |
| 463 | * win, compare the page count read with the blocks on disk |
| 464 | */ |
| 465 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 466 | ~(PAGE_CACHE_SIZE - 1); |
| 467 | if (total_compressed >= total_in) { |
| 468 | will_compress = 0; |
| 469 | } else { |
| 470 | disk_num_bytes = total_compressed; |
| 471 | num_bytes = total_in; |
| 472 | } |
| 473 | } |
| 474 | if (!will_compress && pages) { |
| 475 | /* |
| 476 | * the compression code ran but failed to make things smaller, |
| 477 | * free any pages it allocated and our page pointer array |
| 478 | */ |
| 479 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 480 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 481 | page_cache_release(pages[i]); |
| 482 | } |
| 483 | kfree(pages); |
| 484 | pages = NULL; |
| 485 | total_compressed = 0; |
| 486 | nr_pages_ret = 0; |
| 487 | |
| 488 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 489 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 490 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 491 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 492 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 493 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 494 | if (will_compress) { |
| 495 | *num_added += 1; |
| 496 | |
| 497 | /* the async work queues will take care of doing actual |
| 498 | * allocation on disk for these compressed pages, |
| 499 | * and will submit them to the elevator. |
| 500 | */ |
| 501 | add_async_extent(async_cow, start, num_bytes, |
| 502 | total_compressed, pages, nr_pages_ret); |
| 503 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 504 | if (start + num_bytes < end && start + num_bytes < actual_end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 505 | start += num_bytes; |
| 506 | pages = NULL; |
| 507 | cond_resched(); |
| 508 | goto again; |
| 509 | } |
| 510 | } else { |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 511 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 512 | /* |
| 513 | * No compression, but we still need to write the pages in |
| 514 | * the file we've been given so far. redirty the locked |
| 515 | * page if it corresponds to our extent and set things up |
| 516 | * for the async work queue to run cow_file_range to do |
| 517 | * the normal delalloc dance |
| 518 | */ |
| 519 | if (page_offset(locked_page) >= start && |
| 520 | page_offset(locked_page) <= end) { |
| 521 | __set_page_dirty_nobuffers(locked_page); |
| 522 | /* unlocked later on in the async handlers */ |
| 523 | } |
| 524 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0); |
| 525 | *num_added += 1; |
| 526 | } |
| 527 | |
| 528 | out: |
| 529 | return 0; |
| 530 | |
| 531 | free_pages_out: |
| 532 | for (i = 0; i < nr_pages_ret; i++) { |
| 533 | WARN_ON(pages[i]->mapping); |
| 534 | page_cache_release(pages[i]); |
| 535 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 536 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 537 | |
| 538 | goto out; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * phase two of compressed writeback. This is the ordered portion |
| 543 | * of the code, which only gets called in the order the work was |
| 544 | * queued. We walk all the async extents created by compress_file_range |
| 545 | * and send them down to the disk. |
| 546 | */ |
| 547 | static noinline int submit_compressed_extents(struct inode *inode, |
| 548 | struct async_cow *async_cow) |
| 549 | { |
| 550 | struct async_extent *async_extent; |
| 551 | u64 alloc_hint = 0; |
| 552 | struct btrfs_trans_handle *trans; |
| 553 | struct btrfs_key ins; |
| 554 | struct extent_map *em; |
| 555 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 556 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 557 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 558 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 559 | |
| 560 | if (list_empty(&async_cow->extents)) |
| 561 | return 0; |
| 562 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 563 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 564 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 565 | async_extent = list_entry(async_cow->extents.next, |
| 566 | struct async_extent, list); |
| 567 | list_del(&async_extent->list); |
| 568 | |
| 569 | io_tree = &BTRFS_I(inode)->io_tree; |
| 570 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 571 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 572 | /* did the compression code fall back to uncompressed IO? */ |
| 573 | if (!async_extent->pages) { |
| 574 | int page_started = 0; |
| 575 | unsigned long nr_written = 0; |
| 576 | |
| 577 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 578 | async_extent->start + |
| 579 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 580 | |
| 581 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 582 | ret = cow_file_range(inode, async_cow->locked_page, |
| 583 | async_extent->start, |
| 584 | async_extent->start + |
| 585 | async_extent->ram_size - 1, |
| 586 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * if page_started, cow_file_range inserted an |
| 590 | * inline extent and took care of all the unlocking |
| 591 | * and IO for us. Otherwise, we need to submit |
| 592 | * all those pages down to the drive. |
| 593 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 594 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 595 | extent_write_locked_range(io_tree, |
| 596 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 597 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 598 | async_extent->ram_size - 1, |
| 599 | btrfs_get_extent, |
| 600 | WB_SYNC_ALL); |
| 601 | kfree(async_extent); |
| 602 | cond_resched(); |
| 603 | continue; |
| 604 | } |
| 605 | |
| 606 | lock_extent(io_tree, async_extent->start, |
| 607 | async_extent->start + async_extent->ram_size - 1, |
| 608 | GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 609 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 610 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 611 | ret = btrfs_reserve_extent(trans, root, |
| 612 | async_extent->compressed_size, |
| 613 | async_extent->compressed_size, |
| 614 | 0, alloc_hint, |
| 615 | (u64)-1, &ins, 1); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 616 | btrfs_end_transaction(trans, root); |
| 617 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 618 | if (ret) { |
| 619 | int i; |
| 620 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 621 | WARN_ON(async_extent->pages[i]->mapping); |
| 622 | page_cache_release(async_extent->pages[i]); |
| 623 | } |
| 624 | kfree(async_extent->pages); |
| 625 | async_extent->nr_pages = 0; |
| 626 | async_extent->pages = NULL; |
| 627 | unlock_extent(io_tree, async_extent->start, |
| 628 | async_extent->start + |
| 629 | async_extent->ram_size - 1, GFP_NOFS); |
| 630 | goto retry; |
| 631 | } |
| 632 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 633 | /* |
| 634 | * here we're doing allocation and writeback of the |
| 635 | * compressed pages |
| 636 | */ |
| 637 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 638 | async_extent->start + |
| 639 | async_extent->ram_size - 1, 0); |
| 640 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 641 | em = alloc_extent_map(GFP_NOFS); |
| 642 | em->start = async_extent->start; |
| 643 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 644 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 645 | |
| 646 | em->block_start = ins.objectid; |
| 647 | em->block_len = ins.offset; |
| 648 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 649 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 650 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 651 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 652 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 653 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 654 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 655 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 656 | if (ret != -EEXIST) { |
| 657 | free_extent_map(em); |
| 658 | break; |
| 659 | } |
| 660 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 661 | async_extent->start + |
| 662 | async_extent->ram_size - 1, 0); |
| 663 | } |
| 664 | |
| 665 | ret = btrfs_add_ordered_extent(inode, async_extent->start, |
| 666 | ins.objectid, |
| 667 | async_extent->ram_size, |
| 668 | ins.offset, |
| 669 | BTRFS_ORDERED_COMPRESSED); |
| 670 | BUG_ON(ret); |
| 671 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 672 | /* |
| 673 | * clear dirty, set writeback and unlock the pages. |
| 674 | */ |
| 675 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 676 | &BTRFS_I(inode)->io_tree, |
| 677 | async_extent->start, |
| 678 | async_extent->start + |
| 679 | async_extent->ram_size - 1, |
| 680 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 681 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 682 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 683 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 684 | |
| 685 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 686 | async_extent->start, |
| 687 | async_extent->ram_size, |
| 688 | ins.objectid, |
| 689 | ins.offset, async_extent->pages, |
| 690 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 691 | |
| 692 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 693 | alloc_hint = ins.objectid + ins.offset; |
| 694 | kfree(async_extent); |
| 695 | cond_resched(); |
| 696 | } |
| 697 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | * when extent_io.c finds a delayed allocation range in the file, |
| 703 | * the call backs end up in this code. The basic idea is to |
| 704 | * allocate extents on disk for the range, and create ordered data structs |
| 705 | * in ram to track those extents. |
| 706 | * |
| 707 | * locked_page is the page that writepage had locked already. We use |
| 708 | * it to make sure we don't do extra locks or unlocks. |
| 709 | * |
| 710 | * *page_started is set to one if we unlock locked_page and do everything |
| 711 | * required to start IO on it. It may be clean and already done with |
| 712 | * IO when we return. |
| 713 | */ |
| 714 | static noinline int cow_file_range(struct inode *inode, |
| 715 | struct page *locked_page, |
| 716 | u64 start, u64 end, int *page_started, |
| 717 | unsigned long *nr_written, |
| 718 | int unlock) |
| 719 | { |
| 720 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 721 | struct btrfs_trans_handle *trans; |
| 722 | u64 alloc_hint = 0; |
| 723 | u64 num_bytes; |
| 724 | unsigned long ram_size; |
| 725 | u64 disk_num_bytes; |
| 726 | u64 cur_alloc_size; |
| 727 | u64 blocksize = root->sectorsize; |
| 728 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 729 | u64 isize = i_size_read(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 730 | struct btrfs_key ins; |
| 731 | struct extent_map *em; |
| 732 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 733 | int ret = 0; |
| 734 | |
| 735 | trans = btrfs_join_transaction(root, 1); |
| 736 | BUG_ON(!trans); |
| 737 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 738 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 739 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 740 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 741 | |
| 742 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 743 | num_bytes = max(blocksize, num_bytes); |
| 744 | disk_num_bytes = num_bytes; |
| 745 | ret = 0; |
| 746 | |
| 747 | if (start == 0) { |
| 748 | /* lets try to make an inline extent */ |
| 749 | ret = cow_file_range_inline(trans, root, inode, |
| 750 | start, end, 0, NULL); |
| 751 | if (ret == 0) { |
| 752 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 753 | &BTRFS_I(inode)->io_tree, |
| 754 | start, end, NULL, |
| 755 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 756 | EXTENT_CLEAR_UNLOCK | |
| 757 | EXTENT_CLEAR_DELALLOC | |
| 758 | EXTENT_CLEAR_DIRTY | |
| 759 | EXTENT_SET_WRITEBACK | |
| 760 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 761 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 762 | *nr_written = *nr_written + |
| 763 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 764 | *page_started = 1; |
| 765 | ret = 0; |
| 766 | goto out; |
| 767 | } |
| 768 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 769 | |
| 770 | BUG_ON(disk_num_bytes > |
| 771 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 772 | |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 773 | |
| 774 | read_lock(&BTRFS_I(inode)->extent_tree.lock); |
| 775 | em = search_extent_mapping(&BTRFS_I(inode)->extent_tree, |
| 776 | start, num_bytes); |
| 777 | if (em) { |
Josef Bacik | 6346c93 | 2009-11-10 21:23:47 -0500 | [diff] [blame] | 778 | /* |
| 779 | * if block start isn't an actual block number then find the |
| 780 | * first block in this inode and use that as a hint. If that |
| 781 | * block is also bogus then just don't worry about it. |
| 782 | */ |
| 783 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 784 | free_extent_map(em); |
| 785 | em = search_extent_mapping(em_tree, 0, 0); |
| 786 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 787 | alloc_hint = em->block_start; |
| 788 | if (em) |
| 789 | free_extent_map(em); |
| 790 | } else { |
| 791 | alloc_hint = em->block_start; |
| 792 | free_extent_map(em); |
| 793 | } |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 794 | } |
| 795 | read_unlock(&BTRFS_I(inode)->extent_tree.lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 796 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 797 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 798 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 799 | unsigned long op; |
| 800 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 801 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 802 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 803 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 804 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 805 | BUG_ON(ret); |
| 806 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 807 | em = alloc_extent_map(GFP_NOFS); |
| 808 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 809 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 810 | ram_size = ins.offset; |
| 811 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 812 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 813 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 814 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 815 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 816 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 817 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 818 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 819 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 820 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 821 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 822 | if (ret != -EEXIST) { |
| 823 | free_extent_map(em); |
| 824 | break; |
| 825 | } |
| 826 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 827 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 828 | } |
| 829 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 830 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 831 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 832 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 833 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 834 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 835 | if (root->root_key.objectid == |
| 836 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 837 | ret = btrfs_reloc_clone_csums(inode, start, |
| 838 | cur_alloc_size); |
| 839 | BUG_ON(ret); |
| 840 | } |
| 841 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 842 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 843 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 844 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 845 | /* we're not doing compressed IO, don't unlock the first |
| 846 | * page (which the caller expects to stay locked), don't |
| 847 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 848 | * |
| 849 | * Do set the Private2 bit so we know this page was properly |
| 850 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 851 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 852 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 853 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 854 | EXTENT_SET_PRIVATE2; |
| 855 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 856 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 857 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 858 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 859 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 860 | num_bytes -= cur_alloc_size; |
| 861 | alloc_hint = ins.objectid + ins.offset; |
| 862 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 863 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 864 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 865 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 866 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 867 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 868 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 869 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 870 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 871 | /* |
| 872 | * work queue call back to started compression on a file and pages |
| 873 | */ |
| 874 | static noinline void async_cow_start(struct btrfs_work *work) |
| 875 | { |
| 876 | struct async_cow *async_cow; |
| 877 | int num_added = 0; |
| 878 | async_cow = container_of(work, struct async_cow, work); |
| 879 | |
| 880 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 881 | async_cow->start, async_cow->end, async_cow, |
| 882 | &num_added); |
| 883 | if (num_added == 0) |
| 884 | async_cow->inode = NULL; |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * work queue call back to submit previously compressed pages |
| 889 | */ |
| 890 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 891 | { |
| 892 | struct async_cow *async_cow; |
| 893 | struct btrfs_root *root; |
| 894 | unsigned long nr_pages; |
| 895 | |
| 896 | async_cow = container_of(work, struct async_cow, work); |
| 897 | |
| 898 | root = async_cow->root; |
| 899 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 900 | PAGE_CACHE_SHIFT; |
| 901 | |
| 902 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 903 | |
| 904 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 905 | 5 * 1042 * 1024 && |
| 906 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 907 | wake_up(&root->fs_info->async_submit_wait); |
| 908 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 909 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 910 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 911 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 912 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 913 | static noinline void async_cow_free(struct btrfs_work *work) |
| 914 | { |
| 915 | struct async_cow *async_cow; |
| 916 | async_cow = container_of(work, struct async_cow, work); |
| 917 | kfree(async_cow); |
| 918 | } |
| 919 | |
| 920 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 921 | u64 start, u64 end, int *page_started, |
| 922 | unsigned long *nr_written) |
| 923 | { |
| 924 | struct async_cow *async_cow; |
| 925 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 926 | unsigned long nr_pages; |
| 927 | u64 cur_end; |
| 928 | int limit = 10 * 1024 * 1042; |
| 929 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 930 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 931 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 932 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 933 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
| 934 | async_cow->inode = inode; |
| 935 | async_cow->root = root; |
| 936 | async_cow->locked_page = locked_page; |
| 937 | async_cow->start = start; |
| 938 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 939 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 940 | cur_end = end; |
| 941 | else |
| 942 | cur_end = min(end, start + 512 * 1024 - 1); |
| 943 | |
| 944 | async_cow->end = cur_end; |
| 945 | INIT_LIST_HEAD(&async_cow->extents); |
| 946 | |
| 947 | async_cow->work.func = async_cow_start; |
| 948 | async_cow->work.ordered_func = async_cow_submit; |
| 949 | async_cow->work.ordered_free = async_cow_free; |
| 950 | async_cow->work.flags = 0; |
| 951 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 952 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 953 | PAGE_CACHE_SHIFT; |
| 954 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 955 | |
| 956 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 957 | &async_cow->work); |
| 958 | |
| 959 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 960 | wait_event(root->fs_info->async_submit_wait, |
| 961 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 962 | limit)); |
| 963 | } |
| 964 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 965 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 966 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 967 | wait_event(root->fs_info->async_submit_wait, |
| 968 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 969 | 0)); |
| 970 | } |
| 971 | |
| 972 | *nr_written += nr_pages; |
| 973 | start = cur_end + 1; |
| 974 | } |
| 975 | *page_started = 1; |
| 976 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 977 | } |
| 978 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 979 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 980 | u64 bytenr, u64 num_bytes) |
| 981 | { |
| 982 | int ret; |
| 983 | struct btrfs_ordered_sum *sums; |
| 984 | LIST_HEAD(list); |
| 985 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 986 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
| 987 | bytenr + num_bytes - 1, &list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 988 | if (ret == 0 && list_empty(&list)) |
| 989 | return 0; |
| 990 | |
| 991 | while (!list_empty(&list)) { |
| 992 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 993 | list_del(&sums->list); |
| 994 | kfree(sums); |
| 995 | } |
| 996 | return 1; |
| 997 | } |
| 998 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 999 | /* |
| 1000 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1001 | * of the extents that exist in the file, and COWs the file as required. |
| 1002 | * |
| 1003 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1004 | * blocks on disk |
| 1005 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1006 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1007 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1008 | u64 start, u64 end, int *page_started, int force, |
| 1009 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1010 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1011 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1012 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1013 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1014 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1015 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1016 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1017 | u64 cow_start; |
| 1018 | u64 cur_offset; |
| 1019 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1020 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1021 | u64 disk_bytenr; |
| 1022 | u64 num_bytes; |
| 1023 | int extent_type; |
| 1024 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1025 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1026 | int nocow; |
| 1027 | int check_prev = 1; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1028 | |
| 1029 | path = btrfs_alloc_path(); |
| 1030 | BUG_ON(!path); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1031 | trans = btrfs_join_transaction(root, 1); |
| 1032 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1033 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1034 | cow_start = (u64)-1; |
| 1035 | cur_offset = start; |
| 1036 | while (1) { |
| 1037 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 1038 | cur_offset, 0); |
| 1039 | BUG_ON(ret < 0); |
| 1040 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1041 | leaf = path->nodes[0]; |
| 1042 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1043 | path->slots[0] - 1); |
| 1044 | if (found_key.objectid == inode->i_ino && |
| 1045 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1046 | path->slots[0]--; |
| 1047 | } |
| 1048 | check_prev = 0; |
| 1049 | next_slot: |
| 1050 | leaf = path->nodes[0]; |
| 1051 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1052 | ret = btrfs_next_leaf(root, path); |
| 1053 | if (ret < 0) |
| 1054 | BUG_ON(1); |
| 1055 | if (ret > 0) |
| 1056 | break; |
| 1057 | leaf = path->nodes[0]; |
| 1058 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1059 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1060 | nocow = 0; |
| 1061 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1062 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1063 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1064 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1065 | if (found_key.objectid > inode->i_ino || |
| 1066 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1067 | found_key.offset > end) |
| 1068 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1069 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1070 | if (found_key.offset > cur_offset) { |
| 1071 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1072 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1073 | goto out_check; |
| 1074 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1075 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1076 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1077 | struct btrfs_file_extent_item); |
| 1078 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1079 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1080 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1081 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1082 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1083 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1084 | extent_end = found_key.offset + |
| 1085 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1086 | if (extent_end <= start) { |
| 1087 | path->slots[0]++; |
| 1088 | goto next_slot; |
| 1089 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1090 | if (disk_bytenr == 0) |
| 1091 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1092 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1093 | btrfs_file_extent_encryption(leaf, fi) || |
| 1094 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1095 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1096 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1097 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1098 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1099 | goto out_check; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1100 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1101 | found_key.offset - |
| 1102 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1103 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1104 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1105 | disk_bytenr += cur_offset - found_key.offset; |
| 1106 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1107 | /* |
| 1108 | * force cow if csum exists in the range. |
| 1109 | * this ensure that csum for a given extent are |
| 1110 | * either valid or do not exist. |
| 1111 | */ |
| 1112 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1113 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1114 | nocow = 1; |
| 1115 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1116 | extent_end = found_key.offset + |
| 1117 | btrfs_file_extent_inline_len(leaf, fi); |
| 1118 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1119 | } else { |
| 1120 | BUG_ON(1); |
| 1121 | } |
| 1122 | out_check: |
| 1123 | if (extent_end <= start) { |
| 1124 | path->slots[0]++; |
| 1125 | goto next_slot; |
| 1126 | } |
| 1127 | if (!nocow) { |
| 1128 | if (cow_start == (u64)-1) |
| 1129 | cow_start = cur_offset; |
| 1130 | cur_offset = extent_end; |
| 1131 | if (cur_offset > end) |
| 1132 | break; |
| 1133 | path->slots[0]++; |
| 1134 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1135 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1136 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1137 | btrfs_release_path(root, path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1138 | if (cow_start != (u64)-1) { |
| 1139 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1140 | found_key.offset - 1, page_started, |
| 1141 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1142 | BUG_ON(ret); |
| 1143 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1144 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1145 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1146 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1147 | struct extent_map *em; |
| 1148 | struct extent_map_tree *em_tree; |
| 1149 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 1150 | em = alloc_extent_map(GFP_NOFS); |
| 1151 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1152 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1153 | em->len = num_bytes; |
| 1154 | em->block_len = num_bytes; |
| 1155 | em->block_start = disk_bytenr; |
| 1156 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1157 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1158 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1159 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1160 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1161 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1162 | if (ret != -EEXIST) { |
| 1163 | free_extent_map(em); |
| 1164 | break; |
| 1165 | } |
| 1166 | btrfs_drop_extent_cache(inode, em->start, |
| 1167 | em->start + em->len - 1, 0); |
| 1168 | } |
| 1169 | type = BTRFS_ORDERED_PREALLOC; |
| 1170 | } else { |
| 1171 | type = BTRFS_ORDERED_NOCOW; |
| 1172 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1173 | |
| 1174 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1175 | num_bytes, num_bytes, type); |
| 1176 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1177 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1178 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1179 | cur_offset, cur_offset + num_bytes - 1, |
| 1180 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1181 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1182 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1183 | cur_offset = extent_end; |
| 1184 | if (cur_offset > end) |
| 1185 | break; |
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 | btrfs_release_path(root, path); |
| 1188 | |
| 1189 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1190 | cow_start = cur_offset; |
| 1191 | if (cow_start != (u64)-1) { |
| 1192 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1193 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1194 | BUG_ON(ret); |
| 1195 | } |
| 1196 | |
| 1197 | ret = btrfs_end_transaction(trans, root); |
| 1198 | BUG_ON(ret); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1199 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1200 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1201 | } |
| 1202 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1203 | /* |
| 1204 | * extent_io.c call back to do delayed allocation processing |
| 1205 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1206 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1207 | u64 start, u64 end, int *page_started, |
| 1208 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1209 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1210 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1211 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1212 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1213 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1214 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1215 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1216 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1217 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1218 | page_started, 0, nr_written); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1219 | else if (!btrfs_test_opt(root, COMPRESS) && |
| 1220 | !(BTRFS_I(inode)->force_compress)) |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1221 | ret = cow_file_range(inode, locked_page, start, end, |
| 1222 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1223 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1224 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1225 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1226 | return ret; |
| 1227 | } |
| 1228 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1229 | static int btrfs_split_extent_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1230 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1231 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1232 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1233 | if (!(orig->state & EXTENT_DELALLOC)) |
| 1234 | return 0; |
| 1235 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1236 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | /* |
| 1241 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1242 | * extents so we can keep track of new extents that are just merged onto old |
| 1243 | * extents, such as when we are doing sequential writes, so we can properly |
| 1244 | * account for the metadata space we'll need. |
| 1245 | */ |
| 1246 | static int btrfs_merge_extent_hook(struct inode *inode, |
| 1247 | struct extent_state *new, |
| 1248 | struct extent_state *other) |
| 1249 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1250 | /* not delalloc, ignore it */ |
| 1251 | if (!(other->state & EXTENT_DELALLOC)) |
| 1252 | return 0; |
| 1253 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1254 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1255 | return 0; |
| 1256 | } |
| 1257 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1258 | /* |
| 1259 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1260 | * bytes in this file, and to maintain the list of inodes that |
| 1261 | * have pending delalloc work to be done. |
| 1262 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1263 | static int btrfs_set_bit_hook(struct inode *inode, |
| 1264 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1265 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1266 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1267 | /* |
| 1268 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1269 | * but in this case, we are only testeing for the DELALLOC |
| 1270 | * bit, which is only set or cleared with irqs on |
| 1271 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1272 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1273 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1274 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1275 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1276 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1277 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1278 | else |
| 1279 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1280 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1281 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1282 | BTRFS_I(inode)->delalloc_bytes += len; |
| 1283 | root->fs_info->delalloc_bytes += len; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1284 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1285 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1286 | &root->fs_info->delalloc_inodes); |
| 1287 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1288 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1289 | } |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1293 | /* |
| 1294 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1295 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1296 | static int btrfs_clear_bit_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1297 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1298 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1299 | /* |
| 1300 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1301 | * but in this case, we are only testeing for the DELALLOC |
| 1302 | * bit, which is only set or cleared with irqs on |
| 1303 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1304 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1305 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1306 | u64 len = state->end + 1 - state->start; |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1307 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1308 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1309 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1310 | else if (!(*bits & EXTENT_DO_ACCOUNTING)) |
| 1311 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
| 1312 | |
| 1313 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1314 | btrfs_delalloc_release_metadata(inode, len); |
| 1315 | |
| 1316 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1317 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1318 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1319 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1320 | root->fs_info->delalloc_bytes -= len; |
| 1321 | BTRFS_I(inode)->delalloc_bytes -= len; |
| 1322 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1323 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
| 1324 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1325 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1326 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1327 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1328 | } |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1332 | /* |
| 1333 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1334 | * we don't create bios that span stripes or chunks |
| 1335 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1336 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1337 | size_t size, struct bio *bio, |
| 1338 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1339 | { |
| 1340 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1341 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1342 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1343 | u64 length = 0; |
| 1344 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1345 | int ret; |
| 1346 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1347 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1348 | return 0; |
| 1349 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1350 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1351 | map_tree = &root->fs_info->mapping_tree; |
| 1352 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1353 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1354 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1355 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1356 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1357 | return 1; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1358 | return 0; |
| 1359 | } |
| 1360 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1361 | /* |
| 1362 | * in order to insert checksums into the metadata in large chunks, |
| 1363 | * we wait until bio submission time. All the pages in the bio are |
| 1364 | * checksummed and sums are attached onto the ordered extent record. |
| 1365 | * |
| 1366 | * At IO completion time the cums attached on the ordered extent record |
| 1367 | * are inserted into the btree |
| 1368 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1369 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1370 | struct bio *bio, int mirror_num, |
| 1371 | unsigned long bio_flags) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1372 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1373 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1374 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1375 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1376 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1377 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1378 | return 0; |
| 1379 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1380 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1381 | /* |
| 1382 | * in order to insert checksums into the metadata in large chunks, |
| 1383 | * we wait until bio submission time. All the pages in the bio are |
| 1384 | * checksummed and sums are attached onto the ordered extent record. |
| 1385 | * |
| 1386 | * At IO completion time the cums attached on the ordered extent record |
| 1387 | * are inserted into the btree |
| 1388 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1389 | static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1390 | int mirror_num, unsigned long bio_flags) |
| 1391 | { |
| 1392 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1393 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1394 | } |
| 1395 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1396 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1397 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1398 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1399 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1400 | static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1401 | int mirror_num, unsigned long bio_flags) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1402 | { |
| 1403 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1404 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1405 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1406 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1407 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1408 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1409 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 1410 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1411 | |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1412 | if (!(rw & (1 << BIO_RW))) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1413 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1414 | return btrfs_submit_compressed_read(inode, bio, |
| 1415 | mirror_num, bio_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1416 | } else if (!skip_sum) |
| 1417 | btrfs_lookup_bio_sums(root, inode, bio, NULL); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1418 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1419 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1420 | /* csum items have already been cloned */ |
| 1421 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1422 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1423 | /* we're doing a write, do the async checksumming */ |
| 1424 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1425 | inode, rw, bio, mirror_num, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1426 | bio_flags, __btrfs_submit_bio_start, |
| 1427 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1428 | } |
| 1429 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1430 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1431 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1432 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1433 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1434 | /* |
| 1435 | * given a list of ordered sums record them in the inode. This happens |
| 1436 | * at IO completion time based on sums calculated at bio submission time. |
| 1437 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1438 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1439 | struct inode *inode, u64 file_offset, |
| 1440 | struct list_head *list) |
| 1441 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1442 | struct btrfs_ordered_sum *sum; |
| 1443 | |
| 1444 | btrfs_set_trans_block_group(trans, inode); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1445 | |
| 1446 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1447 | btrfs_csum_file_blocks(trans, |
| 1448 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1449 | } |
| 1450 | return 0; |
| 1451 | } |
| 1452 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1453 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1454 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1455 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1456 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1457 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1458 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1459 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1460 | } |
| 1461 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1462 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1463 | struct btrfs_writepage_fixup { |
| 1464 | struct page *page; |
| 1465 | struct btrfs_work work; |
| 1466 | }; |
| 1467 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1468 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1469 | { |
| 1470 | struct btrfs_writepage_fixup *fixup; |
| 1471 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1472 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1473 | struct page *page; |
| 1474 | struct inode *inode; |
| 1475 | u64 page_start; |
| 1476 | u64 page_end; |
| 1477 | |
| 1478 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1479 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1480 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1481 | lock_page(page); |
| 1482 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1483 | ClearPageChecked(page); |
| 1484 | goto out_page; |
| 1485 | } |
| 1486 | |
| 1487 | inode = page->mapping->host; |
| 1488 | page_start = page_offset(page); |
| 1489 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1490 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1491 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
| 1492 | &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1493 | |
| 1494 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1495 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1496 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1497 | |
| 1498 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1499 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1500 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1501 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1502 | unlock_page(page); |
| 1503 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1504 | goto again; |
| 1505 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1506 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1507 | BUG(); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1508 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1509 | ClearPageChecked(page); |
| 1510 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1511 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1512 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1513 | out_page: |
| 1514 | unlock_page(page); |
| 1515 | page_cache_release(page); |
| 1516 | } |
| 1517 | |
| 1518 | /* |
| 1519 | * There are a few paths in the higher layers of the kernel that directly |
| 1520 | * set the page dirty bit without asking the filesystem if it is a |
| 1521 | * good idea. This causes problems because we want to make sure COW |
| 1522 | * properly happens and the data=ordered rules are followed. |
| 1523 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1524 | * 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] | 1525 | * hasn't been properly setup for IO. We kick off an async process |
| 1526 | * to fix it up. The async helper will wait for ordered extents, set |
| 1527 | * the delalloc bit and make it safe to write the page. |
| 1528 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1529 | 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] | 1530 | { |
| 1531 | struct inode *inode = page->mapping->host; |
| 1532 | struct btrfs_writepage_fixup *fixup; |
| 1533 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1534 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1535 | /* this page is properly in the ordered list */ |
| 1536 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1537 | return 0; |
| 1538 | |
| 1539 | if (PageChecked(page)) |
| 1540 | return -EAGAIN; |
| 1541 | |
| 1542 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1543 | if (!fixup) |
| 1544 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1545 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1546 | SetPageChecked(page); |
| 1547 | page_cache_get(page); |
| 1548 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1549 | fixup->page = page; |
| 1550 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1551 | return -EAGAIN; |
| 1552 | } |
| 1553 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1554 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1555 | struct inode *inode, u64 file_pos, |
| 1556 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1557 | u64 num_bytes, u64 ram_bytes, |
| 1558 | u8 compression, u8 encryption, |
| 1559 | u16 other_encoding, int extent_type) |
| 1560 | { |
| 1561 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1562 | struct btrfs_file_extent_item *fi; |
| 1563 | struct btrfs_path *path; |
| 1564 | struct extent_buffer *leaf; |
| 1565 | struct btrfs_key ins; |
| 1566 | u64 hint; |
| 1567 | int ret; |
| 1568 | |
| 1569 | path = btrfs_alloc_path(); |
| 1570 | BUG_ON(!path); |
| 1571 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1572 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1573 | |
| 1574 | /* |
| 1575 | * we may be replacing one extent in the tree with another. |
| 1576 | * The new extent is pinned in the extent map, and we don't want |
| 1577 | * to drop it from the cache until it is completely in the btree. |
| 1578 | * |
| 1579 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1580 | * the caller is expected to unpin it and allow it to be merged |
| 1581 | * with the others. |
| 1582 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1583 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1584 | &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1585 | BUG_ON(ret); |
| 1586 | |
| 1587 | ins.objectid = inode->i_ino; |
| 1588 | ins.offset = file_pos; |
| 1589 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1590 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1591 | BUG_ON(ret); |
| 1592 | leaf = path->nodes[0]; |
| 1593 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1594 | struct btrfs_file_extent_item); |
| 1595 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1596 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1597 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1598 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1599 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1600 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1601 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1602 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1603 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1604 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1605 | |
| 1606 | btrfs_unlock_up_safe(path, 1); |
| 1607 | btrfs_set_lock_blocking(leaf); |
| 1608 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1609 | btrfs_mark_buffer_dirty(leaf); |
| 1610 | |
| 1611 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1612 | |
| 1613 | ins.objectid = disk_bytenr; |
| 1614 | ins.offset = disk_num_bytes; |
| 1615 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1616 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1617 | root->root_key.objectid, |
| 1618 | inode->i_ino, file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1619 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1620 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1621 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1622 | return 0; |
| 1623 | } |
| 1624 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1625 | /* |
| 1626 | * helper function for btrfs_finish_ordered_io, this |
| 1627 | * just reads in some of the csum leaves to prime them into ram |
| 1628 | * before we start the transaction. It limits the amount of btree |
| 1629 | * reads required while inside the transaction. |
| 1630 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1631 | /* as ordered data IO finishes, this gets called so we can finish |
| 1632 | * an ordered extent if the range of bytes in the file it covers are |
| 1633 | * fully written. |
| 1634 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1635 | 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] | 1636 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1637 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1638 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1639 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1640 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1641 | struct extent_state *cached_state = NULL; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1642 | int compressed = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1643 | int ret; |
| 1644 | |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 1645 | ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 1646 | end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1647 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1648 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1649 | BUG_ON(!ordered_extent); |
Josef Bacik | efd049f | 2010-02-02 20:50:10 +0000 | [diff] [blame] | 1650 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1651 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
| 1652 | BUG_ON(!list_empty(&ordered_extent->list)); |
| 1653 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1654 | if (!ret) { |
| 1655 | trans = btrfs_join_transaction(root, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1656 | btrfs_set_trans_block_group(trans, inode); |
| 1657 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1658 | ret = btrfs_update_inode(trans, root, inode); |
| 1659 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1660 | } |
| 1661 | goto out; |
| 1662 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1663 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1664 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 1665 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1666 | 0, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1667 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1668 | trans = btrfs_join_transaction(root, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1669 | btrfs_set_trans_block_group(trans, inode); |
| 1670 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1671 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1672 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1673 | compressed = 1; |
| 1674 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 1675 | BUG_ON(compressed); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1676 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1677 | ordered_extent->file_offset, |
| 1678 | ordered_extent->file_offset + |
| 1679 | ordered_extent->len); |
| 1680 | BUG_ON(ret); |
| 1681 | } else { |
| 1682 | ret = insert_reserved_file_extent(trans, inode, |
| 1683 | ordered_extent->file_offset, |
| 1684 | ordered_extent->start, |
| 1685 | ordered_extent->disk_len, |
| 1686 | ordered_extent->len, |
| 1687 | ordered_extent->len, |
| 1688 | compressed, 0, 0, |
| 1689 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1690 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1691 | ordered_extent->file_offset, |
| 1692 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1693 | BUG_ON(ret); |
| 1694 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1695 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 1696 | ordered_extent->file_offset + |
| 1697 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
| 1698 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1699 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1700 | &ordered_extent->list); |
| 1701 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1702 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1703 | ret = btrfs_update_inode(trans, root, inode); |
| 1704 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1705 | out: |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 1706 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
| 1707 | if (trans) |
| 1708 | btrfs_end_transaction(trans, root); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1709 | /* once for us */ |
| 1710 | btrfs_put_ordered_extent(ordered_extent); |
| 1711 | /* once for the tree */ |
| 1712 | btrfs_put_ordered_extent(ordered_extent); |
| 1713 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1714 | return 0; |
| 1715 | } |
| 1716 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1717 | 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] | 1718 | struct extent_state *state, int uptodate) |
| 1719 | { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1720 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1721 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1722 | } |
| 1723 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1724 | /* |
| 1725 | * When IO fails, either with EIO or csum verification fails, we |
| 1726 | * try other mirrors that might have a good copy of the data. This |
| 1727 | * io_failure_record is used to record state as we go through all the |
| 1728 | * mirrors. If another mirror has good data, the page is set up to date |
| 1729 | * and things continue. If a good mirror can't be found, the original |
| 1730 | * bio end_io callback is called to indicate things have failed. |
| 1731 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1732 | struct io_failure_record { |
| 1733 | struct page *page; |
| 1734 | u64 start; |
| 1735 | u64 len; |
| 1736 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1737 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1738 | int last_mirror; |
| 1739 | }; |
| 1740 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1741 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1742 | struct page *page, u64 start, u64 end, |
| 1743 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1744 | { |
| 1745 | struct io_failure_record *failrec = NULL; |
| 1746 | u64 private; |
| 1747 | struct extent_map *em; |
| 1748 | struct inode *inode = page->mapping->host; |
| 1749 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1750 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1751 | struct bio *bio; |
| 1752 | int num_copies; |
| 1753 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1754 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1755 | u64 logical; |
| 1756 | |
| 1757 | ret = get_state_private(failure_tree, start, &private); |
| 1758 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1759 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1760 | if (!failrec) |
| 1761 | return -ENOMEM; |
| 1762 | failrec->start = start; |
| 1763 | failrec->len = end - start + 1; |
| 1764 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1765 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1766 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1767 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1768 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1769 | if (em->start > start || em->start + em->len < start) { |
| 1770 | free_extent_map(em); |
| 1771 | em = NULL; |
| 1772 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1773 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1774 | |
| 1775 | if (!em || IS_ERR(em)) { |
| 1776 | kfree(failrec); |
| 1777 | return -EIO; |
| 1778 | } |
| 1779 | logical = start - em->start; |
| 1780 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1781 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1782 | logical = em->block_start; |
| 1783 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 1784 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1785 | failrec->logical = logical; |
| 1786 | free_extent_map(em); |
| 1787 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1788 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1789 | set_state_private(failure_tree, start, |
| 1790 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1791 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1792 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1793 | } |
| 1794 | num_copies = btrfs_num_copies( |
| 1795 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1796 | failrec->logical, failrec->len); |
| 1797 | failrec->last_mirror++; |
| 1798 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1799 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1800 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1801 | failrec->start, |
| 1802 | EXTENT_LOCKED); |
| 1803 | if (state && state->start != failrec->start) |
| 1804 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1805 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1806 | } |
| 1807 | if (!state || failrec->last_mirror > num_copies) { |
| 1808 | set_state_private(failure_tree, failrec->start, 0); |
| 1809 | clear_extent_bits(failure_tree, failrec->start, |
| 1810 | failrec->start + failrec->len - 1, |
| 1811 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1812 | kfree(failrec); |
| 1813 | return -EIO; |
| 1814 | } |
| 1815 | bio = bio_alloc(GFP_NOFS, 1); |
| 1816 | bio->bi_private = state; |
| 1817 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1818 | bio->bi_sector = failrec->logical >> 9; |
| 1819 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1820 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1821 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1822 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1823 | if (failed_bio->bi_rw & (1 << BIO_RW)) |
| 1824 | rw = WRITE; |
| 1825 | else |
| 1826 | rw = READ; |
| 1827 | |
| 1828 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1829 | failrec->last_mirror, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1830 | failrec->bio_flags); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1831 | return 0; |
| 1832 | } |
| 1833 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1834 | /* |
| 1835 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1836 | * to see if we need to process or clean up an io_failure_record |
| 1837 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1838 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1839 | { |
| 1840 | u64 private; |
| 1841 | u64 private_failure; |
| 1842 | struct io_failure_record *failure; |
| 1843 | int ret; |
| 1844 | |
| 1845 | private = 0; |
| 1846 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 1847 | (u64)-1, 1, EXTENT_DIRTY)) { |
| 1848 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1849 | start, &private_failure); |
| 1850 | if (ret == 0) { |
| 1851 | failure = (struct io_failure_record *)(unsigned long) |
| 1852 | private_failure; |
| 1853 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1854 | failure->start, 0); |
| 1855 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1856 | failure->start, |
| 1857 | failure->start + failure->len - 1, |
| 1858 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1859 | GFP_NOFS); |
| 1860 | kfree(failure); |
| 1861 | } |
| 1862 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1863 | return 0; |
| 1864 | } |
| 1865 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1866 | /* |
| 1867 | * when reads are done, we need to check csums to verify the data is correct |
| 1868 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1869 | * the io_failure_record routines to find good copies |
| 1870 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1871 | 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] | 1872 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1873 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1874 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1875 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1876 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1877 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1878 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1879 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1880 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1881 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1882 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1883 | if (PageChecked(page)) { |
| 1884 | ClearPageChecked(page); |
| 1885 | goto good; |
| 1886 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1887 | |
| 1888 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1889 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1890 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1891 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1892 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1893 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1894 | GFP_NOFS); |
| 1895 | return 0; |
| 1896 | } |
| 1897 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1898 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1899 | private = state->private; |
| 1900 | ret = 0; |
| 1901 | } else { |
| 1902 | ret = get_state_private(io_tree, start, &private); |
| 1903 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1904 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1905 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1906 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1907 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1908 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1909 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1910 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1911 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1912 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1913 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1914 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1915 | /* if the io failure tree for this inode is non-empty, |
| 1916 | * check to see if we've recovered from a failed IO |
| 1917 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1918 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1919 | return 0; |
| 1920 | |
| 1921 | zeroit: |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 1922 | if (printk_ratelimit()) { |
| 1923 | printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " |
| 1924 | "private %llu\n", page->mapping->host->i_ino, |
| 1925 | (unsigned long long)start, csum, |
| 1926 | (unsigned long long)private); |
| 1927 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1928 | memset(kaddr + offset, 1, end - start + 1); |
| 1929 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1930 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1931 | if (private == 0) |
| 1932 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1933 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1934 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1935 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 1936 | struct delayed_iput { |
| 1937 | struct list_head list; |
| 1938 | struct inode *inode; |
| 1939 | }; |
| 1940 | |
| 1941 | void btrfs_add_delayed_iput(struct inode *inode) |
| 1942 | { |
| 1943 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 1944 | struct delayed_iput *delayed; |
| 1945 | |
| 1946 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 1947 | return; |
| 1948 | |
| 1949 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 1950 | delayed->inode = inode; |
| 1951 | |
| 1952 | spin_lock(&fs_info->delayed_iput_lock); |
| 1953 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 1954 | spin_unlock(&fs_info->delayed_iput_lock); |
| 1955 | } |
| 1956 | |
| 1957 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 1958 | { |
| 1959 | LIST_HEAD(list); |
| 1960 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1961 | struct delayed_iput *delayed; |
| 1962 | int empty; |
| 1963 | |
| 1964 | spin_lock(&fs_info->delayed_iput_lock); |
| 1965 | empty = list_empty(&fs_info->delayed_iputs); |
| 1966 | spin_unlock(&fs_info->delayed_iput_lock); |
| 1967 | if (empty) |
| 1968 | return; |
| 1969 | |
| 1970 | down_read(&root->fs_info->cleanup_work_sem); |
| 1971 | spin_lock(&fs_info->delayed_iput_lock); |
| 1972 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 1973 | spin_unlock(&fs_info->delayed_iput_lock); |
| 1974 | |
| 1975 | while (!list_empty(&list)) { |
| 1976 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 1977 | list_del(&delayed->list); |
| 1978 | iput(delayed->inode); |
| 1979 | kfree(delayed); |
| 1980 | } |
| 1981 | up_read(&root->fs_info->cleanup_work_sem); |
| 1982 | } |
| 1983 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1984 | /* |
| 1985 | * This creates an orphan entry for the given inode in case something goes |
| 1986 | * wrong in the middle of an unlink/truncate. |
| 1987 | */ |
| 1988 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 1989 | { |
| 1990 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1991 | int ret = 0; |
| 1992 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1993 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1994 | |
| 1995 | /* already on the orphan list, we're good */ |
| 1996 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1997 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1998 | return 0; |
| 1999 | } |
| 2000 | |
| 2001 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2002 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2003 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2004 | |
| 2005 | /* |
| 2006 | * insert an orphan item to track this unlinked/truncated file |
| 2007 | */ |
| 2008 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 2009 | |
| 2010 | return ret; |
| 2011 | } |
| 2012 | |
| 2013 | /* |
| 2014 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2015 | * item for this particular inode. |
| 2016 | */ |
| 2017 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2018 | { |
| 2019 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2020 | int ret = 0; |
| 2021 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2022 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2023 | |
| 2024 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2025 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2026 | return 0; |
| 2027 | } |
| 2028 | |
| 2029 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2030 | if (!trans) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2031 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2032 | return 0; |
| 2033 | } |
| 2034 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2035 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2036 | |
| 2037 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 2038 | |
| 2039 | return ret; |
| 2040 | } |
| 2041 | |
| 2042 | /* |
| 2043 | * this cleans up any orphans that may be left on the list from the last use |
| 2044 | * of this root. |
| 2045 | */ |
| 2046 | void btrfs_orphan_cleanup(struct btrfs_root *root) |
| 2047 | { |
| 2048 | struct btrfs_path *path; |
| 2049 | struct extent_buffer *leaf; |
| 2050 | struct btrfs_item *item; |
| 2051 | struct btrfs_key key, found_key; |
| 2052 | struct btrfs_trans_handle *trans; |
| 2053 | struct inode *inode; |
| 2054 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2055 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2056 | if (!xchg(&root->clean_orphans, 0)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2057 | return; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2058 | |
| 2059 | path = btrfs_alloc_path(); |
| 2060 | BUG_ON(!path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2061 | path->reada = -1; |
| 2062 | |
| 2063 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2064 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2065 | key.offset = (u64)-1; |
| 2066 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2067 | while (1) { |
| 2068 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2069 | if (ret < 0) { |
| 2070 | printk(KERN_ERR "Error searching slot for orphan: %d" |
| 2071 | "\n", ret); |
| 2072 | break; |
| 2073 | } |
| 2074 | |
| 2075 | /* |
| 2076 | * if ret == 0 means we found what we were searching for, which |
| 2077 | * is weird, but possible, so only screw with path if we didnt |
| 2078 | * find the key and see if we have stuff that matches |
| 2079 | */ |
| 2080 | if (ret > 0) { |
| 2081 | if (path->slots[0] == 0) |
| 2082 | break; |
| 2083 | path->slots[0]--; |
| 2084 | } |
| 2085 | |
| 2086 | /* pull out the item */ |
| 2087 | leaf = path->nodes[0]; |
| 2088 | item = btrfs_item_nr(leaf, path->slots[0]); |
| 2089 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2090 | |
| 2091 | /* make sure the item matches what we want */ |
| 2092 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2093 | break; |
| 2094 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2095 | break; |
| 2096 | |
| 2097 | /* release the path since we're done with it */ |
| 2098 | btrfs_release_path(root, path); |
| 2099 | |
| 2100 | /* |
| 2101 | * this is where we are basically btrfs_lookup, without the |
| 2102 | * crossing root thing. we store the inode number in the |
| 2103 | * offset of the orphan item. |
| 2104 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2105 | found_key.objectid = found_key.offset; |
| 2106 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2107 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2108 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2109 | if (IS_ERR(inode)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2110 | break; |
| 2111 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2112 | /* |
| 2113 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2114 | * the proper thing when we hit it |
| 2115 | */ |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2116 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2117 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2118 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2119 | |
| 2120 | /* |
| 2121 | * if this is a bad inode, means we actually succeeded in |
| 2122 | * removing the inode, but not the orphan record, which means |
| 2123 | * we need to manually delete the orphan since iput will just |
| 2124 | * do a destroy_inode |
| 2125 | */ |
| 2126 | if (is_bad_inode(inode)) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2127 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2128 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2129 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2130 | iput(inode); |
| 2131 | continue; |
| 2132 | } |
| 2133 | |
| 2134 | /* if we have links, this was a truncate, lets do that */ |
| 2135 | if (inode->i_nlink) { |
| 2136 | nr_truncate++; |
| 2137 | btrfs_truncate(inode); |
| 2138 | } else { |
| 2139 | nr_unlink++; |
| 2140 | } |
| 2141 | |
| 2142 | /* this will do delete_inode and everything for us */ |
| 2143 | iput(inode); |
| 2144 | } |
| 2145 | |
| 2146 | if (nr_unlink) |
| 2147 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2148 | if (nr_truncate) |
| 2149 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
| 2150 | |
| 2151 | btrfs_free_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2152 | } |
| 2153 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2154 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2155 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2156 | * don't find any xattrs, we know there can't be any acls. |
| 2157 | * |
| 2158 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2159 | */ |
| 2160 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2161 | int slot, u64 objectid) |
| 2162 | { |
| 2163 | u32 nritems = btrfs_header_nritems(leaf); |
| 2164 | struct btrfs_key found_key; |
| 2165 | int scanned = 0; |
| 2166 | |
| 2167 | slot++; |
| 2168 | while (slot < nritems) { |
| 2169 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2170 | |
| 2171 | /* we found a different objectid, there must not be acls */ |
| 2172 | if (found_key.objectid != objectid) |
| 2173 | return 0; |
| 2174 | |
| 2175 | /* we found an xattr, assume we've got an acl */ |
| 2176 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2177 | return 1; |
| 2178 | |
| 2179 | /* |
| 2180 | * we found a key greater than an xattr key, there can't |
| 2181 | * be any acls later on |
| 2182 | */ |
| 2183 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2184 | return 0; |
| 2185 | |
| 2186 | slot++; |
| 2187 | scanned++; |
| 2188 | |
| 2189 | /* |
| 2190 | * it goes inode, inode backrefs, xattrs, extents, |
| 2191 | * so if there are a ton of hard links to an inode there can |
| 2192 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2193 | * this is just an optimization |
| 2194 | */ |
| 2195 | if (scanned >= 8) |
| 2196 | break; |
| 2197 | } |
| 2198 | /* we hit the end of the leaf before we found an xattr or |
| 2199 | * something larger than an xattr. We have to assume the inode |
| 2200 | * has acls |
| 2201 | */ |
| 2202 | return 1; |
| 2203 | } |
| 2204 | |
| 2205 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2206 | * read an inode from the btree into the in-memory inode |
| 2207 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2208 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2209 | { |
| 2210 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2211 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2212 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2213 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2214 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2215 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2216 | int maybe_acls; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2217 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2218 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2219 | int ret; |
| 2220 | |
| 2221 | path = btrfs_alloc_path(); |
| 2222 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2223 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2224 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2225 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2226 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2227 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2228 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2229 | leaf = path->nodes[0]; |
| 2230 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2231 | struct btrfs_inode_item); |
| 2232 | |
| 2233 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2234 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2235 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2236 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2237 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2238 | |
| 2239 | tspec = btrfs_inode_atime(inode_item); |
| 2240 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2241 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2242 | |
| 2243 | tspec = btrfs_inode_mtime(inode_item); |
| 2244 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2245 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2246 | |
| 2247 | tspec = btrfs_inode_ctime(inode_item); |
| 2248 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2249 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2250 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2251 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2252 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2253 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2254 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2255 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2256 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2257 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2258 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2259 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2260 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2261 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2262 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2263 | /* |
| 2264 | * try to precache a NULL acl entry for files that don't have |
| 2265 | * any xattrs or acls |
| 2266 | */ |
| 2267 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 2268 | if (!maybe_acls) |
| 2269 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2270 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2271 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2272 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2273 | btrfs_free_path(path); |
| 2274 | inode_item = NULL; |
| 2275 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2276 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2277 | case S_IFREG: |
| 2278 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2279 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2280 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2281 | inode->i_fop = &btrfs_file_operations; |
| 2282 | inode->i_op = &btrfs_file_inode_operations; |
| 2283 | break; |
| 2284 | case S_IFDIR: |
| 2285 | inode->i_fop = &btrfs_dir_file_operations; |
| 2286 | if (root == root->fs_info->tree_root) |
| 2287 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2288 | else |
| 2289 | inode->i_op = &btrfs_dir_inode_operations; |
| 2290 | break; |
| 2291 | case S_IFLNK: |
| 2292 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2293 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2294 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2295 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2296 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2297 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2298 | init_special_inode(inode, inode->i_mode, rdev); |
| 2299 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2300 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2301 | |
| 2302 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2303 | return; |
| 2304 | |
| 2305 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2306 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2307 | make_bad_inode(inode); |
| 2308 | } |
| 2309 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2310 | /* |
| 2311 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2312 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2313 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2314 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2315 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2316 | struct inode *inode) |
| 2317 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2318 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2319 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2320 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2321 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2322 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2323 | |
| 2324 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2325 | inode->i_atime.tv_sec); |
| 2326 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2327 | inode->i_atime.tv_nsec); |
| 2328 | |
| 2329 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2330 | inode->i_mtime.tv_sec); |
| 2331 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2332 | inode->i_mtime.tv_nsec); |
| 2333 | |
| 2334 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2335 | inode->i_ctime.tv_sec); |
| 2336 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2337 | inode->i_ctime.tv_nsec); |
| 2338 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2339 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2340 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2341 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2342 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2343 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2344 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2345 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2346 | } |
| 2347 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2348 | /* |
| 2349 | * copy everything in the in-memory inode into the btree. |
| 2350 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2351 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2352 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2353 | { |
| 2354 | struct btrfs_inode_item *inode_item; |
| 2355 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2356 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2357 | int ret; |
| 2358 | |
| 2359 | path = btrfs_alloc_path(); |
| 2360 | BUG_ON(!path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2361 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2362 | ret = btrfs_lookup_inode(trans, root, path, |
| 2363 | &BTRFS_I(inode)->location, 1); |
| 2364 | if (ret) { |
| 2365 | if (ret > 0) |
| 2366 | ret = -ENOENT; |
| 2367 | goto failed; |
| 2368 | } |
| 2369 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2370 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2371 | leaf = path->nodes[0]; |
| 2372 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2373 | struct btrfs_inode_item); |
| 2374 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2375 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2376 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2377 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2378 | ret = 0; |
| 2379 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2380 | btrfs_free_path(path); |
| 2381 | return ret; |
| 2382 | } |
| 2383 | |
| 2384 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2385 | /* |
| 2386 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2387 | * recovery code. It remove a link in a directory with a given name, and |
| 2388 | * also drops the back refs in the inode to the directory |
| 2389 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2390 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2391 | struct btrfs_root *root, |
| 2392 | struct inode *dir, struct inode *inode, |
| 2393 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2394 | { |
| 2395 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2396 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2397 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2398 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2399 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2400 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2401 | |
| 2402 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2403 | if (!path) { |
| 2404 | ret = -ENOMEM; |
| 2405 | goto err; |
| 2406 | } |
| 2407 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2408 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2409 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2410 | name, name_len, -1); |
| 2411 | if (IS_ERR(di)) { |
| 2412 | ret = PTR_ERR(di); |
| 2413 | goto err; |
| 2414 | } |
| 2415 | if (!di) { |
| 2416 | ret = -ENOENT; |
| 2417 | goto err; |
| 2418 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2419 | leaf = path->nodes[0]; |
| 2420 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2421 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2422 | if (ret) |
| 2423 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2424 | btrfs_release_path(root, path); |
| 2425 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2426 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2427 | inode->i_ino, |
| 2428 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2429 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2430 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2431 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2432 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2433 | goto err; |
| 2434 | } |
| 2435 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2436 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2437 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2438 | if (IS_ERR(di)) { |
| 2439 | ret = PTR_ERR(di); |
| 2440 | goto err; |
| 2441 | } |
| 2442 | if (!di) { |
| 2443 | ret = -ENOENT; |
| 2444 | goto err; |
| 2445 | } |
| 2446 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2447 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2448 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2449 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 2450 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2451 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2452 | |
| 2453 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2454 | dir, index); |
| 2455 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2456 | err: |
| 2457 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2458 | if (ret) |
| 2459 | goto out; |
| 2460 | |
| 2461 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2462 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2463 | btrfs_update_inode(trans, root, dir); |
| 2464 | btrfs_drop_nlink(inode); |
| 2465 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2466 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2467 | return ret; |
| 2468 | } |
| 2469 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2470 | /* helper to check if there is any shared block in the path */ |
| 2471 | static int check_path_shared(struct btrfs_root *root, |
| 2472 | struct btrfs_path *path) |
| 2473 | { |
| 2474 | struct extent_buffer *eb; |
| 2475 | int level; |
| 2476 | int ret; |
| 2477 | u64 refs; |
| 2478 | |
| 2479 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
| 2480 | if (!path->nodes[level]) |
| 2481 | break; |
| 2482 | eb = path->nodes[level]; |
| 2483 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2484 | continue; |
| 2485 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2486 | &refs, NULL); |
| 2487 | if (refs > 1) |
| 2488 | return 1; |
| 2489 | } |
| 2490 | return 0; |
| 2491 | } |
| 2492 | |
| 2493 | /* |
| 2494 | * helper to start transaction for unlink and rmdir. |
| 2495 | * |
| 2496 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2497 | * so in enospc case, we should make sure they will free space before |
| 2498 | * allowing them to use the global metadata reservation. |
| 2499 | */ |
| 2500 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2501 | struct dentry *dentry) |
| 2502 | { |
| 2503 | struct btrfs_trans_handle *trans; |
| 2504 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2505 | struct btrfs_path *path; |
| 2506 | struct btrfs_inode_ref *ref; |
| 2507 | struct btrfs_dir_item *di; |
| 2508 | struct inode *inode = dentry->d_inode; |
| 2509 | u64 index; |
| 2510 | int check_link = 1; |
| 2511 | int err = -ENOSPC; |
| 2512 | int ret; |
| 2513 | |
| 2514 | trans = btrfs_start_transaction(root, 10); |
| 2515 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 2516 | return trans; |
| 2517 | |
| 2518 | if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 2519 | return ERR_PTR(-ENOSPC); |
| 2520 | |
| 2521 | /* check if there is someone else holds reference */ |
| 2522 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 2523 | return ERR_PTR(-ENOSPC); |
| 2524 | |
| 2525 | if (atomic_read(&inode->i_count) > 2) |
| 2526 | return ERR_PTR(-ENOSPC); |
| 2527 | |
| 2528 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 2529 | return ERR_PTR(-ENOSPC); |
| 2530 | |
| 2531 | path = btrfs_alloc_path(); |
| 2532 | if (!path) { |
| 2533 | root->fs_info->enospc_unlink = 0; |
| 2534 | return ERR_PTR(-ENOMEM); |
| 2535 | } |
| 2536 | |
| 2537 | trans = btrfs_start_transaction(root, 0); |
| 2538 | if (IS_ERR(trans)) { |
| 2539 | btrfs_free_path(path); |
| 2540 | root->fs_info->enospc_unlink = 0; |
| 2541 | return trans; |
| 2542 | } |
| 2543 | |
| 2544 | path->skip_locking = 1; |
| 2545 | path->search_commit_root = 1; |
| 2546 | |
| 2547 | ret = btrfs_lookup_inode(trans, root, path, |
| 2548 | &BTRFS_I(dir)->location, 0); |
| 2549 | if (ret < 0) { |
| 2550 | err = ret; |
| 2551 | goto out; |
| 2552 | } |
| 2553 | if (ret == 0) { |
| 2554 | if (check_path_shared(root, path)) |
| 2555 | goto out; |
| 2556 | } else { |
| 2557 | check_link = 0; |
| 2558 | } |
| 2559 | btrfs_release_path(root, path); |
| 2560 | |
| 2561 | ret = btrfs_lookup_inode(trans, root, path, |
| 2562 | &BTRFS_I(inode)->location, 0); |
| 2563 | if (ret < 0) { |
| 2564 | err = ret; |
| 2565 | goto out; |
| 2566 | } |
| 2567 | if (ret == 0) { |
| 2568 | if (check_path_shared(root, path)) |
| 2569 | goto out; |
| 2570 | } else { |
| 2571 | check_link = 0; |
| 2572 | } |
| 2573 | btrfs_release_path(root, path); |
| 2574 | |
| 2575 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 2576 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 2577 | inode->i_ino, (u64)-1, 0); |
| 2578 | if (ret < 0) { |
| 2579 | err = ret; |
| 2580 | goto out; |
| 2581 | } |
| 2582 | BUG_ON(ret == 0); |
| 2583 | if (check_path_shared(root, path)) |
| 2584 | goto out; |
| 2585 | btrfs_release_path(root, path); |
| 2586 | } |
| 2587 | |
| 2588 | if (!check_link) { |
| 2589 | err = 0; |
| 2590 | goto out; |
| 2591 | } |
| 2592 | |
| 2593 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2594 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2595 | if (IS_ERR(di)) { |
| 2596 | err = PTR_ERR(di); |
| 2597 | goto out; |
| 2598 | } |
| 2599 | if (di) { |
| 2600 | if (check_path_shared(root, path)) |
| 2601 | goto out; |
| 2602 | } else { |
| 2603 | err = 0; |
| 2604 | goto out; |
| 2605 | } |
| 2606 | btrfs_release_path(root, path); |
| 2607 | |
| 2608 | ref = btrfs_lookup_inode_ref(trans, root, path, |
| 2609 | dentry->d_name.name, dentry->d_name.len, |
| 2610 | inode->i_ino, dir->i_ino, 0); |
| 2611 | if (IS_ERR(ref)) { |
| 2612 | err = PTR_ERR(ref); |
| 2613 | goto out; |
| 2614 | } |
| 2615 | BUG_ON(!ref); |
| 2616 | if (check_path_shared(root, path)) |
| 2617 | goto out; |
| 2618 | index = btrfs_inode_ref_index(path->nodes[0], ref); |
| 2619 | btrfs_release_path(root, path); |
| 2620 | |
| 2621 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index, |
| 2622 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2623 | if (IS_ERR(di)) { |
| 2624 | err = PTR_ERR(di); |
| 2625 | goto out; |
| 2626 | } |
| 2627 | BUG_ON(ret == -ENOENT); |
| 2628 | if (check_path_shared(root, path)) |
| 2629 | goto out; |
| 2630 | |
| 2631 | err = 0; |
| 2632 | out: |
| 2633 | btrfs_free_path(path); |
| 2634 | if (err) { |
| 2635 | btrfs_end_transaction(trans, root); |
| 2636 | root->fs_info->enospc_unlink = 0; |
| 2637 | return ERR_PTR(err); |
| 2638 | } |
| 2639 | |
| 2640 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 2641 | return trans; |
| 2642 | } |
| 2643 | |
| 2644 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 2645 | struct btrfs_root *root) |
| 2646 | { |
| 2647 | if (trans->block_rsv == &root->fs_info->global_block_rsv) { |
| 2648 | BUG_ON(!root->fs_info->enospc_unlink); |
| 2649 | root->fs_info->enospc_unlink = 0; |
| 2650 | } |
| 2651 | btrfs_end_transaction_throttle(trans, root); |
| 2652 | } |
| 2653 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2654 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2655 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2656 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2657 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2658 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2659 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2660 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2661 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2662 | trans = __unlink_start_trans(dir, dentry); |
| 2663 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2664 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2665 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2666 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2667 | |
| 2668 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2669 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2670 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2671 | dentry->d_name.name, dentry->d_name.len); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2672 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2673 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2674 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2675 | ret = btrfs_orphan_add(trans, inode); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2676 | BUG_ON(ret); |
| 2677 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2678 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2679 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2680 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2681 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2682 | return ret; |
| 2683 | } |
| 2684 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2685 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2686 | struct btrfs_root *root, |
| 2687 | struct inode *dir, u64 objectid, |
| 2688 | const char *name, int name_len) |
| 2689 | { |
| 2690 | struct btrfs_path *path; |
| 2691 | struct extent_buffer *leaf; |
| 2692 | struct btrfs_dir_item *di; |
| 2693 | struct btrfs_key key; |
| 2694 | u64 index; |
| 2695 | int ret; |
| 2696 | |
| 2697 | path = btrfs_alloc_path(); |
| 2698 | if (!path) |
| 2699 | return -ENOMEM; |
| 2700 | |
| 2701 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2702 | name, name_len, -1); |
| 2703 | BUG_ON(!di || IS_ERR(di)); |
| 2704 | |
| 2705 | leaf = path->nodes[0]; |
| 2706 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2707 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2708 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2709 | BUG_ON(ret); |
| 2710 | btrfs_release_path(root, path); |
| 2711 | |
| 2712 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2713 | objectid, root->root_key.objectid, |
| 2714 | dir->i_ino, &index, name, name_len); |
| 2715 | if (ret < 0) { |
| 2716 | BUG_ON(ret != -ENOENT); |
| 2717 | di = btrfs_search_dir_index_item(root, path, dir->i_ino, |
| 2718 | name, name_len); |
| 2719 | BUG_ON(!di || IS_ERR(di)); |
| 2720 | |
| 2721 | leaf = path->nodes[0]; |
| 2722 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2723 | btrfs_release_path(root, path); |
| 2724 | index = key.offset; |
| 2725 | } |
| 2726 | |
| 2727 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 2728 | index, name, name_len, -1); |
| 2729 | BUG_ON(!di || IS_ERR(di)); |
| 2730 | |
| 2731 | leaf = path->nodes[0]; |
| 2732 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2733 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2734 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2735 | BUG_ON(ret); |
| 2736 | btrfs_release_path(root, path); |
| 2737 | |
| 2738 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2739 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2740 | ret = btrfs_update_inode(trans, root, dir); |
| 2741 | BUG_ON(ret); |
| 2742 | dir->i_sb->s_dirt = 1; |
| 2743 | |
| 2744 | btrfs_free_path(path); |
| 2745 | return 0; |
| 2746 | } |
| 2747 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2748 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 2749 | { |
| 2750 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2751 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2752 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2753 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2754 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2755 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 2756 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2757 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 2758 | return -ENOTEMPTY; |
| 2759 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2760 | trans = __unlink_start_trans(dir, dentry); |
| 2761 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2762 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2763 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2764 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2765 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2766 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 2767 | err = btrfs_unlink_subvol(trans, root, dir, |
| 2768 | BTRFS_I(inode)->location.objectid, |
| 2769 | dentry->d_name.name, |
| 2770 | dentry->d_name.len); |
| 2771 | goto out; |
| 2772 | } |
| 2773 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2774 | err = btrfs_orphan_add(trans, inode); |
| 2775 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2776 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2777 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2778 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2779 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2780 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2781 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2782 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2783 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2784 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2785 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2786 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2787 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2788 | return err; |
| 2789 | } |
| 2790 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2791 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2792 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2793 | * when truncating bytes in a file, it is possible to avoid reading |
| 2794 | * the leaves that contain only checksum items. This can be the |
| 2795 | * majority of the IO required to delete a large file, but it must |
| 2796 | * be done carefully. |
| 2797 | * |
| 2798 | * The keys in the level just above the leaves are checked to make sure |
| 2799 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 2800 | * after the new size. |
| 2801 | * |
| 2802 | * Then the key for the next leaf is checked to make sure it also has |
| 2803 | * a checksum item for the same file. If it does, we know our target leaf |
| 2804 | * contains only checksum items, and it can be safely freed without reading |
| 2805 | * it. |
| 2806 | * |
| 2807 | * This is just an optimization targeted at large files. It may do |
| 2808 | * nothing. It will return 0 unless things went badly. |
| 2809 | */ |
| 2810 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 2811 | struct btrfs_root *root, |
| 2812 | struct btrfs_path *path, |
| 2813 | struct inode *inode, u64 new_size) |
| 2814 | { |
| 2815 | struct btrfs_key key; |
| 2816 | int ret; |
| 2817 | int nritems; |
| 2818 | struct btrfs_key found_key; |
| 2819 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2820 | struct btrfs_leaf_ref *ref; |
| 2821 | u64 leaf_gen; |
| 2822 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2823 | |
| 2824 | path->lowest_level = 1; |
| 2825 | key.objectid = inode->i_ino; |
| 2826 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 2827 | key.offset = new_size; |
| 2828 | again: |
| 2829 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2830 | if (ret < 0) |
| 2831 | goto out; |
| 2832 | |
| 2833 | if (path->nodes[1] == NULL) { |
| 2834 | ret = 0; |
| 2835 | goto out; |
| 2836 | } |
| 2837 | ret = 0; |
| 2838 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 2839 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 2840 | |
| 2841 | if (!nritems) |
| 2842 | goto out; |
| 2843 | |
| 2844 | if (path->slots[1] >= nritems) |
| 2845 | goto next_node; |
| 2846 | |
| 2847 | /* did we find a key greater than anything we want to delete? */ |
| 2848 | if (found_key.objectid > inode->i_ino || |
| 2849 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 2850 | goto out; |
| 2851 | |
| 2852 | /* we check the next key in the node to make sure the leave contains |
| 2853 | * only checksum items. This comparison doesn't work if our |
| 2854 | * leaf is the last one in the node |
| 2855 | */ |
| 2856 | if (path->slots[1] + 1 >= nritems) { |
| 2857 | next_node: |
| 2858 | /* search forward from the last key in the node, this |
| 2859 | * will bring us into the next node in the tree |
| 2860 | */ |
| 2861 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 2862 | |
| 2863 | /* unlikely, but we inc below, so check to be safe */ |
| 2864 | if (found_key.offset == (u64)-1) |
| 2865 | goto out; |
| 2866 | |
| 2867 | /* search_forward needs a path with locks held, do the |
| 2868 | * search again for the original key. It is possible |
| 2869 | * this will race with a balance and return a path that |
| 2870 | * we could modify, but this drop is just an optimization |
| 2871 | * and is allowed to miss some leaves. |
| 2872 | */ |
| 2873 | btrfs_release_path(root, path); |
| 2874 | found_key.offset++; |
| 2875 | |
| 2876 | /* setup a max key for search_forward */ |
| 2877 | other_key.offset = (u64)-1; |
| 2878 | other_key.type = key.type; |
| 2879 | other_key.objectid = key.objectid; |
| 2880 | |
| 2881 | path->keep_locks = 1; |
| 2882 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 2883 | path, 0, 0); |
| 2884 | path->keep_locks = 0; |
| 2885 | if (ret || found_key.objectid != key.objectid || |
| 2886 | found_key.type != key.type) { |
| 2887 | ret = 0; |
| 2888 | goto out; |
| 2889 | } |
| 2890 | |
| 2891 | key.offset = found_key.offset; |
| 2892 | btrfs_release_path(root, path); |
| 2893 | cond_resched(); |
| 2894 | goto again; |
| 2895 | } |
| 2896 | |
| 2897 | /* we know there's one more slot after us in the tree, |
| 2898 | * read that key so we can verify it is also a checksum item |
| 2899 | */ |
| 2900 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 2901 | |
| 2902 | if (found_key.objectid < inode->i_ino) |
| 2903 | goto next_key; |
| 2904 | |
| 2905 | if (found_key.type != key.type || found_key.offset < new_size) |
| 2906 | goto next_key; |
| 2907 | |
| 2908 | /* |
| 2909 | * if the key for the next leaf isn't a csum key from this objectid, |
| 2910 | * we can't be sure there aren't good items inside this leaf. |
| 2911 | * Bail out |
| 2912 | */ |
| 2913 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 2914 | goto out; |
| 2915 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2916 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 2917 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2918 | /* |
| 2919 | * it is safe to delete this leaf, it contains only |
| 2920 | * csum items from this inode at an offset >= new_size |
| 2921 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2922 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2923 | BUG_ON(ret); |
| 2924 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2925 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 2926 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 2927 | if (ref) { |
| 2928 | ref->root_gen = root->root_key.offset; |
| 2929 | ref->bytenr = leaf_start; |
| 2930 | ref->owner = 0; |
| 2931 | ref->generation = leaf_gen; |
| 2932 | ref->nritems = 0; |
| 2933 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2934 | btrfs_sort_leaf_ref(ref); |
| 2935 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2936 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 2937 | WARN_ON(ret); |
| 2938 | btrfs_free_leaf_ref(root, ref); |
| 2939 | } else { |
| 2940 | WARN_ON(1); |
| 2941 | } |
| 2942 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2943 | next_key: |
| 2944 | btrfs_release_path(root, path); |
| 2945 | |
| 2946 | if (other_key.objectid == inode->i_ino && |
| 2947 | other_key.type == key.type && other_key.offset > key.offset) { |
| 2948 | key.offset = other_key.offset; |
| 2949 | cond_resched(); |
| 2950 | goto again; |
| 2951 | } |
| 2952 | ret = 0; |
| 2953 | out: |
| 2954 | /* fixup any changes we've made to the path */ |
| 2955 | path->lowest_level = 0; |
| 2956 | path->keep_locks = 0; |
| 2957 | btrfs_release_path(root, path); |
| 2958 | return ret; |
| 2959 | } |
| 2960 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2961 | #endif |
| 2962 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2963 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2964 | * this can truncate away extent items, csum items and directory items. |
| 2965 | * 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] | 2966 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2967 | * |
| 2968 | * csum items that cross the new i_size are truncated to the new size |
| 2969 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2970 | * |
| 2971 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 2972 | * 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] | 2973 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2974 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 2975 | struct btrfs_root *root, |
| 2976 | struct inode *inode, |
| 2977 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2978 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2979 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2980 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2981 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2982 | struct btrfs_key key; |
| 2983 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2984 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2985 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2986 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2987 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2988 | u64 mask = root->sectorsize - 1; |
| 2989 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2990 | int found_extent; |
| 2991 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2992 | int pending_del_nr = 0; |
| 2993 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2994 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2995 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2996 | int ret; |
| 2997 | int err = 0; |
| 2998 | |
| 2999 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3000 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3001 | if (root->ref_cows) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3002 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3003 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3004 | path = btrfs_alloc_path(); |
| 3005 | BUG_ON(!path); |
Julia Lawall | 33c17ad | 2009-07-22 16:49:01 -0400 | [diff] [blame] | 3006 | path->reada = -1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3007 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3008 | key.objectid = inode->i_ino; |
| 3009 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3010 | key.type = (u8)-1; |
| 3011 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3012 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3013 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3014 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3015 | if (ret < 0) { |
| 3016 | err = ret; |
| 3017 | goto out; |
| 3018 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3019 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3020 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3021 | /* there are no items in the tree for us to truncate, we're |
| 3022 | * done |
| 3023 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3024 | if (path->slots[0] == 0) |
| 3025 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3026 | path->slots[0]--; |
| 3027 | } |
| 3028 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3029 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3030 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3031 | leaf = path->nodes[0]; |
| 3032 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3033 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3034 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3035 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3036 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3037 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3038 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3039 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3040 | break; |
| 3041 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3042 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3043 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3044 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3045 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3046 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3047 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 3048 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 3049 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 3050 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3051 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3052 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3053 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3054 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3055 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3056 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3057 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3058 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3059 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3060 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3061 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3062 | } else { |
| 3063 | if (item_end < new_size) |
| 3064 | break; |
| 3065 | if (found_key.offset >= new_size) |
| 3066 | del_item = 1; |
| 3067 | else |
| 3068 | del_item = 0; |
| 3069 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3070 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3071 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3072 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3073 | goto delete; |
| 3074 | |
| 3075 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3076 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3077 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3078 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3079 | u64 orig_num_bytes = |
| 3080 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3081 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3082 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3083 | extent_num_bytes = extent_num_bytes & |
| 3084 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3085 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3086 | extent_num_bytes); |
| 3087 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3088 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3089 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3090 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3091 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3092 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3093 | extent_num_bytes = |
| 3094 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3095 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3096 | extent_offset = found_key.offset - |
| 3097 | btrfs_file_extent_offset(leaf, fi); |
| 3098 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3099 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3100 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3101 | if (extent_start != 0) { |
| 3102 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3103 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3104 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3105 | } |
| 3106 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3107 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3108 | /* |
| 3109 | * we can't truncate inline items that have had |
| 3110 | * special encodings |
| 3111 | */ |
| 3112 | if (!del_item && |
| 3113 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3114 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3115 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3116 | u32 size = new_size - found_key.offset; |
| 3117 | |
| 3118 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3119 | inode_sub_bytes(inode, item_end + 1 - |
| 3120 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3121 | } |
| 3122 | size = |
| 3123 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3124 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3125 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3126 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3127 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3128 | inode_sub_bytes(inode, item_end + 1 - |
| 3129 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3130 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3131 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3132 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3133 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3134 | if (!pending_del_nr) { |
| 3135 | /* no pending yet, add ourselves */ |
| 3136 | pending_del_slot = path->slots[0]; |
| 3137 | pending_del_nr = 1; |
| 3138 | } else if (pending_del_nr && |
| 3139 | path->slots[0] + 1 == pending_del_slot) { |
| 3140 | /* hop on the pending chunk */ |
| 3141 | pending_del_nr++; |
| 3142 | pending_del_slot = path->slots[0]; |
| 3143 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3144 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3145 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3146 | } else { |
| 3147 | break; |
| 3148 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3149 | if (found_extent && root->ref_cows) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3150 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3151 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3152 | extent_num_bytes, 0, |
| 3153 | btrfs_header_owner(leaf), |
| 3154 | inode->i_ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3155 | BUG_ON(ret); |
| 3156 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3157 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3158 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3159 | break; |
| 3160 | |
| 3161 | if (path->slots[0] == 0 || |
| 3162 | path->slots[0] != pending_del_slot) { |
| 3163 | if (root->ref_cows) { |
| 3164 | err = -EAGAIN; |
| 3165 | goto out; |
| 3166 | } |
| 3167 | if (pending_del_nr) { |
| 3168 | ret = btrfs_del_items(trans, root, path, |
| 3169 | pending_del_slot, |
| 3170 | pending_del_nr); |
| 3171 | BUG_ON(ret); |
| 3172 | pending_del_nr = 0; |
| 3173 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3174 | btrfs_release_path(root, path); |
| 3175 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3176 | } else { |
| 3177 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3178 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3179 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3180 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3181 | if (pending_del_nr) { |
| 3182 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3183 | pending_del_nr); |
| 3184 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3185 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3186 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3187 | } |
| 3188 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3189 | /* |
| 3190 | * taken from block_truncate_page, but does cow as it zeros out |
| 3191 | * any bytes left in the last page in the file. |
| 3192 | */ |
| 3193 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3194 | { |
| 3195 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3196 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3197 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3198 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3199 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3200 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3201 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3202 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3203 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3204 | struct page *page; |
| 3205 | int ret = 0; |
| 3206 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3207 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3208 | |
| 3209 | if ((offset & (blocksize - 1)) == 0) |
| 3210 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 3211 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3212 | if (ret) |
| 3213 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3214 | |
| 3215 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3216 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3217 | page = grab_cache_page(mapping, index); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3218 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 3219 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3220 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3221 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3222 | |
| 3223 | page_start = page_offset(page); |
| 3224 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3225 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3226 | if (!PageUptodate(page)) { |
| 3227 | ret = btrfs_readpage(NULL, page); |
| 3228 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3229 | if (page->mapping != mapping) { |
| 3230 | unlock_page(page); |
| 3231 | page_cache_release(page); |
| 3232 | goto again; |
| 3233 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3234 | if (!PageUptodate(page)) { |
| 3235 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3236 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3237 | } |
| 3238 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3239 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3240 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3241 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 3242 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3243 | set_page_extent_mapped(page); |
| 3244 | |
| 3245 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3246 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3247 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3248 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3249 | unlock_page(page); |
| 3250 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3251 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3252 | btrfs_put_ordered_extent(ordered); |
| 3253 | goto again; |
| 3254 | } |
| 3255 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3256 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3257 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3258 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3259 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3260 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3261 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3262 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3263 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3264 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3265 | goto out_unlock; |
| 3266 | } |
| 3267 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3268 | ret = 0; |
| 3269 | if (offset != PAGE_CACHE_SIZE) { |
| 3270 | kaddr = kmap(page); |
| 3271 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3272 | flush_dcache_page(page); |
| 3273 | kunmap(page); |
| 3274 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3275 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3276 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3277 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3278 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3279 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3280 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3281 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 3282 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3283 | unlock_page(page); |
| 3284 | page_cache_release(page); |
| 3285 | out: |
| 3286 | return ret; |
| 3287 | } |
| 3288 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3289 | int btrfs_cont_expand(struct inode *inode, loff_t size) |
| 3290 | { |
| 3291 | struct btrfs_trans_handle *trans; |
| 3292 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3293 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3294 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3295 | struct extent_state *cached_state = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3296 | u64 mask = root->sectorsize - 1; |
| 3297 | u64 hole_start = (inode->i_size + mask) & ~mask; |
| 3298 | u64 block_end = (size + mask) & ~mask; |
| 3299 | u64 last_byte; |
| 3300 | u64 cur_offset; |
| 3301 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3302 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3303 | |
| 3304 | if (size <= hole_start) |
| 3305 | return 0; |
| 3306 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3307 | while (1) { |
| 3308 | struct btrfs_ordered_extent *ordered; |
| 3309 | btrfs_wait_ordered_range(inode, hole_start, |
| 3310 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3311 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
| 3312 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3313 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3314 | if (!ordered) |
| 3315 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3316 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3317 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3318 | btrfs_put_ordered_extent(ordered); |
| 3319 | } |
| 3320 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3321 | cur_offset = hole_start; |
| 3322 | while (1) { |
| 3323 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3324 | block_end - cur_offset, 0); |
| 3325 | BUG_ON(IS_ERR(em) || !em); |
| 3326 | last_byte = min(extent_map_end(em), block_end); |
| 3327 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3328 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3329 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3330 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3331 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3332 | trans = btrfs_start_transaction(root, 2); |
| 3333 | if (IS_ERR(trans)) { |
| 3334 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3335 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3336 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3337 | btrfs_set_trans_block_group(trans, inode); |
| 3338 | |
| 3339 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3340 | cur_offset + hole_size, |
| 3341 | &hint_byte, 1); |
| 3342 | BUG_ON(err); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3343 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3344 | err = btrfs_insert_file_extent(trans, root, |
| 3345 | inode->i_ino, cur_offset, 0, |
| 3346 | 0, hole_size, 0, hole_size, |
| 3347 | 0, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3348 | BUG_ON(err); |
| 3349 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3350 | btrfs_drop_extent_cache(inode, hole_start, |
| 3351 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3352 | |
| 3353 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3354 | } |
| 3355 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3356 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3357 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3358 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3359 | break; |
| 3360 | } |
| 3361 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3362 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3363 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3364 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3365 | return err; |
| 3366 | } |
| 3367 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3368 | static int btrfs_setattr_size(struct inode *inode, struct iattr *attr) |
| 3369 | { |
| 3370 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3371 | struct btrfs_trans_handle *trans; |
| 3372 | unsigned long nr; |
| 3373 | int ret; |
| 3374 | |
| 3375 | if (attr->ia_size == inode->i_size) |
| 3376 | return 0; |
| 3377 | |
| 3378 | if (attr->ia_size > inode->i_size) { |
| 3379 | unsigned long limit; |
| 3380 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
| 3381 | if (attr->ia_size > inode->i_sb->s_maxbytes) |
| 3382 | return -EFBIG; |
| 3383 | if (limit != RLIM_INFINITY && attr->ia_size > limit) { |
| 3384 | send_sig(SIGXFSZ, current, 0); |
| 3385 | return -EFBIG; |
| 3386 | } |
| 3387 | } |
| 3388 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3389 | trans = btrfs_start_transaction(root, 1); |
| 3390 | btrfs_set_trans_block_group(trans, inode); |
| 3391 | |
| 3392 | ret = btrfs_orphan_add(trans, inode); |
| 3393 | BUG_ON(ret); |
| 3394 | |
| 3395 | nr = trans->blocks_used; |
| 3396 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3397 | btrfs_btree_balance_dirty(root, nr); |
| 3398 | |
| 3399 | if (attr->ia_size > inode->i_size) { |
| 3400 | ret = btrfs_cont_expand(inode, attr->ia_size); |
| 3401 | if (ret) { |
| 3402 | btrfs_truncate(inode); |
| 3403 | return ret; |
| 3404 | } |
| 3405 | |
| 3406 | i_size_write(inode, attr->ia_size); |
| 3407 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
| 3408 | |
| 3409 | trans = btrfs_start_transaction(root, 1); |
| 3410 | btrfs_set_trans_block_group(trans, inode); |
| 3411 | |
| 3412 | ret = btrfs_update_inode(trans, root, inode); |
| 3413 | BUG_ON(ret); |
| 3414 | if (inode->i_nlink > 0) { |
| 3415 | ret = btrfs_orphan_del(trans, inode); |
| 3416 | BUG_ON(ret); |
| 3417 | } |
| 3418 | nr = trans->blocks_used; |
| 3419 | btrfs_end_transaction(trans, root); |
| 3420 | btrfs_btree_balance_dirty(root, nr); |
| 3421 | return 0; |
| 3422 | } |
| 3423 | |
| 3424 | /* |
| 3425 | * We're truncating a file that used to have good data down to |
| 3426 | * zero. Make sure it gets into the ordered flush list so that |
| 3427 | * any new writes get down to disk quickly. |
| 3428 | */ |
| 3429 | if (attr->ia_size == 0) |
| 3430 | BTRFS_I(inode)->ordered_data_close = 1; |
| 3431 | |
| 3432 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3433 | ret = vmtruncate(inode, attr->ia_size); |
| 3434 | BUG_ON(ret); |
| 3435 | |
| 3436 | return 0; |
| 3437 | } |
| 3438 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3439 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3440 | { |
| 3441 | struct inode *inode = dentry->d_inode; |
| 3442 | int err; |
| 3443 | |
| 3444 | err = inode_change_ok(inode, attr); |
| 3445 | if (err) |
| 3446 | return err; |
| 3447 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3448 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3449 | err = btrfs_setattr_size(inode, attr); |
| 3450 | if (err) |
| 3451 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3452 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3453 | attr->ia_valid &= ~ATTR_SIZE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3454 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3455 | if (attr->ia_valid) |
| 3456 | err = inode_setattr(inode, attr); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3457 | |
| 3458 | if (!err && ((attr->ia_valid & ATTR_MODE))) |
| 3459 | err = btrfs_acl_chmod(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3460 | return err; |
| 3461 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3462 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3463 | void btrfs_delete_inode(struct inode *inode) |
| 3464 | { |
| 3465 | struct btrfs_trans_handle *trans; |
| 3466 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3467 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3468 | int ret; |
| 3469 | |
| 3470 | truncate_inode_pages(&inode->i_data, 0); |
| 3471 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3472 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3473 | goto no_delete; |
| 3474 | } |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3475 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3476 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3477 | if (root->fs_info->log_root_recovering) { |
| 3478 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3479 | goto no_delete; |
| 3480 | } |
| 3481 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3482 | if (inode->i_nlink > 0) { |
| 3483 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3484 | goto no_delete; |
| 3485 | } |
| 3486 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3487 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3488 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3489 | while (1) { |
| 3490 | trans = btrfs_start_transaction(root, 1); |
| 3491 | btrfs_set_trans_block_group(trans, inode); |
| 3492 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
| 3493 | |
| 3494 | if (ret != -EAGAIN) |
| 3495 | break; |
| 3496 | |
| 3497 | nr = trans->blocks_used; |
| 3498 | btrfs_end_transaction(trans, root); |
| 3499 | trans = NULL; |
| 3500 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3501 | } |
| 3502 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3503 | if (ret == 0) { |
| 3504 | ret = btrfs_orphan_del(trans, inode); |
| 3505 | BUG_ON(ret); |
| 3506 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3507 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3508 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3509 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3510 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3511 | no_delete: |
| 3512 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3513 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | /* |
| 3517 | * this returns the key found in the dir entry in the location pointer. |
| 3518 | * If no dir entries were found, location->objectid is 0. |
| 3519 | */ |
| 3520 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3521 | struct btrfs_key *location) |
| 3522 | { |
| 3523 | const char *name = dentry->d_name.name; |
| 3524 | int namelen = dentry->d_name.len; |
| 3525 | struct btrfs_dir_item *di; |
| 3526 | struct btrfs_path *path; |
| 3527 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3528 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3529 | |
| 3530 | path = btrfs_alloc_path(); |
| 3531 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3532 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3533 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 3534 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3535 | if (IS_ERR(di)) |
| 3536 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3537 | |
| 3538 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3539 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3540 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3541 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3542 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3543 | btrfs_free_path(path); |
| 3544 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3545 | out_err: |
| 3546 | location->objectid = 0; |
| 3547 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | /* |
| 3551 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3552 | * needs to be changed to reflect the root directory of the tree root. This |
| 3553 | * is kind of like crossing a mount point. |
| 3554 | */ |
| 3555 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3556 | struct inode *dir, |
| 3557 | struct dentry *dentry, |
| 3558 | struct btrfs_key *location, |
| 3559 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3560 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3561 | struct btrfs_path *path; |
| 3562 | struct btrfs_root *new_root; |
| 3563 | struct btrfs_root_ref *ref; |
| 3564 | struct extent_buffer *leaf; |
| 3565 | int ret; |
| 3566 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3567 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3568 | path = btrfs_alloc_path(); |
| 3569 | if (!path) { |
| 3570 | err = -ENOMEM; |
| 3571 | goto out; |
| 3572 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3573 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3574 | err = -ENOENT; |
| 3575 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3576 | BTRFS_I(dir)->root->root_key.objectid, |
| 3577 | location->objectid); |
| 3578 | if (ret) { |
| 3579 | if (ret < 0) |
| 3580 | err = ret; |
| 3581 | goto out; |
| 3582 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3583 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3584 | leaf = path->nodes[0]; |
| 3585 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 3586 | if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || |
| 3587 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3588 | goto out; |
| 3589 | |
| 3590 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3591 | (unsigned long)(ref + 1), |
| 3592 | dentry->d_name.len); |
| 3593 | if (ret) |
| 3594 | goto out; |
| 3595 | |
| 3596 | btrfs_release_path(root->fs_info->tree_root, path); |
| 3597 | |
| 3598 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3599 | if (IS_ERR(new_root)) { |
| 3600 | err = PTR_ERR(new_root); |
| 3601 | goto out; |
| 3602 | } |
| 3603 | |
| 3604 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3605 | err = -ENOENT; |
| 3606 | goto out; |
| 3607 | } |
| 3608 | |
| 3609 | *sub_root = new_root; |
| 3610 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3611 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3612 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3613 | err = 0; |
| 3614 | out: |
| 3615 | btrfs_free_path(path); |
| 3616 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3617 | } |
| 3618 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3619 | static void inode_tree_add(struct inode *inode) |
| 3620 | { |
| 3621 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3622 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3623 | struct rb_node **p; |
| 3624 | struct rb_node *parent; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3625 | again: |
| 3626 | p = &root->inode_tree.rb_node; |
| 3627 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3628 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3629 | if (hlist_unhashed(&inode->i_hash)) |
| 3630 | return; |
| 3631 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3632 | spin_lock(&root->inode_lock); |
| 3633 | while (*p) { |
| 3634 | parent = *p; |
| 3635 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3636 | |
| 3637 | if (inode->i_ino < entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3638 | p = &parent->rb_left; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3639 | else if (inode->i_ino > entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3640 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3641 | else { |
| 3642 | WARN_ON(!(entry->vfs_inode.i_state & |
| 3643 | (I_WILL_FREE | I_FREEING | I_CLEAR))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3644 | rb_erase(parent, &root->inode_tree); |
| 3645 | RB_CLEAR_NODE(parent); |
| 3646 | spin_unlock(&root->inode_lock); |
| 3647 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3648 | } |
| 3649 | } |
| 3650 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3651 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3652 | spin_unlock(&root->inode_lock); |
| 3653 | } |
| 3654 | |
| 3655 | static void inode_tree_del(struct inode *inode) |
| 3656 | { |
| 3657 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3658 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3659 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3660 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3661 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3662 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3663 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3664 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3665 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3666 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3667 | |
| 3668 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
| 3669 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3670 | spin_lock(&root->inode_lock); |
| 3671 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3672 | spin_unlock(&root->inode_lock); |
| 3673 | if (empty) |
| 3674 | btrfs_add_dead_root(root); |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 3679 | { |
| 3680 | struct rb_node *node; |
| 3681 | struct rb_node *prev; |
| 3682 | struct btrfs_inode *entry; |
| 3683 | struct inode *inode; |
| 3684 | u64 objectid = 0; |
| 3685 | |
| 3686 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3687 | |
| 3688 | spin_lock(&root->inode_lock); |
| 3689 | again: |
| 3690 | node = root->inode_tree.rb_node; |
| 3691 | prev = NULL; |
| 3692 | while (node) { |
| 3693 | prev = node; |
| 3694 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3695 | |
| 3696 | if (objectid < entry->vfs_inode.i_ino) |
| 3697 | node = node->rb_left; |
| 3698 | else if (objectid > entry->vfs_inode.i_ino) |
| 3699 | node = node->rb_right; |
| 3700 | else |
| 3701 | break; |
| 3702 | } |
| 3703 | if (!node) { |
| 3704 | while (prev) { |
| 3705 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
| 3706 | if (objectid <= entry->vfs_inode.i_ino) { |
| 3707 | node = prev; |
| 3708 | break; |
| 3709 | } |
| 3710 | prev = rb_next(prev); |
| 3711 | } |
| 3712 | } |
| 3713 | while (node) { |
| 3714 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3715 | objectid = entry->vfs_inode.i_ino + 1; |
| 3716 | inode = igrab(&entry->vfs_inode); |
| 3717 | if (inode) { |
| 3718 | spin_unlock(&root->inode_lock); |
| 3719 | if (atomic_read(&inode->i_count) > 1) |
| 3720 | d_prune_aliases(inode); |
| 3721 | /* |
| 3722 | * btrfs_drop_inode will remove it from |
| 3723 | * the inode cache when its usage count |
| 3724 | * hits zero. |
| 3725 | */ |
| 3726 | iput(inode); |
| 3727 | cond_resched(); |
| 3728 | spin_lock(&root->inode_lock); |
| 3729 | goto again; |
| 3730 | } |
| 3731 | |
| 3732 | if (cond_resched_lock(&root->inode_lock)) |
| 3733 | goto again; |
| 3734 | |
| 3735 | node = rb_next(node); |
| 3736 | } |
| 3737 | spin_unlock(&root->inode_lock); |
| 3738 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3739 | } |
| 3740 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3741 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 3742 | { |
| 3743 | struct btrfs_iget_args *args = p; |
| 3744 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3745 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3746 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3747 | return 0; |
| 3748 | } |
| 3749 | |
| 3750 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 3751 | { |
| 3752 | struct btrfs_iget_args *args = opaque; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3753 | return args->ino == inode->i_ino && |
| 3754 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3755 | } |
| 3756 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3757 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 3758 | u64 objectid, |
| 3759 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3760 | { |
| 3761 | struct inode *inode; |
| 3762 | struct btrfs_iget_args args; |
| 3763 | args.ino = objectid; |
| 3764 | args.root = root; |
| 3765 | |
| 3766 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 3767 | btrfs_init_locked_inode, |
| 3768 | (void *)&args); |
| 3769 | return inode; |
| 3770 | } |
| 3771 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3772 | /* Get an inode object given its location and corresponding root. |
| 3773 | * Returns in *is_new if the inode was read from disk |
| 3774 | */ |
| 3775 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3776 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3777 | { |
| 3778 | struct inode *inode; |
| 3779 | |
| 3780 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 3781 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3782 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3783 | |
| 3784 | if (inode->i_state & I_NEW) { |
| 3785 | BTRFS_I(inode)->root = root; |
| 3786 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 3787 | btrfs_read_locked_inode(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3788 | |
| 3789 | inode_tree_add(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3790 | unlock_new_inode(inode); |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3791 | if (new) |
| 3792 | *new = 1; |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | return inode; |
| 3796 | } |
| 3797 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3798 | static struct inode *new_simple_dir(struct super_block *s, |
| 3799 | struct btrfs_key *key, |
| 3800 | struct btrfs_root *root) |
| 3801 | { |
| 3802 | struct inode *inode = new_inode(s); |
| 3803 | |
| 3804 | if (!inode) |
| 3805 | return ERR_PTR(-ENOMEM); |
| 3806 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3807 | BTRFS_I(inode)->root = root; |
| 3808 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 3809 | BTRFS_I(inode)->dummy_inode = 1; |
| 3810 | |
| 3811 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 3812 | inode->i_op = &simple_dir_inode_operations; |
| 3813 | inode->i_fop = &simple_dir_operations; |
| 3814 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 3815 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 3816 | |
| 3817 | return inode; |
| 3818 | } |
| 3819 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3820 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3821 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3822 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3823 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3824 | struct btrfs_root *sub_root = root; |
| 3825 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3826 | int index; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3827 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3828 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3829 | dentry->d_op = &btrfs_dentry_operations; |
| 3830 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3831 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 3832 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3833 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3834 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3835 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3836 | if (ret < 0) |
| 3837 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3838 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3839 | if (location.objectid == 0) |
| 3840 | return NULL; |
| 3841 | |
| 3842 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3843 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3844 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3845 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3846 | |
| 3847 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 3848 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3849 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3850 | ret = fixup_tree_root_location(root, dir, dentry, |
| 3851 | &location, &sub_root); |
| 3852 | if (ret < 0) { |
| 3853 | if (ret != -ENOENT) |
| 3854 | inode = ERR_PTR(ret); |
| 3855 | else |
| 3856 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 3857 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3858 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3859 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3860 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 3861 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3862 | if (root != sub_root) { |
| 3863 | down_read(&root->fs_info->cleanup_work_sem); |
| 3864 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
| 3865 | btrfs_orphan_cleanup(sub_root); |
| 3866 | up_read(&root->fs_info->cleanup_work_sem); |
| 3867 | } |
| 3868 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3869 | return inode; |
| 3870 | } |
| 3871 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3872 | static int btrfs_dentry_delete(struct dentry *dentry) |
| 3873 | { |
| 3874 | struct btrfs_root *root; |
| 3875 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 3876 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 3877 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3878 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 3879 | if (dentry->d_inode) { |
| 3880 | root = BTRFS_I(dentry->d_inode)->root; |
| 3881 | if (btrfs_root_refs(&root->root_item) == 0) |
| 3882 | return 1; |
| 3883 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3884 | return 0; |
| 3885 | } |
| 3886 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3887 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 3888 | struct nameidata *nd) |
| 3889 | { |
| 3890 | struct inode *inode; |
| 3891 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3892 | inode = btrfs_lookup_dentry(dir, dentry); |
| 3893 | if (IS_ERR(inode)) |
| 3894 | return ERR_CAST(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3895 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3896 | return d_splice_alias(inode, dentry); |
| 3897 | } |
| 3898 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3899 | static unsigned char btrfs_filetype_table[] = { |
| 3900 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 3901 | }; |
| 3902 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 3903 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 3904 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3905 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 3906 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3907 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3908 | struct btrfs_item *item; |
| 3909 | struct btrfs_dir_item *di; |
| 3910 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3911 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3912 | struct btrfs_path *path; |
| 3913 | int ret; |
| 3914 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3915 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3916 | int slot; |
| 3917 | int advance; |
| 3918 | unsigned char d_type; |
| 3919 | int over = 0; |
| 3920 | u32 di_cur; |
| 3921 | u32 di_total; |
| 3922 | u32 di_len; |
| 3923 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3924 | char tmp_name[32]; |
| 3925 | char *name_ptr; |
| 3926 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3927 | |
| 3928 | /* FIXME, use a real flag for deciding about the key type */ |
| 3929 | if (root->fs_info->tree_root == root) |
| 3930 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3931 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3932 | /* special case for "." */ |
| 3933 | if (filp->f_pos == 0) { |
| 3934 | over = filldir(dirent, ".", 1, |
| 3935 | 1, inode->i_ino, |
| 3936 | DT_DIR); |
| 3937 | if (over) |
| 3938 | return 0; |
| 3939 | filp->f_pos = 1; |
| 3940 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3941 | /* special case for .., just use the back ref */ |
| 3942 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3943 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3944 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3945 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3946 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3947 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3948 | filp->f_pos = 2; |
| 3949 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3950 | path = btrfs_alloc_path(); |
| 3951 | path->reada = 2; |
| 3952 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3953 | btrfs_set_key_type(&key, key_type); |
| 3954 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3955 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3956 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3957 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3958 | if (ret < 0) |
| 3959 | goto err; |
| 3960 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3961 | |
| 3962 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3963 | leaf = path->nodes[0]; |
| 3964 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3965 | slot = path->slots[0]; |
| 3966 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3967 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3968 | ret = btrfs_next_leaf(root, path); |
| 3969 | if (ret) |
| 3970 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3971 | leaf = path->nodes[0]; |
| 3972 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3973 | slot = path->slots[0]; |
| 3974 | } else { |
| 3975 | slot++; |
| 3976 | path->slots[0]++; |
| 3977 | } |
| 3978 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3979 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3980 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3981 | item = btrfs_item_nr(leaf, slot); |
| 3982 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3983 | |
| 3984 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3985 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3986 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3987 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3988 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3989 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3990 | |
| 3991 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3992 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3993 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 3994 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3995 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3996 | |
| 3997 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3998 | struct btrfs_key location; |
| 3999 | |
| 4000 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4001 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4002 | name_ptr = tmp_name; |
| 4003 | } else { |
| 4004 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4005 | if (!name_ptr) { |
| 4006 | ret = -ENOMEM; |
| 4007 | goto err; |
| 4008 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4009 | } |
| 4010 | read_extent_buffer(leaf, name_ptr, |
| 4011 | (unsigned long)(di + 1), name_len); |
| 4012 | |
| 4013 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4014 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4015 | |
| 4016 | /* is this a reference to our own snapshot? If so |
| 4017 | * skip it |
| 4018 | */ |
| 4019 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4020 | location.objectid == root->root_key.objectid) { |
| 4021 | over = 0; |
| 4022 | goto skip; |
| 4023 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4024 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4025 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4026 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4027 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4028 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4029 | if (name_ptr != tmp_name) |
| 4030 | kfree(name_ptr); |
| 4031 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4032 | if (over) |
| 4033 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4034 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4035 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4036 | di_cur += di_len; |
| 4037 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4038 | } |
| 4039 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4040 | |
| 4041 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4042 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4043 | /* |
| 4044 | * 32-bit glibc will use getdents64, but then strtol - |
| 4045 | * so the last number we can serve is this. |
| 4046 | */ |
| 4047 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4048 | else |
| 4049 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4050 | nopos: |
| 4051 | ret = 0; |
| 4052 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4053 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4054 | return ret; |
| 4055 | } |
| 4056 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4057 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4058 | { |
| 4059 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4060 | struct btrfs_trans_handle *trans; |
| 4061 | int ret = 0; |
| 4062 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 4063 | if (root->fs_info->btree_inode == inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4064 | return 0; |
| 4065 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4066 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4067 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4068 | btrfs_set_trans_block_group(trans, inode); |
| 4069 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4070 | } |
| 4071 | return ret; |
| 4072 | } |
| 4073 | |
| 4074 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4075 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4076 | * inode changes. But, it is most likely to find the inode in cache. |
| 4077 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4078 | * to keep or drop this code. |
| 4079 | */ |
| 4080 | void btrfs_dirty_inode(struct inode *inode) |
| 4081 | { |
| 4082 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4083 | struct btrfs_trans_handle *trans; |
| 4084 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4085 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4086 | btrfs_set_trans_block_group(trans, inode); |
| 4087 | btrfs_update_inode(trans, root, inode); |
| 4088 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4089 | } |
| 4090 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4091 | /* |
| 4092 | * find the highest existing sequence number in a directory |
| 4093 | * and then set the in-memory index_cnt variable to reflect |
| 4094 | * free sequence numbers |
| 4095 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4096 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4097 | { |
| 4098 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4099 | struct btrfs_key key, found_key; |
| 4100 | struct btrfs_path *path; |
| 4101 | struct extent_buffer *leaf; |
| 4102 | int ret; |
| 4103 | |
| 4104 | key.objectid = inode->i_ino; |
| 4105 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4106 | key.offset = (u64)-1; |
| 4107 | |
| 4108 | path = btrfs_alloc_path(); |
| 4109 | if (!path) |
| 4110 | return -ENOMEM; |
| 4111 | |
| 4112 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4113 | if (ret < 0) |
| 4114 | goto out; |
| 4115 | /* FIXME: we should be able to handle this */ |
| 4116 | if (ret == 0) |
| 4117 | goto out; |
| 4118 | ret = 0; |
| 4119 | |
| 4120 | /* |
| 4121 | * MAGIC NUMBER EXPLANATION: |
| 4122 | * since we search a directory based on f_pos we have to start at 2 |
| 4123 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4124 | * else has to start at 2 |
| 4125 | */ |
| 4126 | if (path->slots[0] == 0) { |
| 4127 | BTRFS_I(inode)->index_cnt = 2; |
| 4128 | goto out; |
| 4129 | } |
| 4130 | |
| 4131 | path->slots[0]--; |
| 4132 | |
| 4133 | leaf = path->nodes[0]; |
| 4134 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4135 | |
| 4136 | if (found_key.objectid != inode->i_ino || |
| 4137 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4138 | BTRFS_I(inode)->index_cnt = 2; |
| 4139 | goto out; |
| 4140 | } |
| 4141 | |
| 4142 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4143 | out: |
| 4144 | btrfs_free_path(path); |
| 4145 | return ret; |
| 4146 | } |
| 4147 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4148 | /* |
| 4149 | * helper to find a free sequence number in a given directory. This current |
| 4150 | * code is very simple, later versions will do smarter things in the btree |
| 4151 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4152 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4153 | { |
| 4154 | int ret = 0; |
| 4155 | |
| 4156 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 4157 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4158 | if (ret) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4159 | return ret; |
| 4160 | } |
| 4161 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4162 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4163 | BTRFS_I(dir)->index_cnt++; |
| 4164 | |
| 4165 | return ret; |
| 4166 | } |
| 4167 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4168 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4169 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4170 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4171 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4172 | u64 ref_objectid, u64 objectid, |
| 4173 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4174 | { |
| 4175 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4176 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4177 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4178 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4179 | struct btrfs_inode_ref *ref; |
| 4180 | struct btrfs_key key[2]; |
| 4181 | u32 sizes[2]; |
| 4182 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4183 | int ret; |
| 4184 | int owner; |
| 4185 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4186 | path = btrfs_alloc_path(); |
| 4187 | BUG_ON(!path); |
| 4188 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4189 | inode = new_inode(root->fs_info->sb); |
| 4190 | if (!inode) |
| 4191 | return ERR_PTR(-ENOMEM); |
| 4192 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4193 | if (dir) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4194 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4195 | if (ret) { |
| 4196 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4197 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4198 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4199 | } |
| 4200 | /* |
| 4201 | * index_cnt is ignored for everything but a dir, |
| 4202 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4203 | * number |
| 4204 | */ |
| 4205 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4206 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4207 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4208 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4209 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4210 | if (mode & S_IFDIR) |
| 4211 | owner = 0; |
| 4212 | else |
| 4213 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4214 | BTRFS_I(inode)->block_group = |
| 4215 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4216 | |
| 4217 | key[0].objectid = objectid; |
| 4218 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4219 | key[0].offset = 0; |
| 4220 | |
| 4221 | key[1].objectid = objectid; |
| 4222 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4223 | key[1].offset = ref_objectid; |
| 4224 | |
| 4225 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4226 | sizes[1] = name_len + sizeof(*ref); |
| 4227 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4228 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4229 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4230 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4231 | goto fail; |
| 4232 | |
Chris Mason | 79683f2 | 2008-11-19 22:00:53 -0500 | [diff] [blame] | 4233 | inode->i_uid = current_fsuid(); |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 4234 | |
Chris Mason | 42f15d7 | 2009-02-06 11:35:57 -0500 | [diff] [blame] | 4235 | if (dir && (dir->i_mode & S_ISGID)) { |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 4236 | inode->i_gid = dir->i_gid; |
| 4237 | if (S_ISDIR(mode)) |
| 4238 | mode |= S_ISGID; |
| 4239 | } else |
| 4240 | inode->i_gid = current_fsgid(); |
| 4241 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4242 | inode->i_mode = mode; |
| 4243 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4244 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4245 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4246 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4247 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4248 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4249 | |
| 4250 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4251 | struct btrfs_inode_ref); |
| 4252 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4253 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4254 | ptr = (unsigned long)(ref + 1); |
| 4255 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4256 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4257 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4258 | btrfs_free_path(path); |
| 4259 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4260 | location = &BTRFS_I(inode)->location; |
| 4261 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4262 | location->offset = 0; |
| 4263 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4264 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4265 | btrfs_inherit_iflags(inode, dir); |
| 4266 | |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4267 | if ((mode & S_IFREG)) { |
| 4268 | if (btrfs_test_opt(root, NODATASUM)) |
| 4269 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 4270 | if (btrfs_test_opt(root, NODATACOW)) |
| 4271 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4272 | } |
| 4273 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4274 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4275 | inode_tree_add(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4276 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4277 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4278 | if (dir) |
| 4279 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4280 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4281 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4282 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4286 | { |
| 4287 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4288 | } |
| 4289 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4290 | /* |
| 4291 | * utility function to add 'inode' into 'parent_inode' with |
| 4292 | * a give name and a given sequence number. |
| 4293 | * if 'add_backref' is true, also insert a backref from the |
| 4294 | * inode to the parent directory. |
| 4295 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4296 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4297 | struct inode *parent_inode, struct inode *inode, |
| 4298 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4299 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4300 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4301 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4302 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4303 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4304 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4305 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4306 | } else { |
| 4307 | key.objectid = inode->i_ino; |
| 4308 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4309 | key.offset = 0; |
| 4310 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4311 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4312 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4313 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4314 | key.objectid, root->root_key.objectid, |
| 4315 | parent_inode->i_ino, |
| 4316 | index, name, name_len); |
| 4317 | } else if (add_backref) { |
| 4318 | ret = btrfs_insert_inode_ref(trans, root, |
| 4319 | name, name_len, inode->i_ino, |
| 4320 | parent_inode->i_ino, index); |
| 4321 | } |
| 4322 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4323 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4324 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4325 | parent_inode->i_ino, &key, |
| 4326 | btrfs_inode_type(inode), index); |
| 4327 | BUG_ON(ret); |
| 4328 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4329 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4330 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4331 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4332 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4333 | } |
| 4334 | return ret; |
| 4335 | } |
| 4336 | |
| 4337 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4338 | struct dentry *dentry, struct inode *inode, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4339 | int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4340 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4341 | int err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4342 | inode, dentry->d_name.name, |
| 4343 | dentry->d_name.len, backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4344 | if (!err) { |
| 4345 | d_instantiate(dentry, inode); |
| 4346 | return 0; |
| 4347 | } |
| 4348 | if (err > 0) |
| 4349 | err = -EEXIST; |
| 4350 | return err; |
| 4351 | } |
| 4352 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4353 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4354 | int mode, dev_t rdev) |
| 4355 | { |
| 4356 | struct btrfs_trans_handle *trans; |
| 4357 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4358 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4359 | int err; |
| 4360 | int drop_inode = 0; |
| 4361 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4362 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4363 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4364 | |
| 4365 | if (!new_valid_dev(rdev)) |
| 4366 | return -EINVAL; |
| 4367 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4368 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4369 | if (err) |
| 4370 | return err; |
| 4371 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4372 | /* |
| 4373 | * 2 for inode item and ref |
| 4374 | * 2 for dir items |
| 4375 | * 1 for xattr if selinux is on |
| 4376 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4377 | trans = btrfs_start_transaction(root, 5); |
| 4378 | if (IS_ERR(trans)) |
| 4379 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4380 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4381 | btrfs_set_trans_block_group(trans, dir); |
| 4382 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4383 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4384 | dentry->d_name.len, |
| 4385 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4386 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4387 | err = PTR_ERR(inode); |
| 4388 | if (IS_ERR(inode)) |
| 4389 | goto out_unlock; |
| 4390 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4391 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4392 | if (err) { |
| 4393 | drop_inode = 1; |
| 4394 | goto out_unlock; |
| 4395 | } |
| 4396 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4397 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4398 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4399 | if (err) |
| 4400 | drop_inode = 1; |
| 4401 | else { |
| 4402 | inode->i_op = &btrfs_special_inode_operations; |
| 4403 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4404 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4405 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4406 | btrfs_update_inode_block_group(trans, inode); |
| 4407 | btrfs_update_inode_block_group(trans, dir); |
| 4408 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4409 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4410 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4411 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4412 | if (drop_inode) { |
| 4413 | inode_dec_link_count(inode); |
| 4414 | iput(inode); |
| 4415 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4416 | return err; |
| 4417 | } |
| 4418 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4419 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4420 | int mode, struct nameidata *nd) |
| 4421 | { |
| 4422 | struct btrfs_trans_handle *trans; |
| 4423 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4424 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4425 | int drop_inode = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4426 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4427 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4428 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4429 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4430 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4431 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4432 | if (err) |
| 4433 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4434 | /* |
| 4435 | * 2 for inode item and ref |
| 4436 | * 2 for dir items |
| 4437 | * 1 for xattr if selinux is on |
| 4438 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4439 | trans = btrfs_start_transaction(root, 5); |
| 4440 | if (IS_ERR(trans)) |
| 4441 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4442 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4443 | btrfs_set_trans_block_group(trans, dir); |
| 4444 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4445 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4446 | dentry->d_name.len, |
| 4447 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4448 | objectid, BTRFS_I(dir)->block_group, mode, |
| 4449 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4450 | err = PTR_ERR(inode); |
| 4451 | if (IS_ERR(inode)) |
| 4452 | goto out_unlock; |
| 4453 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4454 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4455 | if (err) { |
| 4456 | drop_inode = 1; |
| 4457 | goto out_unlock; |
| 4458 | } |
| 4459 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4460 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4461 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4462 | if (err) |
| 4463 | drop_inode = 1; |
| 4464 | else { |
| 4465 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4466 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4467 | inode->i_fop = &btrfs_file_operations; |
| 4468 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4469 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4470 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4471 | btrfs_update_inode_block_group(trans, inode); |
| 4472 | btrfs_update_inode_block_group(trans, dir); |
| 4473 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4474 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4475 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4476 | if (drop_inode) { |
| 4477 | inode_dec_link_count(inode); |
| 4478 | iput(inode); |
| 4479 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4480 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4481 | return err; |
| 4482 | } |
| 4483 | |
| 4484 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4485 | struct dentry *dentry) |
| 4486 | { |
| 4487 | struct btrfs_trans_handle *trans; |
| 4488 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4489 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4490 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4491 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4492 | int err; |
| 4493 | int drop_inode = 0; |
| 4494 | |
| 4495 | if (inode->i_nlink == 0) |
| 4496 | return -ENOENT; |
| 4497 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4498 | /* do not allow sys_link's with other subvols of the same device */ |
| 4499 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
| 4500 | return -EPERM; |
| 4501 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4502 | btrfs_inc_nlink(inode); |
| 4503 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4504 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4505 | if (err) |
| 4506 | goto fail; |
| 4507 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4508 | /* |
| 4509 | * 1 item for inode ref |
| 4510 | * 2 items for dir items |
| 4511 | */ |
| 4512 | trans = btrfs_start_transaction(root, 3); |
| 4513 | if (IS_ERR(trans)) { |
| 4514 | err = PTR_ERR(trans); |
| 4515 | goto fail; |
| 4516 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4517 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4518 | btrfs_set_trans_block_group(trans, dir); |
| 4519 | atomic_inc(&inode->i_count); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4520 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4521 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4522 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4523 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4524 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4525 | } else { |
| 4526 | btrfs_update_inode_block_group(trans, dir); |
| 4527 | err = btrfs_update_inode(trans, root, inode); |
| 4528 | BUG_ON(err); |
| 4529 | btrfs_log_new_name(trans, inode, NULL, dentry->d_parent); |
| 4530 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4531 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4532 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4533 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4534 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4535 | if (drop_inode) { |
| 4536 | inode_dec_link_count(inode); |
| 4537 | iput(inode); |
| 4538 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4539 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4540 | return err; |
| 4541 | } |
| 4542 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4543 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4544 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4545 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4546 | struct btrfs_trans_handle *trans; |
| 4547 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4548 | int err = 0; |
| 4549 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4550 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4551 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4552 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4553 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4554 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4555 | if (err) |
| 4556 | return err; |
| 4557 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4558 | /* |
| 4559 | * 2 items for inode and ref |
| 4560 | * 2 items for dir items |
| 4561 | * 1 for xattr if selinux is on |
| 4562 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4563 | trans = btrfs_start_transaction(root, 5); |
| 4564 | if (IS_ERR(trans)) |
| 4565 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4566 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4567 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4568 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4569 | dentry->d_name.len, |
| 4570 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4571 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 4572 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4573 | if (IS_ERR(inode)) { |
| 4574 | err = PTR_ERR(inode); |
| 4575 | goto out_fail; |
| 4576 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4577 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4578 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4579 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4580 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4581 | if (err) |
| 4582 | goto out_fail; |
| 4583 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4584 | inode->i_op = &btrfs_dir_inode_operations; |
| 4585 | inode->i_fop = &btrfs_dir_file_operations; |
| 4586 | btrfs_set_trans_block_group(trans, inode); |
| 4587 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4588 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4589 | err = btrfs_update_inode(trans, root, inode); |
| 4590 | if (err) |
| 4591 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4592 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4593 | err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4594 | inode, dentry->d_name.name, |
| 4595 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4596 | if (err) |
| 4597 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4598 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4599 | d_instantiate(dentry, inode); |
| 4600 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4601 | btrfs_update_inode_block_group(trans, inode); |
| 4602 | btrfs_update_inode_block_group(trans, dir); |
| 4603 | |
| 4604 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4605 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4606 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4607 | if (drop_on_err) |
| 4608 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4609 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4610 | return err; |
| 4611 | } |
| 4612 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4613 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4614 | * and an extent that you want to insert, deal with overlap and insert |
| 4615 | * the new extent into the tree. |
| 4616 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4617 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4618 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4619 | struct extent_map *em, |
| 4620 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4621 | { |
| 4622 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4623 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4624 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4625 | start_diff = map_start - em->start; |
| 4626 | em->start = map_start; |
| 4627 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4628 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4629 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4630 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4631 | em->block_len -= start_diff; |
| 4632 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4633 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4634 | } |
| 4635 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4636 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4637 | struct inode *inode, struct page *page, |
| 4638 | size_t pg_offset, u64 extent_offset, |
| 4639 | struct btrfs_file_extent_item *item) |
| 4640 | { |
| 4641 | int ret; |
| 4642 | struct extent_buffer *leaf = path->nodes[0]; |
| 4643 | char *tmp; |
| 4644 | size_t max_size; |
| 4645 | unsigned long inline_size; |
| 4646 | unsigned long ptr; |
| 4647 | |
| 4648 | WARN_ON(pg_offset != 0); |
| 4649 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4650 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4651 | btrfs_item_nr(leaf, path->slots[0])); |
| 4652 | tmp = kmalloc(inline_size, GFP_NOFS); |
| 4653 | ptr = btrfs_file_extent_inline_start(item); |
| 4654 | |
| 4655 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4656 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 4657 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4658 | ret = btrfs_zlib_decompress(tmp, page, extent_offset, |
| 4659 | inline_size, max_size); |
| 4660 | if (ret) { |
| 4661 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 4662 | unsigned long copy_size = min_t(u64, |
| 4663 | PAGE_CACHE_SIZE - pg_offset, |
| 4664 | max_size - extent_offset); |
| 4665 | memset(kaddr + pg_offset, 0, copy_size); |
| 4666 | kunmap_atomic(kaddr, KM_USER0); |
| 4667 | } |
| 4668 | kfree(tmp); |
| 4669 | return 0; |
| 4670 | } |
| 4671 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4672 | /* |
| 4673 | * 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] | 4674 | * the ugly parts come from merging extents from the disk with the in-ram |
| 4675 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4676 | * where the in-ram extents might be locked pending data=ordered completion. |
| 4677 | * |
| 4678 | * This also copies inline extents directly into the page. |
| 4679 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4680 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4681 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4682 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4683 | int create) |
| 4684 | { |
| 4685 | int ret; |
| 4686 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4687 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4688 | u64 extent_start = 0; |
| 4689 | u64 extent_end = 0; |
| 4690 | u64 objectid = inode->i_ino; |
| 4691 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4692 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4693 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4694 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4695 | struct extent_buffer *leaf; |
| 4696 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4697 | struct extent_map *em = NULL; |
| 4698 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4699 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4700 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4701 | int compressed; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4702 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4703 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4704 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4705 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4706 | if (em) |
| 4707 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4708 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4709 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4710 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4711 | if (em->start > start || em->start + em->len <= start) |
| 4712 | free_extent_map(em); |
| 4713 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4714 | free_extent_map(em); |
| 4715 | else |
| 4716 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4717 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4718 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4719 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4720 | err = -ENOMEM; |
| 4721 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4722 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4723 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4724 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 4725 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4726 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4727 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4728 | |
| 4729 | if (!path) { |
| 4730 | path = btrfs_alloc_path(); |
| 4731 | BUG_ON(!path); |
| 4732 | } |
| 4733 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4734 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 4735 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4736 | if (ret < 0) { |
| 4737 | err = ret; |
| 4738 | goto out; |
| 4739 | } |
| 4740 | |
| 4741 | if (ret != 0) { |
| 4742 | if (path->slots[0] == 0) |
| 4743 | goto not_found; |
| 4744 | path->slots[0]--; |
| 4745 | } |
| 4746 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4747 | leaf = path->nodes[0]; |
| 4748 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4749 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4750 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4751 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4752 | found_type = btrfs_key_type(&found_key); |
| 4753 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4754 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 4755 | goto not_found; |
| 4756 | } |
| 4757 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4758 | found_type = btrfs_file_extent_type(leaf, item); |
| 4759 | extent_start = found_key.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4760 | compressed = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4761 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4762 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4763 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4764 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4765 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 4766 | size_t size; |
| 4767 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4768 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 4769 | ~((u64)root->sectorsize - 1); |
| 4770 | } |
| 4771 | |
| 4772 | if (start >= extent_end) { |
| 4773 | path->slots[0]++; |
| 4774 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 4775 | ret = btrfs_next_leaf(root, path); |
| 4776 | if (ret < 0) { |
| 4777 | err = ret; |
| 4778 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4779 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4780 | if (ret > 0) |
| 4781 | goto not_found; |
| 4782 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4783 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4784 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4785 | if (found_key.objectid != objectid || |
| 4786 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 4787 | goto not_found; |
| 4788 | if (start + len <= found_key.offset) |
| 4789 | goto not_found; |
| 4790 | em->start = start; |
| 4791 | em->len = found_key.offset - start; |
| 4792 | goto not_found_em; |
| 4793 | } |
| 4794 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4795 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4796 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4797 | em->start = extent_start; |
| 4798 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4799 | em->orig_start = extent_start - |
| 4800 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4801 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 4802 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4803 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4804 | goto insert; |
| 4805 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4806 | if (compressed) { |
| 4807 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 4808 | em->block_start = bytenr; |
| 4809 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 4810 | item); |
| 4811 | } else { |
| 4812 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 4813 | em->block_start = bytenr; |
| 4814 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4815 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 4816 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4817 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4818 | goto insert; |
| 4819 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4820 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4821 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4822 | size_t size; |
| 4823 | size_t extent_offset; |
| 4824 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4825 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4826 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4827 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4828 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4829 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4830 | goto out; |
| 4831 | } |
| 4832 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4833 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4834 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4835 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4836 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4837 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4838 | em->len = (copy_size + root->sectorsize - 1) & |
| 4839 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4840 | em->orig_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4841 | if (compressed) |
| 4842 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4843 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4844 | if (create == 0 && !PageUptodate(page)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4845 | if (btrfs_file_extent_compression(leaf, item) == |
| 4846 | BTRFS_COMPRESS_ZLIB) { |
| 4847 | ret = uncompress_inline(path, inode, page, |
| 4848 | pg_offset, |
| 4849 | extent_offset, item); |
| 4850 | BUG_ON(ret); |
| 4851 | } else { |
| 4852 | map = kmap(page); |
| 4853 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 4854 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 4855 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 4856 | memset(map + pg_offset + copy_size, 0, |
| 4857 | PAGE_CACHE_SIZE - pg_offset - |
| 4858 | copy_size); |
| 4859 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4860 | kunmap(page); |
| 4861 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4862 | flush_dcache_page(page); |
| 4863 | } else if (create && PageUptodate(page)) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 4864 | WARN_ON(1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4865 | if (!trans) { |
| 4866 | kunmap(page); |
| 4867 | free_extent_map(em); |
| 4868 | em = NULL; |
| 4869 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4870 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4871 | goto again; |
| 4872 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4873 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4874 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4875 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4876 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4877 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4878 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4879 | set_extent_uptodate(io_tree, em->start, |
| 4880 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4881 | goto insert; |
| 4882 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4883 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4884 | WARN_ON(1); |
| 4885 | } |
| 4886 | not_found: |
| 4887 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4888 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4889 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4890 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4891 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4892 | insert: |
| 4893 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4894 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4895 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 4896 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 4897 | (unsigned long long)em->len, |
| 4898 | (unsigned long long)start, |
| 4899 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4900 | err = -EIO; |
| 4901 | goto out; |
| 4902 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4903 | |
| 4904 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4905 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4906 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4907 | /* it is possible that someone inserted the extent into the tree |
| 4908 | * while we had the lock dropped. It is also possible that |
| 4909 | * an overlapping map exists in the tree |
| 4910 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4911 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4912 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4913 | |
| 4914 | ret = 0; |
| 4915 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4916 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4917 | if (existing && (existing->start > start || |
| 4918 | existing->start + existing->len <= start)) { |
| 4919 | free_extent_map(existing); |
| 4920 | existing = NULL; |
| 4921 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4922 | if (!existing) { |
| 4923 | existing = lookup_extent_mapping(em_tree, em->start, |
| 4924 | em->len); |
| 4925 | if (existing) { |
| 4926 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4927 | em, start, |
| 4928 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4929 | free_extent_map(existing); |
| 4930 | if (err) { |
| 4931 | free_extent_map(em); |
| 4932 | em = NULL; |
| 4933 | } |
| 4934 | } else { |
| 4935 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4936 | free_extent_map(em); |
| 4937 | em = NULL; |
| 4938 | } |
| 4939 | } else { |
| 4940 | free_extent_map(em); |
| 4941 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4942 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4943 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4944 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4945 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4946 | out: |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4947 | if (path) |
| 4948 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4949 | if (trans) { |
| 4950 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4951 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4952 | err = ret; |
| 4953 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4954 | if (err) { |
| 4955 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4956 | return ERR_PTR(err); |
| 4957 | } |
| 4958 | return em; |
| 4959 | } |
| 4960 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4961 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 4962 | const struct iovec *iov, loff_t offset, |
| 4963 | unsigned long nr_segs) |
| 4964 | { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4965 | return -EINVAL; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4966 | } |
| 4967 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4968 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4969 | __u64 start, __u64 len) |
| 4970 | { |
| 4971 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent); |
| 4972 | } |
| 4973 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4974 | int btrfs_readpage(struct file *file, struct page *page) |
| 4975 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4976 | struct extent_io_tree *tree; |
| 4977 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4978 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4979 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4980 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4981 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 4982 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4983 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4984 | |
| 4985 | |
| 4986 | if (current->flags & PF_MEMALLOC) { |
| 4987 | redirty_page_for_writepage(wbc, page); |
| 4988 | unlock_page(page); |
| 4989 | return 0; |
| 4990 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4991 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4992 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 4993 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4994 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4995 | int btrfs_writepages(struct address_space *mapping, |
| 4996 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 4997 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4998 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4999 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5000 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 5001 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 5002 | } |
| 5003 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 5004 | static int |
| 5005 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 5006 | struct list_head *pages, unsigned nr_pages) |
| 5007 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5008 | struct extent_io_tree *tree; |
| 5009 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 5010 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 5011 | btrfs_get_extent); |
| 5012 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5013 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5014 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5015 | struct extent_io_tree *tree; |
| 5016 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5017 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5018 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5019 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 5020 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5021 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5022 | if (ret == 1) { |
| 5023 | ClearPagePrivate(page); |
| 5024 | set_page_private(page, 0); |
| 5025 | page_cache_release(page); |
| 5026 | } |
| 5027 | return ret; |
| 5028 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5029 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5030 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 5031 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 5032 | if (PageWriteback(page) || PageDirty(page)) |
| 5033 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 5034 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5035 | } |
| 5036 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5037 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 5038 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5039 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5040 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5041 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5042 | u64 page_start = page_offset(page); |
| 5043 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5044 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 5045 | |
| 5046 | /* |
| 5047 | * we have the page locked, so new writeback can't start, |
| 5048 | * and the dirty bit won't be cleared while we are here. |
| 5049 | * |
| 5050 | * Wait for IO on this page so that we can safely clear |
| 5051 | * the PagePrivate2 bit and do ordered accounting |
| 5052 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5053 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 5054 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5055 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5056 | if (offset) { |
| 5057 | btrfs_releasepage(page, GFP_NOFS); |
| 5058 | return; |
| 5059 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5060 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 5061 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5062 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 5063 | page_offset(page)); |
| 5064 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5065 | /* |
| 5066 | * IO on this page will never be started, so we need |
| 5067 | * to account for any ordered extents now |
| 5068 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5069 | clear_extent_bit(tree, page_start, page_end, |
| 5070 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5071 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5072 | &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 5073 | /* |
| 5074 | * whoever cleared the private bit is responsible |
| 5075 | * for the finish_ordered_io |
| 5076 | */ |
| 5077 | if (TestClearPagePrivate2(page)) { |
| 5078 | btrfs_finish_ordered_io(page->mapping->host, |
| 5079 | page_start, page_end); |
| 5080 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5081 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5082 | cached_state = NULL; |
| 5083 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 5084 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5085 | } |
| 5086 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5087 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5088 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5089 | __btrfs_releasepage(page, GFP_NOFS); |
| 5090 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 5091 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 5092 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 5093 | ClearPagePrivate(page); |
| 5094 | set_page_private(page, 0); |
| 5095 | page_cache_release(page); |
| 5096 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5097 | } |
| 5098 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5099 | /* |
| 5100 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 5101 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 5102 | * be careful to check for EOF conditions here. We set the page up correctly |
| 5103 | * for a written page which means we get ENOSPC checking when writing into |
| 5104 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 5105 | * support these features. |
| 5106 | * |
| 5107 | * We are not allowed to take the i_mutex here so we have to play games to |
| 5108 | * protect against truncate races as the page could now be beyond EOF. Because |
| 5109 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 5110 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 5111 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 5112 | * unlock the page. |
| 5113 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5114 | 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] | 5115 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5116 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 5117 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5118 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5119 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5120 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5121 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5122 | char *kaddr; |
| 5123 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5124 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5125 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5126 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5127 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5128 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 5129 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5130 | if (ret) { |
| 5131 | if (ret == -ENOMEM) |
| 5132 | ret = VM_FAULT_OOM; |
| 5133 | else /* -ENOSPC, -EIO, etc */ |
| 5134 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5135 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5136 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5137 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5138 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5139 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5140 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5141 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5142 | page_start = page_offset(page); |
| 5143 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5144 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5145 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5146 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5147 | /* page got truncated out from underneath us */ |
| 5148 | goto out_unlock; |
| 5149 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5150 | wait_on_page_writeback(page); |
| 5151 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5152 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 5153 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5154 | set_page_extent_mapped(page); |
| 5155 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5156 | /* |
| 5157 | * we can't set the delalloc bits if there are pending ordered |
| 5158 | * extents. Drop our locks and wait for them to finish |
| 5159 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5160 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 5161 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5162 | unlock_extent_cached(io_tree, page_start, page_end, |
| 5163 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5164 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5165 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5166 | btrfs_put_ordered_extent(ordered); |
| 5167 | goto again; |
| 5168 | } |
| 5169 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 5170 | /* |
| 5171 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 5172 | * if it was already dirty, so for space accounting reasons we need to |
| 5173 | * clear any delalloc bits for the range we are fixing to save. There |
| 5174 | * is probably a better way to do this, but for now keep consistent with |
| 5175 | * prepare_pages in the normal write path. |
| 5176 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5177 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5178 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5179 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 5180 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5181 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 5182 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5183 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5184 | unlock_extent_cached(io_tree, page_start, page_end, |
| 5185 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5186 | ret = VM_FAULT_SIGBUS; |
| 5187 | goto out_unlock; |
| 5188 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5189 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5190 | |
| 5191 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5192 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5193 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5194 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5195 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5196 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5197 | if (zero_start != PAGE_CACHE_SIZE) { |
| 5198 | kaddr = kmap(page); |
| 5199 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 5200 | flush_dcache_page(page); |
| 5201 | kunmap(page); |
| 5202 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 5203 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5204 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 5205 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5206 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5207 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 5208 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 5209 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5210 | 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] | 5211 | |
| 5212 | out_unlock: |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 5213 | if (!ret) |
| 5214 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5215 | unlock_page(page); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 5216 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5217 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5218 | return ret; |
| 5219 | } |
| 5220 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5221 | static void btrfs_truncate(struct inode *inode) |
| 5222 | { |
| 5223 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5224 | int ret; |
| 5225 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5226 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5227 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5228 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5229 | if (!S_ISREG(inode->i_mode)) { |
| 5230 | WARN_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5231 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5232 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5233 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 5234 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 5235 | if (ret) |
| 5236 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5237 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 5238 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5239 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5240 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5241 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5242 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5243 | |
| 5244 | /* |
| 5245 | * setattr is responsible for setting the ordered_data_close flag, |
| 5246 | * but that is only tested during the last file release. That |
| 5247 | * could happen well after the next commit, leaving a great big |
| 5248 | * window where new writes may get lost if someone chooses to write |
| 5249 | * to this file after truncating to zero |
| 5250 | * |
| 5251 | * The inode doesn't have any dirty data here, and so if we commit |
| 5252 | * this is a noop. If someone immediately starts writing to the inode |
| 5253 | * it is very likely we'll catch some of their writes in this |
| 5254 | * transaction, and the commit will find this file on the ordered |
| 5255 | * data list with good things to send down. |
| 5256 | * |
| 5257 | * This is a best effort solution, there is still a window where |
| 5258 | * using truncate to replace the contents of the file will |
| 5259 | * end up with a zero length file after a crash. |
| 5260 | */ |
| 5261 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 5262 | btrfs_add_ordered_operation(trans, root, inode); |
| 5263 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5264 | while (1) { |
| 5265 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 5266 | inode->i_size, |
| 5267 | BTRFS_EXTENT_DATA_KEY); |
| 5268 | if (ret != -EAGAIN) |
| 5269 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5270 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5271 | ret = btrfs_update_inode(trans, root, inode); |
| 5272 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5273 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5274 | nr = trans->blocks_used; |
| 5275 | btrfs_end_transaction(trans, root); |
| 5276 | btrfs_btree_balance_dirty(root, nr); |
| 5277 | |
| 5278 | trans = btrfs_start_transaction(root, 1); |
| 5279 | btrfs_set_trans_block_group(trans, inode); |
| 5280 | } |
| 5281 | |
| 5282 | if (ret == 0 && inode->i_nlink > 0) { |
| 5283 | ret = btrfs_orphan_del(trans, inode); |
| 5284 | BUG_ON(ret); |
| 5285 | } |
| 5286 | |
| 5287 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5288 | BUG_ON(ret); |
| 5289 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5290 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5291 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5292 | BUG_ON(ret); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5293 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5294 | } |
| 5295 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 5296 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5297 | * create a new subvolume directory/inode (helper for the ioctl). |
| 5298 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5299 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5300 | struct btrfs_root *new_root, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5301 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5302 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5303 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5304 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5305 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5306 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5307 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5308 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5309 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5310 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5311 | inode->i_op = &btrfs_dir_inode_operations; |
| 5312 | inode->i_fop = &btrfs_dir_file_operations; |
| 5313 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5314 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5315 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 5316 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5317 | err = btrfs_update_inode(trans, new_root, inode); |
| 5318 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 5319 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5320 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 5321 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5322 | } |
| 5323 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5324 | /* helper function for file defrag and space balancing. This |
| 5325 | * forces readahead on a given range of bytes in an inode |
| 5326 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5327 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5328 | struct file_ra_state *ra, struct file *file, |
| 5329 | pgoff_t offset, pgoff_t last_index) |
| 5330 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5331 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5332 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5333 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 5334 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5335 | } |
| 5336 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5337 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 5338 | { |
| 5339 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5340 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5341 | |
| 5342 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 5343 | if (!ei) |
| 5344 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5345 | |
| 5346 | ei->root = NULL; |
| 5347 | ei->space_info = NULL; |
| 5348 | ei->generation = 0; |
| 5349 | ei->sequence = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 5350 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5351 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5352 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5353 | ei->delalloc_bytes = 0; |
| 5354 | ei->reserved_bytes = 0; |
| 5355 | ei->disk_i_size = 0; |
| 5356 | ei->flags = 0; |
| 5357 | ei->index_cnt = (u64)-1; |
| 5358 | ei->last_unlink_trans = 0; |
| 5359 | |
| 5360 | spin_lock_init(&ei->accounting_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 5361 | atomic_set(&ei->outstanding_extents, 0); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5362 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5363 | |
| 5364 | ei->ordered_data_close = 0; |
| 5365 | ei->dummy_inode = 0; |
| 5366 | ei->force_compress = 0; |
| 5367 | |
| 5368 | inode = &ei->vfs_inode; |
| 5369 | extent_map_tree_init(&ei->extent_tree, GFP_NOFS); |
| 5370 | extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS); |
| 5371 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS); |
| 5372 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5373 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5374 | INIT_LIST_HEAD(&ei->i_orphan); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5375 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5376 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5377 | RB_CLEAR_NODE(&ei->rb_node); |
| 5378 | |
| 5379 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | void btrfs_destroy_inode(struct inode *inode) |
| 5383 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5384 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5385 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5386 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5387 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 5388 | WARN_ON(inode->i_data.nrpages); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 5389 | WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents)); |
| 5390 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5391 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5392 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 5393 | * This can happen where we create an inode, but somebody else also |
| 5394 | * created the same inode and we need to destroy the one we already |
| 5395 | * created. |
| 5396 | */ |
| 5397 | if (!root) |
| 5398 | goto free; |
| 5399 | |
| 5400 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5401 | * Make sure we're properly removed from the ordered operation |
| 5402 | * lists. |
| 5403 | */ |
| 5404 | smp_mb(); |
| 5405 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 5406 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 5407 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 5408 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 5409 | } |
| 5410 | |
| 5411 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5412 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5413 | printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", |
| 5414 | inode->i_ino); |
| 5415 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5416 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5417 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5418 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5419 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5420 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 5421 | if (!ordered) |
| 5422 | break; |
| 5423 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5424 | printk(KERN_ERR "btrfs found ordered " |
| 5425 | "extent %llu %llu on inode cleanup\n", |
| 5426 | (unsigned long long)ordered->file_offset, |
| 5427 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5428 | btrfs_remove_ordered_extent(inode, ordered); |
| 5429 | btrfs_put_ordered_extent(ordered); |
| 5430 | btrfs_put_ordered_extent(ordered); |
| 5431 | } |
| 5432 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5433 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5434 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 5435 | free: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5436 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 5437 | } |
| 5438 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5439 | void btrfs_drop_inode(struct inode *inode) |
| 5440 | { |
| 5441 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5442 | if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0) |
| 5443 | generic_delete_inode(inode); |
| 5444 | else |
| 5445 | generic_drop_inode(inode); |
| 5446 | } |
| 5447 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5448 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5449 | { |
| 5450 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 5451 | |
| 5452 | inode_init_once(&ei->vfs_inode); |
| 5453 | } |
| 5454 | |
| 5455 | void btrfs_destroy_cachep(void) |
| 5456 | { |
| 5457 | if (btrfs_inode_cachep) |
| 5458 | kmem_cache_destroy(btrfs_inode_cachep); |
| 5459 | if (btrfs_trans_handle_cachep) |
| 5460 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 5461 | if (btrfs_transaction_cachep) |
| 5462 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5463 | if (btrfs_path_cachep) |
| 5464 | kmem_cache_destroy(btrfs_path_cachep); |
| 5465 | } |
| 5466 | |
| 5467 | int btrfs_init_cachep(void) |
| 5468 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5469 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 5470 | sizeof(struct btrfs_inode), 0, |
| 5471 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5472 | if (!btrfs_inode_cachep) |
| 5473 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5474 | |
| 5475 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 5476 | sizeof(struct btrfs_trans_handle), 0, |
| 5477 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5478 | if (!btrfs_trans_handle_cachep) |
| 5479 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5480 | |
| 5481 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 5482 | sizeof(struct btrfs_transaction), 0, |
| 5483 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5484 | if (!btrfs_transaction_cachep) |
| 5485 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5486 | |
| 5487 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 5488 | sizeof(struct btrfs_path), 0, |
| 5489 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5490 | if (!btrfs_path_cachep) |
| 5491 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5492 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5493 | return 0; |
| 5494 | fail: |
| 5495 | btrfs_destroy_cachep(); |
| 5496 | return -ENOMEM; |
| 5497 | } |
| 5498 | |
| 5499 | static int btrfs_getattr(struct vfsmount *mnt, |
| 5500 | struct dentry *dentry, struct kstat *stat) |
| 5501 | { |
| 5502 | struct inode *inode = dentry->d_inode; |
| 5503 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5504 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 5505 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5506 | stat->blocks = (inode_get_bytes(inode) + |
| 5507 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5508 | return 0; |
| 5509 | } |
| 5510 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5511 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 5512 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5513 | { |
| 5514 | struct btrfs_trans_handle *trans; |
| 5515 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5516 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5517 | struct inode *new_inode = new_dentry->d_inode; |
| 5518 | struct inode *old_inode = old_dentry->d_inode; |
| 5519 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5520 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5521 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5522 | int ret; |
| 5523 | |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5524 | if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5525 | return -EPERM; |
| 5526 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5527 | /* we only allow rename subvolume link between subvolumes */ |
| 5528 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5529 | return -EXDEV; |
| 5530 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5531 | if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 5532 | (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) |
| 5533 | return -ENOTEMPTY; |
| 5534 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5535 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5536 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5537 | return -ENOTEMPTY; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5538 | /* |
| 5539 | * we're using rename to replace one file with another. |
| 5540 | * and the replacement file is large. Start IO on it now so |
| 5541 | * we don't add too much work to the end of the transaction |
| 5542 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 5543 | 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] | 5544 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 5545 | filemap_flush(old_inode->i_mapping); |
| 5546 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5547 | /* close the racy window with snapshot create/destroy ioctl */ |
| 5548 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 5549 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5550 | /* |
| 5551 | * We want to reserve the absolute worst case amount of items. So if |
| 5552 | * both inodes are subvols and we need to unlink them then that would |
| 5553 | * require 4 item modifications, but if they are both normal inodes it |
| 5554 | * would require 5 item modifications, so we'll assume their normal |
| 5555 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 5556 | * should cover the worst case number of items we'll modify. |
| 5557 | */ |
| 5558 | trans = btrfs_start_transaction(root, 20); |
| 5559 | if (IS_ERR(trans)) |
| 5560 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5561 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5562 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5563 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5564 | if (dest != root) |
| 5565 | btrfs_record_root_in_trans(trans, dest); |
| 5566 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5567 | ret = btrfs_set_inode_index(new_dir, &index); |
| 5568 | if (ret) |
| 5569 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5570 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5571 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5572 | /* force full log commit if subvolume involved. */ |
| 5573 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 5574 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5575 | ret = btrfs_insert_inode_ref(trans, dest, |
| 5576 | new_dentry->d_name.name, |
| 5577 | new_dentry->d_name.len, |
| 5578 | old_inode->i_ino, |
| 5579 | new_dir->i_ino, index); |
| 5580 | if (ret) |
| 5581 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5582 | /* |
| 5583 | * this is an ugly little race, but the rename is required |
| 5584 | * to make sure that if we crash, the inode is either at the |
| 5585 | * old name or the new one. pinning the log transaction lets |
| 5586 | * us make sure we don't allow a log commit to come in after |
| 5587 | * we unlink the name but before we add the new name back in. |
| 5588 | */ |
| 5589 | btrfs_pin_log_trans(root); |
| 5590 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5591 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5592 | * make sure the inode gets flushed if it is replacing |
| 5593 | * something. |
| 5594 | */ |
| 5595 | if (new_inode && new_inode->i_size && |
| 5596 | old_inode && S_ISREG(old_inode->i_mode)) { |
| 5597 | btrfs_add_ordered_operation(trans, root, old_inode); |
| 5598 | } |
| 5599 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5600 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 5601 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 5602 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5603 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5604 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 5605 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 5606 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5607 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5608 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 5609 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 5610 | old_dentry->d_name.name, |
| 5611 | old_dentry->d_name.len); |
| 5612 | } else { |
| 5613 | btrfs_inc_nlink(old_dentry->d_inode); |
| 5614 | ret = btrfs_unlink_inode(trans, root, old_dir, |
| 5615 | old_dentry->d_inode, |
| 5616 | old_dentry->d_name.name, |
| 5617 | old_dentry->d_name.len); |
| 5618 | } |
| 5619 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5620 | |
| 5621 | if (new_inode) { |
| 5622 | new_inode->i_ctime = CURRENT_TIME; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5623 | if (unlikely(new_inode->i_ino == |
| 5624 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 5625 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 5626 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 5627 | root_objectid, |
| 5628 | new_dentry->d_name.name, |
| 5629 | new_dentry->d_name.len); |
| 5630 | BUG_ON(new_inode->i_nlink == 0); |
| 5631 | } else { |
| 5632 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 5633 | new_dentry->d_inode, |
| 5634 | new_dentry->d_name.name, |
| 5635 | new_dentry->d_name.len); |
| 5636 | } |
| 5637 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5638 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5639 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5640 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5641 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5642 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5643 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5644 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 5645 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5646 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5647 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5648 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5649 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 5650 | btrfs_log_new_name(trans, old_inode, old_dir, |
| 5651 | new_dentry->d_parent); |
| 5652 | btrfs_end_log_trans(root); |
| 5653 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5654 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 5655 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5656 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5657 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 5658 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5659 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5660 | return ret; |
| 5661 | } |
| 5662 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5663 | /* |
| 5664 | * some fairly slow code that needs optimization. This walks the list |
| 5665 | * of all the inodes with pending delalloc and forces them to disk. |
| 5666 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 5667 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5668 | { |
| 5669 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 5670 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5671 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5672 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5673 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 5674 | return -EROFS; |
| 5675 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5676 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5677 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5678 | binode = list_entry(head->next, struct btrfs_inode, |
| 5679 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5680 | inode = igrab(&binode->vfs_inode); |
| 5681 | if (!inode) |
| 5682 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5683 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5684 | if (inode) { |
Chris Mason | 8c8bee1d | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5685 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 5686 | if (delay_iput) |
| 5687 | btrfs_add_delayed_iput(inode); |
| 5688 | else |
| 5689 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5690 | } |
| 5691 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5692 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5693 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5694 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1d | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5695 | |
| 5696 | /* the filemap_flush will queue IO into the worker threads, but |
| 5697 | * we have to make sure the IO is actually started and that |
| 5698 | * ordered extents get created before we return |
| 5699 | */ |
| 5700 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5701 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5702 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1d | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5703 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5704 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 5705 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1d | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5706 | } |
| 5707 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5708 | return 0; |
| 5709 | } |
| 5710 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5711 | int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput) |
| 5712 | { |
| 5713 | struct btrfs_inode *binode; |
| 5714 | struct inode *inode = NULL; |
| 5715 | |
| 5716 | spin_lock(&root->fs_info->delalloc_lock); |
| 5717 | while (!list_empty(&root->fs_info->delalloc_inodes)) { |
| 5718 | binode = list_entry(root->fs_info->delalloc_inodes.next, |
| 5719 | struct btrfs_inode, delalloc_inodes); |
| 5720 | inode = igrab(&binode->vfs_inode); |
| 5721 | if (inode) { |
| 5722 | list_move_tail(&binode->delalloc_inodes, |
| 5723 | &root->fs_info->delalloc_inodes); |
| 5724 | break; |
| 5725 | } |
| 5726 | |
| 5727 | list_del_init(&binode->delalloc_inodes); |
| 5728 | cond_resched_lock(&root->fs_info->delalloc_lock); |
| 5729 | } |
| 5730 | spin_unlock(&root->fs_info->delalloc_lock); |
| 5731 | |
| 5732 | if (inode) { |
| 5733 | write_inode_now(inode, 0); |
| 5734 | if (delay_iput) |
| 5735 | btrfs_add_delayed_iput(inode); |
| 5736 | else |
| 5737 | iput(inode); |
| 5738 | return 1; |
| 5739 | } |
| 5740 | return 0; |
| 5741 | } |
| 5742 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5743 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 5744 | const char *symname) |
| 5745 | { |
| 5746 | struct btrfs_trans_handle *trans; |
| 5747 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5748 | struct btrfs_path *path; |
| 5749 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5750 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5751 | int err; |
| 5752 | int drop_inode = 0; |
| 5753 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5754 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5755 | int name_len; |
| 5756 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5757 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5758 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5759 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5760 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5761 | |
| 5762 | name_len = strlen(symname) + 1; |
| 5763 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 5764 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5765 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5766 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 5767 | if (err) |
| 5768 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5769 | /* |
| 5770 | * 2 items for inode item and ref |
| 5771 | * 2 items for dir items |
| 5772 | * 1 item for xattr if selinux is on |
| 5773 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5774 | trans = btrfs_start_transaction(root, 5); |
| 5775 | if (IS_ERR(trans)) |
| 5776 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5777 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5778 | btrfs_set_trans_block_group(trans, dir); |
| 5779 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5780 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5781 | dentry->d_name.len, |
| 5782 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5783 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 5784 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5785 | err = PTR_ERR(inode); |
| 5786 | if (IS_ERR(inode)) |
| 5787 | goto out_unlock; |
| 5788 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 5789 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5790 | if (err) { |
| 5791 | drop_inode = 1; |
| 5792 | goto out_unlock; |
| 5793 | } |
| 5794 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5795 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5796 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5797 | if (err) |
| 5798 | drop_inode = 1; |
| 5799 | else { |
| 5800 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 5801 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5802 | inode->i_fop = &btrfs_file_operations; |
| 5803 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5804 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5805 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5806 | btrfs_update_inode_block_group(trans, inode); |
| 5807 | btrfs_update_inode_block_group(trans, dir); |
| 5808 | if (drop_inode) |
| 5809 | goto out_unlock; |
| 5810 | |
| 5811 | path = btrfs_alloc_path(); |
| 5812 | BUG_ON(!path); |
| 5813 | key.objectid = inode->i_ino; |
| 5814 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5815 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 5816 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 5817 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 5818 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5819 | if (err) { |
| 5820 | drop_inode = 1; |
| 5821 | goto out_unlock; |
| 5822 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5823 | leaf = path->nodes[0]; |
| 5824 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 5825 | struct btrfs_file_extent_item); |
| 5826 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 5827 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5828 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5829 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 5830 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 5831 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 5832 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 5833 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5834 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5835 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 5836 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5837 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5838 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5839 | inode->i_op = &btrfs_symlink_inode_operations; |
| 5840 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 5841 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5842 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5843 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5844 | err = btrfs_update_inode(trans, root, inode); |
| 5845 | if (err) |
| 5846 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5847 | |
| 5848 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5849 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 5850 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5851 | if (drop_inode) { |
| 5852 | inode_dec_link_count(inode); |
| 5853 | iput(inode); |
| 5854 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5855 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5856 | return err; |
| 5857 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 5858 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5859 | static int prealloc_file_range(struct inode *inode, u64 start, u64 end, |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 5860 | u64 alloc_hint, int mode, loff_t actual_len) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5861 | { |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5862 | struct btrfs_trans_handle *trans; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5863 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5864 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5865 | u64 cur_offset = start; |
| 5866 | u64 num_bytes = end - start; |
| 5867 | int ret = 0; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 5868 | u64 i_size; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5869 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5870 | while (num_bytes > 0) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5871 | trans = btrfs_start_transaction(root, 3); |
| 5872 | if (IS_ERR(trans)) { |
| 5873 | ret = PTR_ERR(trans); |
| 5874 | break; |
| 5875 | } |
Chris Mason | 3a1abec | 2009-12-17 15:47:17 -0500 | [diff] [blame] | 5876 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 5877 | ret = btrfs_reserve_extent(trans, root, num_bytes, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5878 | root->sectorsize, 0, alloc_hint, |
| 5879 | (u64)-1, &ins, 1); |
| 5880 | if (ret) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5881 | btrfs_end_transaction(trans, root); |
| 5882 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5885 | ret = insert_reserved_file_extent(trans, inode, |
| 5886 | cur_offset, ins.objectid, |
| 5887 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 5888 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5889 | BTRFS_FILE_EXTENT_PREALLOC); |
| 5890 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 5891 | btrfs_drop_extent_cache(inode, cur_offset, |
| 5892 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5893 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5894 | num_bytes -= ins.offset; |
| 5895 | cur_offset += ins.offset; |
| 5896 | alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5897 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5898 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5899 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5900 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Aneesh Kumar K.V | 23b5c50 | 2010-02-04 11:33:03 -0500 | [diff] [blame] | 5901 | (actual_len > inode->i_size) && |
| 5902 | (cur_offset > inode->i_size)) { |
| 5903 | |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 5904 | if (cur_offset > actual_len) |
| 5905 | i_size = actual_len; |
| 5906 | else |
| 5907 | i_size = cur_offset; |
| 5908 | i_size_write(inode, i_size); |
| 5909 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5910 | } |
| 5911 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5912 | ret = btrfs_update_inode(trans, root, inode); |
| 5913 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5914 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5915 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5916 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5917 | return ret; |
| 5918 | } |
| 5919 | |
| 5920 | static long btrfs_fallocate(struct inode *inode, int mode, |
| 5921 | loff_t offset, loff_t len) |
| 5922 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5923 | struct extent_state *cached_state = NULL; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5924 | u64 cur_offset; |
| 5925 | u64 last_byte; |
| 5926 | u64 alloc_start; |
| 5927 | u64 alloc_end; |
| 5928 | u64 alloc_hint = 0; |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5929 | u64 locked_end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5930 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 5931 | struct extent_map *em; |
| 5932 | int ret; |
| 5933 | |
| 5934 | alloc_start = offset & ~mask; |
| 5935 | alloc_end = (offset + len + mask) & ~mask; |
| 5936 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5937 | /* |
| 5938 | * wait for ordered IO before we have any locks. We'll loop again |
| 5939 | * below with the locks held. |
| 5940 | */ |
| 5941 | btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start); |
| 5942 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5943 | mutex_lock(&inode->i_mutex); |
| 5944 | if (alloc_start > inode->i_size) { |
| 5945 | ret = btrfs_cont_expand(inode, alloc_start); |
| 5946 | if (ret) |
| 5947 | goto out; |
| 5948 | } |
| 5949 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 5950 | ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start); |
Josef Bacik | a970b0a | 2009-06-27 21:07:34 -0400 | [diff] [blame] | 5951 | if (ret) |
| 5952 | goto out; |
| 5953 | |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5954 | locked_end = alloc_end - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5955 | while (1) { |
| 5956 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5957 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5958 | /* the extent lock is ordered inside the running |
| 5959 | * transaction |
| 5960 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5961 | lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start, |
| 5962 | locked_end, 0, &cached_state, GFP_NOFS); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5963 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 5964 | alloc_end - 1); |
| 5965 | if (ordered && |
| 5966 | ordered->file_offset + ordered->len > alloc_start && |
| 5967 | ordered->file_offset < alloc_end) { |
| 5968 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5969 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 5970 | alloc_start, locked_end, |
| 5971 | &cached_state, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5972 | /* |
| 5973 | * we can't wait on the range with the transaction |
| 5974 | * running or with the extent lock held |
| 5975 | */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5976 | btrfs_wait_ordered_range(inode, alloc_start, |
| 5977 | alloc_end - alloc_start); |
| 5978 | } else { |
| 5979 | if (ordered) |
| 5980 | btrfs_put_ordered_extent(ordered); |
| 5981 | break; |
| 5982 | } |
| 5983 | } |
| 5984 | |
| 5985 | cur_offset = alloc_start; |
| 5986 | while (1) { |
| 5987 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 5988 | alloc_end - cur_offset, 0); |
| 5989 | BUG_ON(IS_ERR(em) || !em); |
| 5990 | last_byte = min(extent_map_end(em), alloc_end); |
| 5991 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5992 | if (em->block_start == EXTENT_MAP_HOLE || |
| 5993 | (cur_offset >= inode->i_size && |
| 5994 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5995 | ret = prealloc_file_range(inode, |
| 5996 | cur_offset, last_byte, |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 5997 | alloc_hint, mode, offset+len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5998 | if (ret < 0) { |
| 5999 | free_extent_map(em); |
| 6000 | break; |
| 6001 | } |
| 6002 | } |
| 6003 | if (em->block_start <= EXTENT_MAP_LAST_BYTE) |
| 6004 | alloc_hint = em->block_start; |
| 6005 | free_extent_map(em); |
| 6006 | |
| 6007 | cur_offset = last_byte; |
| 6008 | if (cur_offset >= alloc_end) { |
| 6009 | ret = 0; |
| 6010 | break; |
| 6011 | } |
| 6012 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6013 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
| 6014 | &cached_state, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 6015 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame^] | 6016 | btrfs_free_reserved_data_space(inode, alloc_end - alloc_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6017 | out: |
| 6018 | mutex_unlock(&inode->i_mutex); |
| 6019 | return ret; |
| 6020 | } |
| 6021 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6022 | static int btrfs_set_page_dirty(struct page *page) |
| 6023 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6024 | return __set_page_dirty_nobuffers(page); |
| 6025 | } |
| 6026 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 6027 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6028 | { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6029 | if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6030 | return -EACCES; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6031 | return generic_permission(inode, mask, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6032 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6033 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6034 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6035 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6036 | .lookup = btrfs_lookup, |
| 6037 | .create = btrfs_create, |
| 6038 | .unlink = btrfs_unlink, |
| 6039 | .link = btrfs_link, |
| 6040 | .mkdir = btrfs_mkdir, |
| 6041 | .rmdir = btrfs_rmdir, |
| 6042 | .rename = btrfs_rename, |
| 6043 | .symlink = btrfs_symlink, |
| 6044 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6045 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6046 | .setxattr = btrfs_setxattr, |
| 6047 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 6048 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6049 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6050 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6051 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6052 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6053 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6054 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6055 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6056 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 6057 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6058 | .llseek = generic_file_llseek, |
| 6059 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 6060 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 6061 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6062 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 6063 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6064 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 6065 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6066 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6067 | }; |
| 6068 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6069 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 6070 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 6071 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6072 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 6073 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6074 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6075 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6076 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 6077 | .set_bit_hook = btrfs_set_bit_hook, |
| 6078 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6079 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 6080 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 6081 | }; |
| 6082 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 6083 | /* |
| 6084 | * btrfs doesn't support the bmap operation because swapfiles |
| 6085 | * use bmap to make a mapping of extents in the file. They assume |
| 6086 | * these extents won't change over the life of the file and they |
| 6087 | * use the bmap result to do IO directly to the drive. |
| 6088 | * |
| 6089 | * the btrfs bmap call would return logical addresses that aren't |
| 6090 | * suitable for IO and they also will change frequently as COW |
| 6091 | * operations happen. So, swapfile + btrfs == corruption. |
| 6092 | * |
| 6093 | * For now we're avoiding this by dropping bmap. |
| 6094 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 6095 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6096 | .readpage = btrfs_readpage, |
| 6097 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6098 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6099 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6100 | .sync_page = block_sync_page, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6101 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6102 | .invalidatepage = btrfs_invalidatepage, |
| 6103 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6104 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 6105 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6106 | }; |
| 6107 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 6108 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6109 | .readpage = btrfs_readpage, |
| 6110 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 6111 | .invalidatepage = btrfs_invalidatepage, |
| 6112 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6113 | }; |
| 6114 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6115 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6116 | .truncate = btrfs_truncate, |
| 6117 | .getattr = btrfs_getattr, |
| 6118 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6119 | .setxattr = btrfs_setxattr, |
| 6120 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 6121 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6122 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6123 | .permission = btrfs_permission, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6124 | .fallocate = btrfs_fallocate, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6125 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6126 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6127 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6128 | .getattr = btrfs_getattr, |
| 6129 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6130 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6131 | .setxattr = btrfs_setxattr, |
| 6132 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6133 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6134 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6135 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6136 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6137 | .readlink = generic_readlink, |
| 6138 | .follow_link = page_follow_link_light, |
| 6139 | .put_link = page_put_link, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6140 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 6141 | .setxattr = btrfs_setxattr, |
| 6142 | .getxattr = btrfs_getxattr, |
| 6143 | .listxattr = btrfs_listxattr, |
| 6144 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6145 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6146 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 6147 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6148 | .d_delete = btrfs_dentry_delete, |
| 6149 | }; |