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> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 39 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 40 | #include "ctree.h" |
| 41 | #include "disk-io.h" |
| 42 | #include "transaction.h" |
| 43 | #include "btrfs_inode.h" |
| 44 | #include "ioctl.h" |
| 45 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 46 | #include "volumes.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 47 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 48 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 49 | #include "tree-log.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 50 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 51 | #include "locking.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 52 | |
| 53 | struct btrfs_iget_args { |
| 54 | u64 ino; |
| 55 | struct btrfs_root *root; |
| 56 | }; |
| 57 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 58 | static const struct inode_operations btrfs_dir_inode_operations; |
| 59 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 60 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 61 | static const struct inode_operations btrfs_special_inode_operations; |
| 62 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 63 | static const struct address_space_operations btrfs_aops; |
| 64 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 65 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 66 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 67 | |
| 68 | static struct kmem_cache *btrfs_inode_cachep; |
| 69 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 70 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 71 | struct kmem_cache *btrfs_path_cachep; |
| 72 | |
| 73 | #define S_SHIFT 12 |
| 74 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 75 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 76 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 77 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 78 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 79 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 80 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 81 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 82 | }; |
| 83 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 84 | static void btrfs_truncate(struct inode *inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 85 | 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] | 86 | static noinline int cow_file_range(struct inode *inode, |
| 87 | struct page *locked_page, |
| 88 | u64 start, u64 end, int *page_started, |
| 89 | unsigned long *nr_written, int unlock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 90 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 91 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
| 92 | struct inode *inode, struct inode *dir) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 93 | { |
| 94 | int err; |
| 95 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 96 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 97 | if (!err) |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 98 | err = btrfs_xattr_security_init(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 99 | return err; |
| 100 | } |
| 101 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 102 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 103 | * this does all the hard work for inserting an inline extent into |
| 104 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 105 | * no overlapping inline items exist in the btree |
| 106 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 107 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 108 | struct btrfs_root *root, struct inode *inode, |
| 109 | u64 start, size_t size, size_t compressed_size, |
| 110 | struct page **compressed_pages) |
| 111 | { |
| 112 | struct btrfs_key key; |
| 113 | struct btrfs_path *path; |
| 114 | struct extent_buffer *leaf; |
| 115 | struct page *page = NULL; |
| 116 | char *kaddr; |
| 117 | unsigned long ptr; |
| 118 | struct btrfs_file_extent_item *ei; |
| 119 | int err = 0; |
| 120 | int ret; |
| 121 | size_t cur_size = size; |
| 122 | size_t datasize; |
| 123 | unsigned long offset; |
| 124 | int use_compress = 0; |
| 125 | |
| 126 | if (compressed_size && compressed_pages) { |
| 127 | use_compress = 1; |
| 128 | cur_size = compressed_size; |
| 129 | } |
| 130 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 131 | path = btrfs_alloc_path(); |
| 132 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 133 | return -ENOMEM; |
| 134 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 135 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 136 | btrfs_set_trans_block_group(trans, inode); |
| 137 | |
| 138 | key.objectid = inode->i_ino; |
| 139 | key.offset = start; |
| 140 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 141 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 142 | |
| 143 | inode_add_bytes(inode, size); |
| 144 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 145 | datasize); |
| 146 | BUG_ON(ret); |
| 147 | if (ret) { |
| 148 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 149 | goto fail; |
| 150 | } |
| 151 | leaf = path->nodes[0]; |
| 152 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 153 | struct btrfs_file_extent_item); |
| 154 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 155 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 156 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 157 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 158 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 159 | ptr = btrfs_file_extent_inline_start(ei); |
| 160 | |
| 161 | if (use_compress) { |
| 162 | struct page *cpage; |
| 163 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 164 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 165 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 166 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 167 | PAGE_CACHE_SIZE); |
| 168 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 169 | kaddr = kmap_atomic(cpage, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 170 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 171 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 172 | |
| 173 | i++; |
| 174 | ptr += cur_size; |
| 175 | compressed_size -= cur_size; |
| 176 | } |
| 177 | btrfs_set_file_extent_compression(leaf, ei, |
| 178 | BTRFS_COMPRESS_ZLIB); |
| 179 | } else { |
| 180 | page = find_get_page(inode->i_mapping, |
| 181 | start >> PAGE_CACHE_SHIFT); |
| 182 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 183 | kaddr = kmap_atomic(page, KM_USER0); |
| 184 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 185 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
| 186 | kunmap_atomic(kaddr, KM_USER0); |
| 187 | page_cache_release(page); |
| 188 | } |
| 189 | btrfs_mark_buffer_dirty(leaf); |
| 190 | btrfs_free_path(path); |
| 191 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 192 | /* |
| 193 | * we're an inline extent, so nobody can |
| 194 | * extend the file past i_size without locking |
| 195 | * a page we already have locked. |
| 196 | * |
| 197 | * We must do any isize and inode updates |
| 198 | * before we unlock the pages. Otherwise we |
| 199 | * could end up racing with unlink. |
| 200 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 201 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
| 202 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 203 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 204 | return 0; |
| 205 | fail: |
| 206 | btrfs_free_path(path); |
| 207 | return err; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /* |
| 212 | * conditionally insert an inline extent into the file. This |
| 213 | * does the checks required to make sure the data is small enough |
| 214 | * to fit as an inline extent. |
| 215 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 216 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 217 | struct btrfs_root *root, |
| 218 | struct inode *inode, u64 start, u64 end, |
| 219 | size_t compressed_size, |
| 220 | struct page **compressed_pages) |
| 221 | { |
| 222 | u64 isize = i_size_read(inode); |
| 223 | u64 actual_end = min(end + 1, isize); |
| 224 | u64 inline_len = actual_end - start; |
| 225 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 226 | ~((u64)root->sectorsize - 1); |
| 227 | u64 hint_byte; |
| 228 | u64 data_len = inline_len; |
| 229 | int ret; |
| 230 | |
| 231 | if (compressed_size) |
| 232 | data_len = compressed_size; |
| 233 | |
| 234 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 235 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 236 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 237 | (!compressed_size && |
| 238 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 239 | end + 1 < isize || |
| 240 | data_len > root->fs_info->max_inline) { |
| 241 | return 1; |
| 242 | } |
| 243 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 244 | ret = btrfs_drop_extents(trans, inode, start, aligned_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 245 | &hint_byte, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 246 | BUG_ON(ret); |
| 247 | |
| 248 | if (isize > actual_end) |
| 249 | inline_len = min_t(u64, isize, actual_end); |
| 250 | ret = insert_inline_extent(trans, root, inode, start, |
| 251 | inline_len, compressed_size, |
| 252 | compressed_pages); |
| 253 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 254 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 258 | struct async_extent { |
| 259 | u64 start; |
| 260 | u64 ram_size; |
| 261 | u64 compressed_size; |
| 262 | struct page **pages; |
| 263 | unsigned long nr_pages; |
| 264 | struct list_head list; |
| 265 | }; |
| 266 | |
| 267 | struct async_cow { |
| 268 | struct inode *inode; |
| 269 | struct btrfs_root *root; |
| 270 | struct page *locked_page; |
| 271 | u64 start; |
| 272 | u64 end; |
| 273 | struct list_head extents; |
| 274 | struct btrfs_work work; |
| 275 | }; |
| 276 | |
| 277 | static noinline int add_async_extent(struct async_cow *cow, |
| 278 | u64 start, u64 ram_size, |
| 279 | u64 compressed_size, |
| 280 | struct page **pages, |
| 281 | unsigned long nr_pages) |
| 282 | { |
| 283 | struct async_extent *async_extent; |
| 284 | |
| 285 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
| 286 | async_extent->start = start; |
| 287 | async_extent->ram_size = ram_size; |
| 288 | async_extent->compressed_size = compressed_size; |
| 289 | async_extent->pages = pages; |
| 290 | async_extent->nr_pages = nr_pages; |
| 291 | list_add_tail(&async_extent->list, &cow->extents); |
| 292 | return 0; |
| 293 | } |
| 294 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 295 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 296 | * we create compressed extents in two phases. The first |
| 297 | * phase compresses a range of pages that have already been |
| 298 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 299 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 300 | * This is done inside an ordered work queue, and the compression |
| 301 | * is spread across many cpus. The actual IO submission is step |
| 302 | * two, and the ordered work queue takes care of making sure that |
| 303 | * happens in the same order things were put onto the queue by |
| 304 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 305 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 306 | * If this code finds it can't get good compression, it puts an |
| 307 | * entry onto the work queue to write the uncompressed bytes. This |
| 308 | * makes sure that both compressed inodes and uncompressed inodes |
| 309 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 310 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 311 | static noinline int compress_file_range(struct inode *inode, |
| 312 | struct page *locked_page, |
| 313 | u64 start, u64 end, |
| 314 | struct async_cow *async_cow, |
| 315 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 316 | { |
| 317 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 318 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 319 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 320 | u64 orig_start; |
| 321 | u64 disk_num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 322 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 323 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 324 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 325 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 326 | struct page **pages = NULL; |
| 327 | unsigned long nr_pages; |
| 328 | unsigned long nr_pages_ret = 0; |
| 329 | unsigned long total_compressed = 0; |
| 330 | unsigned long total_in = 0; |
| 331 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 332 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 333 | int i; |
| 334 | int will_compress; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 335 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 336 | orig_start = start; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 337 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 338 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 339 | again: |
| 340 | will_compress = 0; |
| 341 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 342 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 343 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 344 | /* |
| 345 | * we don't want to send crud past the end of i_size through |
| 346 | * compression, that's just a waste of CPU time. So, if the |
| 347 | * end of the file is before the start of our current |
| 348 | * requested range of bytes, we bail out to the uncompressed |
| 349 | * cleanup code that can deal with all of this. |
| 350 | * |
| 351 | * It isn't really the fastest way to fix things, but this is a |
| 352 | * very uncommon corner. |
| 353 | */ |
| 354 | if (actual_end <= start) |
| 355 | goto cleanup_and_bail_uncompressed; |
| 356 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 357 | total_compressed = actual_end - start; |
| 358 | |
| 359 | /* we want to make sure that amount of ram required to uncompress |
| 360 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 361 | * of a compressed extent to 128k. This is a crucial number |
| 362 | * because it also controls how easily we can spread reads across |
| 363 | * cpus for decompression. |
| 364 | * |
| 365 | * We also want to make sure the amount of IO required to do |
| 366 | * a random read is reasonably small, so we limit the size of |
| 367 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 368 | */ |
| 369 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 370 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 371 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 372 | disk_num_bytes = num_bytes; |
| 373 | total_in = 0; |
| 374 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 375 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 376 | /* |
| 377 | * we do compression for mount -o compress and when the |
| 378 | * inode has not been flagged as nocompress. This flag can |
| 379 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 380 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 381 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 382 | btrfs_test_opt(root, COMPRESS)) { |
| 383 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 384 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 385 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 386 | ret = btrfs_zlib_compress_pages(inode->i_mapping, start, |
| 387 | total_compressed, pages, |
| 388 | nr_pages, &nr_pages_ret, |
| 389 | &total_in, |
| 390 | &total_compressed, |
| 391 | max_compressed); |
| 392 | |
| 393 | if (!ret) { |
| 394 | unsigned long offset = total_compressed & |
| 395 | (PAGE_CACHE_SIZE - 1); |
| 396 | struct page *page = pages[nr_pages_ret - 1]; |
| 397 | char *kaddr; |
| 398 | |
| 399 | /* zero the tail end of the last page, we might be |
| 400 | * sending it down to disk |
| 401 | */ |
| 402 | if (offset) { |
| 403 | kaddr = kmap_atomic(page, KM_USER0); |
| 404 | memset(kaddr + offset, 0, |
| 405 | PAGE_CACHE_SIZE - offset); |
| 406 | kunmap_atomic(kaddr, KM_USER0); |
| 407 | } |
| 408 | will_compress = 1; |
| 409 | } |
| 410 | } |
| 411 | if (start == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 412 | trans = btrfs_join_transaction(root, 1); |
| 413 | BUG_ON(!trans); |
| 414 | btrfs_set_trans_block_group(trans, inode); |
| 415 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 416 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 417 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 418 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 419 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 420 | */ |
| 421 | ret = cow_file_range_inline(trans, root, inode, |
| 422 | start, end, 0, NULL); |
| 423 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 424 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 425 | ret = cow_file_range_inline(trans, root, inode, |
| 426 | start, end, |
| 427 | total_compressed, pages); |
| 428 | } |
| 429 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 430 | /* |
| 431 | * inline extent creation worked, we don't need |
| 432 | * to create any more async work items. Unlock |
| 433 | * and free up our temp pages. |
| 434 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 435 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 436 | &BTRFS_I(inode)->io_tree, |
| 437 | start, end, NULL, |
| 438 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 439 | EXTENT_CLEAR_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 440 | EXTENT_CLEAR_ACCOUNTING | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 441 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 442 | |
| 443 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 444 | goto free_pages_out; |
| 445 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 446 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (will_compress) { |
| 450 | /* |
| 451 | * we aren't doing an inline extent round the compressed size |
| 452 | * up to a block size boundary so the allocator does sane |
| 453 | * things |
| 454 | */ |
| 455 | total_compressed = (total_compressed + blocksize - 1) & |
| 456 | ~(blocksize - 1); |
| 457 | |
| 458 | /* |
| 459 | * one last check to make sure the compression is really a |
| 460 | * win, compare the page count read with the blocks on disk |
| 461 | */ |
| 462 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 463 | ~(PAGE_CACHE_SIZE - 1); |
| 464 | if (total_compressed >= total_in) { |
| 465 | will_compress = 0; |
| 466 | } else { |
| 467 | disk_num_bytes = total_compressed; |
| 468 | num_bytes = total_in; |
| 469 | } |
| 470 | } |
| 471 | if (!will_compress && pages) { |
| 472 | /* |
| 473 | * the compression code ran but failed to make things smaller, |
| 474 | * free any pages it allocated and our page pointer array |
| 475 | */ |
| 476 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 477 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 478 | page_cache_release(pages[i]); |
| 479 | } |
| 480 | kfree(pages); |
| 481 | pages = NULL; |
| 482 | total_compressed = 0; |
| 483 | nr_pages_ret = 0; |
| 484 | |
| 485 | /* flag the file so we don't compress in the future */ |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame^] | 486 | if (!btrfs_test_opt(root, FORCE_COMPRESS)) |
| 487 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 488 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 489 | if (will_compress) { |
| 490 | *num_added += 1; |
| 491 | |
| 492 | /* the async work queues will take care of doing actual |
| 493 | * allocation on disk for these compressed pages, |
| 494 | * and will submit them to the elevator. |
| 495 | */ |
| 496 | add_async_extent(async_cow, start, num_bytes, |
| 497 | total_compressed, pages, nr_pages_ret); |
| 498 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 499 | if (start + num_bytes < end && start + num_bytes < actual_end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 500 | start += num_bytes; |
| 501 | pages = NULL; |
| 502 | cond_resched(); |
| 503 | goto again; |
| 504 | } |
| 505 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 506 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 507 | /* |
| 508 | * No compression, but we still need to write the pages in |
| 509 | * the file we've been given so far. redirty the locked |
| 510 | * page if it corresponds to our extent and set things up |
| 511 | * for the async work queue to run cow_file_range to do |
| 512 | * the normal delalloc dance |
| 513 | */ |
| 514 | if (page_offset(locked_page) >= start && |
| 515 | page_offset(locked_page) <= end) { |
| 516 | __set_page_dirty_nobuffers(locked_page); |
| 517 | /* unlocked later on in the async handlers */ |
| 518 | } |
| 519 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0); |
| 520 | *num_added += 1; |
| 521 | } |
| 522 | |
| 523 | out: |
| 524 | return 0; |
| 525 | |
| 526 | free_pages_out: |
| 527 | for (i = 0; i < nr_pages_ret; i++) { |
| 528 | WARN_ON(pages[i]->mapping); |
| 529 | page_cache_release(pages[i]); |
| 530 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 531 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 532 | |
| 533 | goto out; |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * phase two of compressed writeback. This is the ordered portion |
| 538 | * of the code, which only gets called in the order the work was |
| 539 | * queued. We walk all the async extents created by compress_file_range |
| 540 | * and send them down to the disk. |
| 541 | */ |
| 542 | static noinline int submit_compressed_extents(struct inode *inode, |
| 543 | struct async_cow *async_cow) |
| 544 | { |
| 545 | struct async_extent *async_extent; |
| 546 | u64 alloc_hint = 0; |
| 547 | struct btrfs_trans_handle *trans; |
| 548 | struct btrfs_key ins; |
| 549 | struct extent_map *em; |
| 550 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 551 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 552 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 553 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 554 | |
| 555 | if (list_empty(&async_cow->extents)) |
| 556 | return 0; |
| 557 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 558 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 559 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 560 | async_extent = list_entry(async_cow->extents.next, |
| 561 | struct async_extent, list); |
| 562 | list_del(&async_extent->list); |
| 563 | |
| 564 | io_tree = &BTRFS_I(inode)->io_tree; |
| 565 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 566 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 567 | /* did the compression code fall back to uncompressed IO? */ |
| 568 | if (!async_extent->pages) { |
| 569 | int page_started = 0; |
| 570 | unsigned long nr_written = 0; |
| 571 | |
| 572 | lock_extent(io_tree, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 573 | async_extent->start + |
| 574 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 575 | |
| 576 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 577 | ret = cow_file_range(inode, async_cow->locked_page, |
| 578 | async_extent->start, |
| 579 | async_extent->start + |
| 580 | async_extent->ram_size - 1, |
| 581 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 582 | |
| 583 | /* |
| 584 | * if page_started, cow_file_range inserted an |
| 585 | * inline extent and took care of all the unlocking |
| 586 | * and IO for us. Otherwise, we need to submit |
| 587 | * all those pages down to the drive. |
| 588 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 589 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 590 | extent_write_locked_range(io_tree, |
| 591 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 592 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 593 | async_extent->ram_size - 1, |
| 594 | btrfs_get_extent, |
| 595 | WB_SYNC_ALL); |
| 596 | kfree(async_extent); |
| 597 | cond_resched(); |
| 598 | continue; |
| 599 | } |
| 600 | |
| 601 | lock_extent(io_tree, async_extent->start, |
| 602 | async_extent->start + async_extent->ram_size - 1, |
| 603 | GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 604 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 605 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 606 | ret = btrfs_reserve_extent(trans, root, |
| 607 | async_extent->compressed_size, |
| 608 | async_extent->compressed_size, |
| 609 | 0, alloc_hint, |
| 610 | (u64)-1, &ins, 1); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 611 | btrfs_end_transaction(trans, root); |
| 612 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 613 | if (ret) { |
| 614 | int i; |
| 615 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 616 | WARN_ON(async_extent->pages[i]->mapping); |
| 617 | page_cache_release(async_extent->pages[i]); |
| 618 | } |
| 619 | kfree(async_extent->pages); |
| 620 | async_extent->nr_pages = 0; |
| 621 | async_extent->pages = NULL; |
| 622 | unlock_extent(io_tree, async_extent->start, |
| 623 | async_extent->start + |
| 624 | async_extent->ram_size - 1, GFP_NOFS); |
| 625 | goto retry; |
| 626 | } |
| 627 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 628 | /* |
| 629 | * here we're doing allocation and writeback of the |
| 630 | * compressed pages |
| 631 | */ |
| 632 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 633 | async_extent->start + |
| 634 | async_extent->ram_size - 1, 0); |
| 635 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 636 | em = alloc_extent_map(GFP_NOFS); |
| 637 | em->start = async_extent->start; |
| 638 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 639 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 640 | |
| 641 | em->block_start = ins.objectid; |
| 642 | em->block_len = ins.offset; |
| 643 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 644 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 645 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 646 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 647 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 648 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 649 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 650 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 651 | if (ret != -EEXIST) { |
| 652 | free_extent_map(em); |
| 653 | break; |
| 654 | } |
| 655 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 656 | async_extent->start + |
| 657 | async_extent->ram_size - 1, 0); |
| 658 | } |
| 659 | |
| 660 | ret = btrfs_add_ordered_extent(inode, async_extent->start, |
| 661 | ins.objectid, |
| 662 | async_extent->ram_size, |
| 663 | ins.offset, |
| 664 | BTRFS_ORDERED_COMPRESSED); |
| 665 | BUG_ON(ret); |
| 666 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 667 | /* |
| 668 | * clear dirty, set writeback and unlock the pages. |
| 669 | */ |
| 670 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 671 | &BTRFS_I(inode)->io_tree, |
| 672 | async_extent->start, |
| 673 | async_extent->start + |
| 674 | async_extent->ram_size - 1, |
| 675 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 676 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 677 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 678 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 679 | |
| 680 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 681 | async_extent->start, |
| 682 | async_extent->ram_size, |
| 683 | ins.objectid, |
| 684 | ins.offset, async_extent->pages, |
| 685 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 686 | |
| 687 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 688 | alloc_hint = ins.objectid + ins.offset; |
| 689 | kfree(async_extent); |
| 690 | cond_resched(); |
| 691 | } |
| 692 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * when extent_io.c finds a delayed allocation range in the file, |
| 698 | * the call backs end up in this code. The basic idea is to |
| 699 | * allocate extents on disk for the range, and create ordered data structs |
| 700 | * in ram to track those extents. |
| 701 | * |
| 702 | * locked_page is the page that writepage had locked already. We use |
| 703 | * it to make sure we don't do extra locks or unlocks. |
| 704 | * |
| 705 | * *page_started is set to one if we unlock locked_page and do everything |
| 706 | * required to start IO on it. It may be clean and already done with |
| 707 | * IO when we return. |
| 708 | */ |
| 709 | static noinline int cow_file_range(struct inode *inode, |
| 710 | struct page *locked_page, |
| 711 | u64 start, u64 end, int *page_started, |
| 712 | unsigned long *nr_written, |
| 713 | int unlock) |
| 714 | { |
| 715 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 716 | struct btrfs_trans_handle *trans; |
| 717 | u64 alloc_hint = 0; |
| 718 | u64 num_bytes; |
| 719 | unsigned long ram_size; |
| 720 | u64 disk_num_bytes; |
| 721 | u64 cur_alloc_size; |
| 722 | u64 blocksize = root->sectorsize; |
| 723 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 724 | u64 isize = i_size_read(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 725 | struct btrfs_key ins; |
| 726 | struct extent_map *em; |
| 727 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 728 | int ret = 0; |
| 729 | |
| 730 | trans = btrfs_join_transaction(root, 1); |
| 731 | BUG_ON(!trans); |
| 732 | btrfs_set_trans_block_group(trans, inode); |
| 733 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 734 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 735 | |
| 736 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 737 | num_bytes = max(blocksize, num_bytes); |
| 738 | disk_num_bytes = num_bytes; |
| 739 | ret = 0; |
| 740 | |
| 741 | if (start == 0) { |
| 742 | /* lets try to make an inline extent */ |
| 743 | ret = cow_file_range_inline(trans, root, inode, |
| 744 | start, end, 0, NULL); |
| 745 | if (ret == 0) { |
| 746 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 747 | &BTRFS_I(inode)->io_tree, |
| 748 | start, end, NULL, |
| 749 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 750 | EXTENT_CLEAR_UNLOCK | |
| 751 | EXTENT_CLEAR_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 752 | EXTENT_CLEAR_ACCOUNTING | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 753 | EXTENT_CLEAR_DIRTY | |
| 754 | EXTENT_SET_WRITEBACK | |
| 755 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 756 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 757 | *nr_written = *nr_written + |
| 758 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 759 | *page_started = 1; |
| 760 | ret = 0; |
| 761 | goto out; |
| 762 | } |
| 763 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 764 | |
| 765 | BUG_ON(disk_num_bytes > |
| 766 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 767 | |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 768 | |
| 769 | read_lock(&BTRFS_I(inode)->extent_tree.lock); |
| 770 | em = search_extent_mapping(&BTRFS_I(inode)->extent_tree, |
| 771 | start, num_bytes); |
| 772 | if (em) { |
Josef Bacik | 6346c93 | 2009-11-10 21:23:47 -0500 | [diff] [blame] | 773 | /* |
| 774 | * if block start isn't an actual block number then find the |
| 775 | * first block in this inode and use that as a hint. If that |
| 776 | * block is also bogus then just don't worry about it. |
| 777 | */ |
| 778 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 779 | free_extent_map(em); |
| 780 | em = search_extent_mapping(em_tree, 0, 0); |
| 781 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 782 | alloc_hint = em->block_start; |
| 783 | if (em) |
| 784 | free_extent_map(em); |
| 785 | } else { |
| 786 | alloc_hint = em->block_start; |
| 787 | free_extent_map(em); |
| 788 | } |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 789 | } |
| 790 | read_unlock(&BTRFS_I(inode)->extent_tree.lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 791 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 792 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 793 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 794 | unsigned long op; |
| 795 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 796 | cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 797 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 798 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 799 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 800 | BUG_ON(ret); |
| 801 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 802 | em = alloc_extent_map(GFP_NOFS); |
| 803 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 804 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 805 | ram_size = ins.offset; |
| 806 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 807 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 808 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 809 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 810 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 811 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 812 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 813 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 814 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 815 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 816 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 817 | if (ret != -EEXIST) { |
| 818 | free_extent_map(em); |
| 819 | break; |
| 820 | } |
| 821 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 822 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 823 | } |
| 824 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 825 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 826 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 827 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 828 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 829 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 830 | if (root->root_key.objectid == |
| 831 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 832 | ret = btrfs_reloc_clone_csums(inode, start, |
| 833 | cur_alloc_size); |
| 834 | BUG_ON(ret); |
| 835 | } |
| 836 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 837 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 838 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 839 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 840 | /* we're not doing compressed IO, don't unlock the first |
| 841 | * page (which the caller expects to stay locked), don't |
| 842 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 843 | * |
| 844 | * Do set the Private2 bit so we know this page was properly |
| 845 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 846 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 847 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 848 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 849 | EXTENT_SET_PRIVATE2; |
| 850 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 851 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 852 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 853 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 854 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 855 | num_bytes -= cur_alloc_size; |
| 856 | alloc_hint = ins.objectid + ins.offset; |
| 857 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 858 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 859 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 860 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 861 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 862 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 863 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 864 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 865 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 866 | /* |
| 867 | * work queue call back to started compression on a file and pages |
| 868 | */ |
| 869 | static noinline void async_cow_start(struct btrfs_work *work) |
| 870 | { |
| 871 | struct async_cow *async_cow; |
| 872 | int num_added = 0; |
| 873 | async_cow = container_of(work, struct async_cow, work); |
| 874 | |
| 875 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 876 | async_cow->start, async_cow->end, async_cow, |
| 877 | &num_added); |
| 878 | if (num_added == 0) |
| 879 | async_cow->inode = NULL; |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * work queue call back to submit previously compressed pages |
| 884 | */ |
| 885 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 886 | { |
| 887 | struct async_cow *async_cow; |
| 888 | struct btrfs_root *root; |
| 889 | unsigned long nr_pages; |
| 890 | |
| 891 | async_cow = container_of(work, struct async_cow, work); |
| 892 | |
| 893 | root = async_cow->root; |
| 894 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 895 | PAGE_CACHE_SHIFT; |
| 896 | |
| 897 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 898 | |
| 899 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 900 | 5 * 1042 * 1024 && |
| 901 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 902 | wake_up(&root->fs_info->async_submit_wait); |
| 903 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 904 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 905 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 906 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 907 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 908 | static noinline void async_cow_free(struct btrfs_work *work) |
| 909 | { |
| 910 | struct async_cow *async_cow; |
| 911 | async_cow = container_of(work, struct async_cow, work); |
| 912 | kfree(async_cow); |
| 913 | } |
| 914 | |
| 915 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 916 | u64 start, u64 end, int *page_started, |
| 917 | unsigned long *nr_written) |
| 918 | { |
| 919 | struct async_cow *async_cow; |
| 920 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 921 | unsigned long nr_pages; |
| 922 | u64 cur_end; |
| 923 | int limit = 10 * 1024 * 1042; |
| 924 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 925 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 926 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 927 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 928 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
| 929 | async_cow->inode = inode; |
| 930 | async_cow->root = root; |
| 931 | async_cow->locked_page = locked_page; |
| 932 | async_cow->start = start; |
| 933 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 934 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 935 | cur_end = end; |
| 936 | else |
| 937 | cur_end = min(end, start + 512 * 1024 - 1); |
| 938 | |
| 939 | async_cow->end = cur_end; |
| 940 | INIT_LIST_HEAD(&async_cow->extents); |
| 941 | |
| 942 | async_cow->work.func = async_cow_start; |
| 943 | async_cow->work.ordered_func = async_cow_submit; |
| 944 | async_cow->work.ordered_free = async_cow_free; |
| 945 | async_cow->work.flags = 0; |
| 946 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 947 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 948 | PAGE_CACHE_SHIFT; |
| 949 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 950 | |
| 951 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 952 | &async_cow->work); |
| 953 | |
| 954 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 955 | wait_event(root->fs_info->async_submit_wait, |
| 956 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 957 | limit)); |
| 958 | } |
| 959 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 960 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 961 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 962 | wait_event(root->fs_info->async_submit_wait, |
| 963 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 964 | 0)); |
| 965 | } |
| 966 | |
| 967 | *nr_written += nr_pages; |
| 968 | start = cur_end + 1; |
| 969 | } |
| 970 | *page_started = 1; |
| 971 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 972 | } |
| 973 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 974 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 975 | u64 bytenr, u64 num_bytes) |
| 976 | { |
| 977 | int ret; |
| 978 | struct btrfs_ordered_sum *sums; |
| 979 | LIST_HEAD(list); |
| 980 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 981 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
| 982 | bytenr + num_bytes - 1, &list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 983 | if (ret == 0 && list_empty(&list)) |
| 984 | return 0; |
| 985 | |
| 986 | while (!list_empty(&list)) { |
| 987 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 988 | list_del(&sums->list); |
| 989 | kfree(sums); |
| 990 | } |
| 991 | return 1; |
| 992 | } |
| 993 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 994 | /* |
| 995 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 996 | * of the extents that exist in the file, and COWs the file as required. |
| 997 | * |
| 998 | * If no cow copies or snapshots exist, we write directly to the existing |
| 999 | * blocks on disk |
| 1000 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1001 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1002 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1003 | u64 start, u64 end, int *page_started, int force, |
| 1004 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1005 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1006 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1007 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1008 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1009 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1010 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1011 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1012 | u64 cow_start; |
| 1013 | u64 cur_offset; |
| 1014 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1015 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1016 | u64 disk_bytenr; |
| 1017 | u64 num_bytes; |
| 1018 | int extent_type; |
| 1019 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1020 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1021 | int nocow; |
| 1022 | int check_prev = 1; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1023 | |
| 1024 | path = btrfs_alloc_path(); |
| 1025 | BUG_ON(!path); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1026 | trans = btrfs_join_transaction(root, 1); |
| 1027 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1028 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1029 | cow_start = (u64)-1; |
| 1030 | cur_offset = start; |
| 1031 | while (1) { |
| 1032 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 1033 | cur_offset, 0); |
| 1034 | BUG_ON(ret < 0); |
| 1035 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1036 | leaf = path->nodes[0]; |
| 1037 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1038 | path->slots[0] - 1); |
| 1039 | if (found_key.objectid == inode->i_ino && |
| 1040 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1041 | path->slots[0]--; |
| 1042 | } |
| 1043 | check_prev = 0; |
| 1044 | next_slot: |
| 1045 | leaf = path->nodes[0]; |
| 1046 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1047 | ret = btrfs_next_leaf(root, path); |
| 1048 | if (ret < 0) |
| 1049 | BUG_ON(1); |
| 1050 | if (ret > 0) |
| 1051 | break; |
| 1052 | leaf = path->nodes[0]; |
| 1053 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1054 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1055 | nocow = 0; |
| 1056 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1057 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1058 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
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 | if (found_key.objectid > inode->i_ino || |
| 1061 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1062 | found_key.offset > end) |
| 1063 | break; |
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.offset > cur_offset) { |
| 1066 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1067 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1068 | goto out_check; |
| 1069 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1070 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1071 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1072 | struct btrfs_file_extent_item); |
| 1073 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1074 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1075 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1076 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1077 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1078 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1079 | extent_end = found_key.offset + |
| 1080 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1081 | if (extent_end <= start) { |
| 1082 | path->slots[0]++; |
| 1083 | goto next_slot; |
| 1084 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1085 | if (disk_bytenr == 0) |
| 1086 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1087 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1088 | btrfs_file_extent_encryption(leaf, fi) || |
| 1089 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1090 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1091 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1092 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1093 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1094 | goto out_check; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1095 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1096 | found_key.offset - |
| 1097 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1098 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1099 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1100 | disk_bytenr += cur_offset - found_key.offset; |
| 1101 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1102 | /* |
| 1103 | * force cow if csum exists in the range. |
| 1104 | * this ensure that csum for a given extent are |
| 1105 | * either valid or do not exist. |
| 1106 | */ |
| 1107 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1108 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1109 | nocow = 1; |
| 1110 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1111 | extent_end = found_key.offset + |
| 1112 | btrfs_file_extent_inline_len(leaf, fi); |
| 1113 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1114 | } else { |
| 1115 | BUG_ON(1); |
| 1116 | } |
| 1117 | out_check: |
| 1118 | if (extent_end <= start) { |
| 1119 | path->slots[0]++; |
| 1120 | goto next_slot; |
| 1121 | } |
| 1122 | if (!nocow) { |
| 1123 | if (cow_start == (u64)-1) |
| 1124 | cow_start = cur_offset; |
| 1125 | cur_offset = extent_end; |
| 1126 | if (cur_offset > end) |
| 1127 | break; |
| 1128 | path->slots[0]++; |
| 1129 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1130 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1131 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1132 | btrfs_release_path(root, path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1133 | if (cow_start != (u64)-1) { |
| 1134 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1135 | found_key.offset - 1, page_started, |
| 1136 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1137 | BUG_ON(ret); |
| 1138 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1139 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1140 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1141 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1142 | struct extent_map *em; |
| 1143 | struct extent_map_tree *em_tree; |
| 1144 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 1145 | em = alloc_extent_map(GFP_NOFS); |
| 1146 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1147 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1148 | em->len = num_bytes; |
| 1149 | em->block_len = num_bytes; |
| 1150 | em->block_start = disk_bytenr; |
| 1151 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1152 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1153 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1154 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1155 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1156 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1157 | if (ret != -EEXIST) { |
| 1158 | free_extent_map(em); |
| 1159 | break; |
| 1160 | } |
| 1161 | btrfs_drop_extent_cache(inode, em->start, |
| 1162 | em->start + em->len - 1, 0); |
| 1163 | } |
| 1164 | type = BTRFS_ORDERED_PREALLOC; |
| 1165 | } else { |
| 1166 | type = BTRFS_ORDERED_NOCOW; |
| 1167 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1168 | |
| 1169 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1170 | num_bytes, num_bytes, type); |
| 1171 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1172 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1173 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1174 | cur_offset, cur_offset + num_bytes - 1, |
| 1175 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1176 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1177 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1178 | cur_offset = extent_end; |
| 1179 | if (cur_offset > end) |
| 1180 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1181 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1182 | btrfs_release_path(root, path); |
| 1183 | |
| 1184 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1185 | cow_start = cur_offset; |
| 1186 | if (cow_start != (u64)-1) { |
| 1187 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1188 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1189 | BUG_ON(ret); |
| 1190 | } |
| 1191 | |
| 1192 | ret = btrfs_end_transaction(trans, root); |
| 1193 | BUG_ON(ret); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1194 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1195 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1196 | } |
| 1197 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1198 | /* |
| 1199 | * extent_io.c call back to do delayed allocation processing |
| 1200 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1201 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1202 | u64 start, u64 end, int *page_started, |
| 1203 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1204 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1205 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1206 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1207 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1208 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1209 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1210 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1211 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1212 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1213 | page_started, 0, nr_written); |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1214 | else if (!btrfs_test_opt(root, COMPRESS)) |
| 1215 | ret = cow_file_range(inode, locked_page, start, end, |
| 1216 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1217 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1218 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1219 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1220 | return ret; |
| 1221 | } |
| 1222 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1223 | static int btrfs_split_extent_hook(struct inode *inode, |
| 1224 | struct extent_state *orig, u64 split) |
| 1225 | { |
| 1226 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1227 | u64 size; |
| 1228 | |
| 1229 | if (!(orig->state & EXTENT_DELALLOC)) |
| 1230 | return 0; |
| 1231 | |
| 1232 | size = orig->end - orig->start + 1; |
| 1233 | if (size > root->fs_info->max_extent) { |
| 1234 | u64 num_extents; |
| 1235 | u64 new_size; |
| 1236 | |
| 1237 | new_size = orig->end - split + 1; |
| 1238 | num_extents = div64_u64(size + root->fs_info->max_extent - 1, |
| 1239 | root->fs_info->max_extent); |
| 1240 | |
| 1241 | /* |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1242 | * if we break a large extent up then leave oustanding_extents |
| 1243 | * be, since we've already accounted for the large extent. |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1244 | */ |
| 1245 | if (div64_u64(new_size + root->fs_info->max_extent - 1, |
| 1246 | root->fs_info->max_extent) < num_extents) |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1250 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1251 | BTRFS_I(inode)->outstanding_extents++; |
| 1252 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | /* |
| 1258 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1259 | * extents so we can keep track of new extents that are just merged onto old |
| 1260 | * extents, such as when we are doing sequential writes, so we can properly |
| 1261 | * account for the metadata space we'll need. |
| 1262 | */ |
| 1263 | static int btrfs_merge_extent_hook(struct inode *inode, |
| 1264 | struct extent_state *new, |
| 1265 | struct extent_state *other) |
| 1266 | { |
| 1267 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1268 | u64 new_size, old_size; |
| 1269 | u64 num_extents; |
| 1270 | |
| 1271 | /* not delalloc, ignore it */ |
| 1272 | if (!(other->state & EXTENT_DELALLOC)) |
| 1273 | return 0; |
| 1274 | |
| 1275 | old_size = other->end - other->start + 1; |
| 1276 | if (new->start < other->start) |
| 1277 | new_size = other->end - new->start + 1; |
| 1278 | else |
| 1279 | new_size = new->end - other->start + 1; |
| 1280 | |
| 1281 | /* we're not bigger than the max, unreserve the space and go */ |
| 1282 | if (new_size <= root->fs_info->max_extent) { |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1283 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1284 | BTRFS_I(inode)->outstanding_extents--; |
| 1285 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1286 | return 0; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * If we grew by another max_extent, just return, we want to keep that |
| 1291 | * reserved amount. |
| 1292 | */ |
| 1293 | num_extents = div64_u64(old_size + root->fs_info->max_extent - 1, |
| 1294 | root->fs_info->max_extent); |
| 1295 | if (div64_u64(new_size + root->fs_info->max_extent - 1, |
| 1296 | root->fs_info->max_extent) > num_extents) |
| 1297 | return 0; |
| 1298 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1299 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1300 | BTRFS_I(inode)->outstanding_extents--; |
| 1301 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1302 | |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1306 | /* |
| 1307 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1308 | * bytes in this file, and to maintain the list of inodes that |
| 1309 | * have pending delalloc work to be done. |
| 1310 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1311 | static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1312 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1313 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1314 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1315 | /* |
| 1316 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1317 | * but in this case, we are only testeing for the DELALLOC |
| 1318 | * bit, which is only set or cleared with irqs on |
| 1319 | */ |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1320 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1321 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1322 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1323 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1324 | BTRFS_I(inode)->outstanding_extents++; |
| 1325 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1326 | btrfs_delalloc_reserve_space(root, inode, end - start + 1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1327 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1328 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1329 | root->fs_info->delalloc_bytes += end - start + 1; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1330 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1331 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1332 | &root->fs_info->delalloc_inodes); |
| 1333 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1334 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1335 | } |
| 1336 | return 0; |
| 1337 | } |
| 1338 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1339 | /* |
| 1340 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1341 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1342 | static int btrfs_clear_bit_hook(struct inode *inode, |
| 1343 | struct extent_state *state, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1344 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1345 | /* |
| 1346 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1347 | * but in this case, we are only testeing for the DELALLOC |
| 1348 | * bit, which is only set or cleared with irqs on |
| 1349 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1350 | if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1351 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1352 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1353 | if (bits & EXTENT_DO_ACCOUNTING) { |
| 1354 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1355 | BTRFS_I(inode)->outstanding_extents--; |
| 1356 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
| 1357 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
| 1358 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1359 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1360 | spin_lock(&root->fs_info->delalloc_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1361 | if (state->end - state->start + 1 > |
| 1362 | root->fs_info->delalloc_bytes) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1363 | printk(KERN_INFO "btrfs warning: delalloc account " |
| 1364 | "%llu %llu\n", |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1365 | (unsigned long long) |
| 1366 | state->end - state->start + 1, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1367 | (unsigned long long) |
| 1368 | root->fs_info->delalloc_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1369 | btrfs_delalloc_free_space(root, inode, (u64)-1); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1370 | root->fs_info->delalloc_bytes = 0; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1371 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1372 | } else { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1373 | btrfs_delalloc_free_space(root, inode, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1374 | state->end - |
| 1375 | state->start + 1); |
| 1376 | root->fs_info->delalloc_bytes -= state->end - |
| 1377 | state->start + 1; |
| 1378 | BTRFS_I(inode)->delalloc_bytes -= state->end - |
| 1379 | state->start + 1; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1380 | } |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1381 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
| 1382 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1383 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1384 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1385 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1386 | } |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1390 | /* |
| 1391 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1392 | * we don't create bios that span stripes or chunks |
| 1393 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1394 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1395 | size_t size, struct bio *bio, |
| 1396 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1397 | { |
| 1398 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1399 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1400 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1401 | u64 length = 0; |
| 1402 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1403 | int ret; |
| 1404 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1405 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1406 | return 0; |
| 1407 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1408 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1409 | map_tree = &root->fs_info->mapping_tree; |
| 1410 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1411 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1412 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1413 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1414 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1415 | return 1; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1416 | return 0; |
| 1417 | } |
| 1418 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1419 | /* |
| 1420 | * in order to insert checksums into the metadata in large chunks, |
| 1421 | * we wait until bio submission time. All the pages in the bio are |
| 1422 | * checksummed and sums are attached onto the ordered extent record. |
| 1423 | * |
| 1424 | * At IO completion time the cums attached on the ordered extent record |
| 1425 | * are inserted into the btree |
| 1426 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1427 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1428 | struct bio *bio, int mirror_num, |
| 1429 | unsigned long bio_flags) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1430 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1431 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1432 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1433 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1434 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1435 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1436 | return 0; |
| 1437 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1438 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1439 | /* |
| 1440 | * in order to insert checksums into the metadata in large chunks, |
| 1441 | * we wait until bio submission time. All the pages in the bio are |
| 1442 | * checksummed and sums are attached onto the ordered extent record. |
| 1443 | * |
| 1444 | * At IO completion time the cums attached on the ordered extent record |
| 1445 | * are inserted into the btree |
| 1446 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1447 | 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] | 1448 | int mirror_num, unsigned long bio_flags) |
| 1449 | { |
| 1450 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1451 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1452 | } |
| 1453 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1454 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1455 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1456 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1457 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1458 | 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] | 1459 | int mirror_num, unsigned long bio_flags) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1460 | { |
| 1461 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1462 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1463 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1464 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1465 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1466 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1467 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 1468 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1469 | |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1470 | if (!(rw & (1 << BIO_RW))) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1471 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1472 | return btrfs_submit_compressed_read(inode, bio, |
| 1473 | mirror_num, bio_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1474 | } else if (!skip_sum) |
| 1475 | btrfs_lookup_bio_sums(root, inode, bio, NULL); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1476 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1477 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1478 | /* csum items have already been cloned */ |
| 1479 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1480 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1481 | /* we're doing a write, do the async checksumming */ |
| 1482 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1483 | inode, rw, bio, mirror_num, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1484 | bio_flags, __btrfs_submit_bio_start, |
| 1485 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1486 | } |
| 1487 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1488 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1489 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1490 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1491 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1492 | /* |
| 1493 | * given a list of ordered sums record them in the inode. This happens |
| 1494 | * at IO completion time based on sums calculated at bio submission time. |
| 1495 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1496 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1497 | struct inode *inode, u64 file_offset, |
| 1498 | struct list_head *list) |
| 1499 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1500 | struct btrfs_ordered_sum *sum; |
| 1501 | |
| 1502 | btrfs_set_trans_block_group(trans, inode); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1503 | |
| 1504 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1505 | btrfs_csum_file_blocks(trans, |
| 1506 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1507 | } |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1511 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end) |
| 1512 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1513 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1514 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1515 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
| 1516 | GFP_NOFS); |
| 1517 | } |
| 1518 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1519 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1520 | struct btrfs_writepage_fixup { |
| 1521 | struct page *page; |
| 1522 | struct btrfs_work work; |
| 1523 | }; |
| 1524 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1525 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1526 | { |
| 1527 | struct btrfs_writepage_fixup *fixup; |
| 1528 | struct btrfs_ordered_extent *ordered; |
| 1529 | struct page *page; |
| 1530 | struct inode *inode; |
| 1531 | u64 page_start; |
| 1532 | u64 page_end; |
| 1533 | |
| 1534 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1535 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1536 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1537 | lock_page(page); |
| 1538 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1539 | ClearPageChecked(page); |
| 1540 | goto out_page; |
| 1541 | } |
| 1542 | |
| 1543 | inode = page->mapping->host; |
| 1544 | page_start = page_offset(page); |
| 1545 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1546 | |
| 1547 | lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1548 | |
| 1549 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1550 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1551 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1552 | |
| 1553 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1554 | if (ordered) { |
| 1555 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, |
| 1556 | page_end, GFP_NOFS); |
| 1557 | unlock_page(page); |
| 1558 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1559 | goto again; |
| 1560 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1561 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1562 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1563 | ClearPageChecked(page); |
| 1564 | out: |
| 1565 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); |
| 1566 | out_page: |
| 1567 | unlock_page(page); |
| 1568 | page_cache_release(page); |
| 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | * There are a few paths in the higher layers of the kernel that directly |
| 1573 | * set the page dirty bit without asking the filesystem if it is a |
| 1574 | * good idea. This causes problems because we want to make sure COW |
| 1575 | * properly happens and the data=ordered rules are followed. |
| 1576 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1577 | * 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] | 1578 | * hasn't been properly setup for IO. We kick off an async process |
| 1579 | * to fix it up. The async helper will wait for ordered extents, set |
| 1580 | * the delalloc bit and make it safe to write the page. |
| 1581 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1582 | 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] | 1583 | { |
| 1584 | struct inode *inode = page->mapping->host; |
| 1585 | struct btrfs_writepage_fixup *fixup; |
| 1586 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1587 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1588 | /* this page is properly in the ordered list */ |
| 1589 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1590 | return 0; |
| 1591 | |
| 1592 | if (PageChecked(page)) |
| 1593 | return -EAGAIN; |
| 1594 | |
| 1595 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1596 | if (!fixup) |
| 1597 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1598 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1599 | SetPageChecked(page); |
| 1600 | page_cache_get(page); |
| 1601 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1602 | fixup->page = page; |
| 1603 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1604 | return -EAGAIN; |
| 1605 | } |
| 1606 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1607 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1608 | struct inode *inode, u64 file_pos, |
| 1609 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1610 | u64 num_bytes, u64 ram_bytes, |
| 1611 | u8 compression, u8 encryption, |
| 1612 | u16 other_encoding, int extent_type) |
| 1613 | { |
| 1614 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1615 | struct btrfs_file_extent_item *fi; |
| 1616 | struct btrfs_path *path; |
| 1617 | struct extent_buffer *leaf; |
| 1618 | struct btrfs_key ins; |
| 1619 | u64 hint; |
| 1620 | int ret; |
| 1621 | |
| 1622 | path = btrfs_alloc_path(); |
| 1623 | BUG_ON(!path); |
| 1624 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1625 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1626 | |
| 1627 | /* |
| 1628 | * we may be replacing one extent in the tree with another. |
| 1629 | * The new extent is pinned in the extent map, and we don't want |
| 1630 | * to drop it from the cache until it is completely in the btree. |
| 1631 | * |
| 1632 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1633 | * the caller is expected to unpin it and allow it to be merged |
| 1634 | * with the others. |
| 1635 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1636 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1637 | &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1638 | BUG_ON(ret); |
| 1639 | |
| 1640 | ins.objectid = inode->i_ino; |
| 1641 | ins.offset = file_pos; |
| 1642 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1643 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1644 | BUG_ON(ret); |
| 1645 | leaf = path->nodes[0]; |
| 1646 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1647 | struct btrfs_file_extent_item); |
| 1648 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1649 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1650 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1651 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1652 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1653 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1654 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1655 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1656 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1657 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1658 | |
| 1659 | btrfs_unlock_up_safe(path, 1); |
| 1660 | btrfs_set_lock_blocking(leaf); |
| 1661 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1662 | btrfs_mark_buffer_dirty(leaf); |
| 1663 | |
| 1664 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1665 | |
| 1666 | ins.objectid = disk_bytenr; |
| 1667 | ins.offset = disk_num_bytes; |
| 1668 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1669 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1670 | root->root_key.objectid, |
| 1671 | inode->i_ino, file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1672 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1673 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1674 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1675 | return 0; |
| 1676 | } |
| 1677 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1678 | /* |
| 1679 | * helper function for btrfs_finish_ordered_io, this |
| 1680 | * just reads in some of the csum leaves to prime them into ram |
| 1681 | * before we start the transaction. It limits the amount of btree |
| 1682 | * reads required while inside the transaction. |
| 1683 | */ |
| 1684 | static noinline void reada_csum(struct btrfs_root *root, |
| 1685 | struct btrfs_path *path, |
| 1686 | struct btrfs_ordered_extent *ordered_extent) |
| 1687 | { |
| 1688 | struct btrfs_ordered_sum *sum; |
| 1689 | u64 bytenr; |
| 1690 | |
| 1691 | sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum, |
| 1692 | list); |
| 1693 | bytenr = sum->sums[0].bytenr; |
| 1694 | |
| 1695 | /* |
| 1696 | * we don't care about the results, the point of this search is |
| 1697 | * just to get the btree leaves into ram |
| 1698 | */ |
| 1699 | btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0); |
| 1700 | } |
| 1701 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1702 | /* as ordered data IO finishes, this gets called so we can finish |
| 1703 | * an ordered extent if the range of bytes in the file it covers are |
| 1704 | * fully written. |
| 1705 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1706 | 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] | 1707 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1708 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1709 | struct btrfs_trans_handle *trans; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1710 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1711 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1712 | struct btrfs_path *path; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1713 | int compressed = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1714 | int ret; |
| 1715 | |
| 1716 | ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1717 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1718 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1719 | |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1720 | /* |
| 1721 | * before we join the transaction, try to do some of our IO. |
| 1722 | * This will limit the amount of IO that we have to do with |
| 1723 | * the transaction running. We're unlikely to need to do any |
| 1724 | * IO if the file extents are new, the disk_i_size checks |
| 1725 | * covers the most common case. |
| 1726 | */ |
| 1727 | if (start < BTRFS_I(inode)->disk_i_size) { |
| 1728 | path = btrfs_alloc_path(); |
| 1729 | if (path) { |
| 1730 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 1731 | inode->i_ino, |
| 1732 | start, 0); |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1733 | ordered_extent = btrfs_lookup_ordered_extent(inode, |
| 1734 | start); |
| 1735 | if (!list_empty(&ordered_extent->list)) { |
| 1736 | btrfs_release_path(root, path); |
| 1737 | reada_csum(root, path, ordered_extent); |
| 1738 | } |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1739 | btrfs_free_path(path); |
| 1740 | } |
| 1741 | } |
| 1742 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1743 | if (!ordered_extent) |
| 1744 | ordered_extent = btrfs_lookup_ordered_extent(inode, start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1745 | BUG_ON(!ordered_extent); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1746 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
| 1747 | BUG_ON(!list_empty(&ordered_extent->list)); |
| 1748 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1749 | if (!ret) { |
| 1750 | trans = btrfs_join_transaction(root, 1); |
| 1751 | ret = btrfs_update_inode(trans, root, inode); |
| 1752 | BUG_ON(ret); |
| 1753 | btrfs_end_transaction(trans, root); |
| 1754 | } |
| 1755 | goto out; |
| 1756 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1757 | |
| 1758 | lock_extent(io_tree, ordered_extent->file_offset, |
| 1759 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1760 | GFP_NOFS); |
| 1761 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1762 | trans = btrfs_join_transaction(root, 1); |
| 1763 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1764 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1765 | compressed = 1; |
| 1766 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 1767 | BUG_ON(compressed); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1768 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1769 | ordered_extent->file_offset, |
| 1770 | ordered_extent->file_offset + |
| 1771 | ordered_extent->len); |
| 1772 | BUG_ON(ret); |
| 1773 | } else { |
| 1774 | ret = insert_reserved_file_extent(trans, inode, |
| 1775 | ordered_extent->file_offset, |
| 1776 | ordered_extent->start, |
| 1777 | ordered_extent->disk_len, |
| 1778 | ordered_extent->len, |
| 1779 | ordered_extent->len, |
| 1780 | compressed, 0, 0, |
| 1781 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1782 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1783 | ordered_extent->file_offset, |
| 1784 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1785 | BUG_ON(ret); |
| 1786 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1787 | unlock_extent(io_tree, ordered_extent->file_offset, |
| 1788 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1789 | GFP_NOFS); |
| 1790 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1791 | &ordered_extent->list); |
| 1792 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1793 | /* this also removes the ordered extent from the tree */ |
| 1794 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1795 | ret = btrfs_update_inode(trans, root, inode); |
| 1796 | BUG_ON(ret); |
| 1797 | btrfs_end_transaction(trans, root); |
| 1798 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1799 | /* once for us */ |
| 1800 | btrfs_put_ordered_extent(ordered_extent); |
| 1801 | /* once for the tree */ |
| 1802 | btrfs_put_ordered_extent(ordered_extent); |
| 1803 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1804 | return 0; |
| 1805 | } |
| 1806 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1807 | 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] | 1808 | struct extent_state *state, int uptodate) |
| 1809 | { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1810 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1811 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1812 | } |
| 1813 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1814 | /* |
| 1815 | * When IO fails, either with EIO or csum verification fails, we |
| 1816 | * try other mirrors that might have a good copy of the data. This |
| 1817 | * io_failure_record is used to record state as we go through all the |
| 1818 | * mirrors. If another mirror has good data, the page is set up to date |
| 1819 | * and things continue. If a good mirror can't be found, the original |
| 1820 | * bio end_io callback is called to indicate things have failed. |
| 1821 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1822 | struct io_failure_record { |
| 1823 | struct page *page; |
| 1824 | u64 start; |
| 1825 | u64 len; |
| 1826 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1827 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1828 | int last_mirror; |
| 1829 | }; |
| 1830 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1831 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1832 | struct page *page, u64 start, u64 end, |
| 1833 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1834 | { |
| 1835 | struct io_failure_record *failrec = NULL; |
| 1836 | u64 private; |
| 1837 | struct extent_map *em; |
| 1838 | struct inode *inode = page->mapping->host; |
| 1839 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1840 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1841 | struct bio *bio; |
| 1842 | int num_copies; |
| 1843 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1844 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1845 | u64 logical; |
| 1846 | |
| 1847 | ret = get_state_private(failure_tree, start, &private); |
| 1848 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1849 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1850 | if (!failrec) |
| 1851 | return -ENOMEM; |
| 1852 | failrec->start = start; |
| 1853 | failrec->len = end - start + 1; |
| 1854 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1855 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1856 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1857 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1858 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1859 | if (em->start > start || em->start + em->len < start) { |
| 1860 | free_extent_map(em); |
| 1861 | em = NULL; |
| 1862 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1863 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1864 | |
| 1865 | if (!em || IS_ERR(em)) { |
| 1866 | kfree(failrec); |
| 1867 | return -EIO; |
| 1868 | } |
| 1869 | logical = start - em->start; |
| 1870 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1871 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1872 | logical = em->block_start; |
| 1873 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 1874 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1875 | failrec->logical = logical; |
| 1876 | free_extent_map(em); |
| 1877 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1878 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1879 | set_state_private(failure_tree, start, |
| 1880 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1881 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1882 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1883 | } |
| 1884 | num_copies = btrfs_num_copies( |
| 1885 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1886 | failrec->logical, failrec->len); |
| 1887 | failrec->last_mirror++; |
| 1888 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1889 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1890 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1891 | failrec->start, |
| 1892 | EXTENT_LOCKED); |
| 1893 | if (state && state->start != failrec->start) |
| 1894 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1895 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1896 | } |
| 1897 | if (!state || failrec->last_mirror > num_copies) { |
| 1898 | set_state_private(failure_tree, failrec->start, 0); |
| 1899 | clear_extent_bits(failure_tree, failrec->start, |
| 1900 | failrec->start + failrec->len - 1, |
| 1901 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1902 | kfree(failrec); |
| 1903 | return -EIO; |
| 1904 | } |
| 1905 | bio = bio_alloc(GFP_NOFS, 1); |
| 1906 | bio->bi_private = state; |
| 1907 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1908 | bio->bi_sector = failrec->logical >> 9; |
| 1909 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1910 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1911 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1912 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1913 | if (failed_bio->bi_rw & (1 << BIO_RW)) |
| 1914 | rw = WRITE; |
| 1915 | else |
| 1916 | rw = READ; |
| 1917 | |
| 1918 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1919 | failrec->last_mirror, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1920 | failrec->bio_flags); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1921 | return 0; |
| 1922 | } |
| 1923 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1924 | /* |
| 1925 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1926 | * to see if we need to process or clean up an io_failure_record |
| 1927 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1928 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1929 | { |
| 1930 | u64 private; |
| 1931 | u64 private_failure; |
| 1932 | struct io_failure_record *failure; |
| 1933 | int ret; |
| 1934 | |
| 1935 | private = 0; |
| 1936 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 1937 | (u64)-1, 1, EXTENT_DIRTY)) { |
| 1938 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1939 | start, &private_failure); |
| 1940 | if (ret == 0) { |
| 1941 | failure = (struct io_failure_record *)(unsigned long) |
| 1942 | private_failure; |
| 1943 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1944 | failure->start, 0); |
| 1945 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1946 | failure->start, |
| 1947 | failure->start + failure->len - 1, |
| 1948 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1949 | GFP_NOFS); |
| 1950 | kfree(failure); |
| 1951 | } |
| 1952 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1953 | return 0; |
| 1954 | } |
| 1955 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1956 | /* |
| 1957 | * when reads are done, we need to check csums to verify the data is correct |
| 1958 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1959 | * the io_failure_record routines to find good copies |
| 1960 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1961 | 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] | 1962 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1963 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1964 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1965 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1966 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1967 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1968 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1969 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1970 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1971 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1972 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1973 | if (PageChecked(page)) { |
| 1974 | ClearPageChecked(page); |
| 1975 | goto good; |
| 1976 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1977 | |
| 1978 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1979 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1980 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1981 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1982 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1983 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1984 | GFP_NOFS); |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1988 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1989 | private = state->private; |
| 1990 | ret = 0; |
| 1991 | } else { |
| 1992 | ret = get_state_private(io_tree, start, &private); |
| 1993 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1994 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1995 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1996 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1997 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1998 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1999 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2000 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2001 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2002 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2003 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2004 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2005 | /* if the io failure tree for this inode is non-empty, |
| 2006 | * check to see if we've recovered from a failed IO |
| 2007 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2008 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2009 | return 0; |
| 2010 | |
| 2011 | zeroit: |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 2012 | if (printk_ratelimit()) { |
| 2013 | printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " |
| 2014 | "private %llu\n", page->mapping->host->i_ino, |
| 2015 | (unsigned long long)start, csum, |
| 2016 | (unsigned long long)private); |
| 2017 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2018 | memset(kaddr + offset, 1, end - start + 1); |
| 2019 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2020 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2021 | if (private == 0) |
| 2022 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2023 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2024 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2025 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2026 | struct delayed_iput { |
| 2027 | struct list_head list; |
| 2028 | struct inode *inode; |
| 2029 | }; |
| 2030 | |
| 2031 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2032 | { |
| 2033 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2034 | struct delayed_iput *delayed; |
| 2035 | |
| 2036 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2037 | return; |
| 2038 | |
| 2039 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2040 | delayed->inode = inode; |
| 2041 | |
| 2042 | spin_lock(&fs_info->delayed_iput_lock); |
| 2043 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2044 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2045 | } |
| 2046 | |
| 2047 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2048 | { |
| 2049 | LIST_HEAD(list); |
| 2050 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2051 | struct delayed_iput *delayed; |
| 2052 | int empty; |
| 2053 | |
| 2054 | spin_lock(&fs_info->delayed_iput_lock); |
| 2055 | empty = list_empty(&fs_info->delayed_iputs); |
| 2056 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2057 | if (empty) |
| 2058 | return; |
| 2059 | |
| 2060 | down_read(&root->fs_info->cleanup_work_sem); |
| 2061 | spin_lock(&fs_info->delayed_iput_lock); |
| 2062 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2063 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2064 | |
| 2065 | while (!list_empty(&list)) { |
| 2066 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2067 | list_del(&delayed->list); |
| 2068 | iput(delayed->inode); |
| 2069 | kfree(delayed); |
| 2070 | } |
| 2071 | up_read(&root->fs_info->cleanup_work_sem); |
| 2072 | } |
| 2073 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2074 | /* |
| 2075 | * This creates an orphan entry for the given inode in case something goes |
| 2076 | * wrong in the middle of an unlink/truncate. |
| 2077 | */ |
| 2078 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2079 | { |
| 2080 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2081 | int ret = 0; |
| 2082 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2083 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2084 | |
| 2085 | /* already on the orphan list, we're good */ |
| 2086 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2087 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2088 | return 0; |
| 2089 | } |
| 2090 | |
| 2091 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2092 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2093 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2094 | |
| 2095 | /* |
| 2096 | * insert an orphan item to track this unlinked/truncated file |
| 2097 | */ |
| 2098 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 2099 | |
| 2100 | return ret; |
| 2101 | } |
| 2102 | |
| 2103 | /* |
| 2104 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2105 | * item for this particular inode. |
| 2106 | */ |
| 2107 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2108 | { |
| 2109 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2110 | int ret = 0; |
| 2111 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2112 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2113 | |
| 2114 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2115 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2116 | return 0; |
| 2117 | } |
| 2118 | |
| 2119 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2120 | if (!trans) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2121 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2122 | return 0; |
| 2123 | } |
| 2124 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2125 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2126 | |
| 2127 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 2128 | |
| 2129 | return ret; |
| 2130 | } |
| 2131 | |
| 2132 | /* |
| 2133 | * this cleans up any orphans that may be left on the list from the last use |
| 2134 | * of this root. |
| 2135 | */ |
| 2136 | void btrfs_orphan_cleanup(struct btrfs_root *root) |
| 2137 | { |
| 2138 | struct btrfs_path *path; |
| 2139 | struct extent_buffer *leaf; |
| 2140 | struct btrfs_item *item; |
| 2141 | struct btrfs_key key, found_key; |
| 2142 | struct btrfs_trans_handle *trans; |
| 2143 | struct inode *inode; |
| 2144 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2145 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2146 | if (!xchg(&root->clean_orphans, 0)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2147 | return; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2148 | |
| 2149 | path = btrfs_alloc_path(); |
| 2150 | BUG_ON(!path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2151 | path->reada = -1; |
| 2152 | |
| 2153 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2154 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2155 | key.offset = (u64)-1; |
| 2156 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2157 | while (1) { |
| 2158 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2159 | if (ret < 0) { |
| 2160 | printk(KERN_ERR "Error searching slot for orphan: %d" |
| 2161 | "\n", ret); |
| 2162 | break; |
| 2163 | } |
| 2164 | |
| 2165 | /* |
| 2166 | * if ret == 0 means we found what we were searching for, which |
| 2167 | * is weird, but possible, so only screw with path if we didnt |
| 2168 | * find the key and see if we have stuff that matches |
| 2169 | */ |
| 2170 | if (ret > 0) { |
| 2171 | if (path->slots[0] == 0) |
| 2172 | break; |
| 2173 | path->slots[0]--; |
| 2174 | } |
| 2175 | |
| 2176 | /* pull out the item */ |
| 2177 | leaf = path->nodes[0]; |
| 2178 | item = btrfs_item_nr(leaf, path->slots[0]); |
| 2179 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2180 | |
| 2181 | /* make sure the item matches what we want */ |
| 2182 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2183 | break; |
| 2184 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2185 | break; |
| 2186 | |
| 2187 | /* release the path since we're done with it */ |
| 2188 | btrfs_release_path(root, path); |
| 2189 | |
| 2190 | /* |
| 2191 | * this is where we are basically btrfs_lookup, without the |
| 2192 | * crossing root thing. we store the inode number in the |
| 2193 | * offset of the orphan item. |
| 2194 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2195 | found_key.objectid = found_key.offset; |
| 2196 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2197 | found_key.offset = 0; |
| 2198 | inode = btrfs_iget(root->fs_info->sb, &found_key, root); |
| 2199 | if (IS_ERR(inode)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2200 | break; |
| 2201 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2202 | /* |
| 2203 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2204 | * the proper thing when we hit it |
| 2205 | */ |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2206 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2207 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2208 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2209 | |
| 2210 | /* |
| 2211 | * if this is a bad inode, means we actually succeeded in |
| 2212 | * removing the inode, but not the orphan record, which means |
| 2213 | * we need to manually delete the orphan since iput will just |
| 2214 | * do a destroy_inode |
| 2215 | */ |
| 2216 | if (is_bad_inode(inode)) { |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2217 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2218 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2219 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2220 | iput(inode); |
| 2221 | continue; |
| 2222 | } |
| 2223 | |
| 2224 | /* if we have links, this was a truncate, lets do that */ |
| 2225 | if (inode->i_nlink) { |
| 2226 | nr_truncate++; |
| 2227 | btrfs_truncate(inode); |
| 2228 | } else { |
| 2229 | nr_unlink++; |
| 2230 | } |
| 2231 | |
| 2232 | /* this will do delete_inode and everything for us */ |
| 2233 | iput(inode); |
| 2234 | } |
| 2235 | |
| 2236 | if (nr_unlink) |
| 2237 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2238 | if (nr_truncate) |
| 2239 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
| 2240 | |
| 2241 | btrfs_free_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2242 | } |
| 2243 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2244 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2245 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2246 | * don't find any xattrs, we know there can't be any acls. |
| 2247 | * |
| 2248 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2249 | */ |
| 2250 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2251 | int slot, u64 objectid) |
| 2252 | { |
| 2253 | u32 nritems = btrfs_header_nritems(leaf); |
| 2254 | struct btrfs_key found_key; |
| 2255 | int scanned = 0; |
| 2256 | |
| 2257 | slot++; |
| 2258 | while (slot < nritems) { |
| 2259 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2260 | |
| 2261 | /* we found a different objectid, there must not be acls */ |
| 2262 | if (found_key.objectid != objectid) |
| 2263 | return 0; |
| 2264 | |
| 2265 | /* we found an xattr, assume we've got an acl */ |
| 2266 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2267 | return 1; |
| 2268 | |
| 2269 | /* |
| 2270 | * we found a key greater than an xattr key, there can't |
| 2271 | * be any acls later on |
| 2272 | */ |
| 2273 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2274 | return 0; |
| 2275 | |
| 2276 | slot++; |
| 2277 | scanned++; |
| 2278 | |
| 2279 | /* |
| 2280 | * it goes inode, inode backrefs, xattrs, extents, |
| 2281 | * so if there are a ton of hard links to an inode there can |
| 2282 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2283 | * this is just an optimization |
| 2284 | */ |
| 2285 | if (scanned >= 8) |
| 2286 | break; |
| 2287 | } |
| 2288 | /* we hit the end of the leaf before we found an xattr or |
| 2289 | * something larger than an xattr. We have to assume the inode |
| 2290 | * has acls |
| 2291 | */ |
| 2292 | return 1; |
| 2293 | } |
| 2294 | |
| 2295 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2296 | * read an inode from the btree into the in-memory inode |
| 2297 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2298 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2299 | { |
| 2300 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2301 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2302 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2303 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2304 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2305 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2306 | int maybe_acls; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2307 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2308 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2309 | int ret; |
| 2310 | |
| 2311 | path = btrfs_alloc_path(); |
| 2312 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2313 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2314 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2315 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2316 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2317 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2318 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2319 | leaf = path->nodes[0]; |
| 2320 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2321 | struct btrfs_inode_item); |
| 2322 | |
| 2323 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2324 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2325 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2326 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2327 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2328 | |
| 2329 | tspec = btrfs_inode_atime(inode_item); |
| 2330 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2331 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2332 | |
| 2333 | tspec = btrfs_inode_mtime(inode_item); |
| 2334 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2335 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2336 | |
| 2337 | tspec = btrfs_inode_ctime(inode_item); |
| 2338 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2339 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2340 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2341 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2342 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2343 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2344 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2345 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2346 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2347 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2348 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2349 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2350 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2351 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2352 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2353 | /* |
| 2354 | * try to precache a NULL acl entry for files that don't have |
| 2355 | * any xattrs or acls |
| 2356 | */ |
| 2357 | 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] | 2358 | if (!maybe_acls) |
| 2359 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2360 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2361 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2362 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2363 | btrfs_free_path(path); |
| 2364 | inode_item = NULL; |
| 2365 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2366 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2367 | case S_IFREG: |
| 2368 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2369 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2370 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2371 | inode->i_fop = &btrfs_file_operations; |
| 2372 | inode->i_op = &btrfs_file_inode_operations; |
| 2373 | break; |
| 2374 | case S_IFDIR: |
| 2375 | inode->i_fop = &btrfs_dir_file_operations; |
| 2376 | if (root == root->fs_info->tree_root) |
| 2377 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2378 | else |
| 2379 | inode->i_op = &btrfs_dir_inode_operations; |
| 2380 | break; |
| 2381 | case S_IFLNK: |
| 2382 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2383 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2384 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2385 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2386 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2387 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2388 | init_special_inode(inode, inode->i_mode, rdev); |
| 2389 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2390 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2391 | |
| 2392 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2393 | return; |
| 2394 | |
| 2395 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2396 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2397 | make_bad_inode(inode); |
| 2398 | } |
| 2399 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2400 | /* |
| 2401 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2402 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2403 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2404 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2405 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2406 | struct inode *inode) |
| 2407 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2408 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2409 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2410 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2411 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2412 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2413 | |
| 2414 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2415 | inode->i_atime.tv_sec); |
| 2416 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2417 | inode->i_atime.tv_nsec); |
| 2418 | |
| 2419 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2420 | inode->i_mtime.tv_sec); |
| 2421 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2422 | inode->i_mtime.tv_nsec); |
| 2423 | |
| 2424 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2425 | inode->i_ctime.tv_sec); |
| 2426 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2427 | inode->i_ctime.tv_nsec); |
| 2428 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2429 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2430 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2431 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2432 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2433 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2434 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2435 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2436 | } |
| 2437 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2438 | /* |
| 2439 | * copy everything in the in-memory inode into the btree. |
| 2440 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2441 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2442 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2443 | { |
| 2444 | struct btrfs_inode_item *inode_item; |
| 2445 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2446 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2447 | int ret; |
| 2448 | |
| 2449 | path = btrfs_alloc_path(); |
| 2450 | BUG_ON(!path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2451 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2452 | ret = btrfs_lookup_inode(trans, root, path, |
| 2453 | &BTRFS_I(inode)->location, 1); |
| 2454 | if (ret) { |
| 2455 | if (ret > 0) |
| 2456 | ret = -ENOENT; |
| 2457 | goto failed; |
| 2458 | } |
| 2459 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2460 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2461 | leaf = path->nodes[0]; |
| 2462 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2463 | struct btrfs_inode_item); |
| 2464 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2465 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2466 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2467 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2468 | ret = 0; |
| 2469 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2470 | btrfs_free_path(path); |
| 2471 | return ret; |
| 2472 | } |
| 2473 | |
| 2474 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2475 | /* |
| 2476 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2477 | * recovery code. It remove a link in a directory with a given name, and |
| 2478 | * also drops the back refs in the inode to the directory |
| 2479 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2480 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2481 | struct btrfs_root *root, |
| 2482 | struct inode *dir, struct inode *inode, |
| 2483 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2484 | { |
| 2485 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2486 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2487 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2488 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2489 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2490 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2491 | |
| 2492 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2493 | if (!path) { |
| 2494 | ret = -ENOMEM; |
| 2495 | goto err; |
| 2496 | } |
| 2497 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2498 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2499 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2500 | name, name_len, -1); |
| 2501 | if (IS_ERR(di)) { |
| 2502 | ret = PTR_ERR(di); |
| 2503 | goto err; |
| 2504 | } |
| 2505 | if (!di) { |
| 2506 | ret = -ENOENT; |
| 2507 | goto err; |
| 2508 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2509 | leaf = path->nodes[0]; |
| 2510 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2511 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2512 | if (ret) |
| 2513 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2514 | btrfs_release_path(root, path); |
| 2515 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2516 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2517 | inode->i_ino, |
| 2518 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2519 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2520 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2521 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2522 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2523 | goto err; |
| 2524 | } |
| 2525 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2526 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2527 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2528 | if (IS_ERR(di)) { |
| 2529 | ret = PTR_ERR(di); |
| 2530 | goto err; |
| 2531 | } |
| 2532 | if (!di) { |
| 2533 | ret = -ENOENT; |
| 2534 | goto err; |
| 2535 | } |
| 2536 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2537 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2538 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2539 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 2540 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2541 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2542 | |
| 2543 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2544 | dir, index); |
| 2545 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2546 | err: |
| 2547 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2548 | if (ret) |
| 2549 | goto out; |
| 2550 | |
| 2551 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2552 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2553 | btrfs_update_inode(trans, root, dir); |
| 2554 | btrfs_drop_nlink(inode); |
| 2555 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2556 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2557 | return ret; |
| 2558 | } |
| 2559 | |
| 2560 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2561 | { |
| 2562 | struct btrfs_root *root; |
| 2563 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2564 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2565 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2566 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2567 | |
| 2568 | root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2569 | |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2570 | /* |
| 2571 | * 5 items for unlink inode |
| 2572 | * 1 for orphan |
| 2573 | */ |
| 2574 | ret = btrfs_reserve_metadata_space(root, 6); |
| 2575 | if (ret) |
| 2576 | return ret; |
| 2577 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2578 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2579 | if (IS_ERR(trans)) { |
| 2580 | btrfs_unreserve_metadata_space(root, 6); |
| 2581 | return PTR_ERR(trans); |
| 2582 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2583 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2584 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2585 | |
| 2586 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2587 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2588 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2589 | dentry->d_name.name, dentry->d_name.len); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2590 | |
| 2591 | if (inode->i_nlink == 0) |
| 2592 | ret = btrfs_orphan_add(trans, inode); |
| 2593 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2594 | nr = trans->blocks_used; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2595 | |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2596 | btrfs_end_transaction_throttle(trans, root); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2597 | btrfs_unreserve_metadata_space(root, 6); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2598 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2599 | return ret; |
| 2600 | } |
| 2601 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2602 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2603 | struct btrfs_root *root, |
| 2604 | struct inode *dir, u64 objectid, |
| 2605 | const char *name, int name_len) |
| 2606 | { |
| 2607 | struct btrfs_path *path; |
| 2608 | struct extent_buffer *leaf; |
| 2609 | struct btrfs_dir_item *di; |
| 2610 | struct btrfs_key key; |
| 2611 | u64 index; |
| 2612 | int ret; |
| 2613 | |
| 2614 | path = btrfs_alloc_path(); |
| 2615 | if (!path) |
| 2616 | return -ENOMEM; |
| 2617 | |
| 2618 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2619 | name, name_len, -1); |
| 2620 | BUG_ON(!di || IS_ERR(di)); |
| 2621 | |
| 2622 | leaf = path->nodes[0]; |
| 2623 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2624 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2625 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2626 | BUG_ON(ret); |
| 2627 | btrfs_release_path(root, path); |
| 2628 | |
| 2629 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2630 | objectid, root->root_key.objectid, |
| 2631 | dir->i_ino, &index, name, name_len); |
| 2632 | if (ret < 0) { |
| 2633 | BUG_ON(ret != -ENOENT); |
| 2634 | di = btrfs_search_dir_index_item(root, path, dir->i_ino, |
| 2635 | name, name_len); |
| 2636 | BUG_ON(!di || IS_ERR(di)); |
| 2637 | |
| 2638 | leaf = path->nodes[0]; |
| 2639 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2640 | btrfs_release_path(root, path); |
| 2641 | index = key.offset; |
| 2642 | } |
| 2643 | |
| 2644 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 2645 | index, name, name_len, -1); |
| 2646 | BUG_ON(!di || IS_ERR(di)); |
| 2647 | |
| 2648 | leaf = path->nodes[0]; |
| 2649 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2650 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2651 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2652 | BUG_ON(ret); |
| 2653 | btrfs_release_path(root, path); |
| 2654 | |
| 2655 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2656 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2657 | ret = btrfs_update_inode(trans, root, dir); |
| 2658 | BUG_ON(ret); |
| 2659 | dir->i_sb->s_dirt = 1; |
| 2660 | |
| 2661 | btrfs_free_path(path); |
| 2662 | return 0; |
| 2663 | } |
| 2664 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2665 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 2666 | { |
| 2667 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2668 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2669 | int ret; |
| 2670 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2671 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2672 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2673 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 2674 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2675 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 2676 | return -ENOTEMPTY; |
| 2677 | |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2678 | ret = btrfs_reserve_metadata_space(root, 5); |
| 2679 | if (ret) |
| 2680 | return ret; |
| 2681 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2682 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2683 | if (IS_ERR(trans)) { |
| 2684 | btrfs_unreserve_metadata_space(root, 5); |
| 2685 | return PTR_ERR(trans); |
| 2686 | } |
| 2687 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2688 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2689 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2690 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 2691 | err = btrfs_unlink_subvol(trans, root, dir, |
| 2692 | BTRFS_I(inode)->location.objectid, |
| 2693 | dentry->d_name.name, |
| 2694 | dentry->d_name.len); |
| 2695 | goto out; |
| 2696 | } |
| 2697 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2698 | err = btrfs_orphan_add(trans, inode); |
| 2699 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2700 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2701 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2702 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2703 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2704 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2705 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2706 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2707 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2708 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2709 | ret = btrfs_end_transaction_throttle(trans, root); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2710 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2711 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2712 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2713 | if (ret && !err) |
| 2714 | err = ret; |
| 2715 | return err; |
| 2716 | } |
| 2717 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2718 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2719 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2720 | * when truncating bytes in a file, it is possible to avoid reading |
| 2721 | * the leaves that contain only checksum items. This can be the |
| 2722 | * majority of the IO required to delete a large file, but it must |
| 2723 | * be done carefully. |
| 2724 | * |
| 2725 | * The keys in the level just above the leaves are checked to make sure |
| 2726 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 2727 | * after the new size. |
| 2728 | * |
| 2729 | * Then the key for the next leaf is checked to make sure it also has |
| 2730 | * a checksum item for the same file. If it does, we know our target leaf |
| 2731 | * contains only checksum items, and it can be safely freed without reading |
| 2732 | * it. |
| 2733 | * |
| 2734 | * This is just an optimization targeted at large files. It may do |
| 2735 | * nothing. It will return 0 unless things went badly. |
| 2736 | */ |
| 2737 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 2738 | struct btrfs_root *root, |
| 2739 | struct btrfs_path *path, |
| 2740 | struct inode *inode, u64 new_size) |
| 2741 | { |
| 2742 | struct btrfs_key key; |
| 2743 | int ret; |
| 2744 | int nritems; |
| 2745 | struct btrfs_key found_key; |
| 2746 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2747 | struct btrfs_leaf_ref *ref; |
| 2748 | u64 leaf_gen; |
| 2749 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2750 | |
| 2751 | path->lowest_level = 1; |
| 2752 | key.objectid = inode->i_ino; |
| 2753 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 2754 | key.offset = new_size; |
| 2755 | again: |
| 2756 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2757 | if (ret < 0) |
| 2758 | goto out; |
| 2759 | |
| 2760 | if (path->nodes[1] == NULL) { |
| 2761 | ret = 0; |
| 2762 | goto out; |
| 2763 | } |
| 2764 | ret = 0; |
| 2765 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 2766 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 2767 | |
| 2768 | if (!nritems) |
| 2769 | goto out; |
| 2770 | |
| 2771 | if (path->slots[1] >= nritems) |
| 2772 | goto next_node; |
| 2773 | |
| 2774 | /* did we find a key greater than anything we want to delete? */ |
| 2775 | if (found_key.objectid > inode->i_ino || |
| 2776 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 2777 | goto out; |
| 2778 | |
| 2779 | /* we check the next key in the node to make sure the leave contains |
| 2780 | * only checksum items. This comparison doesn't work if our |
| 2781 | * leaf is the last one in the node |
| 2782 | */ |
| 2783 | if (path->slots[1] + 1 >= nritems) { |
| 2784 | next_node: |
| 2785 | /* search forward from the last key in the node, this |
| 2786 | * will bring us into the next node in the tree |
| 2787 | */ |
| 2788 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 2789 | |
| 2790 | /* unlikely, but we inc below, so check to be safe */ |
| 2791 | if (found_key.offset == (u64)-1) |
| 2792 | goto out; |
| 2793 | |
| 2794 | /* search_forward needs a path with locks held, do the |
| 2795 | * search again for the original key. It is possible |
| 2796 | * this will race with a balance and return a path that |
| 2797 | * we could modify, but this drop is just an optimization |
| 2798 | * and is allowed to miss some leaves. |
| 2799 | */ |
| 2800 | btrfs_release_path(root, path); |
| 2801 | found_key.offset++; |
| 2802 | |
| 2803 | /* setup a max key for search_forward */ |
| 2804 | other_key.offset = (u64)-1; |
| 2805 | other_key.type = key.type; |
| 2806 | other_key.objectid = key.objectid; |
| 2807 | |
| 2808 | path->keep_locks = 1; |
| 2809 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 2810 | path, 0, 0); |
| 2811 | path->keep_locks = 0; |
| 2812 | if (ret || found_key.objectid != key.objectid || |
| 2813 | found_key.type != key.type) { |
| 2814 | ret = 0; |
| 2815 | goto out; |
| 2816 | } |
| 2817 | |
| 2818 | key.offset = found_key.offset; |
| 2819 | btrfs_release_path(root, path); |
| 2820 | cond_resched(); |
| 2821 | goto again; |
| 2822 | } |
| 2823 | |
| 2824 | /* we know there's one more slot after us in the tree, |
| 2825 | * read that key so we can verify it is also a checksum item |
| 2826 | */ |
| 2827 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 2828 | |
| 2829 | if (found_key.objectid < inode->i_ino) |
| 2830 | goto next_key; |
| 2831 | |
| 2832 | if (found_key.type != key.type || found_key.offset < new_size) |
| 2833 | goto next_key; |
| 2834 | |
| 2835 | /* |
| 2836 | * if the key for the next leaf isn't a csum key from this objectid, |
| 2837 | * we can't be sure there aren't good items inside this leaf. |
| 2838 | * Bail out |
| 2839 | */ |
| 2840 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 2841 | goto out; |
| 2842 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2843 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 2844 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2845 | /* |
| 2846 | * it is safe to delete this leaf, it contains only |
| 2847 | * csum items from this inode at an offset >= new_size |
| 2848 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2849 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2850 | BUG_ON(ret); |
| 2851 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2852 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 2853 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 2854 | if (ref) { |
| 2855 | ref->root_gen = root->root_key.offset; |
| 2856 | ref->bytenr = leaf_start; |
| 2857 | ref->owner = 0; |
| 2858 | ref->generation = leaf_gen; |
| 2859 | ref->nritems = 0; |
| 2860 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2861 | btrfs_sort_leaf_ref(ref); |
| 2862 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2863 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 2864 | WARN_ON(ret); |
| 2865 | btrfs_free_leaf_ref(root, ref); |
| 2866 | } else { |
| 2867 | WARN_ON(1); |
| 2868 | } |
| 2869 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2870 | next_key: |
| 2871 | btrfs_release_path(root, path); |
| 2872 | |
| 2873 | if (other_key.objectid == inode->i_ino && |
| 2874 | other_key.type == key.type && other_key.offset > key.offset) { |
| 2875 | key.offset = other_key.offset; |
| 2876 | cond_resched(); |
| 2877 | goto again; |
| 2878 | } |
| 2879 | ret = 0; |
| 2880 | out: |
| 2881 | /* fixup any changes we've made to the path */ |
| 2882 | path->lowest_level = 0; |
| 2883 | path->keep_locks = 0; |
| 2884 | btrfs_release_path(root, path); |
| 2885 | return ret; |
| 2886 | } |
| 2887 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2888 | #endif |
| 2889 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2890 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2891 | * this can truncate away extent items, csum items and directory items. |
| 2892 | * 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] | 2893 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2894 | * |
| 2895 | * csum items that cross the new i_size are truncated to the new size |
| 2896 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2897 | * |
| 2898 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 2899 | * 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] | 2900 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2901 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 2902 | struct btrfs_root *root, |
| 2903 | struct inode *inode, |
| 2904 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2905 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2906 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2907 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2908 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2909 | struct btrfs_key key; |
| 2910 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2911 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2912 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2913 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2914 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2915 | u64 mask = root->sectorsize - 1; |
| 2916 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2917 | int found_extent; |
| 2918 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2919 | int pending_del_nr = 0; |
| 2920 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2921 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2922 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2923 | int ret; |
| 2924 | int err = 0; |
| 2925 | |
| 2926 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2927 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2928 | if (root->ref_cows) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2929 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2930 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2931 | path = btrfs_alloc_path(); |
| 2932 | BUG_ON(!path); |
Julia Lawall | 33c17ad | 2009-07-22 16:49:01 -0400 | [diff] [blame] | 2933 | path->reada = -1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2934 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2935 | key.objectid = inode->i_ino; |
| 2936 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2937 | key.type = (u8)-1; |
| 2938 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2939 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2940 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2941 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2942 | if (ret < 0) { |
| 2943 | err = ret; |
| 2944 | goto out; |
| 2945 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2946 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2947 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2948 | /* there are no items in the tree for us to truncate, we're |
| 2949 | * done |
| 2950 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2951 | if (path->slots[0] == 0) |
| 2952 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2953 | path->slots[0]--; |
| 2954 | } |
| 2955 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2956 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2957 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2958 | leaf = path->nodes[0]; |
| 2959 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2960 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2961 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2962 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2963 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2964 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2965 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2966 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2967 | break; |
| 2968 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2969 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2970 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2971 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2972 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2973 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2974 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 2975 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 2976 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 2977 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2978 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2979 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2980 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2981 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2982 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2983 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2984 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 2985 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2986 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2987 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2988 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2989 | } else { |
| 2990 | if (item_end < new_size) |
| 2991 | break; |
| 2992 | if (found_key.offset >= new_size) |
| 2993 | del_item = 1; |
| 2994 | else |
| 2995 | del_item = 0; |
| 2996 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2997 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2998 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2999 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3000 | goto delete; |
| 3001 | |
| 3002 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3003 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3004 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3005 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3006 | u64 orig_num_bytes = |
| 3007 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3008 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3009 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3010 | extent_num_bytes = extent_num_bytes & |
| 3011 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3012 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3013 | extent_num_bytes); |
| 3014 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3015 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3016 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3017 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3018 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3019 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3020 | extent_num_bytes = |
| 3021 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3022 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3023 | extent_offset = found_key.offset - |
| 3024 | btrfs_file_extent_offset(leaf, fi); |
| 3025 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3026 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3027 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3028 | if (extent_start != 0) { |
| 3029 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3030 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3031 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3032 | } |
| 3033 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3034 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3035 | /* |
| 3036 | * we can't truncate inline items that have had |
| 3037 | * special encodings |
| 3038 | */ |
| 3039 | if (!del_item && |
| 3040 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3041 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3042 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3043 | u32 size = new_size - found_key.offset; |
| 3044 | |
| 3045 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3046 | inode_sub_bytes(inode, item_end + 1 - |
| 3047 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3048 | } |
| 3049 | size = |
| 3050 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3051 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3052 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3053 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3054 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3055 | inode_sub_bytes(inode, item_end + 1 - |
| 3056 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3057 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3058 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3059 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3060 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3061 | if (!pending_del_nr) { |
| 3062 | /* no pending yet, add ourselves */ |
| 3063 | pending_del_slot = path->slots[0]; |
| 3064 | pending_del_nr = 1; |
| 3065 | } else if (pending_del_nr && |
| 3066 | path->slots[0] + 1 == pending_del_slot) { |
| 3067 | /* hop on the pending chunk */ |
| 3068 | pending_del_nr++; |
| 3069 | pending_del_slot = path->slots[0]; |
| 3070 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3071 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3072 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3073 | } else { |
| 3074 | break; |
| 3075 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3076 | if (found_extent && root->ref_cows) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3077 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3078 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3079 | extent_num_bytes, 0, |
| 3080 | btrfs_header_owner(leaf), |
| 3081 | inode->i_ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3082 | BUG_ON(ret); |
| 3083 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3084 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3085 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3086 | break; |
| 3087 | |
| 3088 | if (path->slots[0] == 0 || |
| 3089 | path->slots[0] != pending_del_slot) { |
| 3090 | if (root->ref_cows) { |
| 3091 | err = -EAGAIN; |
| 3092 | goto out; |
| 3093 | } |
| 3094 | if (pending_del_nr) { |
| 3095 | ret = btrfs_del_items(trans, root, path, |
| 3096 | pending_del_slot, |
| 3097 | pending_del_nr); |
| 3098 | BUG_ON(ret); |
| 3099 | pending_del_nr = 0; |
| 3100 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3101 | btrfs_release_path(root, path); |
| 3102 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3103 | } else { |
| 3104 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3105 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3106 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3107 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3108 | if (pending_del_nr) { |
| 3109 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3110 | pending_del_nr); |
| 3111 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3112 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3113 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3114 | } |
| 3115 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3116 | /* |
| 3117 | * taken from block_truncate_page, but does cow as it zeros out |
| 3118 | * any bytes left in the last page in the file. |
| 3119 | */ |
| 3120 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3121 | { |
| 3122 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3123 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3124 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3125 | struct btrfs_ordered_extent *ordered; |
| 3126 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3127 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3128 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3129 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3130 | struct page *page; |
| 3131 | int ret = 0; |
| 3132 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3133 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3134 | |
| 3135 | if ((offset & (blocksize - 1)) == 0) |
| 3136 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3137 | ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE); |
| 3138 | if (ret) |
| 3139 | goto out; |
| 3140 | |
| 3141 | ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1); |
| 3142 | if (ret) |
| 3143 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3144 | |
| 3145 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3146 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3147 | page = grab_cache_page(mapping, index); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3148 | if (!page) { |
| 3149 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
| 3150 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3151 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3152 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3153 | |
| 3154 | page_start = page_offset(page); |
| 3155 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3156 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3157 | if (!PageUptodate(page)) { |
| 3158 | ret = btrfs_readpage(NULL, page); |
| 3159 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3160 | if (page->mapping != mapping) { |
| 3161 | unlock_page(page); |
| 3162 | page_cache_release(page); |
| 3163 | goto again; |
| 3164 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3165 | if (!PageUptodate(page)) { |
| 3166 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3167 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3168 | } |
| 3169 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3170 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3171 | |
| 3172 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3173 | set_page_extent_mapped(page); |
| 3174 | |
| 3175 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3176 | if (ordered) { |
| 3177 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3178 | unlock_page(page); |
| 3179 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3180 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3181 | btrfs_put_ordered_extent(ordered); |
| 3182 | goto again; |
| 3183 | } |
| 3184 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3185 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 3186 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
| 3187 | GFP_NOFS); |
| 3188 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3189 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end); |
| 3190 | if (ret) { |
| 3191 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3192 | goto out_unlock; |
| 3193 | } |
| 3194 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3195 | ret = 0; |
| 3196 | if (offset != PAGE_CACHE_SIZE) { |
| 3197 | kaddr = kmap(page); |
| 3198 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3199 | flush_dcache_page(page); |
| 3200 | kunmap(page); |
| 3201 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3202 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3203 | set_page_dirty(page); |
| 3204 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3205 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3206 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3207 | if (ret) |
| 3208 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
| 3209 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3210 | unlock_page(page); |
| 3211 | page_cache_release(page); |
| 3212 | out: |
| 3213 | return ret; |
| 3214 | } |
| 3215 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3216 | int btrfs_cont_expand(struct inode *inode, loff_t size) |
| 3217 | { |
| 3218 | struct btrfs_trans_handle *trans; |
| 3219 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3220 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3221 | struct extent_map *em; |
| 3222 | u64 mask = root->sectorsize - 1; |
| 3223 | u64 hole_start = (inode->i_size + mask) & ~mask; |
| 3224 | u64 block_end = (size + mask) & ~mask; |
| 3225 | u64 last_byte; |
| 3226 | u64 cur_offset; |
| 3227 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3228 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3229 | |
| 3230 | if (size <= hole_start) |
| 3231 | return 0; |
| 3232 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3233 | while (1) { |
| 3234 | struct btrfs_ordered_extent *ordered; |
| 3235 | btrfs_wait_ordered_range(inode, hole_start, |
| 3236 | block_end - hole_start); |
| 3237 | lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3238 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3239 | if (!ordered) |
| 3240 | break; |
| 3241 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3242 | btrfs_put_ordered_extent(ordered); |
| 3243 | } |
| 3244 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3245 | cur_offset = hole_start; |
| 3246 | while (1) { |
| 3247 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3248 | block_end - cur_offset, 0); |
| 3249 | BUG_ON(IS_ERR(em) || !em); |
| 3250 | last_byte = min(extent_map_end(em), block_end); |
| 3251 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3252 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3253 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3254 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3255 | |
| 3256 | err = btrfs_reserve_metadata_space(root, 2); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3257 | if (err) |
| 3258 | break; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3259 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3260 | trans = btrfs_start_transaction(root, 1); |
| 3261 | btrfs_set_trans_block_group(trans, inode); |
| 3262 | |
| 3263 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3264 | cur_offset + hole_size, |
| 3265 | &hint_byte, 1); |
| 3266 | BUG_ON(err); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3267 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3268 | err = btrfs_insert_file_extent(trans, root, |
| 3269 | inode->i_ino, cur_offset, 0, |
| 3270 | 0, hole_size, 0, hole_size, |
| 3271 | 0, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3272 | BUG_ON(err); |
| 3273 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3274 | btrfs_drop_extent_cache(inode, hole_start, |
| 3275 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3276 | |
| 3277 | btrfs_end_transaction(trans, root); |
| 3278 | btrfs_unreserve_metadata_space(root, 2); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3279 | } |
| 3280 | free_extent_map(em); |
| 3281 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3282 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3283 | break; |
| 3284 | } |
| 3285 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3286 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3287 | return err; |
| 3288 | } |
| 3289 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3290 | static int btrfs_setattr_size(struct inode *inode, struct iattr *attr) |
| 3291 | { |
| 3292 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3293 | struct btrfs_trans_handle *trans; |
| 3294 | unsigned long nr; |
| 3295 | int ret; |
| 3296 | |
| 3297 | if (attr->ia_size == inode->i_size) |
| 3298 | return 0; |
| 3299 | |
| 3300 | if (attr->ia_size > inode->i_size) { |
| 3301 | unsigned long limit; |
| 3302 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
| 3303 | if (attr->ia_size > inode->i_sb->s_maxbytes) |
| 3304 | return -EFBIG; |
| 3305 | if (limit != RLIM_INFINITY && attr->ia_size > limit) { |
| 3306 | send_sig(SIGXFSZ, current, 0); |
| 3307 | return -EFBIG; |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | ret = btrfs_reserve_metadata_space(root, 1); |
| 3312 | if (ret) |
| 3313 | return ret; |
| 3314 | |
| 3315 | trans = btrfs_start_transaction(root, 1); |
| 3316 | btrfs_set_trans_block_group(trans, inode); |
| 3317 | |
| 3318 | ret = btrfs_orphan_add(trans, inode); |
| 3319 | BUG_ON(ret); |
| 3320 | |
| 3321 | nr = trans->blocks_used; |
| 3322 | btrfs_end_transaction(trans, root); |
| 3323 | btrfs_unreserve_metadata_space(root, 1); |
| 3324 | btrfs_btree_balance_dirty(root, nr); |
| 3325 | |
| 3326 | if (attr->ia_size > inode->i_size) { |
| 3327 | ret = btrfs_cont_expand(inode, attr->ia_size); |
| 3328 | if (ret) { |
| 3329 | btrfs_truncate(inode); |
| 3330 | return ret; |
| 3331 | } |
| 3332 | |
| 3333 | i_size_write(inode, attr->ia_size); |
| 3334 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
| 3335 | |
| 3336 | trans = btrfs_start_transaction(root, 1); |
| 3337 | btrfs_set_trans_block_group(trans, inode); |
| 3338 | |
| 3339 | ret = btrfs_update_inode(trans, root, inode); |
| 3340 | BUG_ON(ret); |
| 3341 | if (inode->i_nlink > 0) { |
| 3342 | ret = btrfs_orphan_del(trans, inode); |
| 3343 | BUG_ON(ret); |
| 3344 | } |
| 3345 | nr = trans->blocks_used; |
| 3346 | btrfs_end_transaction(trans, root); |
| 3347 | btrfs_btree_balance_dirty(root, nr); |
| 3348 | return 0; |
| 3349 | } |
| 3350 | |
| 3351 | /* |
| 3352 | * We're truncating a file that used to have good data down to |
| 3353 | * zero. Make sure it gets into the ordered flush list so that |
| 3354 | * any new writes get down to disk quickly. |
| 3355 | */ |
| 3356 | if (attr->ia_size == 0) |
| 3357 | BTRFS_I(inode)->ordered_data_close = 1; |
| 3358 | |
| 3359 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3360 | ret = vmtruncate(inode, attr->ia_size); |
| 3361 | BUG_ON(ret); |
| 3362 | |
| 3363 | return 0; |
| 3364 | } |
| 3365 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3366 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3367 | { |
| 3368 | struct inode *inode = dentry->d_inode; |
| 3369 | int err; |
| 3370 | |
| 3371 | err = inode_change_ok(inode, attr); |
| 3372 | if (err) |
| 3373 | return err; |
| 3374 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3375 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3376 | err = btrfs_setattr_size(inode, attr); |
| 3377 | if (err) |
| 3378 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3379 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3380 | attr->ia_valid &= ~ATTR_SIZE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3381 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3382 | if (attr->ia_valid) |
| 3383 | err = inode_setattr(inode, attr); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3384 | |
| 3385 | if (!err && ((attr->ia_valid & ATTR_MODE))) |
| 3386 | err = btrfs_acl_chmod(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3387 | return err; |
| 3388 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3389 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3390 | void btrfs_delete_inode(struct inode *inode) |
| 3391 | { |
| 3392 | struct btrfs_trans_handle *trans; |
| 3393 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3394 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3395 | int ret; |
| 3396 | |
| 3397 | truncate_inode_pages(&inode->i_data, 0); |
| 3398 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3399 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3400 | goto no_delete; |
| 3401 | } |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3402 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3403 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3404 | if (root->fs_info->log_root_recovering) { |
| 3405 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3406 | goto no_delete; |
| 3407 | } |
| 3408 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3409 | if (inode->i_nlink > 0) { |
| 3410 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3411 | goto no_delete; |
| 3412 | } |
| 3413 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3414 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3415 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3416 | while (1) { |
| 3417 | trans = btrfs_start_transaction(root, 1); |
| 3418 | btrfs_set_trans_block_group(trans, inode); |
| 3419 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
| 3420 | |
| 3421 | if (ret != -EAGAIN) |
| 3422 | break; |
| 3423 | |
| 3424 | nr = trans->blocks_used; |
| 3425 | btrfs_end_transaction(trans, root); |
| 3426 | trans = NULL; |
| 3427 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3428 | } |
| 3429 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3430 | if (ret == 0) { |
| 3431 | ret = btrfs_orphan_del(trans, inode); |
| 3432 | BUG_ON(ret); |
| 3433 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3434 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3435 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3436 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3437 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3438 | no_delete: |
| 3439 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3440 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | /* |
| 3444 | * this returns the key found in the dir entry in the location pointer. |
| 3445 | * If no dir entries were found, location->objectid is 0. |
| 3446 | */ |
| 3447 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3448 | struct btrfs_key *location) |
| 3449 | { |
| 3450 | const char *name = dentry->d_name.name; |
| 3451 | int namelen = dentry->d_name.len; |
| 3452 | struct btrfs_dir_item *di; |
| 3453 | struct btrfs_path *path; |
| 3454 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3455 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3456 | |
| 3457 | path = btrfs_alloc_path(); |
| 3458 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3459 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3460 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 3461 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3462 | if (IS_ERR(di)) |
| 3463 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3464 | |
| 3465 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3466 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3467 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3468 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3469 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3470 | btrfs_free_path(path); |
| 3471 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3472 | out_err: |
| 3473 | location->objectid = 0; |
| 3474 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | /* |
| 3478 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3479 | * needs to be changed to reflect the root directory of the tree root. This |
| 3480 | * is kind of like crossing a mount point. |
| 3481 | */ |
| 3482 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3483 | struct inode *dir, |
| 3484 | struct dentry *dentry, |
| 3485 | struct btrfs_key *location, |
| 3486 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3487 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3488 | struct btrfs_path *path; |
| 3489 | struct btrfs_root *new_root; |
| 3490 | struct btrfs_root_ref *ref; |
| 3491 | struct extent_buffer *leaf; |
| 3492 | int ret; |
| 3493 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3494 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3495 | path = btrfs_alloc_path(); |
| 3496 | if (!path) { |
| 3497 | err = -ENOMEM; |
| 3498 | goto out; |
| 3499 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3500 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3501 | err = -ENOENT; |
| 3502 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3503 | BTRFS_I(dir)->root->root_key.objectid, |
| 3504 | location->objectid); |
| 3505 | if (ret) { |
| 3506 | if (ret < 0) |
| 3507 | err = ret; |
| 3508 | goto out; |
| 3509 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3510 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3511 | leaf = path->nodes[0]; |
| 3512 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 3513 | if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || |
| 3514 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3515 | goto out; |
| 3516 | |
| 3517 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3518 | (unsigned long)(ref + 1), |
| 3519 | dentry->d_name.len); |
| 3520 | if (ret) |
| 3521 | goto out; |
| 3522 | |
| 3523 | btrfs_release_path(root->fs_info->tree_root, path); |
| 3524 | |
| 3525 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3526 | if (IS_ERR(new_root)) { |
| 3527 | err = PTR_ERR(new_root); |
| 3528 | goto out; |
| 3529 | } |
| 3530 | |
| 3531 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3532 | err = -ENOENT; |
| 3533 | goto out; |
| 3534 | } |
| 3535 | |
| 3536 | *sub_root = new_root; |
| 3537 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3538 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3539 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3540 | err = 0; |
| 3541 | out: |
| 3542 | btrfs_free_path(path); |
| 3543 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3544 | } |
| 3545 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3546 | static void inode_tree_add(struct inode *inode) |
| 3547 | { |
| 3548 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3549 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3550 | struct rb_node **p; |
| 3551 | struct rb_node *parent; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3552 | again: |
| 3553 | p = &root->inode_tree.rb_node; |
| 3554 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3555 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3556 | if (hlist_unhashed(&inode->i_hash)) |
| 3557 | return; |
| 3558 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3559 | spin_lock(&root->inode_lock); |
| 3560 | while (*p) { |
| 3561 | parent = *p; |
| 3562 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3563 | |
| 3564 | if (inode->i_ino < entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3565 | p = &parent->rb_left; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3566 | else if (inode->i_ino > entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3567 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3568 | else { |
| 3569 | WARN_ON(!(entry->vfs_inode.i_state & |
| 3570 | (I_WILL_FREE | I_FREEING | I_CLEAR))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3571 | rb_erase(parent, &root->inode_tree); |
| 3572 | RB_CLEAR_NODE(parent); |
| 3573 | spin_unlock(&root->inode_lock); |
| 3574 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3575 | } |
| 3576 | } |
| 3577 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3578 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3579 | spin_unlock(&root->inode_lock); |
| 3580 | } |
| 3581 | |
| 3582 | static void inode_tree_del(struct inode *inode) |
| 3583 | { |
| 3584 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3585 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3586 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3587 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3588 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3589 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3590 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3591 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3592 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3593 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3594 | |
| 3595 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
| 3596 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3597 | spin_lock(&root->inode_lock); |
| 3598 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3599 | spin_unlock(&root->inode_lock); |
| 3600 | if (empty) |
| 3601 | btrfs_add_dead_root(root); |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 3606 | { |
| 3607 | struct rb_node *node; |
| 3608 | struct rb_node *prev; |
| 3609 | struct btrfs_inode *entry; |
| 3610 | struct inode *inode; |
| 3611 | u64 objectid = 0; |
| 3612 | |
| 3613 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3614 | |
| 3615 | spin_lock(&root->inode_lock); |
| 3616 | again: |
| 3617 | node = root->inode_tree.rb_node; |
| 3618 | prev = NULL; |
| 3619 | while (node) { |
| 3620 | prev = node; |
| 3621 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3622 | |
| 3623 | if (objectid < entry->vfs_inode.i_ino) |
| 3624 | node = node->rb_left; |
| 3625 | else if (objectid > entry->vfs_inode.i_ino) |
| 3626 | node = node->rb_right; |
| 3627 | else |
| 3628 | break; |
| 3629 | } |
| 3630 | if (!node) { |
| 3631 | while (prev) { |
| 3632 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
| 3633 | if (objectid <= entry->vfs_inode.i_ino) { |
| 3634 | node = prev; |
| 3635 | break; |
| 3636 | } |
| 3637 | prev = rb_next(prev); |
| 3638 | } |
| 3639 | } |
| 3640 | while (node) { |
| 3641 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3642 | objectid = entry->vfs_inode.i_ino + 1; |
| 3643 | inode = igrab(&entry->vfs_inode); |
| 3644 | if (inode) { |
| 3645 | spin_unlock(&root->inode_lock); |
| 3646 | if (atomic_read(&inode->i_count) > 1) |
| 3647 | d_prune_aliases(inode); |
| 3648 | /* |
| 3649 | * btrfs_drop_inode will remove it from |
| 3650 | * the inode cache when its usage count |
| 3651 | * hits zero. |
| 3652 | */ |
| 3653 | iput(inode); |
| 3654 | cond_resched(); |
| 3655 | spin_lock(&root->inode_lock); |
| 3656 | goto again; |
| 3657 | } |
| 3658 | |
| 3659 | if (cond_resched_lock(&root->inode_lock)) |
| 3660 | goto again; |
| 3661 | |
| 3662 | node = rb_next(node); |
| 3663 | } |
| 3664 | spin_unlock(&root->inode_lock); |
| 3665 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3666 | } |
| 3667 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3668 | static noinline void init_btrfs_i(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3669 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3670 | struct btrfs_inode *bi = BTRFS_I(inode); |
| 3671 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3672 | bi->generation = 0; |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 3673 | bi->sequence = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3674 | bi->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 3675 | bi->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3676 | bi->logged_trans = 0; |
| 3677 | bi->delalloc_bytes = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3678 | bi->reserved_bytes = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3679 | bi->disk_i_size = 0; |
| 3680 | bi->flags = 0; |
| 3681 | bi->index_cnt = (u64)-1; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3682 | bi->last_unlink_trans = 0; |
Chris Mason | 2757495 | 2009-05-14 13:10:02 -0400 | [diff] [blame] | 3683 | bi->ordered_data_close = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3684 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 3685 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3686 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3687 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
| 3688 | inode->i_mapping, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3689 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3690 | INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3691 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 3692 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3693 | mutex_init(&BTRFS_I(inode)->log_mutex); |
| 3694 | } |
| 3695 | |
| 3696 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 3697 | { |
| 3698 | struct btrfs_iget_args *args = p; |
| 3699 | inode->i_ino = args->ino; |
| 3700 | init_btrfs_i(inode); |
| 3701 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3702 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3703 | return 0; |
| 3704 | } |
| 3705 | |
| 3706 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 3707 | { |
| 3708 | struct btrfs_iget_args *args = opaque; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3709 | return args->ino == inode->i_ino && |
| 3710 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3711 | } |
| 3712 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3713 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 3714 | u64 objectid, |
| 3715 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3716 | { |
| 3717 | struct inode *inode; |
| 3718 | struct btrfs_iget_args args; |
| 3719 | args.ino = objectid; |
| 3720 | args.root = root; |
| 3721 | |
| 3722 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 3723 | btrfs_init_locked_inode, |
| 3724 | (void *)&args); |
| 3725 | return inode; |
| 3726 | } |
| 3727 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3728 | /* Get an inode object given its location and corresponding root. |
| 3729 | * Returns in *is_new if the inode was read from disk |
| 3730 | */ |
| 3731 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3732 | struct btrfs_root *root) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3733 | { |
| 3734 | struct inode *inode; |
| 3735 | |
| 3736 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 3737 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3738 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3739 | |
| 3740 | if (inode->i_state & I_NEW) { |
| 3741 | BTRFS_I(inode)->root = root; |
| 3742 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 3743 | btrfs_read_locked_inode(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3744 | |
| 3745 | inode_tree_add(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3746 | unlock_new_inode(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | return inode; |
| 3750 | } |
| 3751 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3752 | static struct inode *new_simple_dir(struct super_block *s, |
| 3753 | struct btrfs_key *key, |
| 3754 | struct btrfs_root *root) |
| 3755 | { |
| 3756 | struct inode *inode = new_inode(s); |
| 3757 | |
| 3758 | if (!inode) |
| 3759 | return ERR_PTR(-ENOMEM); |
| 3760 | |
| 3761 | init_btrfs_i(inode); |
| 3762 | |
| 3763 | BTRFS_I(inode)->root = root; |
| 3764 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 3765 | BTRFS_I(inode)->dummy_inode = 1; |
| 3766 | |
| 3767 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 3768 | inode->i_op = &simple_dir_inode_operations; |
| 3769 | inode->i_fop = &simple_dir_operations; |
| 3770 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 3771 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 3772 | |
| 3773 | return inode; |
| 3774 | } |
| 3775 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3776 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3777 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3778 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3779 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3780 | struct btrfs_root *sub_root = root; |
| 3781 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3782 | int index; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3783 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3784 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3785 | dentry->d_op = &btrfs_dentry_operations; |
| 3786 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3787 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 3788 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3789 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3790 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3791 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3792 | if (ret < 0) |
| 3793 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3794 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3795 | if (location.objectid == 0) |
| 3796 | return NULL; |
| 3797 | |
| 3798 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
| 3799 | inode = btrfs_iget(dir->i_sb, &location, root); |
Josef Bacik | 6c090a1 | 2010-01-15 20:08:22 +0000 | [diff] [blame] | 3800 | if (unlikely(root->clean_orphans) && |
| 3801 | !(inode->i_sb->s_flags & MS_RDONLY)) { |
| 3802 | down_read(&root->fs_info->cleanup_work_sem); |
| 3803 | btrfs_orphan_cleanup(root); |
| 3804 | up_read(&root->fs_info->cleanup_work_sem); |
| 3805 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3806 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3807 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3808 | |
| 3809 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 3810 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3811 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3812 | ret = fixup_tree_root_location(root, dir, dentry, |
| 3813 | &location, &sub_root); |
| 3814 | if (ret < 0) { |
| 3815 | if (ret != -ENOENT) |
| 3816 | inode = ERR_PTR(ret); |
| 3817 | else |
| 3818 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 3819 | } else { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3820 | inode = btrfs_iget(dir->i_sb, &location, sub_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3821 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3822 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 3823 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3824 | if (root != sub_root) { |
| 3825 | down_read(&root->fs_info->cleanup_work_sem); |
| 3826 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
| 3827 | btrfs_orphan_cleanup(sub_root); |
| 3828 | up_read(&root->fs_info->cleanup_work_sem); |
| 3829 | } |
| 3830 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3831 | return inode; |
| 3832 | } |
| 3833 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3834 | static int btrfs_dentry_delete(struct dentry *dentry) |
| 3835 | { |
| 3836 | struct btrfs_root *root; |
| 3837 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 3838 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 3839 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3840 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 3841 | if (dentry->d_inode) { |
| 3842 | root = BTRFS_I(dentry->d_inode)->root; |
| 3843 | if (btrfs_root_refs(&root->root_item) == 0) |
| 3844 | return 1; |
| 3845 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3846 | return 0; |
| 3847 | } |
| 3848 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3849 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 3850 | struct nameidata *nd) |
| 3851 | { |
| 3852 | struct inode *inode; |
| 3853 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3854 | inode = btrfs_lookup_dentry(dir, dentry); |
| 3855 | if (IS_ERR(inode)) |
| 3856 | return ERR_CAST(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3857 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3858 | return d_splice_alias(inode, dentry); |
| 3859 | } |
| 3860 | |
| 3861 | static unsigned char btrfs_filetype_table[] = { |
| 3862 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 3863 | }; |
| 3864 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 3865 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 3866 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3867 | { |
| 3868 | struct inode *inode = filp->f_dentry->d_inode; |
| 3869 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3870 | struct btrfs_item *item; |
| 3871 | struct btrfs_dir_item *di; |
| 3872 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3873 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3874 | struct btrfs_path *path; |
| 3875 | int ret; |
| 3876 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3877 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3878 | int slot; |
| 3879 | int advance; |
| 3880 | unsigned char d_type; |
| 3881 | int over = 0; |
| 3882 | u32 di_cur; |
| 3883 | u32 di_total; |
| 3884 | u32 di_len; |
| 3885 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3886 | char tmp_name[32]; |
| 3887 | char *name_ptr; |
| 3888 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3889 | |
| 3890 | /* FIXME, use a real flag for deciding about the key type */ |
| 3891 | if (root->fs_info->tree_root == root) |
| 3892 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3893 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3894 | /* special case for "." */ |
| 3895 | if (filp->f_pos == 0) { |
| 3896 | over = filldir(dirent, ".", 1, |
| 3897 | 1, inode->i_ino, |
| 3898 | DT_DIR); |
| 3899 | if (over) |
| 3900 | return 0; |
| 3901 | filp->f_pos = 1; |
| 3902 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3903 | /* special case for .., just use the back ref */ |
| 3904 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3905 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3906 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3907 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3908 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3909 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3910 | filp->f_pos = 2; |
| 3911 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3912 | path = btrfs_alloc_path(); |
| 3913 | path->reada = 2; |
| 3914 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3915 | btrfs_set_key_type(&key, key_type); |
| 3916 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3917 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3918 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3919 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3920 | if (ret < 0) |
| 3921 | goto err; |
| 3922 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3923 | |
| 3924 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3925 | leaf = path->nodes[0]; |
| 3926 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3927 | slot = path->slots[0]; |
| 3928 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3929 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3930 | ret = btrfs_next_leaf(root, path); |
| 3931 | if (ret) |
| 3932 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3933 | leaf = path->nodes[0]; |
| 3934 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3935 | slot = path->slots[0]; |
| 3936 | } else { |
| 3937 | slot++; |
| 3938 | path->slots[0]++; |
| 3939 | } |
| 3940 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3941 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3942 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3943 | item = btrfs_item_nr(leaf, slot); |
| 3944 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3945 | |
| 3946 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3947 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3948 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3949 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3950 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3951 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3952 | |
| 3953 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3954 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3955 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 3956 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3957 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3958 | |
| 3959 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3960 | struct btrfs_key location; |
| 3961 | |
| 3962 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3963 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3964 | name_ptr = tmp_name; |
| 3965 | } else { |
| 3966 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3967 | if (!name_ptr) { |
| 3968 | ret = -ENOMEM; |
| 3969 | goto err; |
| 3970 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3971 | } |
| 3972 | read_extent_buffer(leaf, name_ptr, |
| 3973 | (unsigned long)(di + 1), name_len); |
| 3974 | |
| 3975 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 3976 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3977 | |
| 3978 | /* is this a reference to our own snapshot? If so |
| 3979 | * skip it |
| 3980 | */ |
| 3981 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 3982 | location.objectid == root->root_key.objectid) { |
| 3983 | over = 0; |
| 3984 | goto skip; |
| 3985 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3986 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3987 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3988 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3989 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3990 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3991 | if (name_ptr != tmp_name) |
| 3992 | kfree(name_ptr); |
| 3993 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3994 | if (over) |
| 3995 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 3996 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3997 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3998 | di_cur += di_len; |
| 3999 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4000 | } |
| 4001 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4002 | |
| 4003 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4004 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4005 | /* |
| 4006 | * 32-bit glibc will use getdents64, but then strtol - |
| 4007 | * so the last number we can serve is this. |
| 4008 | */ |
| 4009 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4010 | else |
| 4011 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4012 | nopos: |
| 4013 | ret = 0; |
| 4014 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4015 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4016 | return ret; |
| 4017 | } |
| 4018 | |
| 4019 | int btrfs_write_inode(struct inode *inode, int wait) |
| 4020 | { |
| 4021 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4022 | struct btrfs_trans_handle *trans; |
| 4023 | int ret = 0; |
| 4024 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 4025 | if (root->fs_info->btree_inode == inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4026 | return 0; |
| 4027 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4028 | if (wait) { |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4029 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4030 | btrfs_set_trans_block_group(trans, inode); |
| 4031 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4032 | } |
| 4033 | return ret; |
| 4034 | } |
| 4035 | |
| 4036 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4037 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4038 | * inode changes. But, it is most likely to find the inode in cache. |
| 4039 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4040 | * to keep or drop this code. |
| 4041 | */ |
| 4042 | void btrfs_dirty_inode(struct inode *inode) |
| 4043 | { |
| 4044 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4045 | struct btrfs_trans_handle *trans; |
| 4046 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4047 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4048 | btrfs_set_trans_block_group(trans, inode); |
| 4049 | btrfs_update_inode(trans, root, inode); |
| 4050 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4051 | } |
| 4052 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4053 | /* |
| 4054 | * find the highest existing sequence number in a directory |
| 4055 | * and then set the in-memory index_cnt variable to reflect |
| 4056 | * free sequence numbers |
| 4057 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4058 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4059 | { |
| 4060 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4061 | struct btrfs_key key, found_key; |
| 4062 | struct btrfs_path *path; |
| 4063 | struct extent_buffer *leaf; |
| 4064 | int ret; |
| 4065 | |
| 4066 | key.objectid = inode->i_ino; |
| 4067 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4068 | key.offset = (u64)-1; |
| 4069 | |
| 4070 | path = btrfs_alloc_path(); |
| 4071 | if (!path) |
| 4072 | return -ENOMEM; |
| 4073 | |
| 4074 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4075 | if (ret < 0) |
| 4076 | goto out; |
| 4077 | /* FIXME: we should be able to handle this */ |
| 4078 | if (ret == 0) |
| 4079 | goto out; |
| 4080 | ret = 0; |
| 4081 | |
| 4082 | /* |
| 4083 | * MAGIC NUMBER EXPLANATION: |
| 4084 | * since we search a directory based on f_pos we have to start at 2 |
| 4085 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4086 | * else has to start at 2 |
| 4087 | */ |
| 4088 | if (path->slots[0] == 0) { |
| 4089 | BTRFS_I(inode)->index_cnt = 2; |
| 4090 | goto out; |
| 4091 | } |
| 4092 | |
| 4093 | path->slots[0]--; |
| 4094 | |
| 4095 | leaf = path->nodes[0]; |
| 4096 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4097 | |
| 4098 | if (found_key.objectid != inode->i_ino || |
| 4099 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4100 | BTRFS_I(inode)->index_cnt = 2; |
| 4101 | goto out; |
| 4102 | } |
| 4103 | |
| 4104 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4105 | out: |
| 4106 | btrfs_free_path(path); |
| 4107 | return ret; |
| 4108 | } |
| 4109 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4110 | /* |
| 4111 | * helper to find a free sequence number in a given directory. This current |
| 4112 | * code is very simple, later versions will do smarter things in the btree |
| 4113 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4114 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4115 | { |
| 4116 | int ret = 0; |
| 4117 | |
| 4118 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 4119 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4120 | if (ret) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4121 | return ret; |
| 4122 | } |
| 4123 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4124 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4125 | BTRFS_I(dir)->index_cnt++; |
| 4126 | |
| 4127 | return ret; |
| 4128 | } |
| 4129 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4130 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4131 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4132 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4133 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4134 | u64 ref_objectid, u64 objectid, |
| 4135 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4136 | { |
| 4137 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4138 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4139 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4140 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4141 | struct btrfs_inode_ref *ref; |
| 4142 | struct btrfs_key key[2]; |
| 4143 | u32 sizes[2]; |
| 4144 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4145 | int ret; |
| 4146 | int owner; |
| 4147 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4148 | path = btrfs_alloc_path(); |
| 4149 | BUG_ON(!path); |
| 4150 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4151 | inode = new_inode(root->fs_info->sb); |
| 4152 | if (!inode) |
| 4153 | return ERR_PTR(-ENOMEM); |
| 4154 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4155 | if (dir) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4156 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4157 | if (ret) { |
| 4158 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4159 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4160 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4161 | } |
| 4162 | /* |
| 4163 | * index_cnt is ignored for everything but a dir, |
| 4164 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4165 | * number |
| 4166 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4167 | init_btrfs_i(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4168 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4169 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4170 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4171 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4172 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4173 | if (mode & S_IFDIR) |
| 4174 | owner = 0; |
| 4175 | else |
| 4176 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4177 | BTRFS_I(inode)->block_group = |
| 4178 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4179 | |
| 4180 | key[0].objectid = objectid; |
| 4181 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4182 | key[0].offset = 0; |
| 4183 | |
| 4184 | key[1].objectid = objectid; |
| 4185 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4186 | key[1].offset = ref_objectid; |
| 4187 | |
| 4188 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4189 | sizes[1] = name_len + sizeof(*ref); |
| 4190 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4191 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4192 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4193 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4194 | goto fail; |
| 4195 | |
Chris Mason | 79683f2 | 2008-11-19 22:00:53 -0500 | [diff] [blame] | 4196 | inode->i_uid = current_fsuid(); |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 4197 | |
Chris Mason | 42f15d7 | 2009-02-06 11:35:57 -0500 | [diff] [blame] | 4198 | if (dir && (dir->i_mode & S_ISGID)) { |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 4199 | inode->i_gid = dir->i_gid; |
| 4200 | if (S_ISDIR(mode)) |
| 4201 | mode |= S_ISGID; |
| 4202 | } else |
| 4203 | inode->i_gid = current_fsgid(); |
| 4204 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4205 | inode->i_mode = mode; |
| 4206 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4207 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4208 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4209 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4210 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4211 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4212 | |
| 4213 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4214 | struct btrfs_inode_ref); |
| 4215 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4216 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4217 | ptr = (unsigned long)(ref + 1); |
| 4218 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4219 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4220 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4221 | btrfs_free_path(path); |
| 4222 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4223 | location = &BTRFS_I(inode)->location; |
| 4224 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4225 | location->offset = 0; |
| 4226 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4227 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4228 | btrfs_inherit_iflags(inode, dir); |
| 4229 | |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4230 | if ((mode & S_IFREG)) { |
| 4231 | if (btrfs_test_opt(root, NODATASUM)) |
| 4232 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 4233 | if (btrfs_test_opt(root, NODATACOW)) |
| 4234 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4235 | } |
| 4236 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4237 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4238 | inode_tree_add(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4239 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4240 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4241 | if (dir) |
| 4242 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4243 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4244 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4245 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4249 | { |
| 4250 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4251 | } |
| 4252 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4253 | /* |
| 4254 | * utility function to add 'inode' into 'parent_inode' with |
| 4255 | * a give name and a given sequence number. |
| 4256 | * if 'add_backref' is true, also insert a backref from the |
| 4257 | * inode to the parent directory. |
| 4258 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4259 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4260 | struct inode *parent_inode, struct inode *inode, |
| 4261 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4262 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4263 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4264 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4265 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4266 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4267 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4268 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4269 | } else { |
| 4270 | key.objectid = inode->i_ino; |
| 4271 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4272 | key.offset = 0; |
| 4273 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4274 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4275 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4276 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4277 | key.objectid, root->root_key.objectid, |
| 4278 | parent_inode->i_ino, |
| 4279 | index, name, name_len); |
| 4280 | } else if (add_backref) { |
| 4281 | ret = btrfs_insert_inode_ref(trans, root, |
| 4282 | name, name_len, inode->i_ino, |
| 4283 | parent_inode->i_ino, index); |
| 4284 | } |
| 4285 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4286 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4287 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4288 | parent_inode->i_ino, &key, |
| 4289 | btrfs_inode_type(inode), index); |
| 4290 | BUG_ON(ret); |
| 4291 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4292 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4293 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4294 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4295 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4296 | } |
| 4297 | return ret; |
| 4298 | } |
| 4299 | |
| 4300 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4301 | struct dentry *dentry, struct inode *inode, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4302 | int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4303 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4304 | int err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4305 | inode, dentry->d_name.name, |
| 4306 | dentry->d_name.len, backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4307 | if (!err) { |
| 4308 | d_instantiate(dentry, inode); |
| 4309 | return 0; |
| 4310 | } |
| 4311 | if (err > 0) |
| 4312 | err = -EEXIST; |
| 4313 | return err; |
| 4314 | } |
| 4315 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4316 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4317 | int mode, dev_t rdev) |
| 4318 | { |
| 4319 | struct btrfs_trans_handle *trans; |
| 4320 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4321 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4322 | int err; |
| 4323 | int drop_inode = 0; |
| 4324 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4325 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4326 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4327 | |
| 4328 | if (!new_valid_dev(rdev)) |
| 4329 | return -EINVAL; |
| 4330 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4331 | /* |
| 4332 | * 2 for inode item and ref |
| 4333 | * 2 for dir items |
| 4334 | * 1 for xattr if selinux is on |
| 4335 | */ |
| 4336 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4337 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4338 | return err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4339 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4340 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4341 | if (!trans) |
| 4342 | goto fail; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4343 | btrfs_set_trans_block_group(trans, dir); |
| 4344 | |
| 4345 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4346 | if (err) { |
| 4347 | err = -ENOSPC; |
| 4348 | goto out_unlock; |
| 4349 | } |
| 4350 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4351 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4352 | dentry->d_name.len, |
| 4353 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4354 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4355 | err = PTR_ERR(inode); |
| 4356 | if (IS_ERR(inode)) |
| 4357 | goto out_unlock; |
| 4358 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4359 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4360 | if (err) { |
| 4361 | drop_inode = 1; |
| 4362 | goto out_unlock; |
| 4363 | } |
| 4364 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4365 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4366 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4367 | if (err) |
| 4368 | drop_inode = 1; |
| 4369 | else { |
| 4370 | inode->i_op = &btrfs_special_inode_operations; |
| 4371 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4372 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4373 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4374 | btrfs_update_inode_block_group(trans, inode); |
| 4375 | btrfs_update_inode_block_group(trans, dir); |
| 4376 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4377 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4378 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4379 | fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4380 | btrfs_unreserve_metadata_space(root, 5); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4381 | if (drop_inode) { |
| 4382 | inode_dec_link_count(inode); |
| 4383 | iput(inode); |
| 4384 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4385 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4386 | return err; |
| 4387 | } |
| 4388 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4389 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4390 | int mode, struct nameidata *nd) |
| 4391 | { |
| 4392 | struct btrfs_trans_handle *trans; |
| 4393 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4394 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4395 | int err; |
| 4396 | int drop_inode = 0; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4397 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4398 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4399 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4400 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4401 | /* |
| 4402 | * 2 for inode item and ref |
| 4403 | * 2 for dir items |
| 4404 | * 1 for xattr if selinux is on |
| 4405 | */ |
| 4406 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4407 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4408 | return err; |
| 4409 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4410 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4411 | if (!trans) |
| 4412 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4413 | btrfs_set_trans_block_group(trans, dir); |
| 4414 | |
| 4415 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4416 | if (err) { |
| 4417 | err = -ENOSPC; |
| 4418 | goto out_unlock; |
| 4419 | } |
| 4420 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4421 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4422 | dentry->d_name.len, |
| 4423 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4424 | objectid, BTRFS_I(dir)->block_group, mode, |
| 4425 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4426 | err = PTR_ERR(inode); |
| 4427 | if (IS_ERR(inode)) |
| 4428 | goto out_unlock; |
| 4429 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4430 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4431 | if (err) { |
| 4432 | drop_inode = 1; |
| 4433 | goto out_unlock; |
| 4434 | } |
| 4435 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4436 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4437 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4438 | if (err) |
| 4439 | drop_inode = 1; |
| 4440 | else { |
| 4441 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4442 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4443 | inode->i_fop = &btrfs_file_operations; |
| 4444 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4445 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4446 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4447 | btrfs_update_inode_block_group(trans, inode); |
| 4448 | btrfs_update_inode_block_group(trans, dir); |
| 4449 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4450 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4451 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4452 | fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4453 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4454 | if (drop_inode) { |
| 4455 | inode_dec_link_count(inode); |
| 4456 | iput(inode); |
| 4457 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4458 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4459 | return err; |
| 4460 | } |
| 4461 | |
| 4462 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4463 | struct dentry *dentry) |
| 4464 | { |
| 4465 | struct btrfs_trans_handle *trans; |
| 4466 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4467 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4468 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4469 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4470 | int err; |
| 4471 | int drop_inode = 0; |
| 4472 | |
| 4473 | if (inode->i_nlink == 0) |
| 4474 | return -ENOENT; |
| 4475 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4476 | /* do not allow sys_link's with other subvols of the same device */ |
| 4477 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
| 4478 | return -EPERM; |
| 4479 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4480 | /* |
| 4481 | * 1 item for inode ref |
| 4482 | * 2 items for dir items |
| 4483 | */ |
| 4484 | err = btrfs_reserve_metadata_space(root, 3); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4485 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4486 | return err; |
| 4487 | |
| 4488 | btrfs_inc_nlink(inode); |
| 4489 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4490 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4491 | if (err) |
| 4492 | goto fail; |
| 4493 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4494 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4495 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4496 | btrfs_set_trans_block_group(trans, dir); |
| 4497 | atomic_inc(&inode->i_count); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4498 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4499 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4500 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4501 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4502 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4503 | } else { |
| 4504 | btrfs_update_inode_block_group(trans, dir); |
| 4505 | err = btrfs_update_inode(trans, root, inode); |
| 4506 | BUG_ON(err); |
| 4507 | btrfs_log_new_name(trans, inode, NULL, dentry->d_parent); |
| 4508 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4509 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4510 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4511 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4512 | fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4513 | btrfs_unreserve_metadata_space(root, 3); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4514 | if (drop_inode) { |
| 4515 | inode_dec_link_count(inode); |
| 4516 | iput(inode); |
| 4517 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4518 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4519 | return err; |
| 4520 | } |
| 4521 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4522 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4523 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4524 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4525 | struct btrfs_trans_handle *trans; |
| 4526 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4527 | int err = 0; |
| 4528 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4529 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4530 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4531 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4532 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4533 | /* |
| 4534 | * 2 items for inode and ref |
| 4535 | * 2 items for dir items |
| 4536 | * 1 for xattr if selinux is on |
| 4537 | */ |
| 4538 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4539 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4540 | return err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4541 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4542 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4543 | if (!trans) { |
| 4544 | err = -ENOMEM; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4545 | goto out_unlock; |
| 4546 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4547 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4548 | |
| 4549 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4550 | if (err) { |
| 4551 | err = -ENOSPC; |
| 4552 | goto out_unlock; |
| 4553 | } |
| 4554 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4555 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4556 | dentry->d_name.len, |
| 4557 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4558 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 4559 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4560 | if (IS_ERR(inode)) { |
| 4561 | err = PTR_ERR(inode); |
| 4562 | goto out_fail; |
| 4563 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4564 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4565 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4566 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4567 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4568 | if (err) |
| 4569 | goto out_fail; |
| 4570 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4571 | inode->i_op = &btrfs_dir_inode_operations; |
| 4572 | inode->i_fop = &btrfs_dir_file_operations; |
| 4573 | btrfs_set_trans_block_group(trans, inode); |
| 4574 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4575 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4576 | err = btrfs_update_inode(trans, root, inode); |
| 4577 | if (err) |
| 4578 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4579 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4580 | err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4581 | inode, dentry->d_name.name, |
| 4582 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4583 | if (err) |
| 4584 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4585 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4586 | d_instantiate(dentry, inode); |
| 4587 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4588 | btrfs_update_inode_block_group(trans, inode); |
| 4589 | btrfs_update_inode_block_group(trans, dir); |
| 4590 | |
| 4591 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4592 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4593 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4594 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4595 | out_unlock: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4596 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4597 | if (drop_on_err) |
| 4598 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4599 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4600 | return err; |
| 4601 | } |
| 4602 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4603 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4604 | * and an extent that you want to insert, deal with overlap and insert |
| 4605 | * the new extent into the tree. |
| 4606 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4607 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4608 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4609 | struct extent_map *em, |
| 4610 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4611 | { |
| 4612 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4613 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4614 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4615 | start_diff = map_start - em->start; |
| 4616 | em->start = map_start; |
| 4617 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4618 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4619 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4620 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4621 | em->block_len -= start_diff; |
| 4622 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4623 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4624 | } |
| 4625 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4626 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4627 | struct inode *inode, struct page *page, |
| 4628 | size_t pg_offset, u64 extent_offset, |
| 4629 | struct btrfs_file_extent_item *item) |
| 4630 | { |
| 4631 | int ret; |
| 4632 | struct extent_buffer *leaf = path->nodes[0]; |
| 4633 | char *tmp; |
| 4634 | size_t max_size; |
| 4635 | unsigned long inline_size; |
| 4636 | unsigned long ptr; |
| 4637 | |
| 4638 | WARN_ON(pg_offset != 0); |
| 4639 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4640 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4641 | btrfs_item_nr(leaf, path->slots[0])); |
| 4642 | tmp = kmalloc(inline_size, GFP_NOFS); |
| 4643 | ptr = btrfs_file_extent_inline_start(item); |
| 4644 | |
| 4645 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4646 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 4647 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4648 | ret = btrfs_zlib_decompress(tmp, page, extent_offset, |
| 4649 | inline_size, max_size); |
| 4650 | if (ret) { |
| 4651 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 4652 | unsigned long copy_size = min_t(u64, |
| 4653 | PAGE_CACHE_SIZE - pg_offset, |
| 4654 | max_size - extent_offset); |
| 4655 | memset(kaddr + pg_offset, 0, copy_size); |
| 4656 | kunmap_atomic(kaddr, KM_USER0); |
| 4657 | } |
| 4658 | kfree(tmp); |
| 4659 | return 0; |
| 4660 | } |
| 4661 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4662 | /* |
| 4663 | * 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] | 4664 | * the ugly parts come from merging extents from the disk with the in-ram |
| 4665 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4666 | * where the in-ram extents might be locked pending data=ordered completion. |
| 4667 | * |
| 4668 | * This also copies inline extents directly into the page. |
| 4669 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4670 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4671 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4672 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4673 | int create) |
| 4674 | { |
| 4675 | int ret; |
| 4676 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4677 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4678 | u64 extent_start = 0; |
| 4679 | u64 extent_end = 0; |
| 4680 | u64 objectid = inode->i_ino; |
| 4681 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4682 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4683 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4684 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4685 | struct extent_buffer *leaf; |
| 4686 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4687 | struct extent_map *em = NULL; |
| 4688 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4689 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4690 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4691 | int compressed; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4692 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4693 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4694 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4695 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4696 | if (em) |
| 4697 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4698 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4699 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4700 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4701 | if (em->start > start || em->start + em->len <= start) |
| 4702 | free_extent_map(em); |
| 4703 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4704 | free_extent_map(em); |
| 4705 | else |
| 4706 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4707 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4708 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4709 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4710 | err = -ENOMEM; |
| 4711 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4712 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4713 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4714 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 4715 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4716 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4717 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4718 | |
| 4719 | if (!path) { |
| 4720 | path = btrfs_alloc_path(); |
| 4721 | BUG_ON(!path); |
| 4722 | } |
| 4723 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4724 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 4725 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4726 | if (ret < 0) { |
| 4727 | err = ret; |
| 4728 | goto out; |
| 4729 | } |
| 4730 | |
| 4731 | if (ret != 0) { |
| 4732 | if (path->slots[0] == 0) |
| 4733 | goto not_found; |
| 4734 | path->slots[0]--; |
| 4735 | } |
| 4736 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4737 | leaf = path->nodes[0]; |
| 4738 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4739 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4740 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4741 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4742 | found_type = btrfs_key_type(&found_key); |
| 4743 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4744 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 4745 | goto not_found; |
| 4746 | } |
| 4747 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4748 | found_type = btrfs_file_extent_type(leaf, item); |
| 4749 | extent_start = found_key.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4750 | compressed = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4751 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4752 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4753 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4754 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4755 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 4756 | size_t size; |
| 4757 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4758 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 4759 | ~((u64)root->sectorsize - 1); |
| 4760 | } |
| 4761 | |
| 4762 | if (start >= extent_end) { |
| 4763 | path->slots[0]++; |
| 4764 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 4765 | ret = btrfs_next_leaf(root, path); |
| 4766 | if (ret < 0) { |
| 4767 | err = ret; |
| 4768 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4769 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4770 | if (ret > 0) |
| 4771 | goto not_found; |
| 4772 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4773 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4774 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4775 | if (found_key.objectid != objectid || |
| 4776 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 4777 | goto not_found; |
| 4778 | if (start + len <= found_key.offset) |
| 4779 | goto not_found; |
| 4780 | em->start = start; |
| 4781 | em->len = found_key.offset - start; |
| 4782 | goto not_found_em; |
| 4783 | } |
| 4784 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4785 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4786 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4787 | em->start = extent_start; |
| 4788 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4789 | em->orig_start = extent_start - |
| 4790 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4791 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 4792 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4793 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4794 | goto insert; |
| 4795 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4796 | if (compressed) { |
| 4797 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 4798 | em->block_start = bytenr; |
| 4799 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 4800 | item); |
| 4801 | } else { |
| 4802 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 4803 | em->block_start = bytenr; |
| 4804 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4805 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 4806 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4807 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4808 | goto insert; |
| 4809 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4810 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4811 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4812 | size_t size; |
| 4813 | size_t extent_offset; |
| 4814 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4815 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4816 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4817 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4818 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4819 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4820 | goto out; |
| 4821 | } |
| 4822 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4823 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4824 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4825 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4826 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4827 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4828 | em->len = (copy_size + root->sectorsize - 1) & |
| 4829 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4830 | em->orig_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4831 | if (compressed) |
| 4832 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4833 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4834 | if (create == 0 && !PageUptodate(page)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4835 | if (btrfs_file_extent_compression(leaf, item) == |
| 4836 | BTRFS_COMPRESS_ZLIB) { |
| 4837 | ret = uncompress_inline(path, inode, page, |
| 4838 | pg_offset, |
| 4839 | extent_offset, item); |
| 4840 | BUG_ON(ret); |
| 4841 | } else { |
| 4842 | map = kmap(page); |
| 4843 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 4844 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 4845 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 4846 | memset(map + pg_offset + copy_size, 0, |
| 4847 | PAGE_CACHE_SIZE - pg_offset - |
| 4848 | copy_size); |
| 4849 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4850 | kunmap(page); |
| 4851 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4852 | flush_dcache_page(page); |
| 4853 | } else if (create && PageUptodate(page)) { |
| 4854 | if (!trans) { |
| 4855 | kunmap(page); |
| 4856 | free_extent_map(em); |
| 4857 | em = NULL; |
| 4858 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4859 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4860 | goto again; |
| 4861 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4862 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4863 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4864 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4865 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4866 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4867 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4868 | set_extent_uptodate(io_tree, em->start, |
| 4869 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4870 | goto insert; |
| 4871 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4872 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4873 | WARN_ON(1); |
| 4874 | } |
| 4875 | not_found: |
| 4876 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4877 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4878 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4879 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4880 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4881 | insert: |
| 4882 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4883 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4884 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 4885 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 4886 | (unsigned long long)em->len, |
| 4887 | (unsigned long long)start, |
| 4888 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4889 | err = -EIO; |
| 4890 | goto out; |
| 4891 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4892 | |
| 4893 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4894 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4895 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4896 | /* it is possible that someone inserted the extent into the tree |
| 4897 | * while we had the lock dropped. It is also possible that |
| 4898 | * an overlapping map exists in the tree |
| 4899 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4900 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4901 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4902 | |
| 4903 | ret = 0; |
| 4904 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4905 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4906 | if (existing && (existing->start > start || |
| 4907 | existing->start + existing->len <= start)) { |
| 4908 | free_extent_map(existing); |
| 4909 | existing = NULL; |
| 4910 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4911 | if (!existing) { |
| 4912 | existing = lookup_extent_mapping(em_tree, em->start, |
| 4913 | em->len); |
| 4914 | if (existing) { |
| 4915 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4916 | em, start, |
| 4917 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4918 | free_extent_map(existing); |
| 4919 | if (err) { |
| 4920 | free_extent_map(em); |
| 4921 | em = NULL; |
| 4922 | } |
| 4923 | } else { |
| 4924 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4925 | free_extent_map(em); |
| 4926 | em = NULL; |
| 4927 | } |
| 4928 | } else { |
| 4929 | free_extent_map(em); |
| 4930 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4931 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4932 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4933 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4934 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4935 | out: |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4936 | if (path) |
| 4937 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4938 | if (trans) { |
| 4939 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4940 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4941 | err = ret; |
| 4942 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4943 | if (err) { |
| 4944 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4945 | return ERR_PTR(err); |
| 4946 | } |
| 4947 | return em; |
| 4948 | } |
| 4949 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4950 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 4951 | const struct iovec *iov, loff_t offset, |
| 4952 | unsigned long nr_segs) |
| 4953 | { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4954 | return -EINVAL; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4955 | } |
| 4956 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4957 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4958 | __u64 start, __u64 len) |
| 4959 | { |
| 4960 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent); |
| 4961 | } |
| 4962 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4963 | int btrfs_readpage(struct file *file, struct page *page) |
| 4964 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4965 | struct extent_io_tree *tree; |
| 4966 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4967 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4968 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4969 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4970 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 4971 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4972 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4973 | |
| 4974 | |
| 4975 | if (current->flags & PF_MEMALLOC) { |
| 4976 | redirty_page_for_writepage(wbc, page); |
| 4977 | unlock_page(page); |
| 4978 | return 0; |
| 4979 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4980 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4981 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 4982 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4983 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4984 | int btrfs_writepages(struct address_space *mapping, |
| 4985 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 4986 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4987 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4988 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4989 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 4990 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 4991 | } |
| 4992 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 4993 | static int |
| 4994 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 4995 | struct list_head *pages, unsigned nr_pages) |
| 4996 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4997 | struct extent_io_tree *tree; |
| 4998 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 4999 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 5000 | btrfs_get_extent); |
| 5001 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5002 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5003 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5004 | struct extent_io_tree *tree; |
| 5005 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5006 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5007 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5008 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 5009 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5010 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5011 | if (ret == 1) { |
| 5012 | ClearPagePrivate(page); |
| 5013 | set_page_private(page, 0); |
| 5014 | page_cache_release(page); |
| 5015 | } |
| 5016 | return ret; |
| 5017 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5018 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5019 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 5020 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 5021 | if (PageWriteback(page) || PageDirty(page)) |
| 5022 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 5023 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5024 | } |
| 5025 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5026 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 5027 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5028 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5029 | struct btrfs_ordered_extent *ordered; |
| 5030 | u64 page_start = page_offset(page); |
| 5031 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5032 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 5033 | |
| 5034 | /* |
| 5035 | * we have the page locked, so new writeback can't start, |
| 5036 | * and the dirty bit won't be cleared while we are here. |
| 5037 | * |
| 5038 | * Wait for IO on this page so that we can safely clear |
| 5039 | * the PagePrivate2 bit and do ordered accounting |
| 5040 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5041 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 5042 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5043 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5044 | if (offset) { |
| 5045 | btrfs_releasepage(page, GFP_NOFS); |
| 5046 | return; |
| 5047 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5048 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 5049 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 5050 | page_offset(page)); |
| 5051 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5052 | /* |
| 5053 | * IO on this page will never be started, so we need |
| 5054 | * to account for any ordered extents now |
| 5055 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5056 | clear_extent_bit(tree, page_start, page_end, |
| 5057 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5058 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
| 5059 | NULL, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 5060 | /* |
| 5061 | * whoever cleared the private bit is responsible |
| 5062 | * for the finish_ordered_io |
| 5063 | */ |
| 5064 | if (TestClearPagePrivate2(page)) { |
| 5065 | btrfs_finish_ordered_io(page->mapping->host, |
| 5066 | page_start, page_end); |
| 5067 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5068 | btrfs_put_ordered_extent(ordered); |
| 5069 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 5070 | } |
| 5071 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5072 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 5073 | EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5074 | __btrfs_releasepage(page, GFP_NOFS); |
| 5075 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 5076 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 5077 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 5078 | ClearPagePrivate(page); |
| 5079 | set_page_private(page, 0); |
| 5080 | page_cache_release(page); |
| 5081 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5082 | } |
| 5083 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5084 | /* |
| 5085 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 5086 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 5087 | * be careful to check for EOF conditions here. We set the page up correctly |
| 5088 | * for a written page which means we get ENOSPC checking when writing into |
| 5089 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 5090 | * support these features. |
| 5091 | * |
| 5092 | * We are not allowed to take the i_mutex here so we have to play games to |
| 5093 | * protect against truncate races as the page could now be beyond EOF. Because |
| 5094 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 5095 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 5096 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 5097 | * unlock the page. |
| 5098 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5099 | 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] | 5100 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5101 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 5102 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5103 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5104 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5105 | struct btrfs_ordered_extent *ordered; |
| 5106 | char *kaddr; |
| 5107 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5108 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5109 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5110 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5111 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5112 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5113 | ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5114 | if (ret) { |
| 5115 | if (ret == -ENOMEM) |
| 5116 | ret = VM_FAULT_OOM; |
| 5117 | else /* -ENOSPC, -EIO, etc */ |
| 5118 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5119 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5120 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5121 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5122 | ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1); |
| 5123 | if (ret) { |
| 5124 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
| 5125 | ret = VM_FAULT_SIGBUS; |
| 5126 | goto out; |
| 5127 | } |
| 5128 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5129 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5130 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5131 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5132 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5133 | page_start = page_offset(page); |
| 5134 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5135 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5136 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5137 | (page_start >= size)) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5138 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5139 | /* page got truncated out from underneath us */ |
| 5140 | goto out_unlock; |
| 5141 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5142 | wait_on_page_writeback(page); |
| 5143 | |
| 5144 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5145 | set_page_extent_mapped(page); |
| 5146 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5147 | /* |
| 5148 | * we can't set the delalloc bits if there are pending ordered |
| 5149 | * extents. Drop our locks and wait for them to finish |
| 5150 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5151 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 5152 | if (ordered) { |
| 5153 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5154 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5155 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5156 | btrfs_put_ordered_extent(ordered); |
| 5157 | goto again; |
| 5158 | } |
| 5159 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 5160 | /* |
| 5161 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 5162 | * if it was already dirty, so for space accounting reasons we need to |
| 5163 | * clear any delalloc bits for the range we are fixing to save. There |
| 5164 | * is probably a better way to do this, but for now keep consistent with |
| 5165 | * prepare_pages in the normal write path. |
| 5166 | */ |
| 5167 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5168 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
| 5169 | GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 5170 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5171 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end); |
| 5172 | if (ret) { |
| 5173 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5174 | ret = VM_FAULT_SIGBUS; |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 5175 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5176 | goto out_unlock; |
| 5177 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5178 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5179 | |
| 5180 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5181 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5182 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5183 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5184 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5185 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5186 | if (zero_start != PAGE_CACHE_SIZE) { |
| 5187 | kaddr = kmap(page); |
| 5188 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 5189 | flush_dcache_page(page); |
| 5190 | kunmap(page); |
| 5191 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 5192 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5193 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 5194 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5195 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5196 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 5197 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 5198 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5199 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5200 | |
| 5201 | out_unlock: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5202 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 5203 | if (!ret) |
| 5204 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5205 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5206 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5207 | return ret; |
| 5208 | } |
| 5209 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5210 | static void btrfs_truncate(struct inode *inode) |
| 5211 | { |
| 5212 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5213 | int ret; |
| 5214 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5215 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5216 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5217 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5218 | if (!S_ISREG(inode->i_mode)) { |
| 5219 | WARN_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5220 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5221 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5222 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 5223 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 5224 | if (ret) |
| 5225 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5226 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 5227 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5228 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5229 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5230 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5231 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5232 | |
| 5233 | /* |
| 5234 | * setattr is responsible for setting the ordered_data_close flag, |
| 5235 | * but that is only tested during the last file release. That |
| 5236 | * could happen well after the next commit, leaving a great big |
| 5237 | * window where new writes may get lost if someone chooses to write |
| 5238 | * to this file after truncating to zero |
| 5239 | * |
| 5240 | * The inode doesn't have any dirty data here, and so if we commit |
| 5241 | * this is a noop. If someone immediately starts writing to the inode |
| 5242 | * it is very likely we'll catch some of their writes in this |
| 5243 | * transaction, and the commit will find this file on the ordered |
| 5244 | * data list with good things to send down. |
| 5245 | * |
| 5246 | * This is a best effort solution, there is still a window where |
| 5247 | * using truncate to replace the contents of the file will |
| 5248 | * end up with a zero length file after a crash. |
| 5249 | */ |
| 5250 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 5251 | btrfs_add_ordered_operation(trans, root, inode); |
| 5252 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5253 | while (1) { |
| 5254 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 5255 | inode->i_size, |
| 5256 | BTRFS_EXTENT_DATA_KEY); |
| 5257 | if (ret != -EAGAIN) |
| 5258 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5259 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5260 | ret = btrfs_update_inode(trans, root, inode); |
| 5261 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5262 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5263 | nr = trans->blocks_used; |
| 5264 | btrfs_end_transaction(trans, root); |
| 5265 | btrfs_btree_balance_dirty(root, nr); |
| 5266 | |
| 5267 | trans = btrfs_start_transaction(root, 1); |
| 5268 | btrfs_set_trans_block_group(trans, inode); |
| 5269 | } |
| 5270 | |
| 5271 | if (ret == 0 && inode->i_nlink > 0) { |
| 5272 | ret = btrfs_orphan_del(trans, inode); |
| 5273 | BUG_ON(ret); |
| 5274 | } |
| 5275 | |
| 5276 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5277 | BUG_ON(ret); |
| 5278 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5279 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5280 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5281 | BUG_ON(ret); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5282 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5283 | } |
| 5284 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 5285 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5286 | * create a new subvolume directory/inode (helper for the ioctl). |
| 5287 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5288 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5289 | struct btrfs_root *new_root, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5290 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5291 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5292 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5293 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5294 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5295 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5296 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5297 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5298 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5299 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5300 | inode->i_op = &btrfs_dir_inode_operations; |
| 5301 | inode->i_fop = &btrfs_dir_file_operations; |
| 5302 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5303 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5304 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 5305 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5306 | err = btrfs_update_inode(trans, new_root, inode); |
| 5307 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 5308 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5309 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 5310 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5311 | } |
| 5312 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5313 | /* helper function for file defrag and space balancing. This |
| 5314 | * forces readahead on a given range of bytes in an inode |
| 5315 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5316 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5317 | struct file_ra_state *ra, struct file *file, |
| 5318 | pgoff_t offset, pgoff_t last_index) |
| 5319 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5320 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5321 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5322 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 5323 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5324 | } |
| 5325 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5326 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 5327 | { |
| 5328 | struct btrfs_inode *ei; |
| 5329 | |
| 5330 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 5331 | if (!ei) |
| 5332 | return NULL; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 5333 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5334 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5335 | ei->logged_trans = 0; |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5336 | ei->outstanding_extents = 0; |
| 5337 | ei->reserved_extents = 0; |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 5338 | ei->root = NULL; |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5339 | spin_lock_init(&ei->accounting_lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5340 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5341 | INIT_LIST_HEAD(&ei->i_orphan); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5342 | INIT_LIST_HEAD(&ei->ordered_operations); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5343 | return &ei->vfs_inode; |
| 5344 | } |
| 5345 | |
| 5346 | void btrfs_destroy_inode(struct inode *inode) |
| 5347 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5348 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5349 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5350 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5351 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 5352 | WARN_ON(inode->i_data.nrpages); |
| 5353 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5354 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 5355 | * This can happen where we create an inode, but somebody else also |
| 5356 | * created the same inode and we need to destroy the one we already |
| 5357 | * created. |
| 5358 | */ |
| 5359 | if (!root) |
| 5360 | goto free; |
| 5361 | |
| 5362 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5363 | * Make sure we're properly removed from the ordered operation |
| 5364 | * lists. |
| 5365 | */ |
| 5366 | smp_mb(); |
| 5367 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 5368 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 5369 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 5370 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 5371 | } |
| 5372 | |
| 5373 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5374 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5375 | printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", |
| 5376 | inode->i_ino); |
| 5377 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5378 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5379 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5380 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5381 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5382 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 5383 | if (!ordered) |
| 5384 | break; |
| 5385 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5386 | printk(KERN_ERR "btrfs found ordered " |
| 5387 | "extent %llu %llu on inode cleanup\n", |
| 5388 | (unsigned long long)ordered->file_offset, |
| 5389 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5390 | btrfs_remove_ordered_extent(inode, ordered); |
| 5391 | btrfs_put_ordered_extent(ordered); |
| 5392 | btrfs_put_ordered_extent(ordered); |
| 5393 | } |
| 5394 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5395 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5396 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 5397 | free: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5398 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 5399 | } |
| 5400 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5401 | void btrfs_drop_inode(struct inode *inode) |
| 5402 | { |
| 5403 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5404 | |
| 5405 | if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0) |
| 5406 | generic_delete_inode(inode); |
| 5407 | else |
| 5408 | generic_drop_inode(inode); |
| 5409 | } |
| 5410 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5411 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5412 | { |
| 5413 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 5414 | |
| 5415 | inode_init_once(&ei->vfs_inode); |
| 5416 | } |
| 5417 | |
| 5418 | void btrfs_destroy_cachep(void) |
| 5419 | { |
| 5420 | if (btrfs_inode_cachep) |
| 5421 | kmem_cache_destroy(btrfs_inode_cachep); |
| 5422 | if (btrfs_trans_handle_cachep) |
| 5423 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 5424 | if (btrfs_transaction_cachep) |
| 5425 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5426 | if (btrfs_path_cachep) |
| 5427 | kmem_cache_destroy(btrfs_path_cachep); |
| 5428 | } |
| 5429 | |
| 5430 | int btrfs_init_cachep(void) |
| 5431 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5432 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 5433 | sizeof(struct btrfs_inode), 0, |
| 5434 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5435 | if (!btrfs_inode_cachep) |
| 5436 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5437 | |
| 5438 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 5439 | sizeof(struct btrfs_trans_handle), 0, |
| 5440 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5441 | if (!btrfs_trans_handle_cachep) |
| 5442 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5443 | |
| 5444 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 5445 | sizeof(struct btrfs_transaction), 0, |
| 5446 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5447 | if (!btrfs_transaction_cachep) |
| 5448 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5449 | |
| 5450 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 5451 | sizeof(struct btrfs_path), 0, |
| 5452 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5453 | if (!btrfs_path_cachep) |
| 5454 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5455 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5456 | return 0; |
| 5457 | fail: |
| 5458 | btrfs_destroy_cachep(); |
| 5459 | return -ENOMEM; |
| 5460 | } |
| 5461 | |
| 5462 | static int btrfs_getattr(struct vfsmount *mnt, |
| 5463 | struct dentry *dentry, struct kstat *stat) |
| 5464 | { |
| 5465 | struct inode *inode = dentry->d_inode; |
| 5466 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5467 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 5468 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5469 | stat->blocks = (inode_get_bytes(inode) + |
| 5470 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5471 | return 0; |
| 5472 | } |
| 5473 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5474 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 5475 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5476 | { |
| 5477 | struct btrfs_trans_handle *trans; |
| 5478 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5479 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5480 | struct inode *new_inode = new_dentry->d_inode; |
| 5481 | struct inode *old_inode = old_dentry->d_inode; |
| 5482 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5483 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5484 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5485 | int ret; |
| 5486 | |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5487 | if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5488 | return -EPERM; |
| 5489 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5490 | /* we only allow rename subvolume link between subvolumes */ |
| 5491 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5492 | return -EXDEV; |
| 5493 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5494 | if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 5495 | (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) |
| 5496 | return -ENOTEMPTY; |
| 5497 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5498 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5499 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5500 | return -ENOTEMPTY; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 5501 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5502 | /* |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5503 | * We want to reserve the absolute worst case amount of items. So if |
| 5504 | * both inodes are subvols and we need to unlink them then that would |
| 5505 | * require 4 item modifications, but if they are both normal inodes it |
| 5506 | * would require 5 item modifications, so we'll assume their normal |
| 5507 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 5508 | * should cover the worst case number of items we'll modify. |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5509 | */ |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5510 | ret = btrfs_reserve_metadata_space(root, 11); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5511 | if (ret) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5512 | return ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5513 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5514 | /* |
| 5515 | * we're using rename to replace one file with another. |
| 5516 | * and the replacement file is large. Start IO on it now so |
| 5517 | * we don't add too much work to the end of the transaction |
| 5518 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 5519 | 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] | 5520 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 5521 | filemap_flush(old_inode->i_mapping); |
| 5522 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5523 | /* close the racy window with snapshot create/destroy ioctl */ |
| 5524 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 5525 | down_read(&root->fs_info->subvol_sem); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5526 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5527 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5528 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5529 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5530 | if (dest != root) |
| 5531 | btrfs_record_root_in_trans(trans, dest); |
| 5532 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5533 | ret = btrfs_set_inode_index(new_dir, &index); |
| 5534 | if (ret) |
| 5535 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5536 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5537 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5538 | /* force full log commit if subvolume involved. */ |
| 5539 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 5540 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5541 | ret = btrfs_insert_inode_ref(trans, dest, |
| 5542 | new_dentry->d_name.name, |
| 5543 | new_dentry->d_name.len, |
| 5544 | old_inode->i_ino, |
| 5545 | new_dir->i_ino, index); |
| 5546 | if (ret) |
| 5547 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5548 | /* |
| 5549 | * this is an ugly little race, but the rename is required |
| 5550 | * to make sure that if we crash, the inode is either at the |
| 5551 | * old name or the new one. pinning the log transaction lets |
| 5552 | * us make sure we don't allow a log commit to come in after |
| 5553 | * we unlink the name but before we add the new name back in. |
| 5554 | */ |
| 5555 | btrfs_pin_log_trans(root); |
| 5556 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5557 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5558 | * make sure the inode gets flushed if it is replacing |
| 5559 | * something. |
| 5560 | */ |
| 5561 | if (new_inode && new_inode->i_size && |
| 5562 | old_inode && S_ISREG(old_inode->i_mode)) { |
| 5563 | btrfs_add_ordered_operation(trans, root, old_inode); |
| 5564 | } |
| 5565 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5566 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 5567 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 5568 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5569 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5570 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 5571 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 5572 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5573 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5574 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 5575 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 5576 | old_dentry->d_name.name, |
| 5577 | old_dentry->d_name.len); |
| 5578 | } else { |
| 5579 | btrfs_inc_nlink(old_dentry->d_inode); |
| 5580 | ret = btrfs_unlink_inode(trans, root, old_dir, |
| 5581 | old_dentry->d_inode, |
| 5582 | old_dentry->d_name.name, |
| 5583 | old_dentry->d_name.len); |
| 5584 | } |
| 5585 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5586 | |
| 5587 | if (new_inode) { |
| 5588 | new_inode->i_ctime = CURRENT_TIME; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5589 | if (unlikely(new_inode->i_ino == |
| 5590 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 5591 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 5592 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 5593 | root_objectid, |
| 5594 | new_dentry->d_name.name, |
| 5595 | new_dentry->d_name.len); |
| 5596 | BUG_ON(new_inode->i_nlink == 0); |
| 5597 | } else { |
| 5598 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 5599 | new_dentry->d_inode, |
| 5600 | new_dentry->d_name.name, |
| 5601 | new_dentry->d_name.len); |
| 5602 | } |
| 5603 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5604 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5605 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5606 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5607 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5608 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5609 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5610 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 5611 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5612 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5613 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5614 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5615 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 5616 | btrfs_log_new_name(trans, old_inode, old_dir, |
| 5617 | new_dentry->d_parent); |
| 5618 | btrfs_end_log_trans(root); |
| 5619 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5620 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 5621 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5622 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5623 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 5624 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5625 | |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5626 | btrfs_unreserve_metadata_space(root, 11); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5627 | return ret; |
| 5628 | } |
| 5629 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5630 | /* |
| 5631 | * some fairly slow code that needs optimization. This walks the list |
| 5632 | * of all the inodes with pending delalloc and forces them to disk. |
| 5633 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 5634 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5635 | { |
| 5636 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 5637 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5638 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5639 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5640 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 5641 | return -EROFS; |
| 5642 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5643 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5644 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5645 | binode = list_entry(head->next, struct btrfs_inode, |
| 5646 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5647 | inode = igrab(&binode->vfs_inode); |
| 5648 | if (!inode) |
| 5649 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5650 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5651 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5652 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 5653 | if (delay_iput) |
| 5654 | btrfs_add_delayed_iput(inode); |
| 5655 | else |
| 5656 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5657 | } |
| 5658 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5659 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5660 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5661 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5662 | |
| 5663 | /* the filemap_flush will queue IO into the worker threads, but |
| 5664 | * we have to make sure the IO is actually started and that |
| 5665 | * ordered extents get created before we return |
| 5666 | */ |
| 5667 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5668 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5669 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5670 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5671 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 5672 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5673 | } |
| 5674 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5675 | return 0; |
| 5676 | } |
| 5677 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5678 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 5679 | const char *symname) |
| 5680 | { |
| 5681 | struct btrfs_trans_handle *trans; |
| 5682 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5683 | struct btrfs_path *path; |
| 5684 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5685 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5686 | int err; |
| 5687 | int drop_inode = 0; |
| 5688 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5689 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5690 | int name_len; |
| 5691 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5692 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5693 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5694 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5695 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5696 | |
| 5697 | name_len = strlen(symname) + 1; |
| 5698 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 5699 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5700 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5701 | /* |
| 5702 | * 2 items for inode item and ref |
| 5703 | * 2 items for dir items |
| 5704 | * 1 item for xattr if selinux is on |
| 5705 | */ |
| 5706 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5707 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5708 | return err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5709 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5710 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5711 | if (!trans) |
| 5712 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5713 | btrfs_set_trans_block_group(trans, dir); |
| 5714 | |
| 5715 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 5716 | if (err) { |
| 5717 | err = -ENOSPC; |
| 5718 | goto out_unlock; |
| 5719 | } |
| 5720 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5721 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5722 | dentry->d_name.len, |
| 5723 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5724 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 5725 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5726 | err = PTR_ERR(inode); |
| 5727 | if (IS_ERR(inode)) |
| 5728 | goto out_unlock; |
| 5729 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 5730 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5731 | if (err) { |
| 5732 | drop_inode = 1; |
| 5733 | goto out_unlock; |
| 5734 | } |
| 5735 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5736 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5737 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5738 | if (err) |
| 5739 | drop_inode = 1; |
| 5740 | else { |
| 5741 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 5742 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5743 | inode->i_fop = &btrfs_file_operations; |
| 5744 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5745 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5746 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5747 | btrfs_update_inode_block_group(trans, inode); |
| 5748 | btrfs_update_inode_block_group(trans, dir); |
| 5749 | if (drop_inode) |
| 5750 | goto out_unlock; |
| 5751 | |
| 5752 | path = btrfs_alloc_path(); |
| 5753 | BUG_ON(!path); |
| 5754 | key.objectid = inode->i_ino; |
| 5755 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5756 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 5757 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 5758 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 5759 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5760 | if (err) { |
| 5761 | drop_inode = 1; |
| 5762 | goto out_unlock; |
| 5763 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5764 | leaf = path->nodes[0]; |
| 5765 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 5766 | struct btrfs_file_extent_item); |
| 5767 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 5768 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5769 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5770 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 5771 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 5772 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 5773 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 5774 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5775 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5776 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 5777 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5778 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5779 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5780 | inode->i_op = &btrfs_symlink_inode_operations; |
| 5781 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 5782 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5783 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5784 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5785 | err = btrfs_update_inode(trans, root, inode); |
| 5786 | if (err) |
| 5787 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5788 | |
| 5789 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5790 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 5791 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5792 | out_fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5793 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5794 | if (drop_inode) { |
| 5795 | inode_dec_link_count(inode); |
| 5796 | iput(inode); |
| 5797 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5798 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5799 | return err; |
| 5800 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 5801 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5802 | static int prealloc_file_range(struct inode *inode, u64 start, u64 end, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 5803 | u64 alloc_hint, int mode) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5804 | { |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5805 | struct btrfs_trans_handle *trans; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5806 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5807 | struct btrfs_key ins; |
| 5808 | u64 alloc_size; |
| 5809 | u64 cur_offset = start; |
| 5810 | u64 num_bytes = end - start; |
| 5811 | int ret = 0; |
| 5812 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5813 | while (num_bytes > 0) { |
| 5814 | alloc_size = min(num_bytes, root->fs_info->max_extent); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5815 | |
Chris Mason | 3a1abec | 2009-12-17 15:47:17 -0500 | [diff] [blame] | 5816 | trans = btrfs_start_transaction(root, 1); |
| 5817 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5818 | ret = btrfs_reserve_extent(trans, root, alloc_size, |
| 5819 | root->sectorsize, 0, alloc_hint, |
| 5820 | (u64)-1, &ins, 1); |
| 5821 | if (ret) { |
| 5822 | WARN_ON(1); |
Chris Mason | 3a1abec | 2009-12-17 15:47:17 -0500 | [diff] [blame] | 5823 | goto stop_trans; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5824 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5825 | |
| 5826 | ret = btrfs_reserve_metadata_space(root, 3); |
| 5827 | if (ret) { |
| 5828 | btrfs_free_reserved_extent(root, ins.objectid, |
| 5829 | ins.offset); |
Chris Mason | 3a1abec | 2009-12-17 15:47:17 -0500 | [diff] [blame] | 5830 | goto stop_trans; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5831 | } |
| 5832 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5833 | ret = insert_reserved_file_extent(trans, inode, |
| 5834 | cur_offset, ins.objectid, |
| 5835 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 5836 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5837 | BTRFS_FILE_EXTENT_PREALLOC); |
| 5838 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 5839 | btrfs_drop_extent_cache(inode, cur_offset, |
| 5840 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5841 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5842 | num_bytes -= ins.offset; |
| 5843 | cur_offset += ins.offset; |
| 5844 | alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5845 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5846 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5847 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5848 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5849 | cur_offset > inode->i_size) { |
| 5850 | i_size_write(inode, cur_offset); |
| 5851 | btrfs_ordered_update_i_size(inode, cur_offset, NULL); |
| 5852 | } |
| 5853 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5854 | ret = btrfs_update_inode(trans, root, inode); |
| 5855 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5856 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5857 | btrfs_end_transaction(trans, root); |
| 5858 | btrfs_unreserve_metadata_space(root, 3); |
| 5859 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5860 | return ret; |
Chris Mason | 3a1abec | 2009-12-17 15:47:17 -0500 | [diff] [blame] | 5861 | |
| 5862 | stop_trans: |
| 5863 | btrfs_end_transaction(trans, root); |
| 5864 | return ret; |
| 5865 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5866 | } |
| 5867 | |
| 5868 | static long btrfs_fallocate(struct inode *inode, int mode, |
| 5869 | loff_t offset, loff_t len) |
| 5870 | { |
| 5871 | u64 cur_offset; |
| 5872 | u64 last_byte; |
| 5873 | u64 alloc_start; |
| 5874 | u64 alloc_end; |
| 5875 | u64 alloc_hint = 0; |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5876 | u64 locked_end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5877 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 5878 | struct extent_map *em; |
| 5879 | int ret; |
| 5880 | |
| 5881 | alloc_start = offset & ~mask; |
| 5882 | alloc_end = (offset + len + mask) & ~mask; |
| 5883 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5884 | /* |
| 5885 | * wait for ordered IO before we have any locks. We'll loop again |
| 5886 | * below with the locks held. |
| 5887 | */ |
| 5888 | btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start); |
| 5889 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5890 | mutex_lock(&inode->i_mutex); |
| 5891 | if (alloc_start > inode->i_size) { |
| 5892 | ret = btrfs_cont_expand(inode, alloc_start); |
| 5893 | if (ret) |
| 5894 | goto out; |
| 5895 | } |
| 5896 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5897 | ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode, |
Josef Bacik | a970b0a | 2009-06-27 21:07:34 -0400 | [diff] [blame] | 5898 | alloc_end - alloc_start); |
| 5899 | if (ret) |
| 5900 | goto out; |
| 5901 | |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5902 | locked_end = alloc_end - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5903 | while (1) { |
| 5904 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5905 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5906 | /* the extent lock is ordered inside the running |
| 5907 | * transaction |
| 5908 | */ |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5909 | lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
| 5910 | GFP_NOFS); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5911 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 5912 | alloc_end - 1); |
| 5913 | if (ordered && |
| 5914 | ordered->file_offset + ordered->len > alloc_start && |
| 5915 | ordered->file_offset < alloc_end) { |
| 5916 | btrfs_put_ordered_extent(ordered); |
| 5917 | unlock_extent(&BTRFS_I(inode)->io_tree, |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5918 | alloc_start, locked_end, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5919 | /* |
| 5920 | * we can't wait on the range with the transaction |
| 5921 | * running or with the extent lock held |
| 5922 | */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5923 | btrfs_wait_ordered_range(inode, alloc_start, |
| 5924 | alloc_end - alloc_start); |
| 5925 | } else { |
| 5926 | if (ordered) |
| 5927 | btrfs_put_ordered_extent(ordered); |
| 5928 | break; |
| 5929 | } |
| 5930 | } |
| 5931 | |
| 5932 | cur_offset = alloc_start; |
| 5933 | while (1) { |
| 5934 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 5935 | alloc_end - cur_offset, 0); |
| 5936 | BUG_ON(IS_ERR(em) || !em); |
| 5937 | last_byte = min(extent_map_end(em), alloc_end); |
| 5938 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5939 | if (em->block_start == EXTENT_MAP_HOLE || |
| 5940 | (cur_offset >= inode->i_size && |
| 5941 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5942 | ret = prealloc_file_range(inode, |
| 5943 | cur_offset, last_byte, |
| 5944 | alloc_hint, mode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5945 | if (ret < 0) { |
| 5946 | free_extent_map(em); |
| 5947 | break; |
| 5948 | } |
| 5949 | } |
| 5950 | if (em->block_start <= EXTENT_MAP_LAST_BYTE) |
| 5951 | alloc_hint = em->block_start; |
| 5952 | free_extent_map(em); |
| 5953 | |
| 5954 | cur_offset = last_byte; |
| 5955 | if (cur_offset >= alloc_end) { |
| 5956 | ret = 0; |
| 5957 | break; |
| 5958 | } |
| 5959 | } |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5960 | unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5961 | GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5962 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5963 | btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode, |
| 5964 | alloc_end - alloc_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5965 | out: |
| 5966 | mutex_unlock(&inode->i_mutex); |
| 5967 | return ret; |
| 5968 | } |
| 5969 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5970 | static int btrfs_set_page_dirty(struct page *page) |
| 5971 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5972 | return __set_page_dirty_nobuffers(page); |
| 5973 | } |
| 5974 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5975 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5976 | { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5977 | if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5978 | return -EACCES; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5979 | return generic_permission(inode, mask, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5980 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5981 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 5982 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5983 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5984 | .lookup = btrfs_lookup, |
| 5985 | .create = btrfs_create, |
| 5986 | .unlink = btrfs_unlink, |
| 5987 | .link = btrfs_link, |
| 5988 | .mkdir = btrfs_mkdir, |
| 5989 | .rmdir = btrfs_rmdir, |
| 5990 | .rename = btrfs_rename, |
| 5991 | .symlink = btrfs_symlink, |
| 5992 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5993 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5994 | .setxattr = btrfs_setxattr, |
| 5995 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5996 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5997 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5998 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5999 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6000 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6001 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6002 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6003 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6004 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 6005 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6006 | .llseek = generic_file_llseek, |
| 6007 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 6008 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 6009 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6010 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 6011 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6012 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 6013 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6014 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6015 | }; |
| 6016 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6017 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 6018 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 6019 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6020 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 6021 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6022 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6023 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6024 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 6025 | .set_bit_hook = btrfs_set_bit_hook, |
| 6026 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6027 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 6028 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 6029 | }; |
| 6030 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 6031 | /* |
| 6032 | * btrfs doesn't support the bmap operation because swapfiles |
| 6033 | * use bmap to make a mapping of extents in the file. They assume |
| 6034 | * these extents won't change over the life of the file and they |
| 6035 | * use the bmap result to do IO directly to the drive. |
| 6036 | * |
| 6037 | * the btrfs bmap call would return logical addresses that aren't |
| 6038 | * suitable for IO and they also will change frequently as COW |
| 6039 | * operations happen. So, swapfile + btrfs == corruption. |
| 6040 | * |
| 6041 | * For now we're avoiding this by dropping bmap. |
| 6042 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 6043 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6044 | .readpage = btrfs_readpage, |
| 6045 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6046 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6047 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6048 | .sync_page = block_sync_page, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6049 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6050 | .invalidatepage = btrfs_invalidatepage, |
| 6051 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6052 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 6053 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6054 | }; |
| 6055 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 6056 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6057 | .readpage = btrfs_readpage, |
| 6058 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 6059 | .invalidatepage = btrfs_invalidatepage, |
| 6060 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6061 | }; |
| 6062 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6063 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6064 | .truncate = btrfs_truncate, |
| 6065 | .getattr = btrfs_getattr, |
| 6066 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6067 | .setxattr = btrfs_setxattr, |
| 6068 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 6069 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6070 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6071 | .permission = btrfs_permission, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6072 | .fallocate = btrfs_fallocate, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6073 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6074 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6075 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6076 | .getattr = btrfs_getattr, |
| 6077 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6078 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6079 | .setxattr = btrfs_setxattr, |
| 6080 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6081 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6082 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6083 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6084 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6085 | .readlink = generic_readlink, |
| 6086 | .follow_link = page_follow_link_light, |
| 6087 | .put_link = page_put_link, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6088 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 6089 | .setxattr = btrfs_setxattr, |
| 6090 | .getxattr = btrfs_getxattr, |
| 6091 | .listxattr = btrfs_listxattr, |
| 6092 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6093 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6094 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 6095 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6096 | .d_delete = btrfs_dentry_delete, |
| 6097 | }; |