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 | b888db2b | 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 | b888db2b | 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 | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 382 | (btrfs_test_opt(root, COMPRESS) || |
| 383 | (BTRFS_I(inode)->force_compress))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 384 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 385 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 386 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 387 | ret = btrfs_zlib_compress_pages(inode->i_mapping, start, |
| 388 | total_compressed, pages, |
| 389 | nr_pages, &nr_pages_ret, |
| 390 | &total_in, |
| 391 | &total_compressed, |
| 392 | max_compressed); |
| 393 | |
| 394 | if (!ret) { |
| 395 | unsigned long offset = total_compressed & |
| 396 | (PAGE_CACHE_SIZE - 1); |
| 397 | struct page *page = pages[nr_pages_ret - 1]; |
| 398 | char *kaddr; |
| 399 | |
| 400 | /* zero the tail end of the last page, we might be |
| 401 | * sending it down to disk |
| 402 | */ |
| 403 | if (offset) { |
| 404 | kaddr = kmap_atomic(page, KM_USER0); |
| 405 | memset(kaddr + offset, 0, |
| 406 | PAGE_CACHE_SIZE - offset); |
| 407 | kunmap_atomic(kaddr, KM_USER0); |
| 408 | } |
| 409 | will_compress = 1; |
| 410 | } |
| 411 | } |
| 412 | if (start == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 413 | trans = btrfs_join_transaction(root, 1); |
| 414 | BUG_ON(!trans); |
| 415 | btrfs_set_trans_block_group(trans, inode); |
| 416 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 417 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 418 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 419 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 420 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 421 | */ |
| 422 | ret = cow_file_range_inline(trans, root, inode, |
| 423 | start, end, 0, NULL); |
| 424 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 425 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 426 | ret = cow_file_range_inline(trans, root, inode, |
| 427 | start, end, |
| 428 | total_compressed, pages); |
| 429 | } |
| 430 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 431 | /* |
| 432 | * inline extent creation worked, we don't need |
| 433 | * to create any more async work items. Unlock |
| 434 | * and free up our temp pages. |
| 435 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 436 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 437 | &BTRFS_I(inode)->io_tree, |
| 438 | start, end, NULL, |
| 439 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 440 | EXTENT_CLEAR_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 441 | EXTENT_CLEAR_ACCOUNTING | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 442 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 443 | |
| 444 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 445 | goto free_pages_out; |
| 446 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 447 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | if (will_compress) { |
| 451 | /* |
| 452 | * we aren't doing an inline extent round the compressed size |
| 453 | * up to a block size boundary so the allocator does sane |
| 454 | * things |
| 455 | */ |
| 456 | total_compressed = (total_compressed + blocksize - 1) & |
| 457 | ~(blocksize - 1); |
| 458 | |
| 459 | /* |
| 460 | * one last check to make sure the compression is really a |
| 461 | * win, compare the page count read with the blocks on disk |
| 462 | */ |
| 463 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 464 | ~(PAGE_CACHE_SIZE - 1); |
| 465 | if (total_compressed >= total_in) { |
| 466 | will_compress = 0; |
| 467 | } else { |
| 468 | disk_num_bytes = total_compressed; |
| 469 | num_bytes = total_in; |
| 470 | } |
| 471 | } |
| 472 | if (!will_compress && pages) { |
| 473 | /* |
| 474 | * the compression code ran but failed to make things smaller, |
| 475 | * free any pages it allocated and our page pointer array |
| 476 | */ |
| 477 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 478 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 479 | page_cache_release(pages[i]); |
| 480 | } |
| 481 | kfree(pages); |
| 482 | pages = NULL; |
| 483 | total_compressed = 0; |
| 484 | nr_pages_ret = 0; |
| 485 | |
| 486 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 487 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 488 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 489 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 490 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 491 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 492 | if (will_compress) { |
| 493 | *num_added += 1; |
| 494 | |
| 495 | /* the async work queues will take care of doing actual |
| 496 | * allocation on disk for these compressed pages, |
| 497 | * and will submit them to the elevator. |
| 498 | */ |
| 499 | add_async_extent(async_cow, start, num_bytes, |
| 500 | total_compressed, pages, nr_pages_ret); |
| 501 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 502 | if (start + num_bytes < end && start + num_bytes < actual_end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 503 | start += num_bytes; |
| 504 | pages = NULL; |
| 505 | cond_resched(); |
| 506 | goto again; |
| 507 | } |
| 508 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 509 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 510 | /* |
| 511 | * No compression, but we still need to write the pages in |
| 512 | * the file we've been given so far. redirty the locked |
| 513 | * page if it corresponds to our extent and set things up |
| 514 | * for the async work queue to run cow_file_range to do |
| 515 | * the normal delalloc dance |
| 516 | */ |
| 517 | if (page_offset(locked_page) >= start && |
| 518 | page_offset(locked_page) <= end) { |
| 519 | __set_page_dirty_nobuffers(locked_page); |
| 520 | /* unlocked later on in the async handlers */ |
| 521 | } |
| 522 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0); |
| 523 | *num_added += 1; |
| 524 | } |
| 525 | |
| 526 | out: |
| 527 | return 0; |
| 528 | |
| 529 | free_pages_out: |
| 530 | for (i = 0; i < nr_pages_ret; i++) { |
| 531 | WARN_ON(pages[i]->mapping); |
| 532 | page_cache_release(pages[i]); |
| 533 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 534 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 535 | |
| 536 | goto out; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * phase two of compressed writeback. This is the ordered portion |
| 541 | * of the code, which only gets called in the order the work was |
| 542 | * queued. We walk all the async extents created by compress_file_range |
| 543 | * and send them down to the disk. |
| 544 | */ |
| 545 | static noinline int submit_compressed_extents(struct inode *inode, |
| 546 | struct async_cow *async_cow) |
| 547 | { |
| 548 | struct async_extent *async_extent; |
| 549 | u64 alloc_hint = 0; |
| 550 | struct btrfs_trans_handle *trans; |
| 551 | struct btrfs_key ins; |
| 552 | struct extent_map *em; |
| 553 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 554 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 555 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 556 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 557 | |
| 558 | if (list_empty(&async_cow->extents)) |
| 559 | return 0; |
| 560 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 561 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 562 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 563 | async_extent = list_entry(async_cow->extents.next, |
| 564 | struct async_extent, list); |
| 565 | list_del(&async_extent->list); |
| 566 | |
| 567 | io_tree = &BTRFS_I(inode)->io_tree; |
| 568 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 569 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 570 | /* did the compression code fall back to uncompressed IO? */ |
| 571 | if (!async_extent->pages) { |
| 572 | int page_started = 0; |
| 573 | unsigned long nr_written = 0; |
| 574 | |
| 575 | lock_extent(io_tree, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 576 | async_extent->start + |
| 577 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 578 | |
| 579 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 580 | ret = cow_file_range(inode, async_cow->locked_page, |
| 581 | async_extent->start, |
| 582 | async_extent->start + |
| 583 | async_extent->ram_size - 1, |
| 584 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 585 | |
| 586 | /* |
| 587 | * if page_started, cow_file_range inserted an |
| 588 | * inline extent and took care of all the unlocking |
| 589 | * and IO for us. Otherwise, we need to submit |
| 590 | * all those pages down to the drive. |
| 591 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 592 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 593 | extent_write_locked_range(io_tree, |
| 594 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 595 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 596 | async_extent->ram_size - 1, |
| 597 | btrfs_get_extent, |
| 598 | WB_SYNC_ALL); |
| 599 | kfree(async_extent); |
| 600 | cond_resched(); |
| 601 | continue; |
| 602 | } |
| 603 | |
| 604 | lock_extent(io_tree, async_extent->start, |
| 605 | async_extent->start + async_extent->ram_size - 1, |
| 606 | GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 607 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 608 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 609 | ret = btrfs_reserve_extent(trans, root, |
| 610 | async_extent->compressed_size, |
| 611 | async_extent->compressed_size, |
| 612 | 0, alloc_hint, |
| 613 | (u64)-1, &ins, 1); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 614 | btrfs_end_transaction(trans, root); |
| 615 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 616 | if (ret) { |
| 617 | int i; |
| 618 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 619 | WARN_ON(async_extent->pages[i]->mapping); |
| 620 | page_cache_release(async_extent->pages[i]); |
| 621 | } |
| 622 | kfree(async_extent->pages); |
| 623 | async_extent->nr_pages = 0; |
| 624 | async_extent->pages = NULL; |
| 625 | unlock_extent(io_tree, async_extent->start, |
| 626 | async_extent->start + |
| 627 | async_extent->ram_size - 1, GFP_NOFS); |
| 628 | goto retry; |
| 629 | } |
| 630 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 631 | /* |
| 632 | * here we're doing allocation and writeback of the |
| 633 | * compressed pages |
| 634 | */ |
| 635 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 636 | async_extent->start + |
| 637 | async_extent->ram_size - 1, 0); |
| 638 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 639 | em = alloc_extent_map(GFP_NOFS); |
| 640 | em->start = async_extent->start; |
| 641 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 642 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 643 | |
| 644 | em->block_start = ins.objectid; |
| 645 | em->block_len = ins.offset; |
| 646 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 647 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 648 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 649 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 650 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 651 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 652 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 653 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 654 | if (ret != -EEXIST) { |
| 655 | free_extent_map(em); |
| 656 | break; |
| 657 | } |
| 658 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 659 | async_extent->start + |
| 660 | async_extent->ram_size - 1, 0); |
| 661 | } |
| 662 | |
| 663 | ret = btrfs_add_ordered_extent(inode, async_extent->start, |
| 664 | ins.objectid, |
| 665 | async_extent->ram_size, |
| 666 | ins.offset, |
| 667 | BTRFS_ORDERED_COMPRESSED); |
| 668 | BUG_ON(ret); |
| 669 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 670 | /* |
| 671 | * clear dirty, set writeback and unlock the pages. |
| 672 | */ |
| 673 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 674 | &BTRFS_I(inode)->io_tree, |
| 675 | async_extent->start, |
| 676 | async_extent->start + |
| 677 | async_extent->ram_size - 1, |
| 678 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 679 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 680 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 681 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 682 | |
| 683 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 684 | async_extent->start, |
| 685 | async_extent->ram_size, |
| 686 | ins.objectid, |
| 687 | ins.offset, async_extent->pages, |
| 688 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 689 | |
| 690 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 691 | alloc_hint = ins.objectid + ins.offset; |
| 692 | kfree(async_extent); |
| 693 | cond_resched(); |
| 694 | } |
| 695 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * when extent_io.c finds a delayed allocation range in the file, |
| 701 | * the call backs end up in this code. The basic idea is to |
| 702 | * allocate extents on disk for the range, and create ordered data structs |
| 703 | * in ram to track those extents. |
| 704 | * |
| 705 | * locked_page is the page that writepage had locked already. We use |
| 706 | * it to make sure we don't do extra locks or unlocks. |
| 707 | * |
| 708 | * *page_started is set to one if we unlock locked_page and do everything |
| 709 | * required to start IO on it. It may be clean and already done with |
| 710 | * IO when we return. |
| 711 | */ |
| 712 | static noinline int cow_file_range(struct inode *inode, |
| 713 | struct page *locked_page, |
| 714 | u64 start, u64 end, int *page_started, |
| 715 | unsigned long *nr_written, |
| 716 | int unlock) |
| 717 | { |
| 718 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 719 | struct btrfs_trans_handle *trans; |
| 720 | u64 alloc_hint = 0; |
| 721 | u64 num_bytes; |
| 722 | unsigned long ram_size; |
| 723 | u64 disk_num_bytes; |
| 724 | u64 cur_alloc_size; |
| 725 | u64 blocksize = root->sectorsize; |
| 726 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 727 | u64 isize = i_size_read(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 728 | struct btrfs_key ins; |
| 729 | struct extent_map *em; |
| 730 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 731 | int ret = 0; |
| 732 | |
| 733 | trans = btrfs_join_transaction(root, 1); |
| 734 | BUG_ON(!trans); |
| 735 | btrfs_set_trans_block_group(trans, inode); |
| 736 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 737 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 738 | |
| 739 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 740 | num_bytes = max(blocksize, num_bytes); |
| 741 | disk_num_bytes = num_bytes; |
| 742 | ret = 0; |
| 743 | |
| 744 | if (start == 0) { |
| 745 | /* lets try to make an inline extent */ |
| 746 | ret = cow_file_range_inline(trans, root, inode, |
| 747 | start, end, 0, NULL); |
| 748 | if (ret == 0) { |
| 749 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 750 | &BTRFS_I(inode)->io_tree, |
| 751 | start, end, NULL, |
| 752 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 753 | EXTENT_CLEAR_UNLOCK | |
| 754 | EXTENT_CLEAR_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 755 | EXTENT_CLEAR_ACCOUNTING | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 756 | EXTENT_CLEAR_DIRTY | |
| 757 | EXTENT_SET_WRITEBACK | |
| 758 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 759 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 760 | *nr_written = *nr_written + |
| 761 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 762 | *page_started = 1; |
| 763 | ret = 0; |
| 764 | goto out; |
| 765 | } |
| 766 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 767 | |
| 768 | BUG_ON(disk_num_bytes > |
| 769 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 770 | |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 771 | |
| 772 | read_lock(&BTRFS_I(inode)->extent_tree.lock); |
| 773 | em = search_extent_mapping(&BTRFS_I(inode)->extent_tree, |
| 774 | start, num_bytes); |
| 775 | if (em) { |
Josef Bacik | 6346c93 | 2009-11-10 21:23:47 -0500 | [diff] [blame] | 776 | /* |
| 777 | * if block start isn't an actual block number then find the |
| 778 | * first block in this inode and use that as a hint. If that |
| 779 | * block is also bogus then just don't worry about it. |
| 780 | */ |
| 781 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 782 | free_extent_map(em); |
| 783 | em = search_extent_mapping(em_tree, 0, 0); |
| 784 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 785 | alloc_hint = em->block_start; |
| 786 | if (em) |
| 787 | free_extent_map(em); |
| 788 | } else { |
| 789 | alloc_hint = em->block_start; |
| 790 | free_extent_map(em); |
| 791 | } |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 792 | } |
| 793 | read_unlock(&BTRFS_I(inode)->extent_tree.lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 794 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 795 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 796 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 797 | unsigned long op; |
| 798 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 799 | cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 800 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 801 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 802 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 803 | BUG_ON(ret); |
| 804 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 805 | em = alloc_extent_map(GFP_NOFS); |
| 806 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 807 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 808 | ram_size = ins.offset; |
| 809 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 810 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 811 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 812 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 813 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 814 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 815 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 816 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 817 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 818 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 819 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 820 | if (ret != -EEXIST) { |
| 821 | free_extent_map(em); |
| 822 | break; |
| 823 | } |
| 824 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 825 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 826 | } |
| 827 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 828 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 829 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 830 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 831 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 832 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 833 | if (root->root_key.objectid == |
| 834 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 835 | ret = btrfs_reloc_clone_csums(inode, start, |
| 836 | cur_alloc_size); |
| 837 | BUG_ON(ret); |
| 838 | } |
| 839 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 840 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 841 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 842 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 843 | /* we're not doing compressed IO, don't unlock the first |
| 844 | * page (which the caller expects to stay locked), don't |
| 845 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 846 | * |
| 847 | * Do set the Private2 bit so we know this page was properly |
| 848 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 849 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 850 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 851 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 852 | EXTENT_SET_PRIVATE2; |
| 853 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 854 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 855 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 856 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 857 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 858 | num_bytes -= cur_alloc_size; |
| 859 | alloc_hint = ins.objectid + ins.offset; |
| 860 | start += cur_alloc_size; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 861 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 862 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 863 | ret = 0; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 864 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 865 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 866 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 867 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 868 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 869 | /* |
| 870 | * work queue call back to started compression on a file and pages |
| 871 | */ |
| 872 | static noinline void async_cow_start(struct btrfs_work *work) |
| 873 | { |
| 874 | struct async_cow *async_cow; |
| 875 | int num_added = 0; |
| 876 | async_cow = container_of(work, struct async_cow, work); |
| 877 | |
| 878 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 879 | async_cow->start, async_cow->end, async_cow, |
| 880 | &num_added); |
| 881 | if (num_added == 0) |
| 882 | async_cow->inode = NULL; |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * work queue call back to submit previously compressed pages |
| 887 | */ |
| 888 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 889 | { |
| 890 | struct async_cow *async_cow; |
| 891 | struct btrfs_root *root; |
| 892 | unsigned long nr_pages; |
| 893 | |
| 894 | async_cow = container_of(work, struct async_cow, work); |
| 895 | |
| 896 | root = async_cow->root; |
| 897 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 898 | PAGE_CACHE_SHIFT; |
| 899 | |
| 900 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 901 | |
| 902 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 903 | 5 * 1042 * 1024 && |
| 904 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 905 | wake_up(&root->fs_info->async_submit_wait); |
| 906 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 907 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 908 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 909 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 910 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 911 | static noinline void async_cow_free(struct btrfs_work *work) |
| 912 | { |
| 913 | struct async_cow *async_cow; |
| 914 | async_cow = container_of(work, struct async_cow, work); |
| 915 | kfree(async_cow); |
| 916 | } |
| 917 | |
| 918 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 919 | u64 start, u64 end, int *page_started, |
| 920 | unsigned long *nr_written) |
| 921 | { |
| 922 | struct async_cow *async_cow; |
| 923 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 924 | unsigned long nr_pages; |
| 925 | u64 cur_end; |
| 926 | int limit = 10 * 1024 * 1042; |
| 927 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 928 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 929 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 930 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 931 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
| 932 | async_cow->inode = inode; |
| 933 | async_cow->root = root; |
| 934 | async_cow->locked_page = locked_page; |
| 935 | async_cow->start = start; |
| 936 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 937 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 938 | cur_end = end; |
| 939 | else |
| 940 | cur_end = min(end, start + 512 * 1024 - 1); |
| 941 | |
| 942 | async_cow->end = cur_end; |
| 943 | INIT_LIST_HEAD(&async_cow->extents); |
| 944 | |
| 945 | async_cow->work.func = async_cow_start; |
| 946 | async_cow->work.ordered_func = async_cow_submit; |
| 947 | async_cow->work.ordered_free = async_cow_free; |
| 948 | async_cow->work.flags = 0; |
| 949 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 950 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 951 | PAGE_CACHE_SHIFT; |
| 952 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 953 | |
| 954 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 955 | &async_cow->work); |
| 956 | |
| 957 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 958 | wait_event(root->fs_info->async_submit_wait, |
| 959 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 960 | limit)); |
| 961 | } |
| 962 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 963 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 964 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 965 | wait_event(root->fs_info->async_submit_wait, |
| 966 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 967 | 0)); |
| 968 | } |
| 969 | |
| 970 | *nr_written += nr_pages; |
| 971 | start = cur_end + 1; |
| 972 | } |
| 973 | *page_started = 1; |
| 974 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 975 | } |
| 976 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 977 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 978 | u64 bytenr, u64 num_bytes) |
| 979 | { |
| 980 | int ret; |
| 981 | struct btrfs_ordered_sum *sums; |
| 982 | LIST_HEAD(list); |
| 983 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 984 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
| 985 | bytenr + num_bytes - 1, &list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 986 | if (ret == 0 && list_empty(&list)) |
| 987 | return 0; |
| 988 | |
| 989 | while (!list_empty(&list)) { |
| 990 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 991 | list_del(&sums->list); |
| 992 | kfree(sums); |
| 993 | } |
| 994 | return 1; |
| 995 | } |
| 996 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 997 | /* |
| 998 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 999 | * of the extents that exist in the file, and COWs the file as required. |
| 1000 | * |
| 1001 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1002 | * blocks on disk |
| 1003 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1004 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1005 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1006 | u64 start, u64 end, int *page_started, int force, |
| 1007 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1008 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1009 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1010 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1011 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1012 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1013 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1014 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1015 | u64 cow_start; |
| 1016 | u64 cur_offset; |
| 1017 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1018 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1019 | u64 disk_bytenr; |
| 1020 | u64 num_bytes; |
| 1021 | int extent_type; |
| 1022 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1023 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1024 | int nocow; |
| 1025 | int check_prev = 1; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1026 | |
| 1027 | path = btrfs_alloc_path(); |
| 1028 | BUG_ON(!path); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1029 | trans = btrfs_join_transaction(root, 1); |
| 1030 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1031 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1032 | cow_start = (u64)-1; |
| 1033 | cur_offset = start; |
| 1034 | while (1) { |
| 1035 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 1036 | cur_offset, 0); |
| 1037 | BUG_ON(ret < 0); |
| 1038 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1039 | leaf = path->nodes[0]; |
| 1040 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1041 | path->slots[0] - 1); |
| 1042 | if (found_key.objectid == inode->i_ino && |
| 1043 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1044 | path->slots[0]--; |
| 1045 | } |
| 1046 | check_prev = 0; |
| 1047 | next_slot: |
| 1048 | leaf = path->nodes[0]; |
| 1049 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1050 | ret = btrfs_next_leaf(root, path); |
| 1051 | if (ret < 0) |
| 1052 | BUG_ON(1); |
| 1053 | if (ret > 0) |
| 1054 | break; |
| 1055 | leaf = path->nodes[0]; |
| 1056 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1057 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1058 | nocow = 0; |
| 1059 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1060 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1061 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1062 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1063 | if (found_key.objectid > inode->i_ino || |
| 1064 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1065 | found_key.offset > end) |
| 1066 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1067 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1068 | if (found_key.offset > cur_offset) { |
| 1069 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1070 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1071 | goto out_check; |
| 1072 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1073 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1074 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1075 | struct btrfs_file_extent_item); |
| 1076 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1077 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1078 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1079 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1080 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1081 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1082 | extent_end = found_key.offset + |
| 1083 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1084 | if (extent_end <= start) { |
| 1085 | path->slots[0]++; |
| 1086 | goto next_slot; |
| 1087 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1088 | if (disk_bytenr == 0) |
| 1089 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1090 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1091 | btrfs_file_extent_encryption(leaf, fi) || |
| 1092 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1093 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1094 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1095 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1096 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1097 | goto out_check; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1098 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1099 | found_key.offset - |
| 1100 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1101 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1102 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1103 | disk_bytenr += cur_offset - found_key.offset; |
| 1104 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1105 | /* |
| 1106 | * force cow if csum exists in the range. |
| 1107 | * this ensure that csum for a given extent are |
| 1108 | * either valid or do not exist. |
| 1109 | */ |
| 1110 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1111 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1112 | nocow = 1; |
| 1113 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1114 | extent_end = found_key.offset + |
| 1115 | btrfs_file_extent_inline_len(leaf, fi); |
| 1116 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1117 | } else { |
| 1118 | BUG_ON(1); |
| 1119 | } |
| 1120 | out_check: |
| 1121 | if (extent_end <= start) { |
| 1122 | path->slots[0]++; |
| 1123 | goto next_slot; |
| 1124 | } |
| 1125 | if (!nocow) { |
| 1126 | if (cow_start == (u64)-1) |
| 1127 | cow_start = cur_offset; |
| 1128 | cur_offset = extent_end; |
| 1129 | if (cur_offset > end) |
| 1130 | break; |
| 1131 | path->slots[0]++; |
| 1132 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1133 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1134 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1135 | btrfs_release_path(root, path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1136 | if (cow_start != (u64)-1) { |
| 1137 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1138 | found_key.offset - 1, page_started, |
| 1139 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1140 | BUG_ON(ret); |
| 1141 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1142 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1143 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1144 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1145 | struct extent_map *em; |
| 1146 | struct extent_map_tree *em_tree; |
| 1147 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 1148 | em = alloc_extent_map(GFP_NOFS); |
| 1149 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1150 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1151 | em->len = num_bytes; |
| 1152 | em->block_len = num_bytes; |
| 1153 | em->block_start = disk_bytenr; |
| 1154 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1155 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1156 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1157 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1158 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1159 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1160 | if (ret != -EEXIST) { |
| 1161 | free_extent_map(em); |
| 1162 | break; |
| 1163 | } |
| 1164 | btrfs_drop_extent_cache(inode, em->start, |
| 1165 | em->start + em->len - 1, 0); |
| 1166 | } |
| 1167 | type = BTRFS_ORDERED_PREALLOC; |
| 1168 | } else { |
| 1169 | type = BTRFS_ORDERED_NOCOW; |
| 1170 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1171 | |
| 1172 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1173 | num_bytes, num_bytes, type); |
| 1174 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1175 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1176 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1177 | cur_offset, cur_offset + num_bytes - 1, |
| 1178 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1179 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1180 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1181 | cur_offset = extent_end; |
| 1182 | if (cur_offset > end) |
| 1183 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1184 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1185 | btrfs_release_path(root, path); |
| 1186 | |
| 1187 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1188 | cow_start = cur_offset; |
| 1189 | if (cow_start != (u64)-1) { |
| 1190 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1191 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1192 | BUG_ON(ret); |
| 1193 | } |
| 1194 | |
| 1195 | ret = btrfs_end_transaction(trans, root); |
| 1196 | BUG_ON(ret); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1197 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1198 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1199 | } |
| 1200 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1201 | /* |
| 1202 | * extent_io.c call back to do delayed allocation processing |
| 1203 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1204 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1205 | u64 start, u64 end, int *page_started, |
| 1206 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1207 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1208 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1209 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1210 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1211 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -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, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1214 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1215 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1216 | page_started, 0, nr_written); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1217 | else if (!btrfs_test_opt(root, COMPRESS) && |
| 1218 | !(BTRFS_I(inode)->force_compress)) |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1219 | ret = cow_file_range(inode, locked_page, start, end, |
| 1220 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1221 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1222 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1223 | page_started, nr_written); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1224 | return ret; |
| 1225 | } |
| 1226 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1227 | static int btrfs_split_extent_hook(struct inode *inode, |
| 1228 | struct extent_state *orig, u64 split) |
| 1229 | { |
| 1230 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1231 | u64 size; |
| 1232 | |
| 1233 | if (!(orig->state & EXTENT_DELALLOC)) |
| 1234 | return 0; |
| 1235 | |
| 1236 | size = orig->end - orig->start + 1; |
| 1237 | if (size > root->fs_info->max_extent) { |
| 1238 | u64 num_extents; |
| 1239 | u64 new_size; |
| 1240 | |
| 1241 | new_size = orig->end - split + 1; |
| 1242 | num_extents = div64_u64(size + root->fs_info->max_extent - 1, |
| 1243 | root->fs_info->max_extent); |
| 1244 | |
| 1245 | /* |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1246 | * if we break a large extent up then leave oustanding_extents |
| 1247 | * be, since we've already accounted for the large extent. |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1248 | */ |
| 1249 | if (div64_u64(new_size + root->fs_info->max_extent - 1, |
| 1250 | root->fs_info->max_extent) < num_extents) |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1254 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1255 | BTRFS_I(inode)->outstanding_extents++; |
| 1256 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1263 | * extents so we can keep track of new extents that are just merged onto old |
| 1264 | * extents, such as when we are doing sequential writes, so we can properly |
| 1265 | * account for the metadata space we'll need. |
| 1266 | */ |
| 1267 | static int btrfs_merge_extent_hook(struct inode *inode, |
| 1268 | struct extent_state *new, |
| 1269 | struct extent_state *other) |
| 1270 | { |
| 1271 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1272 | u64 new_size, old_size; |
| 1273 | u64 num_extents; |
| 1274 | |
| 1275 | /* not delalloc, ignore it */ |
| 1276 | if (!(other->state & EXTENT_DELALLOC)) |
| 1277 | return 0; |
| 1278 | |
| 1279 | old_size = other->end - other->start + 1; |
| 1280 | if (new->start < other->start) |
| 1281 | new_size = other->end - new->start + 1; |
| 1282 | else |
| 1283 | new_size = new->end - other->start + 1; |
| 1284 | |
| 1285 | /* we're not bigger than the max, unreserve the space and go */ |
| 1286 | if (new_size <= root->fs_info->max_extent) { |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1287 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1288 | BTRFS_I(inode)->outstanding_extents--; |
| 1289 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * If we grew by another max_extent, just return, we want to keep that |
| 1295 | * reserved amount. |
| 1296 | */ |
| 1297 | num_extents = div64_u64(old_size + root->fs_info->max_extent - 1, |
| 1298 | root->fs_info->max_extent); |
| 1299 | if (div64_u64(new_size + root->fs_info->max_extent - 1, |
| 1300 | root->fs_info->max_extent) > num_extents) |
| 1301 | return 0; |
| 1302 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1303 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1304 | BTRFS_I(inode)->outstanding_extents--; |
| 1305 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1310 | /* |
| 1311 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1312 | * bytes in this file, and to maintain the list of inodes that |
| 1313 | * have pending delalloc work to be done. |
| 1314 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1315 | 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] | 1316 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1317 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1318 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1319 | /* |
| 1320 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1321 | * but in this case, we are only testeing for the DELALLOC |
| 1322 | * bit, which is only set or cleared with irqs on |
| 1323 | */ |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1324 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1325 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1326 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1327 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1328 | BTRFS_I(inode)->outstanding_extents++; |
| 1329 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1330 | btrfs_delalloc_reserve_space(root, inode, end - start + 1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1331 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1332 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1333 | root->fs_info->delalloc_bytes += end - start + 1; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1334 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1335 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1336 | &root->fs_info->delalloc_inodes); |
| 1337 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1338 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1339 | } |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1343 | /* |
| 1344 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1345 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1346 | static int btrfs_clear_bit_hook(struct inode *inode, |
| 1347 | struct extent_state *state, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1348 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1349 | /* |
| 1350 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1351 | * but in this case, we are only testeing for the DELALLOC |
| 1352 | * bit, which is only set or cleared with irqs on |
| 1353 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1354 | if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1355 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1356 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1357 | if (bits & EXTENT_DO_ACCOUNTING) { |
| 1358 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1359 | BTRFS_I(inode)->outstanding_extents--; |
| 1360 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
| 1361 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
| 1362 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1363 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1364 | spin_lock(&root->fs_info->delalloc_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1365 | if (state->end - state->start + 1 > |
| 1366 | root->fs_info->delalloc_bytes) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1367 | printk(KERN_INFO "btrfs warning: delalloc account " |
| 1368 | "%llu %llu\n", |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1369 | (unsigned long long) |
| 1370 | state->end - state->start + 1, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1371 | (unsigned long long) |
| 1372 | root->fs_info->delalloc_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1373 | btrfs_delalloc_free_space(root, inode, (u64)-1); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1374 | root->fs_info->delalloc_bytes = 0; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1375 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1376 | } else { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1377 | btrfs_delalloc_free_space(root, inode, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1378 | state->end - |
| 1379 | state->start + 1); |
| 1380 | root->fs_info->delalloc_bytes -= state->end - |
| 1381 | state->start + 1; |
| 1382 | BTRFS_I(inode)->delalloc_bytes -= state->end - |
| 1383 | state->start + 1; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1384 | } |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1385 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
| 1386 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1387 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1388 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1389 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1390 | } |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1394 | /* |
| 1395 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1396 | * we don't create bios that span stripes or chunks |
| 1397 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1398 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1399 | size_t size, struct bio *bio, |
| 1400 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1401 | { |
| 1402 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1403 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1404 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1405 | u64 length = 0; |
| 1406 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1407 | int ret; |
| 1408 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1409 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1410 | return 0; |
| 1411 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1412 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1413 | map_tree = &root->fs_info->mapping_tree; |
| 1414 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1415 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1416 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1417 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1418 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1419 | return 1; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1420 | return 0; |
| 1421 | } |
| 1422 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1423 | /* |
| 1424 | * in order to insert checksums into the metadata in large chunks, |
| 1425 | * we wait until bio submission time. All the pages in the bio are |
| 1426 | * checksummed and sums are attached onto the ordered extent record. |
| 1427 | * |
| 1428 | * At IO completion time the cums attached on the ordered extent record |
| 1429 | * are inserted into the btree |
| 1430 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1431 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1432 | struct bio *bio, int mirror_num, |
| 1433 | unsigned long bio_flags) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1434 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1435 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1436 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1437 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1438 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1439 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1440 | return 0; |
| 1441 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1442 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1443 | /* |
| 1444 | * in order to insert checksums into the metadata in large chunks, |
| 1445 | * we wait until bio submission time. All the pages in the bio are |
| 1446 | * checksummed and sums are attached onto the ordered extent record. |
| 1447 | * |
| 1448 | * At IO completion time the cums attached on the ordered extent record |
| 1449 | * are inserted into the btree |
| 1450 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1451 | 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] | 1452 | int mirror_num, unsigned long bio_flags) |
| 1453 | { |
| 1454 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1455 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1456 | } |
| 1457 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1458 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1459 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1460 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1461 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1462 | 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] | 1463 | int mirror_num, unsigned long bio_flags) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1464 | { |
| 1465 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1466 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1467 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1468 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1469 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1470 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1471 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 1472 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1473 | |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1474 | if (!(rw & (1 << BIO_RW))) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1475 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1476 | return btrfs_submit_compressed_read(inode, bio, |
| 1477 | mirror_num, bio_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1478 | } else if (!skip_sum) |
| 1479 | btrfs_lookup_bio_sums(root, inode, bio, NULL); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1480 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1481 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1482 | /* csum items have already been cloned */ |
| 1483 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1484 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1485 | /* we're doing a write, do the async checksumming */ |
| 1486 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1487 | inode, rw, bio, mirror_num, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1488 | bio_flags, __btrfs_submit_bio_start, |
| 1489 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1490 | } |
| 1491 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1492 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1493 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1494 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1495 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1496 | /* |
| 1497 | * given a list of ordered sums record them in the inode. This happens |
| 1498 | * at IO completion time based on sums calculated at bio submission time. |
| 1499 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1500 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1501 | struct inode *inode, u64 file_offset, |
| 1502 | struct list_head *list) |
| 1503 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1504 | struct btrfs_ordered_sum *sum; |
| 1505 | |
| 1506 | btrfs_set_trans_block_group(trans, inode); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1507 | |
| 1508 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1509 | btrfs_csum_file_blocks(trans, |
| 1510 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1511 | } |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1515 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end) |
| 1516 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1517 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1518 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1519 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
| 1520 | GFP_NOFS); |
| 1521 | } |
| 1522 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1523 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1524 | struct btrfs_writepage_fixup { |
| 1525 | struct page *page; |
| 1526 | struct btrfs_work work; |
| 1527 | }; |
| 1528 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1529 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1530 | { |
| 1531 | struct btrfs_writepage_fixup *fixup; |
| 1532 | struct btrfs_ordered_extent *ordered; |
| 1533 | struct page *page; |
| 1534 | struct inode *inode; |
| 1535 | u64 page_start; |
| 1536 | u64 page_end; |
| 1537 | |
| 1538 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1539 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1540 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1541 | lock_page(page); |
| 1542 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1543 | ClearPageChecked(page); |
| 1544 | goto out_page; |
| 1545 | } |
| 1546 | |
| 1547 | inode = page->mapping->host; |
| 1548 | page_start = page_offset(page); |
| 1549 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1550 | |
| 1551 | 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] | 1552 | |
| 1553 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1554 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1555 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1556 | |
| 1557 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1558 | if (ordered) { |
| 1559 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, |
| 1560 | page_end, GFP_NOFS); |
| 1561 | unlock_page(page); |
| 1562 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1563 | goto again; |
| 1564 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1565 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1566 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1567 | ClearPageChecked(page); |
| 1568 | out: |
| 1569 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); |
| 1570 | out_page: |
| 1571 | unlock_page(page); |
| 1572 | page_cache_release(page); |
| 1573 | } |
| 1574 | |
| 1575 | /* |
| 1576 | * There are a few paths in the higher layers of the kernel that directly |
| 1577 | * set the page dirty bit without asking the filesystem if it is a |
| 1578 | * good idea. This causes problems because we want to make sure COW |
| 1579 | * properly happens and the data=ordered rules are followed. |
| 1580 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1581 | * 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] | 1582 | * hasn't been properly setup for IO. We kick off an async process |
| 1583 | * to fix it up. The async helper will wait for ordered extents, set |
| 1584 | * the delalloc bit and make it safe to write the page. |
| 1585 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1586 | 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] | 1587 | { |
| 1588 | struct inode *inode = page->mapping->host; |
| 1589 | struct btrfs_writepage_fixup *fixup; |
| 1590 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1591 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1592 | /* this page is properly in the ordered list */ |
| 1593 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1594 | return 0; |
| 1595 | |
| 1596 | if (PageChecked(page)) |
| 1597 | return -EAGAIN; |
| 1598 | |
| 1599 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1600 | if (!fixup) |
| 1601 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1602 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1603 | SetPageChecked(page); |
| 1604 | page_cache_get(page); |
| 1605 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1606 | fixup->page = page; |
| 1607 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1608 | return -EAGAIN; |
| 1609 | } |
| 1610 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1611 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1612 | struct inode *inode, u64 file_pos, |
| 1613 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1614 | u64 num_bytes, u64 ram_bytes, |
| 1615 | u8 compression, u8 encryption, |
| 1616 | u16 other_encoding, int extent_type) |
| 1617 | { |
| 1618 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1619 | struct btrfs_file_extent_item *fi; |
| 1620 | struct btrfs_path *path; |
| 1621 | struct extent_buffer *leaf; |
| 1622 | struct btrfs_key ins; |
| 1623 | u64 hint; |
| 1624 | int ret; |
| 1625 | |
| 1626 | path = btrfs_alloc_path(); |
| 1627 | BUG_ON(!path); |
| 1628 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1629 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1630 | |
| 1631 | /* |
| 1632 | * we may be replacing one extent in the tree with another. |
| 1633 | * The new extent is pinned in the extent map, and we don't want |
| 1634 | * to drop it from the cache until it is completely in the btree. |
| 1635 | * |
| 1636 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1637 | * the caller is expected to unpin it and allow it to be merged |
| 1638 | * with the others. |
| 1639 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1640 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1641 | &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1642 | BUG_ON(ret); |
| 1643 | |
| 1644 | ins.objectid = inode->i_ino; |
| 1645 | ins.offset = file_pos; |
| 1646 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1647 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1648 | BUG_ON(ret); |
| 1649 | leaf = path->nodes[0]; |
| 1650 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1651 | struct btrfs_file_extent_item); |
| 1652 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1653 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1654 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1655 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1656 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1657 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1658 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1659 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1660 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1661 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1662 | |
| 1663 | btrfs_unlock_up_safe(path, 1); |
| 1664 | btrfs_set_lock_blocking(leaf); |
| 1665 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1666 | btrfs_mark_buffer_dirty(leaf); |
| 1667 | |
| 1668 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1669 | |
| 1670 | ins.objectid = disk_bytenr; |
| 1671 | ins.offset = disk_num_bytes; |
| 1672 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1673 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1674 | root->root_key.objectid, |
| 1675 | inode->i_ino, file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1676 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1677 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1678 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1679 | return 0; |
| 1680 | } |
| 1681 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1682 | /* |
| 1683 | * helper function for btrfs_finish_ordered_io, this |
| 1684 | * just reads in some of the csum leaves to prime them into ram |
| 1685 | * before we start the transaction. It limits the amount of btree |
| 1686 | * reads required while inside the transaction. |
| 1687 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1688 | /* as ordered data IO finishes, this gets called so we can finish |
| 1689 | * an ordered extent if the range of bytes in the file it covers are |
| 1690 | * fully written. |
| 1691 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1692 | 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] | 1693 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1694 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1695 | struct btrfs_trans_handle *trans; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1696 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1697 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1698 | int compressed = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1699 | int ret; |
| 1700 | |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame^] | 1701 | ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 1702 | end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1703 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1704 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1705 | BUG_ON(!ordered_extent); |
Josef Bacik | efd049f | 2010-02-02 20:50:10 +0000 | [diff] [blame] | 1706 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1707 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
| 1708 | BUG_ON(!list_empty(&ordered_extent->list)); |
| 1709 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1710 | if (!ret) { |
| 1711 | trans = btrfs_join_transaction(root, 1); |
| 1712 | ret = btrfs_update_inode(trans, root, inode); |
| 1713 | BUG_ON(ret); |
| 1714 | btrfs_end_transaction(trans, root); |
| 1715 | } |
| 1716 | goto out; |
| 1717 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1718 | |
| 1719 | lock_extent(io_tree, ordered_extent->file_offset, |
| 1720 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1721 | GFP_NOFS); |
| 1722 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1723 | trans = btrfs_join_transaction(root, 1); |
| 1724 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1725 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1726 | compressed = 1; |
| 1727 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 1728 | BUG_ON(compressed); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1729 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1730 | ordered_extent->file_offset, |
| 1731 | ordered_extent->file_offset + |
| 1732 | ordered_extent->len); |
| 1733 | BUG_ON(ret); |
| 1734 | } else { |
| 1735 | ret = insert_reserved_file_extent(trans, inode, |
| 1736 | ordered_extent->file_offset, |
| 1737 | ordered_extent->start, |
| 1738 | ordered_extent->disk_len, |
| 1739 | ordered_extent->len, |
| 1740 | ordered_extent->len, |
| 1741 | compressed, 0, 0, |
| 1742 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1743 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1744 | ordered_extent->file_offset, |
| 1745 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1746 | BUG_ON(ret); |
| 1747 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1748 | unlock_extent(io_tree, ordered_extent->file_offset, |
| 1749 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1750 | GFP_NOFS); |
| 1751 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1752 | &ordered_extent->list); |
| 1753 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1754 | /* this also removes the ordered extent from the tree */ |
| 1755 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1756 | ret = btrfs_update_inode(trans, root, inode); |
| 1757 | BUG_ON(ret); |
| 1758 | btrfs_end_transaction(trans, root); |
| 1759 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1760 | /* once for us */ |
| 1761 | btrfs_put_ordered_extent(ordered_extent); |
| 1762 | /* once for the tree */ |
| 1763 | btrfs_put_ordered_extent(ordered_extent); |
| 1764 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1765 | return 0; |
| 1766 | } |
| 1767 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1768 | 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] | 1769 | struct extent_state *state, int uptodate) |
| 1770 | { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1771 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1772 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1773 | } |
| 1774 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1775 | /* |
| 1776 | * When IO fails, either with EIO or csum verification fails, we |
| 1777 | * try other mirrors that might have a good copy of the data. This |
| 1778 | * io_failure_record is used to record state as we go through all the |
| 1779 | * mirrors. If another mirror has good data, the page is set up to date |
| 1780 | * and things continue. If a good mirror can't be found, the original |
| 1781 | * bio end_io callback is called to indicate things have failed. |
| 1782 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1783 | struct io_failure_record { |
| 1784 | struct page *page; |
| 1785 | u64 start; |
| 1786 | u64 len; |
| 1787 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1788 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1789 | int last_mirror; |
| 1790 | }; |
| 1791 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1792 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1793 | struct page *page, u64 start, u64 end, |
| 1794 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1795 | { |
| 1796 | struct io_failure_record *failrec = NULL; |
| 1797 | u64 private; |
| 1798 | struct extent_map *em; |
| 1799 | struct inode *inode = page->mapping->host; |
| 1800 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1801 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1802 | struct bio *bio; |
| 1803 | int num_copies; |
| 1804 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1805 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1806 | u64 logical; |
| 1807 | |
| 1808 | ret = get_state_private(failure_tree, start, &private); |
| 1809 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1810 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1811 | if (!failrec) |
| 1812 | return -ENOMEM; |
| 1813 | failrec->start = start; |
| 1814 | failrec->len = end - start + 1; |
| 1815 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1816 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1817 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1818 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1819 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1820 | if (em->start > start || em->start + em->len < start) { |
| 1821 | free_extent_map(em); |
| 1822 | em = NULL; |
| 1823 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1824 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1825 | |
| 1826 | if (!em || IS_ERR(em)) { |
| 1827 | kfree(failrec); |
| 1828 | return -EIO; |
| 1829 | } |
| 1830 | logical = start - em->start; |
| 1831 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1832 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1833 | logical = em->block_start; |
| 1834 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 1835 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1836 | failrec->logical = logical; |
| 1837 | free_extent_map(em); |
| 1838 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1839 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1840 | set_state_private(failure_tree, start, |
| 1841 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1842 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1843 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1844 | } |
| 1845 | num_copies = btrfs_num_copies( |
| 1846 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1847 | failrec->logical, failrec->len); |
| 1848 | failrec->last_mirror++; |
| 1849 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1850 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1851 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1852 | failrec->start, |
| 1853 | EXTENT_LOCKED); |
| 1854 | if (state && state->start != failrec->start) |
| 1855 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1856 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1857 | } |
| 1858 | if (!state || failrec->last_mirror > num_copies) { |
| 1859 | set_state_private(failure_tree, failrec->start, 0); |
| 1860 | clear_extent_bits(failure_tree, failrec->start, |
| 1861 | failrec->start + failrec->len - 1, |
| 1862 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1863 | kfree(failrec); |
| 1864 | return -EIO; |
| 1865 | } |
| 1866 | bio = bio_alloc(GFP_NOFS, 1); |
| 1867 | bio->bi_private = state; |
| 1868 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1869 | bio->bi_sector = failrec->logical >> 9; |
| 1870 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1871 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1872 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1873 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1874 | if (failed_bio->bi_rw & (1 << BIO_RW)) |
| 1875 | rw = WRITE; |
| 1876 | else |
| 1877 | rw = READ; |
| 1878 | |
| 1879 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1880 | failrec->last_mirror, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1881 | failrec->bio_flags); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1882 | return 0; |
| 1883 | } |
| 1884 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1885 | /* |
| 1886 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1887 | * to see if we need to process or clean up an io_failure_record |
| 1888 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1889 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1890 | { |
| 1891 | u64 private; |
| 1892 | u64 private_failure; |
| 1893 | struct io_failure_record *failure; |
| 1894 | int ret; |
| 1895 | |
| 1896 | private = 0; |
| 1897 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 1898 | (u64)-1, 1, EXTENT_DIRTY)) { |
| 1899 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1900 | start, &private_failure); |
| 1901 | if (ret == 0) { |
| 1902 | failure = (struct io_failure_record *)(unsigned long) |
| 1903 | private_failure; |
| 1904 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1905 | failure->start, 0); |
| 1906 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1907 | failure->start, |
| 1908 | failure->start + failure->len - 1, |
| 1909 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1910 | GFP_NOFS); |
| 1911 | kfree(failure); |
| 1912 | } |
| 1913 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1914 | return 0; |
| 1915 | } |
| 1916 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1917 | /* |
| 1918 | * when reads are done, we need to check csums to verify the data is correct |
| 1919 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1920 | * the io_failure_record routines to find good copies |
| 1921 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1922 | 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] | 1923 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1924 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1925 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1926 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1927 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1928 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1929 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1930 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1931 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1932 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1933 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1934 | if (PageChecked(page)) { |
| 1935 | ClearPageChecked(page); |
| 1936 | goto good; |
| 1937 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1938 | |
| 1939 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1940 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1941 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1942 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1943 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1944 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1945 | GFP_NOFS); |
| 1946 | return 0; |
| 1947 | } |
| 1948 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1949 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1950 | private = state->private; |
| 1951 | ret = 0; |
| 1952 | } else { |
| 1953 | ret = get_state_private(io_tree, start, &private); |
| 1954 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1955 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1956 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1957 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1958 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1959 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1960 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1961 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1962 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1963 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1964 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1965 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1966 | /* if the io failure tree for this inode is non-empty, |
| 1967 | * check to see if we've recovered from a failed IO |
| 1968 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1969 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1970 | return 0; |
| 1971 | |
| 1972 | zeroit: |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 1973 | if (printk_ratelimit()) { |
| 1974 | printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " |
| 1975 | "private %llu\n", page->mapping->host->i_ino, |
| 1976 | (unsigned long long)start, csum, |
| 1977 | (unsigned long long)private); |
| 1978 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1979 | memset(kaddr + offset, 1, end - start + 1); |
| 1980 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1981 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1982 | if (private == 0) |
| 1983 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1984 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1985 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1986 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 1987 | struct delayed_iput { |
| 1988 | struct list_head list; |
| 1989 | struct inode *inode; |
| 1990 | }; |
| 1991 | |
| 1992 | void btrfs_add_delayed_iput(struct inode *inode) |
| 1993 | { |
| 1994 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 1995 | struct delayed_iput *delayed; |
| 1996 | |
| 1997 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 1998 | return; |
| 1999 | |
| 2000 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2001 | delayed->inode = inode; |
| 2002 | |
| 2003 | spin_lock(&fs_info->delayed_iput_lock); |
| 2004 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2005 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2006 | } |
| 2007 | |
| 2008 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2009 | { |
| 2010 | LIST_HEAD(list); |
| 2011 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2012 | struct delayed_iput *delayed; |
| 2013 | int empty; |
| 2014 | |
| 2015 | spin_lock(&fs_info->delayed_iput_lock); |
| 2016 | empty = list_empty(&fs_info->delayed_iputs); |
| 2017 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2018 | if (empty) |
| 2019 | return; |
| 2020 | |
| 2021 | down_read(&root->fs_info->cleanup_work_sem); |
| 2022 | spin_lock(&fs_info->delayed_iput_lock); |
| 2023 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2024 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2025 | |
| 2026 | while (!list_empty(&list)) { |
| 2027 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2028 | list_del(&delayed->list); |
| 2029 | iput(delayed->inode); |
| 2030 | kfree(delayed); |
| 2031 | } |
| 2032 | up_read(&root->fs_info->cleanup_work_sem); |
| 2033 | } |
| 2034 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2035 | /* |
| 2036 | * This creates an orphan entry for the given inode in case something goes |
| 2037 | * wrong in the middle of an unlink/truncate. |
| 2038 | */ |
| 2039 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2040 | { |
| 2041 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2042 | int ret = 0; |
| 2043 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2044 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2045 | |
| 2046 | /* already on the orphan list, we're good */ |
| 2047 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2048 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2049 | return 0; |
| 2050 | } |
| 2051 | |
| 2052 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2053 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2054 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2055 | |
| 2056 | /* |
| 2057 | * insert an orphan item to track this unlinked/truncated file |
| 2058 | */ |
| 2059 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 2060 | |
| 2061 | return ret; |
| 2062 | } |
| 2063 | |
| 2064 | /* |
| 2065 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2066 | * item for this particular inode. |
| 2067 | */ |
| 2068 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2069 | { |
| 2070 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2071 | int ret = 0; |
| 2072 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2073 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2074 | |
| 2075 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2076 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2077 | return 0; |
| 2078 | } |
| 2079 | |
| 2080 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2081 | if (!trans) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2082 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2083 | return 0; |
| 2084 | } |
| 2085 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2086 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2087 | |
| 2088 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 2089 | |
| 2090 | return ret; |
| 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | * this cleans up any orphans that may be left on the list from the last use |
| 2095 | * of this root. |
| 2096 | */ |
| 2097 | void btrfs_orphan_cleanup(struct btrfs_root *root) |
| 2098 | { |
| 2099 | struct btrfs_path *path; |
| 2100 | struct extent_buffer *leaf; |
| 2101 | struct btrfs_item *item; |
| 2102 | struct btrfs_key key, found_key; |
| 2103 | struct btrfs_trans_handle *trans; |
| 2104 | struct inode *inode; |
| 2105 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2106 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2107 | if (!xchg(&root->clean_orphans, 0)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2108 | return; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2109 | |
| 2110 | path = btrfs_alloc_path(); |
| 2111 | BUG_ON(!path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2112 | path->reada = -1; |
| 2113 | |
| 2114 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2115 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2116 | key.offset = (u64)-1; |
| 2117 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2118 | while (1) { |
| 2119 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2120 | if (ret < 0) { |
| 2121 | printk(KERN_ERR "Error searching slot for orphan: %d" |
| 2122 | "\n", ret); |
| 2123 | break; |
| 2124 | } |
| 2125 | |
| 2126 | /* |
| 2127 | * if ret == 0 means we found what we were searching for, which |
| 2128 | * is weird, but possible, so only screw with path if we didnt |
| 2129 | * find the key and see if we have stuff that matches |
| 2130 | */ |
| 2131 | if (ret > 0) { |
| 2132 | if (path->slots[0] == 0) |
| 2133 | break; |
| 2134 | path->slots[0]--; |
| 2135 | } |
| 2136 | |
| 2137 | /* pull out the item */ |
| 2138 | leaf = path->nodes[0]; |
| 2139 | item = btrfs_item_nr(leaf, path->slots[0]); |
| 2140 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2141 | |
| 2142 | /* make sure the item matches what we want */ |
| 2143 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2144 | break; |
| 2145 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2146 | break; |
| 2147 | |
| 2148 | /* release the path since we're done with it */ |
| 2149 | btrfs_release_path(root, path); |
| 2150 | |
| 2151 | /* |
| 2152 | * this is where we are basically btrfs_lookup, without the |
| 2153 | * crossing root thing. we store the inode number in the |
| 2154 | * offset of the orphan item. |
| 2155 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2156 | found_key.objectid = found_key.offset; |
| 2157 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2158 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2159 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2160 | if (IS_ERR(inode)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2161 | break; |
| 2162 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2163 | /* |
| 2164 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2165 | * the proper thing when we hit it |
| 2166 | */ |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2167 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2168 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2169 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2170 | |
| 2171 | /* |
| 2172 | * if this is a bad inode, means we actually succeeded in |
| 2173 | * removing the inode, but not the orphan record, which means |
| 2174 | * we need to manually delete the orphan since iput will just |
| 2175 | * do a destroy_inode |
| 2176 | */ |
| 2177 | if (is_bad_inode(inode)) { |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2178 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2179 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2180 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2181 | iput(inode); |
| 2182 | continue; |
| 2183 | } |
| 2184 | |
| 2185 | /* if we have links, this was a truncate, lets do that */ |
| 2186 | if (inode->i_nlink) { |
| 2187 | nr_truncate++; |
| 2188 | btrfs_truncate(inode); |
| 2189 | } else { |
| 2190 | nr_unlink++; |
| 2191 | } |
| 2192 | |
| 2193 | /* this will do delete_inode and everything for us */ |
| 2194 | iput(inode); |
| 2195 | } |
| 2196 | |
| 2197 | if (nr_unlink) |
| 2198 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2199 | if (nr_truncate) |
| 2200 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
| 2201 | |
| 2202 | btrfs_free_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2203 | } |
| 2204 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2205 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2206 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2207 | * don't find any xattrs, we know there can't be any acls. |
| 2208 | * |
| 2209 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2210 | */ |
| 2211 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2212 | int slot, u64 objectid) |
| 2213 | { |
| 2214 | u32 nritems = btrfs_header_nritems(leaf); |
| 2215 | struct btrfs_key found_key; |
| 2216 | int scanned = 0; |
| 2217 | |
| 2218 | slot++; |
| 2219 | while (slot < nritems) { |
| 2220 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2221 | |
| 2222 | /* we found a different objectid, there must not be acls */ |
| 2223 | if (found_key.objectid != objectid) |
| 2224 | return 0; |
| 2225 | |
| 2226 | /* we found an xattr, assume we've got an acl */ |
| 2227 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2228 | return 1; |
| 2229 | |
| 2230 | /* |
| 2231 | * we found a key greater than an xattr key, there can't |
| 2232 | * be any acls later on |
| 2233 | */ |
| 2234 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2235 | return 0; |
| 2236 | |
| 2237 | slot++; |
| 2238 | scanned++; |
| 2239 | |
| 2240 | /* |
| 2241 | * it goes inode, inode backrefs, xattrs, extents, |
| 2242 | * so if there are a ton of hard links to an inode there can |
| 2243 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2244 | * this is just an optimization |
| 2245 | */ |
| 2246 | if (scanned >= 8) |
| 2247 | break; |
| 2248 | } |
| 2249 | /* we hit the end of the leaf before we found an xattr or |
| 2250 | * something larger than an xattr. We have to assume the inode |
| 2251 | * has acls |
| 2252 | */ |
| 2253 | return 1; |
| 2254 | } |
| 2255 | |
| 2256 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2257 | * read an inode from the btree into the in-memory inode |
| 2258 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2259 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2260 | { |
| 2261 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2262 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2263 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2264 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2265 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2266 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2267 | int maybe_acls; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2268 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2269 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2270 | int ret; |
| 2271 | |
| 2272 | path = btrfs_alloc_path(); |
| 2273 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2274 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2275 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2276 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2277 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2278 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2279 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2280 | leaf = path->nodes[0]; |
| 2281 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2282 | struct btrfs_inode_item); |
| 2283 | |
| 2284 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2285 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2286 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2287 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2288 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2289 | |
| 2290 | tspec = btrfs_inode_atime(inode_item); |
| 2291 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2292 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2293 | |
| 2294 | tspec = btrfs_inode_mtime(inode_item); |
| 2295 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2296 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2297 | |
| 2298 | tspec = btrfs_inode_ctime(inode_item); |
| 2299 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2300 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2301 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2302 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2303 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2304 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2305 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2306 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2307 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2308 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2309 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2310 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2311 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2312 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2313 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2314 | /* |
| 2315 | * try to precache a NULL acl entry for files that don't have |
| 2316 | * any xattrs or acls |
| 2317 | */ |
| 2318 | 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] | 2319 | if (!maybe_acls) |
| 2320 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2321 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2322 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2323 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2324 | btrfs_free_path(path); |
| 2325 | inode_item = NULL; |
| 2326 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2327 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2328 | case S_IFREG: |
| 2329 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2330 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2331 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2332 | inode->i_fop = &btrfs_file_operations; |
| 2333 | inode->i_op = &btrfs_file_inode_operations; |
| 2334 | break; |
| 2335 | case S_IFDIR: |
| 2336 | inode->i_fop = &btrfs_dir_file_operations; |
| 2337 | if (root == root->fs_info->tree_root) |
| 2338 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2339 | else |
| 2340 | inode->i_op = &btrfs_dir_inode_operations; |
| 2341 | break; |
| 2342 | case S_IFLNK: |
| 2343 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2344 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2345 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2346 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2347 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2348 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2349 | init_special_inode(inode, inode->i_mode, rdev); |
| 2350 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2351 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2352 | |
| 2353 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2354 | return; |
| 2355 | |
| 2356 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2357 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2358 | make_bad_inode(inode); |
| 2359 | } |
| 2360 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2361 | /* |
| 2362 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2363 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2364 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2365 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2366 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2367 | struct inode *inode) |
| 2368 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2369 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2370 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2371 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2372 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2373 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2374 | |
| 2375 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2376 | inode->i_atime.tv_sec); |
| 2377 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2378 | inode->i_atime.tv_nsec); |
| 2379 | |
| 2380 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2381 | inode->i_mtime.tv_sec); |
| 2382 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2383 | inode->i_mtime.tv_nsec); |
| 2384 | |
| 2385 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2386 | inode->i_ctime.tv_sec); |
| 2387 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2388 | inode->i_ctime.tv_nsec); |
| 2389 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2390 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2391 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2392 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2393 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2394 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2395 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2396 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2397 | } |
| 2398 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2399 | /* |
| 2400 | * copy everything in the in-memory inode into the btree. |
| 2401 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2402 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2403 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2404 | { |
| 2405 | struct btrfs_inode_item *inode_item; |
| 2406 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2407 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2408 | int ret; |
| 2409 | |
| 2410 | path = btrfs_alloc_path(); |
| 2411 | BUG_ON(!path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2412 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2413 | ret = btrfs_lookup_inode(trans, root, path, |
| 2414 | &BTRFS_I(inode)->location, 1); |
| 2415 | if (ret) { |
| 2416 | if (ret > 0) |
| 2417 | ret = -ENOENT; |
| 2418 | goto failed; |
| 2419 | } |
| 2420 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2421 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2422 | leaf = path->nodes[0]; |
| 2423 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2424 | struct btrfs_inode_item); |
| 2425 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2426 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2427 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2428 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2429 | ret = 0; |
| 2430 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2431 | btrfs_free_path(path); |
| 2432 | return ret; |
| 2433 | } |
| 2434 | |
| 2435 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2436 | /* |
| 2437 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2438 | * recovery code. It remove a link in a directory with a given name, and |
| 2439 | * also drops the back refs in the inode to the directory |
| 2440 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2441 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2442 | struct btrfs_root *root, |
| 2443 | struct inode *dir, struct inode *inode, |
| 2444 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2445 | { |
| 2446 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2447 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2448 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2449 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2450 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2451 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2452 | |
| 2453 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2454 | if (!path) { |
| 2455 | ret = -ENOMEM; |
| 2456 | goto err; |
| 2457 | } |
| 2458 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2459 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2460 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2461 | name, name_len, -1); |
| 2462 | if (IS_ERR(di)) { |
| 2463 | ret = PTR_ERR(di); |
| 2464 | goto err; |
| 2465 | } |
| 2466 | if (!di) { |
| 2467 | ret = -ENOENT; |
| 2468 | goto err; |
| 2469 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2470 | leaf = path->nodes[0]; |
| 2471 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2472 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2473 | if (ret) |
| 2474 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2475 | btrfs_release_path(root, path); |
| 2476 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2477 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2478 | inode->i_ino, |
| 2479 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2480 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2481 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2482 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2483 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2484 | goto err; |
| 2485 | } |
| 2486 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2487 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2488 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2489 | if (IS_ERR(di)) { |
| 2490 | ret = PTR_ERR(di); |
| 2491 | goto err; |
| 2492 | } |
| 2493 | if (!di) { |
| 2494 | ret = -ENOENT; |
| 2495 | goto err; |
| 2496 | } |
| 2497 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2498 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2499 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2500 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 2501 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2502 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2503 | |
| 2504 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2505 | dir, index); |
| 2506 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2507 | err: |
| 2508 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2509 | if (ret) |
| 2510 | goto out; |
| 2511 | |
| 2512 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2513 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2514 | btrfs_update_inode(trans, root, dir); |
| 2515 | btrfs_drop_nlink(inode); |
| 2516 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2517 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2518 | return ret; |
| 2519 | } |
| 2520 | |
| 2521 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2522 | { |
| 2523 | struct btrfs_root *root; |
| 2524 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2525 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2526 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2527 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2528 | |
| 2529 | root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2530 | |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2531 | /* |
| 2532 | * 5 items for unlink inode |
| 2533 | * 1 for orphan |
| 2534 | */ |
| 2535 | ret = btrfs_reserve_metadata_space(root, 6); |
| 2536 | if (ret) |
| 2537 | return ret; |
| 2538 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2539 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2540 | if (IS_ERR(trans)) { |
| 2541 | btrfs_unreserve_metadata_space(root, 6); |
| 2542 | return PTR_ERR(trans); |
| 2543 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2544 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2545 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2546 | |
| 2547 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2548 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2549 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2550 | dentry->d_name.name, dentry->d_name.len); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2551 | |
| 2552 | if (inode->i_nlink == 0) |
| 2553 | ret = btrfs_orphan_add(trans, inode); |
| 2554 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2555 | nr = trans->blocks_used; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2556 | |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2557 | btrfs_end_transaction_throttle(trans, root); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2558 | btrfs_unreserve_metadata_space(root, 6); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2559 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2560 | return ret; |
| 2561 | } |
| 2562 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2563 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2564 | struct btrfs_root *root, |
| 2565 | struct inode *dir, u64 objectid, |
| 2566 | const char *name, int name_len) |
| 2567 | { |
| 2568 | struct btrfs_path *path; |
| 2569 | struct extent_buffer *leaf; |
| 2570 | struct btrfs_dir_item *di; |
| 2571 | struct btrfs_key key; |
| 2572 | u64 index; |
| 2573 | int ret; |
| 2574 | |
| 2575 | path = btrfs_alloc_path(); |
| 2576 | if (!path) |
| 2577 | return -ENOMEM; |
| 2578 | |
| 2579 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2580 | name, name_len, -1); |
| 2581 | BUG_ON(!di || IS_ERR(di)); |
| 2582 | |
| 2583 | leaf = path->nodes[0]; |
| 2584 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2585 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2586 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2587 | BUG_ON(ret); |
| 2588 | btrfs_release_path(root, path); |
| 2589 | |
| 2590 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2591 | objectid, root->root_key.objectid, |
| 2592 | dir->i_ino, &index, name, name_len); |
| 2593 | if (ret < 0) { |
| 2594 | BUG_ON(ret != -ENOENT); |
| 2595 | di = btrfs_search_dir_index_item(root, path, dir->i_ino, |
| 2596 | name, name_len); |
| 2597 | BUG_ON(!di || IS_ERR(di)); |
| 2598 | |
| 2599 | leaf = path->nodes[0]; |
| 2600 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2601 | btrfs_release_path(root, path); |
| 2602 | index = key.offset; |
| 2603 | } |
| 2604 | |
| 2605 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 2606 | index, name, name_len, -1); |
| 2607 | BUG_ON(!di || IS_ERR(di)); |
| 2608 | |
| 2609 | leaf = path->nodes[0]; |
| 2610 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2611 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2612 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2613 | BUG_ON(ret); |
| 2614 | btrfs_release_path(root, path); |
| 2615 | |
| 2616 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2617 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2618 | ret = btrfs_update_inode(trans, root, dir); |
| 2619 | BUG_ON(ret); |
| 2620 | dir->i_sb->s_dirt = 1; |
| 2621 | |
| 2622 | btrfs_free_path(path); |
| 2623 | return 0; |
| 2624 | } |
| 2625 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2626 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 2627 | { |
| 2628 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2629 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2630 | int ret; |
| 2631 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2632 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2633 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2634 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 2635 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2636 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 2637 | return -ENOTEMPTY; |
| 2638 | |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2639 | ret = btrfs_reserve_metadata_space(root, 5); |
| 2640 | if (ret) |
| 2641 | return ret; |
| 2642 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2643 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2644 | if (IS_ERR(trans)) { |
| 2645 | btrfs_unreserve_metadata_space(root, 5); |
| 2646 | return PTR_ERR(trans); |
| 2647 | } |
| 2648 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2649 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2650 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2651 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 2652 | err = btrfs_unlink_subvol(trans, root, dir, |
| 2653 | BTRFS_I(inode)->location.objectid, |
| 2654 | dentry->d_name.name, |
| 2655 | dentry->d_name.len); |
| 2656 | goto out; |
| 2657 | } |
| 2658 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2659 | err = btrfs_orphan_add(trans, inode); |
| 2660 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2661 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2662 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2663 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2664 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2665 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2666 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2667 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2668 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2669 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2670 | ret = btrfs_end_transaction_throttle(trans, root); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2671 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2672 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2673 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2674 | if (ret && !err) |
| 2675 | err = ret; |
| 2676 | return err; |
| 2677 | } |
| 2678 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2679 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2680 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2681 | * when truncating bytes in a file, it is possible to avoid reading |
| 2682 | * the leaves that contain only checksum items. This can be the |
| 2683 | * majority of the IO required to delete a large file, but it must |
| 2684 | * be done carefully. |
| 2685 | * |
| 2686 | * The keys in the level just above the leaves are checked to make sure |
| 2687 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 2688 | * after the new size. |
| 2689 | * |
| 2690 | * Then the key for the next leaf is checked to make sure it also has |
| 2691 | * a checksum item for the same file. If it does, we know our target leaf |
| 2692 | * contains only checksum items, and it can be safely freed without reading |
| 2693 | * it. |
| 2694 | * |
| 2695 | * This is just an optimization targeted at large files. It may do |
| 2696 | * nothing. It will return 0 unless things went badly. |
| 2697 | */ |
| 2698 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 2699 | struct btrfs_root *root, |
| 2700 | struct btrfs_path *path, |
| 2701 | struct inode *inode, u64 new_size) |
| 2702 | { |
| 2703 | struct btrfs_key key; |
| 2704 | int ret; |
| 2705 | int nritems; |
| 2706 | struct btrfs_key found_key; |
| 2707 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2708 | struct btrfs_leaf_ref *ref; |
| 2709 | u64 leaf_gen; |
| 2710 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2711 | |
| 2712 | path->lowest_level = 1; |
| 2713 | key.objectid = inode->i_ino; |
| 2714 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 2715 | key.offset = new_size; |
| 2716 | again: |
| 2717 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2718 | if (ret < 0) |
| 2719 | goto out; |
| 2720 | |
| 2721 | if (path->nodes[1] == NULL) { |
| 2722 | ret = 0; |
| 2723 | goto out; |
| 2724 | } |
| 2725 | ret = 0; |
| 2726 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 2727 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 2728 | |
| 2729 | if (!nritems) |
| 2730 | goto out; |
| 2731 | |
| 2732 | if (path->slots[1] >= nritems) |
| 2733 | goto next_node; |
| 2734 | |
| 2735 | /* did we find a key greater than anything we want to delete? */ |
| 2736 | if (found_key.objectid > inode->i_ino || |
| 2737 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 2738 | goto out; |
| 2739 | |
| 2740 | /* we check the next key in the node to make sure the leave contains |
| 2741 | * only checksum items. This comparison doesn't work if our |
| 2742 | * leaf is the last one in the node |
| 2743 | */ |
| 2744 | if (path->slots[1] + 1 >= nritems) { |
| 2745 | next_node: |
| 2746 | /* search forward from the last key in the node, this |
| 2747 | * will bring us into the next node in the tree |
| 2748 | */ |
| 2749 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 2750 | |
| 2751 | /* unlikely, but we inc below, so check to be safe */ |
| 2752 | if (found_key.offset == (u64)-1) |
| 2753 | goto out; |
| 2754 | |
| 2755 | /* search_forward needs a path with locks held, do the |
| 2756 | * search again for the original key. It is possible |
| 2757 | * this will race with a balance and return a path that |
| 2758 | * we could modify, but this drop is just an optimization |
| 2759 | * and is allowed to miss some leaves. |
| 2760 | */ |
| 2761 | btrfs_release_path(root, path); |
| 2762 | found_key.offset++; |
| 2763 | |
| 2764 | /* setup a max key for search_forward */ |
| 2765 | other_key.offset = (u64)-1; |
| 2766 | other_key.type = key.type; |
| 2767 | other_key.objectid = key.objectid; |
| 2768 | |
| 2769 | path->keep_locks = 1; |
| 2770 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 2771 | path, 0, 0); |
| 2772 | path->keep_locks = 0; |
| 2773 | if (ret || found_key.objectid != key.objectid || |
| 2774 | found_key.type != key.type) { |
| 2775 | ret = 0; |
| 2776 | goto out; |
| 2777 | } |
| 2778 | |
| 2779 | key.offset = found_key.offset; |
| 2780 | btrfs_release_path(root, path); |
| 2781 | cond_resched(); |
| 2782 | goto again; |
| 2783 | } |
| 2784 | |
| 2785 | /* we know there's one more slot after us in the tree, |
| 2786 | * read that key so we can verify it is also a checksum item |
| 2787 | */ |
| 2788 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 2789 | |
| 2790 | if (found_key.objectid < inode->i_ino) |
| 2791 | goto next_key; |
| 2792 | |
| 2793 | if (found_key.type != key.type || found_key.offset < new_size) |
| 2794 | goto next_key; |
| 2795 | |
| 2796 | /* |
| 2797 | * if the key for the next leaf isn't a csum key from this objectid, |
| 2798 | * we can't be sure there aren't good items inside this leaf. |
| 2799 | * Bail out |
| 2800 | */ |
| 2801 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 2802 | goto out; |
| 2803 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2804 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 2805 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2806 | /* |
| 2807 | * it is safe to delete this leaf, it contains only |
| 2808 | * csum items from this inode at an offset >= new_size |
| 2809 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2810 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2811 | BUG_ON(ret); |
| 2812 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2813 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 2814 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 2815 | if (ref) { |
| 2816 | ref->root_gen = root->root_key.offset; |
| 2817 | ref->bytenr = leaf_start; |
| 2818 | ref->owner = 0; |
| 2819 | ref->generation = leaf_gen; |
| 2820 | ref->nritems = 0; |
| 2821 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2822 | btrfs_sort_leaf_ref(ref); |
| 2823 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2824 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 2825 | WARN_ON(ret); |
| 2826 | btrfs_free_leaf_ref(root, ref); |
| 2827 | } else { |
| 2828 | WARN_ON(1); |
| 2829 | } |
| 2830 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2831 | next_key: |
| 2832 | btrfs_release_path(root, path); |
| 2833 | |
| 2834 | if (other_key.objectid == inode->i_ino && |
| 2835 | other_key.type == key.type && other_key.offset > key.offset) { |
| 2836 | key.offset = other_key.offset; |
| 2837 | cond_resched(); |
| 2838 | goto again; |
| 2839 | } |
| 2840 | ret = 0; |
| 2841 | out: |
| 2842 | /* fixup any changes we've made to the path */ |
| 2843 | path->lowest_level = 0; |
| 2844 | path->keep_locks = 0; |
| 2845 | btrfs_release_path(root, path); |
| 2846 | return ret; |
| 2847 | } |
| 2848 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2849 | #endif |
| 2850 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2851 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2852 | * this can truncate away extent items, csum items and directory items. |
| 2853 | * 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] | 2854 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2855 | * |
| 2856 | * csum items that cross the new i_size are truncated to the new size |
| 2857 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2858 | * |
| 2859 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 2860 | * 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] | 2861 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2862 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 2863 | struct btrfs_root *root, |
| 2864 | struct inode *inode, |
| 2865 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2866 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2867 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2868 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2869 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2870 | struct btrfs_key key; |
| 2871 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2872 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2873 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2874 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2875 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2876 | u64 mask = root->sectorsize - 1; |
| 2877 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2878 | int found_extent; |
| 2879 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2880 | int pending_del_nr = 0; |
| 2881 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2882 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2883 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2884 | int ret; |
| 2885 | int err = 0; |
| 2886 | |
| 2887 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2888 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2889 | if (root->ref_cows) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2890 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2891 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2892 | path = btrfs_alloc_path(); |
| 2893 | BUG_ON(!path); |
Julia Lawall | 33c17ad | 2009-07-22 16:49:01 -0400 | [diff] [blame] | 2894 | path->reada = -1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2895 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2896 | key.objectid = inode->i_ino; |
| 2897 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2898 | key.type = (u8)-1; |
| 2899 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2900 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2901 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2902 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2903 | if (ret < 0) { |
| 2904 | err = ret; |
| 2905 | goto out; |
| 2906 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2907 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2908 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2909 | /* there are no items in the tree for us to truncate, we're |
| 2910 | * done |
| 2911 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2912 | if (path->slots[0] == 0) |
| 2913 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2914 | path->slots[0]--; |
| 2915 | } |
| 2916 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2917 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2918 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2919 | leaf = path->nodes[0]; |
| 2920 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2921 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2922 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2923 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2924 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2925 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2926 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2927 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2928 | break; |
| 2929 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2930 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2931 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2932 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2933 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2934 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2935 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 2936 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 2937 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 2938 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2939 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2940 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2941 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2942 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2943 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2944 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2945 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 2946 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2947 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2948 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2949 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 2950 | } else { |
| 2951 | if (item_end < new_size) |
| 2952 | break; |
| 2953 | if (found_key.offset >= new_size) |
| 2954 | del_item = 1; |
| 2955 | else |
| 2956 | del_item = 0; |
| 2957 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2958 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2959 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2960 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 2961 | goto delete; |
| 2962 | |
| 2963 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2964 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2965 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2966 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2967 | u64 orig_num_bytes = |
| 2968 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2969 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2970 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 2971 | extent_num_bytes = extent_num_bytes & |
| 2972 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2973 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2974 | extent_num_bytes); |
| 2975 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2976 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2977 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2978 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2979 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2980 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2981 | extent_num_bytes = |
| 2982 | btrfs_file_extent_disk_num_bytes(leaf, |
| 2983 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2984 | extent_offset = found_key.offset - |
| 2985 | btrfs_file_extent_offset(leaf, fi); |
| 2986 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2987 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2988 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2989 | if (extent_start != 0) { |
| 2990 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2991 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2992 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2993 | } |
| 2994 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2995 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2996 | /* |
| 2997 | * we can't truncate inline items that have had |
| 2998 | * special encodings |
| 2999 | */ |
| 3000 | if (!del_item && |
| 3001 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3002 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3003 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3004 | u32 size = new_size - found_key.offset; |
| 3005 | |
| 3006 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3007 | inode_sub_bytes(inode, item_end + 1 - |
| 3008 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3009 | } |
| 3010 | size = |
| 3011 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3012 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3013 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3014 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3015 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3016 | inode_sub_bytes(inode, item_end + 1 - |
| 3017 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3018 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3019 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3020 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3021 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3022 | if (!pending_del_nr) { |
| 3023 | /* no pending yet, add ourselves */ |
| 3024 | pending_del_slot = path->slots[0]; |
| 3025 | pending_del_nr = 1; |
| 3026 | } else if (pending_del_nr && |
| 3027 | path->slots[0] + 1 == pending_del_slot) { |
| 3028 | /* hop on the pending chunk */ |
| 3029 | pending_del_nr++; |
| 3030 | pending_del_slot = path->slots[0]; |
| 3031 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3032 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3033 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3034 | } else { |
| 3035 | break; |
| 3036 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3037 | if (found_extent && root->ref_cows) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3038 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3039 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3040 | extent_num_bytes, 0, |
| 3041 | btrfs_header_owner(leaf), |
| 3042 | inode->i_ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3043 | BUG_ON(ret); |
| 3044 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3045 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3046 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3047 | break; |
| 3048 | |
| 3049 | if (path->slots[0] == 0 || |
| 3050 | path->slots[0] != pending_del_slot) { |
| 3051 | if (root->ref_cows) { |
| 3052 | err = -EAGAIN; |
| 3053 | goto out; |
| 3054 | } |
| 3055 | if (pending_del_nr) { |
| 3056 | ret = btrfs_del_items(trans, root, path, |
| 3057 | pending_del_slot, |
| 3058 | pending_del_nr); |
| 3059 | BUG_ON(ret); |
| 3060 | pending_del_nr = 0; |
| 3061 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3062 | btrfs_release_path(root, path); |
| 3063 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3064 | } else { |
| 3065 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3066 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3067 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3068 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3069 | if (pending_del_nr) { |
| 3070 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3071 | pending_del_nr); |
| 3072 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3073 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3074 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3075 | } |
| 3076 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3077 | /* |
| 3078 | * taken from block_truncate_page, but does cow as it zeros out |
| 3079 | * any bytes left in the last page in the file. |
| 3080 | */ |
| 3081 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3082 | { |
| 3083 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3084 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3085 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3086 | struct btrfs_ordered_extent *ordered; |
| 3087 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3088 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3089 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3090 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3091 | struct page *page; |
| 3092 | int ret = 0; |
| 3093 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3094 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3095 | |
| 3096 | if ((offset & (blocksize - 1)) == 0) |
| 3097 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3098 | ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE); |
| 3099 | if (ret) |
| 3100 | goto out; |
| 3101 | |
| 3102 | ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1); |
| 3103 | if (ret) |
| 3104 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3105 | |
| 3106 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3107 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3108 | page = grab_cache_page(mapping, index); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3109 | if (!page) { |
| 3110 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
| 3111 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3112 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3113 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3114 | |
| 3115 | page_start = page_offset(page); |
| 3116 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3117 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3118 | if (!PageUptodate(page)) { |
| 3119 | ret = btrfs_readpage(NULL, page); |
| 3120 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3121 | if (page->mapping != mapping) { |
| 3122 | unlock_page(page); |
| 3123 | page_cache_release(page); |
| 3124 | goto again; |
| 3125 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3126 | if (!PageUptodate(page)) { |
| 3127 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3128 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3129 | } |
| 3130 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3131 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3132 | |
| 3133 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3134 | set_page_extent_mapped(page); |
| 3135 | |
| 3136 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3137 | if (ordered) { |
| 3138 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3139 | unlock_page(page); |
| 3140 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3141 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3142 | btrfs_put_ordered_extent(ordered); |
| 3143 | goto again; |
| 3144 | } |
| 3145 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3146 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 3147 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
| 3148 | GFP_NOFS); |
| 3149 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3150 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end); |
| 3151 | if (ret) { |
| 3152 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3153 | goto out_unlock; |
| 3154 | } |
| 3155 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3156 | ret = 0; |
| 3157 | if (offset != PAGE_CACHE_SIZE) { |
| 3158 | kaddr = kmap(page); |
| 3159 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3160 | flush_dcache_page(page); |
| 3161 | kunmap(page); |
| 3162 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3163 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3164 | set_page_dirty(page); |
| 3165 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3166 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3167 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3168 | if (ret) |
| 3169 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
| 3170 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3171 | unlock_page(page); |
| 3172 | page_cache_release(page); |
| 3173 | out: |
| 3174 | return ret; |
| 3175 | } |
| 3176 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3177 | int btrfs_cont_expand(struct inode *inode, loff_t size) |
| 3178 | { |
| 3179 | struct btrfs_trans_handle *trans; |
| 3180 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3181 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3182 | struct extent_map *em; |
| 3183 | u64 mask = root->sectorsize - 1; |
| 3184 | u64 hole_start = (inode->i_size + mask) & ~mask; |
| 3185 | u64 block_end = (size + mask) & ~mask; |
| 3186 | u64 last_byte; |
| 3187 | u64 cur_offset; |
| 3188 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3189 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3190 | |
| 3191 | if (size <= hole_start) |
| 3192 | return 0; |
| 3193 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3194 | while (1) { |
| 3195 | struct btrfs_ordered_extent *ordered; |
| 3196 | btrfs_wait_ordered_range(inode, hole_start, |
| 3197 | block_end - hole_start); |
| 3198 | lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3199 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3200 | if (!ordered) |
| 3201 | break; |
| 3202 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3203 | btrfs_put_ordered_extent(ordered); |
| 3204 | } |
| 3205 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3206 | cur_offset = hole_start; |
| 3207 | while (1) { |
| 3208 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3209 | block_end - cur_offset, 0); |
| 3210 | BUG_ON(IS_ERR(em) || !em); |
| 3211 | last_byte = min(extent_map_end(em), block_end); |
| 3212 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3213 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3214 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3215 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3216 | |
| 3217 | err = btrfs_reserve_metadata_space(root, 2); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3218 | if (err) |
| 3219 | break; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3220 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3221 | trans = btrfs_start_transaction(root, 1); |
| 3222 | btrfs_set_trans_block_group(trans, inode); |
| 3223 | |
| 3224 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3225 | cur_offset + hole_size, |
| 3226 | &hint_byte, 1); |
| 3227 | BUG_ON(err); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3228 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3229 | err = btrfs_insert_file_extent(trans, root, |
| 3230 | inode->i_ino, cur_offset, 0, |
| 3231 | 0, hole_size, 0, hole_size, |
| 3232 | 0, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3233 | BUG_ON(err); |
| 3234 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3235 | btrfs_drop_extent_cache(inode, hole_start, |
| 3236 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3237 | |
| 3238 | btrfs_end_transaction(trans, root); |
| 3239 | btrfs_unreserve_metadata_space(root, 2); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3240 | } |
| 3241 | free_extent_map(em); |
| 3242 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3243 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3244 | break; |
| 3245 | } |
| 3246 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3247 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3248 | return err; |
| 3249 | } |
| 3250 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3251 | static int btrfs_setattr_size(struct inode *inode, struct iattr *attr) |
| 3252 | { |
| 3253 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3254 | struct btrfs_trans_handle *trans; |
| 3255 | unsigned long nr; |
| 3256 | int ret; |
| 3257 | |
| 3258 | if (attr->ia_size == inode->i_size) |
| 3259 | return 0; |
| 3260 | |
| 3261 | if (attr->ia_size > inode->i_size) { |
| 3262 | unsigned long limit; |
| 3263 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
| 3264 | if (attr->ia_size > inode->i_sb->s_maxbytes) |
| 3265 | return -EFBIG; |
| 3266 | if (limit != RLIM_INFINITY && attr->ia_size > limit) { |
| 3267 | send_sig(SIGXFSZ, current, 0); |
| 3268 | return -EFBIG; |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | ret = btrfs_reserve_metadata_space(root, 1); |
| 3273 | if (ret) |
| 3274 | return ret; |
| 3275 | |
| 3276 | trans = btrfs_start_transaction(root, 1); |
| 3277 | btrfs_set_trans_block_group(trans, inode); |
| 3278 | |
| 3279 | ret = btrfs_orphan_add(trans, inode); |
| 3280 | BUG_ON(ret); |
| 3281 | |
| 3282 | nr = trans->blocks_used; |
| 3283 | btrfs_end_transaction(trans, root); |
| 3284 | btrfs_unreserve_metadata_space(root, 1); |
| 3285 | btrfs_btree_balance_dirty(root, nr); |
| 3286 | |
| 3287 | if (attr->ia_size > inode->i_size) { |
| 3288 | ret = btrfs_cont_expand(inode, attr->ia_size); |
| 3289 | if (ret) { |
| 3290 | btrfs_truncate(inode); |
| 3291 | return ret; |
| 3292 | } |
| 3293 | |
| 3294 | i_size_write(inode, attr->ia_size); |
| 3295 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
| 3296 | |
| 3297 | trans = btrfs_start_transaction(root, 1); |
| 3298 | btrfs_set_trans_block_group(trans, inode); |
| 3299 | |
| 3300 | ret = btrfs_update_inode(trans, root, inode); |
| 3301 | BUG_ON(ret); |
| 3302 | if (inode->i_nlink > 0) { |
| 3303 | ret = btrfs_orphan_del(trans, inode); |
| 3304 | BUG_ON(ret); |
| 3305 | } |
| 3306 | nr = trans->blocks_used; |
| 3307 | btrfs_end_transaction(trans, root); |
| 3308 | btrfs_btree_balance_dirty(root, nr); |
| 3309 | return 0; |
| 3310 | } |
| 3311 | |
| 3312 | /* |
| 3313 | * We're truncating a file that used to have good data down to |
| 3314 | * zero. Make sure it gets into the ordered flush list so that |
| 3315 | * any new writes get down to disk quickly. |
| 3316 | */ |
| 3317 | if (attr->ia_size == 0) |
| 3318 | BTRFS_I(inode)->ordered_data_close = 1; |
| 3319 | |
| 3320 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3321 | ret = vmtruncate(inode, attr->ia_size); |
| 3322 | BUG_ON(ret); |
| 3323 | |
| 3324 | return 0; |
| 3325 | } |
| 3326 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3327 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3328 | { |
| 3329 | struct inode *inode = dentry->d_inode; |
| 3330 | int err; |
| 3331 | |
| 3332 | err = inode_change_ok(inode, attr); |
| 3333 | if (err) |
| 3334 | return err; |
| 3335 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3336 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3337 | err = btrfs_setattr_size(inode, attr); |
| 3338 | if (err) |
| 3339 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3340 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3341 | attr->ia_valid &= ~ATTR_SIZE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3342 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3343 | if (attr->ia_valid) |
| 3344 | err = inode_setattr(inode, attr); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3345 | |
| 3346 | if (!err && ((attr->ia_valid & ATTR_MODE))) |
| 3347 | err = btrfs_acl_chmod(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3348 | return err; |
| 3349 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3350 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3351 | void btrfs_delete_inode(struct inode *inode) |
| 3352 | { |
| 3353 | struct btrfs_trans_handle *trans; |
| 3354 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3355 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3356 | int ret; |
| 3357 | |
| 3358 | truncate_inode_pages(&inode->i_data, 0); |
| 3359 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3360 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3361 | goto no_delete; |
| 3362 | } |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3363 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3364 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3365 | if (root->fs_info->log_root_recovering) { |
| 3366 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3367 | goto no_delete; |
| 3368 | } |
| 3369 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3370 | if (inode->i_nlink > 0) { |
| 3371 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3372 | goto no_delete; |
| 3373 | } |
| 3374 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3375 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3376 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3377 | while (1) { |
| 3378 | trans = btrfs_start_transaction(root, 1); |
| 3379 | btrfs_set_trans_block_group(trans, inode); |
| 3380 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
| 3381 | |
| 3382 | if (ret != -EAGAIN) |
| 3383 | break; |
| 3384 | |
| 3385 | nr = trans->blocks_used; |
| 3386 | btrfs_end_transaction(trans, root); |
| 3387 | trans = NULL; |
| 3388 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3389 | } |
| 3390 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3391 | if (ret == 0) { |
| 3392 | ret = btrfs_orphan_del(trans, inode); |
| 3393 | BUG_ON(ret); |
| 3394 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3395 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3396 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3397 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3398 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3399 | no_delete: |
| 3400 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3401 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | /* |
| 3405 | * this returns the key found in the dir entry in the location pointer. |
| 3406 | * If no dir entries were found, location->objectid is 0. |
| 3407 | */ |
| 3408 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3409 | struct btrfs_key *location) |
| 3410 | { |
| 3411 | const char *name = dentry->d_name.name; |
| 3412 | int namelen = dentry->d_name.len; |
| 3413 | struct btrfs_dir_item *di; |
| 3414 | struct btrfs_path *path; |
| 3415 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3416 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3417 | |
| 3418 | path = btrfs_alloc_path(); |
| 3419 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3420 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3421 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 3422 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3423 | if (IS_ERR(di)) |
| 3424 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3425 | |
| 3426 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3427 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3428 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3429 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3430 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3431 | btrfs_free_path(path); |
| 3432 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3433 | out_err: |
| 3434 | location->objectid = 0; |
| 3435 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /* |
| 3439 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3440 | * needs to be changed to reflect the root directory of the tree root. This |
| 3441 | * is kind of like crossing a mount point. |
| 3442 | */ |
| 3443 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3444 | struct inode *dir, |
| 3445 | struct dentry *dentry, |
| 3446 | struct btrfs_key *location, |
| 3447 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3448 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3449 | struct btrfs_path *path; |
| 3450 | struct btrfs_root *new_root; |
| 3451 | struct btrfs_root_ref *ref; |
| 3452 | struct extent_buffer *leaf; |
| 3453 | int ret; |
| 3454 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3455 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3456 | path = btrfs_alloc_path(); |
| 3457 | if (!path) { |
| 3458 | err = -ENOMEM; |
| 3459 | goto out; |
| 3460 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3461 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3462 | err = -ENOENT; |
| 3463 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3464 | BTRFS_I(dir)->root->root_key.objectid, |
| 3465 | location->objectid); |
| 3466 | if (ret) { |
| 3467 | if (ret < 0) |
| 3468 | err = ret; |
| 3469 | goto out; |
| 3470 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3471 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3472 | leaf = path->nodes[0]; |
| 3473 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 3474 | if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || |
| 3475 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3476 | goto out; |
| 3477 | |
| 3478 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3479 | (unsigned long)(ref + 1), |
| 3480 | dentry->d_name.len); |
| 3481 | if (ret) |
| 3482 | goto out; |
| 3483 | |
| 3484 | btrfs_release_path(root->fs_info->tree_root, path); |
| 3485 | |
| 3486 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3487 | if (IS_ERR(new_root)) { |
| 3488 | err = PTR_ERR(new_root); |
| 3489 | goto out; |
| 3490 | } |
| 3491 | |
| 3492 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3493 | err = -ENOENT; |
| 3494 | goto out; |
| 3495 | } |
| 3496 | |
| 3497 | *sub_root = new_root; |
| 3498 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3499 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3500 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3501 | err = 0; |
| 3502 | out: |
| 3503 | btrfs_free_path(path); |
| 3504 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3505 | } |
| 3506 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3507 | static void inode_tree_add(struct inode *inode) |
| 3508 | { |
| 3509 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3510 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3511 | struct rb_node **p; |
| 3512 | struct rb_node *parent; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3513 | again: |
| 3514 | p = &root->inode_tree.rb_node; |
| 3515 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3516 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3517 | if (hlist_unhashed(&inode->i_hash)) |
| 3518 | return; |
| 3519 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3520 | spin_lock(&root->inode_lock); |
| 3521 | while (*p) { |
| 3522 | parent = *p; |
| 3523 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3524 | |
| 3525 | if (inode->i_ino < entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3526 | p = &parent->rb_left; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3527 | else if (inode->i_ino > entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3528 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3529 | else { |
| 3530 | WARN_ON(!(entry->vfs_inode.i_state & |
| 3531 | (I_WILL_FREE | I_FREEING | I_CLEAR))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3532 | rb_erase(parent, &root->inode_tree); |
| 3533 | RB_CLEAR_NODE(parent); |
| 3534 | spin_unlock(&root->inode_lock); |
| 3535 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3539 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3540 | spin_unlock(&root->inode_lock); |
| 3541 | } |
| 3542 | |
| 3543 | static void inode_tree_del(struct inode *inode) |
| 3544 | { |
| 3545 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3546 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3547 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3548 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3549 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3550 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3551 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3552 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3553 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3554 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3555 | |
| 3556 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
| 3557 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3558 | spin_lock(&root->inode_lock); |
| 3559 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3560 | spin_unlock(&root->inode_lock); |
| 3561 | if (empty) |
| 3562 | btrfs_add_dead_root(root); |
| 3563 | } |
| 3564 | } |
| 3565 | |
| 3566 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 3567 | { |
| 3568 | struct rb_node *node; |
| 3569 | struct rb_node *prev; |
| 3570 | struct btrfs_inode *entry; |
| 3571 | struct inode *inode; |
| 3572 | u64 objectid = 0; |
| 3573 | |
| 3574 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3575 | |
| 3576 | spin_lock(&root->inode_lock); |
| 3577 | again: |
| 3578 | node = root->inode_tree.rb_node; |
| 3579 | prev = NULL; |
| 3580 | while (node) { |
| 3581 | prev = node; |
| 3582 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3583 | |
| 3584 | if (objectid < entry->vfs_inode.i_ino) |
| 3585 | node = node->rb_left; |
| 3586 | else if (objectid > entry->vfs_inode.i_ino) |
| 3587 | node = node->rb_right; |
| 3588 | else |
| 3589 | break; |
| 3590 | } |
| 3591 | if (!node) { |
| 3592 | while (prev) { |
| 3593 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
| 3594 | if (objectid <= entry->vfs_inode.i_ino) { |
| 3595 | node = prev; |
| 3596 | break; |
| 3597 | } |
| 3598 | prev = rb_next(prev); |
| 3599 | } |
| 3600 | } |
| 3601 | while (node) { |
| 3602 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3603 | objectid = entry->vfs_inode.i_ino + 1; |
| 3604 | inode = igrab(&entry->vfs_inode); |
| 3605 | if (inode) { |
| 3606 | spin_unlock(&root->inode_lock); |
| 3607 | if (atomic_read(&inode->i_count) > 1) |
| 3608 | d_prune_aliases(inode); |
| 3609 | /* |
| 3610 | * btrfs_drop_inode will remove it from |
| 3611 | * the inode cache when its usage count |
| 3612 | * hits zero. |
| 3613 | */ |
| 3614 | iput(inode); |
| 3615 | cond_resched(); |
| 3616 | spin_lock(&root->inode_lock); |
| 3617 | goto again; |
| 3618 | } |
| 3619 | |
| 3620 | if (cond_resched_lock(&root->inode_lock)) |
| 3621 | goto again; |
| 3622 | |
| 3623 | node = rb_next(node); |
| 3624 | } |
| 3625 | spin_unlock(&root->inode_lock); |
| 3626 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3627 | } |
| 3628 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3629 | static noinline void init_btrfs_i(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3630 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3631 | struct btrfs_inode *bi = BTRFS_I(inode); |
| 3632 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3633 | bi->generation = 0; |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 3634 | bi->sequence = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3635 | bi->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 3636 | bi->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3637 | bi->logged_trans = 0; |
| 3638 | bi->delalloc_bytes = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3639 | bi->reserved_bytes = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3640 | bi->disk_i_size = 0; |
| 3641 | bi->flags = 0; |
| 3642 | bi->index_cnt = (u64)-1; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3643 | bi->last_unlink_trans = 0; |
Chris Mason | 2757495 | 2009-05-14 13:10:02 -0400 | [diff] [blame] | 3644 | bi->ordered_data_close = 0; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3645 | bi->force_compress = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3646 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 3647 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3648 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3649 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
| 3650 | inode->i_mapping, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3651 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3652 | INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3653 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 3654 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3655 | mutex_init(&BTRFS_I(inode)->log_mutex); |
| 3656 | } |
| 3657 | |
| 3658 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 3659 | { |
| 3660 | struct btrfs_iget_args *args = p; |
| 3661 | inode->i_ino = args->ino; |
| 3662 | init_btrfs_i(inode); |
| 3663 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3664 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3665 | return 0; |
| 3666 | } |
| 3667 | |
| 3668 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 3669 | { |
| 3670 | struct btrfs_iget_args *args = opaque; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3671 | return args->ino == inode->i_ino && |
| 3672 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3673 | } |
| 3674 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3675 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 3676 | u64 objectid, |
| 3677 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3678 | { |
| 3679 | struct inode *inode; |
| 3680 | struct btrfs_iget_args args; |
| 3681 | args.ino = objectid; |
| 3682 | args.root = root; |
| 3683 | |
| 3684 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 3685 | btrfs_init_locked_inode, |
| 3686 | (void *)&args); |
| 3687 | return inode; |
| 3688 | } |
| 3689 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3690 | /* Get an inode object given its location and corresponding root. |
| 3691 | * Returns in *is_new if the inode was read from disk |
| 3692 | */ |
| 3693 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3694 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3695 | { |
| 3696 | struct inode *inode; |
| 3697 | |
| 3698 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 3699 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3700 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3701 | |
| 3702 | if (inode->i_state & I_NEW) { |
| 3703 | BTRFS_I(inode)->root = root; |
| 3704 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 3705 | btrfs_read_locked_inode(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3706 | |
| 3707 | inode_tree_add(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3708 | unlock_new_inode(inode); |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3709 | if (new) |
| 3710 | *new = 1; |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3711 | } |
| 3712 | |
| 3713 | return inode; |
| 3714 | } |
| 3715 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3716 | static struct inode *new_simple_dir(struct super_block *s, |
| 3717 | struct btrfs_key *key, |
| 3718 | struct btrfs_root *root) |
| 3719 | { |
| 3720 | struct inode *inode = new_inode(s); |
| 3721 | |
| 3722 | if (!inode) |
| 3723 | return ERR_PTR(-ENOMEM); |
| 3724 | |
| 3725 | init_btrfs_i(inode); |
| 3726 | |
| 3727 | BTRFS_I(inode)->root = root; |
| 3728 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 3729 | BTRFS_I(inode)->dummy_inode = 1; |
| 3730 | |
| 3731 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 3732 | inode->i_op = &simple_dir_inode_operations; |
| 3733 | inode->i_fop = &simple_dir_operations; |
| 3734 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 3735 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 3736 | |
| 3737 | return inode; |
| 3738 | } |
| 3739 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3740 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3741 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3742 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3743 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3744 | struct btrfs_root *sub_root = root; |
| 3745 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3746 | int index; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3747 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3748 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3749 | dentry->d_op = &btrfs_dentry_operations; |
| 3750 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3751 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 3752 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3753 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3754 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3755 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3756 | if (ret < 0) |
| 3757 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3758 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3759 | if (location.objectid == 0) |
| 3760 | return NULL; |
| 3761 | |
| 3762 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3763 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3764 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3765 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3766 | |
| 3767 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 3768 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3769 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3770 | ret = fixup_tree_root_location(root, dir, dentry, |
| 3771 | &location, &sub_root); |
| 3772 | if (ret < 0) { |
| 3773 | if (ret != -ENOENT) |
| 3774 | inode = ERR_PTR(ret); |
| 3775 | else |
| 3776 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 3777 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3778 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3779 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3780 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 3781 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3782 | if (root != sub_root) { |
| 3783 | down_read(&root->fs_info->cleanup_work_sem); |
| 3784 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
| 3785 | btrfs_orphan_cleanup(sub_root); |
| 3786 | up_read(&root->fs_info->cleanup_work_sem); |
| 3787 | } |
| 3788 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3789 | return inode; |
| 3790 | } |
| 3791 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3792 | static int btrfs_dentry_delete(struct dentry *dentry) |
| 3793 | { |
| 3794 | struct btrfs_root *root; |
| 3795 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 3796 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 3797 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3798 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 3799 | if (dentry->d_inode) { |
| 3800 | root = BTRFS_I(dentry->d_inode)->root; |
| 3801 | if (btrfs_root_refs(&root->root_item) == 0) |
| 3802 | return 1; |
| 3803 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3804 | return 0; |
| 3805 | } |
| 3806 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3807 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 3808 | struct nameidata *nd) |
| 3809 | { |
| 3810 | struct inode *inode; |
| 3811 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3812 | inode = btrfs_lookup_dentry(dir, dentry); |
| 3813 | if (IS_ERR(inode)) |
| 3814 | return ERR_CAST(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3815 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3816 | return d_splice_alias(inode, dentry); |
| 3817 | } |
| 3818 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3819 | static unsigned char btrfs_filetype_table[] = { |
| 3820 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 3821 | }; |
| 3822 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 3823 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 3824 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3825 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 3826 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3827 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3828 | struct btrfs_item *item; |
| 3829 | struct btrfs_dir_item *di; |
| 3830 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3831 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3832 | struct btrfs_path *path; |
| 3833 | int ret; |
| 3834 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3835 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3836 | int slot; |
| 3837 | int advance; |
| 3838 | unsigned char d_type; |
| 3839 | int over = 0; |
| 3840 | u32 di_cur; |
| 3841 | u32 di_total; |
| 3842 | u32 di_len; |
| 3843 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3844 | char tmp_name[32]; |
| 3845 | char *name_ptr; |
| 3846 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3847 | |
| 3848 | /* FIXME, use a real flag for deciding about the key type */ |
| 3849 | if (root->fs_info->tree_root == root) |
| 3850 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3851 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3852 | /* special case for "." */ |
| 3853 | if (filp->f_pos == 0) { |
| 3854 | over = filldir(dirent, ".", 1, |
| 3855 | 1, inode->i_ino, |
| 3856 | DT_DIR); |
| 3857 | if (over) |
| 3858 | return 0; |
| 3859 | filp->f_pos = 1; |
| 3860 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3861 | /* special case for .., just use the back ref */ |
| 3862 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3863 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3864 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3865 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3866 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3867 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3868 | filp->f_pos = 2; |
| 3869 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3870 | path = btrfs_alloc_path(); |
| 3871 | path->reada = 2; |
| 3872 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3873 | btrfs_set_key_type(&key, key_type); |
| 3874 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3875 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3876 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3877 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3878 | if (ret < 0) |
| 3879 | goto err; |
| 3880 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3881 | |
| 3882 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3883 | leaf = path->nodes[0]; |
| 3884 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3885 | slot = path->slots[0]; |
| 3886 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3887 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3888 | ret = btrfs_next_leaf(root, path); |
| 3889 | if (ret) |
| 3890 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3891 | leaf = path->nodes[0]; |
| 3892 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3893 | slot = path->slots[0]; |
| 3894 | } else { |
| 3895 | slot++; |
| 3896 | path->slots[0]++; |
| 3897 | } |
| 3898 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3899 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3900 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3901 | item = btrfs_item_nr(leaf, slot); |
| 3902 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3903 | |
| 3904 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3905 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3906 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3907 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3908 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3909 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3910 | |
| 3911 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3912 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3913 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 3914 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3915 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3916 | |
| 3917 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3918 | struct btrfs_key location; |
| 3919 | |
| 3920 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3921 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3922 | name_ptr = tmp_name; |
| 3923 | } else { |
| 3924 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3925 | if (!name_ptr) { |
| 3926 | ret = -ENOMEM; |
| 3927 | goto err; |
| 3928 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3929 | } |
| 3930 | read_extent_buffer(leaf, name_ptr, |
| 3931 | (unsigned long)(di + 1), name_len); |
| 3932 | |
| 3933 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 3934 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3935 | |
| 3936 | /* is this a reference to our own snapshot? If so |
| 3937 | * skip it |
| 3938 | */ |
| 3939 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 3940 | location.objectid == root->root_key.objectid) { |
| 3941 | over = 0; |
| 3942 | goto skip; |
| 3943 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3944 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3945 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3946 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3947 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3948 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3949 | if (name_ptr != tmp_name) |
| 3950 | kfree(name_ptr); |
| 3951 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3952 | if (over) |
| 3953 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 3954 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3955 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3956 | di_cur += di_len; |
| 3957 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 3958 | } |
| 3959 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3960 | |
| 3961 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 3962 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 3963 | /* |
| 3964 | * 32-bit glibc will use getdents64, but then strtol - |
| 3965 | * so the last number we can serve is this. |
| 3966 | */ |
| 3967 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 3968 | else |
| 3969 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3970 | nopos: |
| 3971 | ret = 0; |
| 3972 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3973 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3974 | return ret; |
| 3975 | } |
| 3976 | |
| 3977 | int btrfs_write_inode(struct inode *inode, int wait) |
| 3978 | { |
| 3979 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3980 | struct btrfs_trans_handle *trans; |
| 3981 | int ret = 0; |
| 3982 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3983 | if (root->fs_info->btree_inode == inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 3984 | return 0; |
| 3985 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3986 | if (wait) { |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 3987 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3988 | btrfs_set_trans_block_group(trans, inode); |
| 3989 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3990 | } |
| 3991 | return ret; |
| 3992 | } |
| 3993 | |
| 3994 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3995 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3996 | * inode changes. But, it is most likely to find the inode in cache. |
| 3997 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 3998 | * to keep or drop this code. |
| 3999 | */ |
| 4000 | void btrfs_dirty_inode(struct inode *inode) |
| 4001 | { |
| 4002 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4003 | struct btrfs_trans_handle *trans; |
| 4004 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4005 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4006 | btrfs_set_trans_block_group(trans, inode); |
| 4007 | btrfs_update_inode(trans, root, inode); |
| 4008 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4009 | } |
| 4010 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4011 | /* |
| 4012 | * find the highest existing sequence number in a directory |
| 4013 | * and then set the in-memory index_cnt variable to reflect |
| 4014 | * free sequence numbers |
| 4015 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4016 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4017 | { |
| 4018 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4019 | struct btrfs_key key, found_key; |
| 4020 | struct btrfs_path *path; |
| 4021 | struct extent_buffer *leaf; |
| 4022 | int ret; |
| 4023 | |
| 4024 | key.objectid = inode->i_ino; |
| 4025 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4026 | key.offset = (u64)-1; |
| 4027 | |
| 4028 | path = btrfs_alloc_path(); |
| 4029 | if (!path) |
| 4030 | return -ENOMEM; |
| 4031 | |
| 4032 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4033 | if (ret < 0) |
| 4034 | goto out; |
| 4035 | /* FIXME: we should be able to handle this */ |
| 4036 | if (ret == 0) |
| 4037 | goto out; |
| 4038 | ret = 0; |
| 4039 | |
| 4040 | /* |
| 4041 | * MAGIC NUMBER EXPLANATION: |
| 4042 | * since we search a directory based on f_pos we have to start at 2 |
| 4043 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4044 | * else has to start at 2 |
| 4045 | */ |
| 4046 | if (path->slots[0] == 0) { |
| 4047 | BTRFS_I(inode)->index_cnt = 2; |
| 4048 | goto out; |
| 4049 | } |
| 4050 | |
| 4051 | path->slots[0]--; |
| 4052 | |
| 4053 | leaf = path->nodes[0]; |
| 4054 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4055 | |
| 4056 | if (found_key.objectid != inode->i_ino || |
| 4057 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4058 | BTRFS_I(inode)->index_cnt = 2; |
| 4059 | goto out; |
| 4060 | } |
| 4061 | |
| 4062 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4063 | out: |
| 4064 | btrfs_free_path(path); |
| 4065 | return ret; |
| 4066 | } |
| 4067 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4068 | /* |
| 4069 | * helper to find a free sequence number in a given directory. This current |
| 4070 | * code is very simple, later versions will do smarter things in the btree |
| 4071 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4072 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4073 | { |
| 4074 | int ret = 0; |
| 4075 | |
| 4076 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 4077 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4078 | if (ret) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4079 | return ret; |
| 4080 | } |
| 4081 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4082 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4083 | BTRFS_I(dir)->index_cnt++; |
| 4084 | |
| 4085 | return ret; |
| 4086 | } |
| 4087 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4088 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4089 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4090 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4091 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4092 | u64 ref_objectid, u64 objectid, |
| 4093 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4094 | { |
| 4095 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4096 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4097 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4098 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4099 | struct btrfs_inode_ref *ref; |
| 4100 | struct btrfs_key key[2]; |
| 4101 | u32 sizes[2]; |
| 4102 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4103 | int ret; |
| 4104 | int owner; |
| 4105 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4106 | path = btrfs_alloc_path(); |
| 4107 | BUG_ON(!path); |
| 4108 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4109 | inode = new_inode(root->fs_info->sb); |
| 4110 | if (!inode) |
| 4111 | return ERR_PTR(-ENOMEM); |
| 4112 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4113 | if (dir) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4114 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4115 | if (ret) { |
| 4116 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4117 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4118 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4119 | } |
| 4120 | /* |
| 4121 | * index_cnt is ignored for everything but a dir, |
| 4122 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4123 | * number |
| 4124 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4125 | init_btrfs_i(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4126 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4127 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4128 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4129 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4130 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4131 | if (mode & S_IFDIR) |
| 4132 | owner = 0; |
| 4133 | else |
| 4134 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4135 | BTRFS_I(inode)->block_group = |
| 4136 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4137 | |
| 4138 | key[0].objectid = objectid; |
| 4139 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4140 | key[0].offset = 0; |
| 4141 | |
| 4142 | key[1].objectid = objectid; |
| 4143 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4144 | key[1].offset = ref_objectid; |
| 4145 | |
| 4146 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4147 | sizes[1] = name_len + sizeof(*ref); |
| 4148 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4149 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4150 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4151 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4152 | goto fail; |
| 4153 | |
Chris Mason | 79683f2 | 2008-11-19 22:00:53 -0500 | [diff] [blame] | 4154 | inode->i_uid = current_fsuid(); |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 4155 | |
Chris Mason | 42f15d7 | 2009-02-06 11:35:57 -0500 | [diff] [blame] | 4156 | if (dir && (dir->i_mode & S_ISGID)) { |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 4157 | inode->i_gid = dir->i_gid; |
| 4158 | if (S_ISDIR(mode)) |
| 4159 | mode |= S_ISGID; |
| 4160 | } else |
| 4161 | inode->i_gid = current_fsgid(); |
| 4162 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4163 | inode->i_mode = mode; |
| 4164 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4165 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4166 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4167 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4168 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4169 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4170 | |
| 4171 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4172 | struct btrfs_inode_ref); |
| 4173 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4174 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4175 | ptr = (unsigned long)(ref + 1); |
| 4176 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4177 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4178 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4179 | btrfs_free_path(path); |
| 4180 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4181 | location = &BTRFS_I(inode)->location; |
| 4182 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4183 | location->offset = 0; |
| 4184 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4185 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4186 | btrfs_inherit_iflags(inode, dir); |
| 4187 | |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4188 | if ((mode & S_IFREG)) { |
| 4189 | if (btrfs_test_opt(root, NODATASUM)) |
| 4190 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 4191 | if (btrfs_test_opt(root, NODATACOW)) |
| 4192 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4193 | } |
| 4194 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4195 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4196 | inode_tree_add(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4197 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4198 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4199 | if (dir) |
| 4200 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4201 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4202 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4203 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4207 | { |
| 4208 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4209 | } |
| 4210 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4211 | /* |
| 4212 | * utility function to add 'inode' into 'parent_inode' with |
| 4213 | * a give name and a given sequence number. |
| 4214 | * if 'add_backref' is true, also insert a backref from the |
| 4215 | * inode to the parent directory. |
| 4216 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4217 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4218 | struct inode *parent_inode, struct inode *inode, |
| 4219 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4220 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4221 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4222 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4223 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4224 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4225 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4226 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4227 | } else { |
| 4228 | key.objectid = inode->i_ino; |
| 4229 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4230 | key.offset = 0; |
| 4231 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4232 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4233 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4234 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4235 | key.objectid, root->root_key.objectid, |
| 4236 | parent_inode->i_ino, |
| 4237 | index, name, name_len); |
| 4238 | } else if (add_backref) { |
| 4239 | ret = btrfs_insert_inode_ref(trans, root, |
| 4240 | name, name_len, inode->i_ino, |
| 4241 | parent_inode->i_ino, index); |
| 4242 | } |
| 4243 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4244 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4245 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4246 | parent_inode->i_ino, &key, |
| 4247 | btrfs_inode_type(inode), index); |
| 4248 | BUG_ON(ret); |
| 4249 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4250 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4251 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4252 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4253 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4254 | } |
| 4255 | return ret; |
| 4256 | } |
| 4257 | |
| 4258 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4259 | struct dentry *dentry, struct inode *inode, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4260 | int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4261 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4262 | int err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4263 | inode, dentry->d_name.name, |
| 4264 | dentry->d_name.len, backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4265 | if (!err) { |
| 4266 | d_instantiate(dentry, inode); |
| 4267 | return 0; |
| 4268 | } |
| 4269 | if (err > 0) |
| 4270 | err = -EEXIST; |
| 4271 | return err; |
| 4272 | } |
| 4273 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4274 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4275 | int mode, dev_t rdev) |
| 4276 | { |
| 4277 | struct btrfs_trans_handle *trans; |
| 4278 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4279 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4280 | int err; |
| 4281 | int drop_inode = 0; |
| 4282 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4283 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4284 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4285 | |
| 4286 | if (!new_valid_dev(rdev)) |
| 4287 | return -EINVAL; |
| 4288 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4289 | /* |
| 4290 | * 2 for inode item and ref |
| 4291 | * 2 for dir items |
| 4292 | * 1 for xattr if selinux is on |
| 4293 | */ |
| 4294 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4295 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4296 | return err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4297 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4298 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4299 | if (!trans) |
| 4300 | goto fail; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4301 | btrfs_set_trans_block_group(trans, dir); |
| 4302 | |
| 4303 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4304 | if (err) { |
| 4305 | err = -ENOSPC; |
| 4306 | goto out_unlock; |
| 4307 | } |
| 4308 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4309 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4310 | dentry->d_name.len, |
| 4311 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4312 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4313 | err = PTR_ERR(inode); |
| 4314 | if (IS_ERR(inode)) |
| 4315 | goto out_unlock; |
| 4316 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4317 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4318 | if (err) { |
| 4319 | drop_inode = 1; |
| 4320 | goto out_unlock; |
| 4321 | } |
| 4322 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4323 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4324 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4325 | if (err) |
| 4326 | drop_inode = 1; |
| 4327 | else { |
| 4328 | inode->i_op = &btrfs_special_inode_operations; |
| 4329 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4330 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4331 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4332 | btrfs_update_inode_block_group(trans, inode); |
| 4333 | btrfs_update_inode_block_group(trans, dir); |
| 4334 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4335 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4336 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4337 | fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4338 | btrfs_unreserve_metadata_space(root, 5); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4339 | if (drop_inode) { |
| 4340 | inode_dec_link_count(inode); |
| 4341 | iput(inode); |
| 4342 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4343 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4344 | return err; |
| 4345 | } |
| 4346 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4347 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4348 | int mode, struct nameidata *nd) |
| 4349 | { |
| 4350 | struct btrfs_trans_handle *trans; |
| 4351 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4352 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4353 | int err; |
| 4354 | int drop_inode = 0; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4355 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4356 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4357 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4358 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4359 | /* |
| 4360 | * 2 for inode item and ref |
| 4361 | * 2 for dir items |
| 4362 | * 1 for xattr if selinux is on |
| 4363 | */ |
| 4364 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4365 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4366 | return err; |
| 4367 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4368 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4369 | if (!trans) |
| 4370 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4371 | btrfs_set_trans_block_group(trans, dir); |
| 4372 | |
| 4373 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4374 | if (err) { |
| 4375 | err = -ENOSPC; |
| 4376 | goto out_unlock; |
| 4377 | } |
| 4378 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4379 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4380 | dentry->d_name.len, |
| 4381 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4382 | objectid, BTRFS_I(dir)->block_group, mode, |
| 4383 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4384 | err = PTR_ERR(inode); |
| 4385 | if (IS_ERR(inode)) |
| 4386 | goto out_unlock; |
| 4387 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4388 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4389 | if (err) { |
| 4390 | drop_inode = 1; |
| 4391 | goto out_unlock; |
| 4392 | } |
| 4393 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4394 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4395 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4396 | if (err) |
| 4397 | drop_inode = 1; |
| 4398 | else { |
| 4399 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4400 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4401 | inode->i_fop = &btrfs_file_operations; |
| 4402 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4403 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4404 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4405 | btrfs_update_inode_block_group(trans, inode); |
| 4406 | btrfs_update_inode_block_group(trans, dir); |
| 4407 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4408 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4409 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4410 | fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4411 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4412 | if (drop_inode) { |
| 4413 | inode_dec_link_count(inode); |
| 4414 | iput(inode); |
| 4415 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4416 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4417 | return err; |
| 4418 | } |
| 4419 | |
| 4420 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4421 | struct dentry *dentry) |
| 4422 | { |
| 4423 | struct btrfs_trans_handle *trans; |
| 4424 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4425 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4426 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4427 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4428 | int err; |
| 4429 | int drop_inode = 0; |
| 4430 | |
| 4431 | if (inode->i_nlink == 0) |
| 4432 | return -ENOENT; |
| 4433 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4434 | /* do not allow sys_link's with other subvols of the same device */ |
| 4435 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
| 4436 | return -EPERM; |
| 4437 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4438 | /* |
| 4439 | * 1 item for inode ref |
| 4440 | * 2 items for dir items |
| 4441 | */ |
| 4442 | err = btrfs_reserve_metadata_space(root, 3); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4443 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4444 | return err; |
| 4445 | |
| 4446 | btrfs_inc_nlink(inode); |
| 4447 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4448 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4449 | if (err) |
| 4450 | goto fail; |
| 4451 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4452 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4453 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4454 | btrfs_set_trans_block_group(trans, dir); |
| 4455 | atomic_inc(&inode->i_count); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4456 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4457 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4458 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4459 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4460 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4461 | } else { |
| 4462 | btrfs_update_inode_block_group(trans, dir); |
| 4463 | err = btrfs_update_inode(trans, root, inode); |
| 4464 | BUG_ON(err); |
| 4465 | btrfs_log_new_name(trans, inode, NULL, dentry->d_parent); |
| 4466 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4467 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4468 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4469 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4470 | fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4471 | btrfs_unreserve_metadata_space(root, 3); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4472 | if (drop_inode) { |
| 4473 | inode_dec_link_count(inode); |
| 4474 | iput(inode); |
| 4475 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4476 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4477 | return err; |
| 4478 | } |
| 4479 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4480 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4481 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4482 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4483 | struct btrfs_trans_handle *trans; |
| 4484 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4485 | int err = 0; |
| 4486 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4487 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4488 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4489 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4490 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4491 | /* |
| 4492 | * 2 items for inode and ref |
| 4493 | * 2 items for dir items |
| 4494 | * 1 for xattr if selinux is on |
| 4495 | */ |
| 4496 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4497 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4498 | return err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4499 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4500 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4501 | if (!trans) { |
| 4502 | err = -ENOMEM; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4503 | goto out_unlock; |
| 4504 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4505 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4506 | |
| 4507 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4508 | if (err) { |
| 4509 | err = -ENOSPC; |
Miao Xie | 0be2e98 | 2010-02-11 08:06:58 +0000 | [diff] [blame] | 4510 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4511 | } |
| 4512 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4513 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4514 | dentry->d_name.len, |
| 4515 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4516 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 4517 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4518 | if (IS_ERR(inode)) { |
| 4519 | err = PTR_ERR(inode); |
| 4520 | goto out_fail; |
| 4521 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4522 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4523 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4524 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4525 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4526 | if (err) |
| 4527 | goto out_fail; |
| 4528 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4529 | inode->i_op = &btrfs_dir_inode_operations; |
| 4530 | inode->i_fop = &btrfs_dir_file_operations; |
| 4531 | btrfs_set_trans_block_group(trans, inode); |
| 4532 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4533 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4534 | err = btrfs_update_inode(trans, root, inode); |
| 4535 | if (err) |
| 4536 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4537 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4538 | err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4539 | inode, dentry->d_name.name, |
| 4540 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4541 | if (err) |
| 4542 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4543 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4544 | d_instantiate(dentry, inode); |
| 4545 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4546 | btrfs_update_inode_block_group(trans, inode); |
| 4547 | btrfs_update_inode_block_group(trans, dir); |
| 4548 | |
| 4549 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4550 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4551 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4552 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4553 | out_unlock: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4554 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4555 | if (drop_on_err) |
| 4556 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4557 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4558 | return err; |
| 4559 | } |
| 4560 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4561 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4562 | * and an extent that you want to insert, deal with overlap and insert |
| 4563 | * the new extent into the tree. |
| 4564 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4565 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4566 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4567 | struct extent_map *em, |
| 4568 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4569 | { |
| 4570 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4571 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4572 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4573 | start_diff = map_start - em->start; |
| 4574 | em->start = map_start; |
| 4575 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4576 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4577 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4578 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4579 | em->block_len -= start_diff; |
| 4580 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4581 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4582 | } |
| 4583 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4584 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4585 | struct inode *inode, struct page *page, |
| 4586 | size_t pg_offset, u64 extent_offset, |
| 4587 | struct btrfs_file_extent_item *item) |
| 4588 | { |
| 4589 | int ret; |
| 4590 | struct extent_buffer *leaf = path->nodes[0]; |
| 4591 | char *tmp; |
| 4592 | size_t max_size; |
| 4593 | unsigned long inline_size; |
| 4594 | unsigned long ptr; |
| 4595 | |
| 4596 | WARN_ON(pg_offset != 0); |
| 4597 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4598 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4599 | btrfs_item_nr(leaf, path->slots[0])); |
| 4600 | tmp = kmalloc(inline_size, GFP_NOFS); |
| 4601 | ptr = btrfs_file_extent_inline_start(item); |
| 4602 | |
| 4603 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4604 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 4605 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4606 | ret = btrfs_zlib_decompress(tmp, page, extent_offset, |
| 4607 | inline_size, max_size); |
| 4608 | if (ret) { |
| 4609 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 4610 | unsigned long copy_size = min_t(u64, |
| 4611 | PAGE_CACHE_SIZE - pg_offset, |
| 4612 | max_size - extent_offset); |
| 4613 | memset(kaddr + pg_offset, 0, copy_size); |
| 4614 | kunmap_atomic(kaddr, KM_USER0); |
| 4615 | } |
| 4616 | kfree(tmp); |
| 4617 | return 0; |
| 4618 | } |
| 4619 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4620 | /* |
| 4621 | * 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] | 4622 | * the ugly parts come from merging extents from the disk with the in-ram |
| 4623 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4624 | * where the in-ram extents might be locked pending data=ordered completion. |
| 4625 | * |
| 4626 | * This also copies inline extents directly into the page. |
| 4627 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4628 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4629 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4630 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4631 | int create) |
| 4632 | { |
| 4633 | int ret; |
| 4634 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4635 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4636 | u64 extent_start = 0; |
| 4637 | u64 extent_end = 0; |
| 4638 | u64 objectid = inode->i_ino; |
| 4639 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4640 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4641 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4642 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4643 | struct extent_buffer *leaf; |
| 4644 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4645 | struct extent_map *em = NULL; |
| 4646 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4647 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4648 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4649 | int compressed; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4650 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4651 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4652 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4653 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4654 | if (em) |
| 4655 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4656 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4657 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4658 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4659 | if (em->start > start || em->start + em->len <= start) |
| 4660 | free_extent_map(em); |
| 4661 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4662 | free_extent_map(em); |
| 4663 | else |
| 4664 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4665 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4666 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4667 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4668 | err = -ENOMEM; |
| 4669 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4670 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4671 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4672 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 4673 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4674 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4675 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4676 | |
| 4677 | if (!path) { |
| 4678 | path = btrfs_alloc_path(); |
| 4679 | BUG_ON(!path); |
| 4680 | } |
| 4681 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4682 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 4683 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4684 | if (ret < 0) { |
| 4685 | err = ret; |
| 4686 | goto out; |
| 4687 | } |
| 4688 | |
| 4689 | if (ret != 0) { |
| 4690 | if (path->slots[0] == 0) |
| 4691 | goto not_found; |
| 4692 | path->slots[0]--; |
| 4693 | } |
| 4694 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4695 | leaf = path->nodes[0]; |
| 4696 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4697 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4698 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4699 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4700 | found_type = btrfs_key_type(&found_key); |
| 4701 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4702 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 4703 | goto not_found; |
| 4704 | } |
| 4705 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4706 | found_type = btrfs_file_extent_type(leaf, item); |
| 4707 | extent_start = found_key.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4708 | compressed = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4709 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4710 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4711 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4712 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4713 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 4714 | size_t size; |
| 4715 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4716 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 4717 | ~((u64)root->sectorsize - 1); |
| 4718 | } |
| 4719 | |
| 4720 | if (start >= extent_end) { |
| 4721 | path->slots[0]++; |
| 4722 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 4723 | ret = btrfs_next_leaf(root, path); |
| 4724 | if (ret < 0) { |
| 4725 | err = ret; |
| 4726 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4727 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4728 | if (ret > 0) |
| 4729 | goto not_found; |
| 4730 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4731 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4732 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4733 | if (found_key.objectid != objectid || |
| 4734 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 4735 | goto not_found; |
| 4736 | if (start + len <= found_key.offset) |
| 4737 | goto not_found; |
| 4738 | em->start = start; |
| 4739 | em->len = found_key.offset - start; |
| 4740 | goto not_found_em; |
| 4741 | } |
| 4742 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4743 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4744 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4745 | em->start = extent_start; |
| 4746 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4747 | em->orig_start = extent_start - |
| 4748 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4749 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 4750 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4751 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4752 | goto insert; |
| 4753 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4754 | if (compressed) { |
| 4755 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 4756 | em->block_start = bytenr; |
| 4757 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 4758 | item); |
| 4759 | } else { |
| 4760 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 4761 | em->block_start = bytenr; |
| 4762 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4763 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 4764 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4765 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4766 | goto insert; |
| 4767 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4768 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4769 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4770 | size_t size; |
| 4771 | size_t extent_offset; |
| 4772 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4773 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4774 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4775 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4776 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4777 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4778 | goto out; |
| 4779 | } |
| 4780 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4781 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4782 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4783 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4784 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4785 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4786 | em->len = (copy_size + root->sectorsize - 1) & |
| 4787 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4788 | em->orig_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4789 | if (compressed) |
| 4790 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4791 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4792 | if (create == 0 && !PageUptodate(page)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4793 | if (btrfs_file_extent_compression(leaf, item) == |
| 4794 | BTRFS_COMPRESS_ZLIB) { |
| 4795 | ret = uncompress_inline(path, inode, page, |
| 4796 | pg_offset, |
| 4797 | extent_offset, item); |
| 4798 | BUG_ON(ret); |
| 4799 | } else { |
| 4800 | map = kmap(page); |
| 4801 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 4802 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 4803 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 4804 | memset(map + pg_offset + copy_size, 0, |
| 4805 | PAGE_CACHE_SIZE - pg_offset - |
| 4806 | copy_size); |
| 4807 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4808 | kunmap(page); |
| 4809 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4810 | flush_dcache_page(page); |
| 4811 | } else if (create && PageUptodate(page)) { |
| 4812 | if (!trans) { |
| 4813 | kunmap(page); |
| 4814 | free_extent_map(em); |
| 4815 | em = NULL; |
| 4816 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4817 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4818 | goto again; |
| 4819 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4820 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4821 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4822 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4823 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4824 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4825 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4826 | set_extent_uptodate(io_tree, em->start, |
| 4827 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4828 | goto insert; |
| 4829 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4830 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4831 | WARN_ON(1); |
| 4832 | } |
| 4833 | not_found: |
| 4834 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4835 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4836 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4837 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4838 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4839 | insert: |
| 4840 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4841 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4842 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 4843 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 4844 | (unsigned long long)em->len, |
| 4845 | (unsigned long long)start, |
| 4846 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4847 | err = -EIO; |
| 4848 | goto out; |
| 4849 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4850 | |
| 4851 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4852 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4853 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4854 | /* it is possible that someone inserted the extent into the tree |
| 4855 | * while we had the lock dropped. It is also possible that |
| 4856 | * an overlapping map exists in the tree |
| 4857 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4858 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4859 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4860 | |
| 4861 | ret = 0; |
| 4862 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4863 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4864 | if (existing && (existing->start > start || |
| 4865 | existing->start + existing->len <= start)) { |
| 4866 | free_extent_map(existing); |
| 4867 | existing = NULL; |
| 4868 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4869 | if (!existing) { |
| 4870 | existing = lookup_extent_mapping(em_tree, em->start, |
| 4871 | em->len); |
| 4872 | if (existing) { |
| 4873 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4874 | em, start, |
| 4875 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4876 | free_extent_map(existing); |
| 4877 | if (err) { |
| 4878 | free_extent_map(em); |
| 4879 | em = NULL; |
| 4880 | } |
| 4881 | } else { |
| 4882 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4883 | free_extent_map(em); |
| 4884 | em = NULL; |
| 4885 | } |
| 4886 | } else { |
| 4887 | free_extent_map(em); |
| 4888 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4889 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4890 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4891 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4892 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4893 | out: |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4894 | if (path) |
| 4895 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4896 | if (trans) { |
| 4897 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4898 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4899 | err = ret; |
| 4900 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4901 | if (err) { |
| 4902 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4903 | return ERR_PTR(err); |
| 4904 | } |
| 4905 | return em; |
| 4906 | } |
| 4907 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4908 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 4909 | const struct iovec *iov, loff_t offset, |
| 4910 | unsigned long nr_segs) |
| 4911 | { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4912 | return -EINVAL; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4913 | } |
| 4914 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4915 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4916 | __u64 start, __u64 len) |
| 4917 | { |
| 4918 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent); |
| 4919 | } |
| 4920 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4921 | int btrfs_readpage(struct file *file, struct page *page) |
| 4922 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4923 | struct extent_io_tree *tree; |
| 4924 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4925 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4926 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4927 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4928 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 4929 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4930 | struct extent_io_tree *tree; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4931 | |
| 4932 | |
| 4933 | if (current->flags & PF_MEMALLOC) { |
| 4934 | redirty_page_for_writepage(wbc, page); |
| 4935 | unlock_page(page); |
| 4936 | return 0; |
| 4937 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4938 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4939 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 4940 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4941 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4942 | int btrfs_writepages(struct address_space *mapping, |
| 4943 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 4944 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4945 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4946 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4947 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 4948 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 4949 | } |
| 4950 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 4951 | static int |
| 4952 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 4953 | struct list_head *pages, unsigned nr_pages) |
| 4954 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4955 | struct extent_io_tree *tree; |
| 4956 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 4957 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 4958 | btrfs_get_extent); |
| 4959 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4960 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4961 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4962 | struct extent_io_tree *tree; |
| 4963 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4964 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4965 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4966 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 4967 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4968 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4969 | if (ret == 1) { |
| 4970 | ClearPagePrivate(page); |
| 4971 | set_page_private(page, 0); |
| 4972 | page_cache_release(page); |
| 4973 | } |
| 4974 | return ret; |
| 4975 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4976 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4977 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 4978 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 4979 | if (PageWriteback(page) || PageDirty(page)) |
| 4980 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 4981 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4982 | } |
| 4983 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4984 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 4985 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4986 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4987 | struct btrfs_ordered_extent *ordered; |
| 4988 | u64 page_start = page_offset(page); |
| 4989 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4990 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4991 | |
| 4992 | /* |
| 4993 | * we have the page locked, so new writeback can't start, |
| 4994 | * and the dirty bit won't be cleared while we are here. |
| 4995 | * |
| 4996 | * Wait for IO on this page so that we can safely clear |
| 4997 | * the PagePrivate2 bit and do ordered accounting |
| 4998 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4999 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 5000 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5001 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5002 | if (offset) { |
| 5003 | btrfs_releasepage(page, GFP_NOFS); |
| 5004 | return; |
| 5005 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5006 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 5007 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 5008 | page_offset(page)); |
| 5009 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5010 | /* |
| 5011 | * IO on this page will never be started, so we need |
| 5012 | * to account for any ordered extents now |
| 5013 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5014 | clear_extent_bit(tree, page_start, page_end, |
| 5015 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5016 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
| 5017 | NULL, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 5018 | /* |
| 5019 | * whoever cleared the private bit is responsible |
| 5020 | * for the finish_ordered_io |
| 5021 | */ |
| 5022 | if (TestClearPagePrivate2(page)) { |
| 5023 | btrfs_finish_ordered_io(page->mapping->host, |
| 5024 | page_start, page_end); |
| 5025 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5026 | btrfs_put_ordered_extent(ordered); |
| 5027 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 5028 | } |
| 5029 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5030 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 5031 | EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5032 | __btrfs_releasepage(page, GFP_NOFS); |
| 5033 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 5034 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 5035 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 5036 | ClearPagePrivate(page); |
| 5037 | set_page_private(page, 0); |
| 5038 | page_cache_release(page); |
| 5039 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5040 | } |
| 5041 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5042 | /* |
| 5043 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 5044 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 5045 | * be careful to check for EOF conditions here. We set the page up correctly |
| 5046 | * for a written page which means we get ENOSPC checking when writing into |
| 5047 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 5048 | * support these features. |
| 5049 | * |
| 5050 | * We are not allowed to take the i_mutex here so we have to play games to |
| 5051 | * protect against truncate races as the page could now be beyond EOF. Because |
| 5052 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 5053 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 5054 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 5055 | * unlock the page. |
| 5056 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5057 | 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] | 5058 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5059 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 5060 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5061 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5062 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5063 | struct btrfs_ordered_extent *ordered; |
| 5064 | char *kaddr; |
| 5065 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5066 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5067 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5068 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5069 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5070 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5071 | ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5072 | if (ret) { |
| 5073 | if (ret == -ENOMEM) |
| 5074 | ret = VM_FAULT_OOM; |
| 5075 | else /* -ENOSPC, -EIO, etc */ |
| 5076 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5077 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5078 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5079 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5080 | ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1); |
| 5081 | if (ret) { |
| 5082 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
| 5083 | ret = VM_FAULT_SIGBUS; |
| 5084 | goto out; |
| 5085 | } |
| 5086 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5087 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5088 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5089 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5090 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5091 | page_start = page_offset(page); |
| 5092 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5093 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5094 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5095 | (page_start >= size)) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5096 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5097 | /* page got truncated out from underneath us */ |
| 5098 | goto out_unlock; |
| 5099 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5100 | wait_on_page_writeback(page); |
| 5101 | |
| 5102 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5103 | set_page_extent_mapped(page); |
| 5104 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5105 | /* |
| 5106 | * we can't set the delalloc bits if there are pending ordered |
| 5107 | * extents. Drop our locks and wait for them to finish |
| 5108 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5109 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 5110 | if (ordered) { |
| 5111 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5112 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5113 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5114 | btrfs_put_ordered_extent(ordered); |
| 5115 | goto again; |
| 5116 | } |
| 5117 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 5118 | /* |
| 5119 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 5120 | * if it was already dirty, so for space accounting reasons we need to |
| 5121 | * clear any delalloc bits for the range we are fixing to save. There |
| 5122 | * is probably a better way to do this, but for now keep consistent with |
| 5123 | * prepare_pages in the normal write path. |
| 5124 | */ |
| 5125 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5126 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
| 5127 | GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 5128 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5129 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end); |
| 5130 | if (ret) { |
| 5131 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5132 | ret = VM_FAULT_SIGBUS; |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 5133 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5134 | goto out_unlock; |
| 5135 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5136 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5137 | |
| 5138 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5139 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5140 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5141 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5142 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5143 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5144 | if (zero_start != PAGE_CACHE_SIZE) { |
| 5145 | kaddr = kmap(page); |
| 5146 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 5147 | flush_dcache_page(page); |
| 5148 | kunmap(page); |
| 5149 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 5150 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5151 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 5152 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5153 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5154 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 5155 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 5156 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5157 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5158 | |
| 5159 | out_unlock: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5160 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 5161 | if (!ret) |
| 5162 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5163 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5164 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5165 | return ret; |
| 5166 | } |
| 5167 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5168 | static void btrfs_truncate(struct inode *inode) |
| 5169 | { |
| 5170 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5171 | int ret; |
| 5172 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5173 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5174 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5175 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5176 | if (!S_ISREG(inode->i_mode)) { |
| 5177 | WARN_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5178 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5179 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5180 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 5181 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 5182 | if (ret) |
| 5183 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5184 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 5185 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5186 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5187 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5188 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5189 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5190 | |
| 5191 | /* |
| 5192 | * setattr is responsible for setting the ordered_data_close flag, |
| 5193 | * but that is only tested during the last file release. That |
| 5194 | * could happen well after the next commit, leaving a great big |
| 5195 | * window where new writes may get lost if someone chooses to write |
| 5196 | * to this file after truncating to zero |
| 5197 | * |
| 5198 | * The inode doesn't have any dirty data here, and so if we commit |
| 5199 | * this is a noop. If someone immediately starts writing to the inode |
| 5200 | * it is very likely we'll catch some of their writes in this |
| 5201 | * transaction, and the commit will find this file on the ordered |
| 5202 | * data list with good things to send down. |
| 5203 | * |
| 5204 | * This is a best effort solution, there is still a window where |
| 5205 | * using truncate to replace the contents of the file will |
| 5206 | * end up with a zero length file after a crash. |
| 5207 | */ |
| 5208 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 5209 | btrfs_add_ordered_operation(trans, root, inode); |
| 5210 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5211 | while (1) { |
| 5212 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 5213 | inode->i_size, |
| 5214 | BTRFS_EXTENT_DATA_KEY); |
| 5215 | if (ret != -EAGAIN) |
| 5216 | break; |
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 | ret = btrfs_update_inode(trans, root, inode); |
| 5219 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5220 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5221 | nr = trans->blocks_used; |
| 5222 | btrfs_end_transaction(trans, root); |
| 5223 | btrfs_btree_balance_dirty(root, nr); |
| 5224 | |
| 5225 | trans = btrfs_start_transaction(root, 1); |
| 5226 | btrfs_set_trans_block_group(trans, inode); |
| 5227 | } |
| 5228 | |
| 5229 | if (ret == 0 && inode->i_nlink > 0) { |
| 5230 | ret = btrfs_orphan_del(trans, inode); |
| 5231 | BUG_ON(ret); |
| 5232 | } |
| 5233 | |
| 5234 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5235 | BUG_ON(ret); |
| 5236 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5237 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5238 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5239 | BUG_ON(ret); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5240 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5241 | } |
| 5242 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 5243 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5244 | * create a new subvolume directory/inode (helper for the ioctl). |
| 5245 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5246 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5247 | struct btrfs_root *new_root, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5248 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5249 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5250 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5251 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5252 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5253 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5254 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5255 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5256 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5257 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5258 | inode->i_op = &btrfs_dir_inode_operations; |
| 5259 | inode->i_fop = &btrfs_dir_file_operations; |
| 5260 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5261 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5262 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 5263 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5264 | err = btrfs_update_inode(trans, new_root, inode); |
| 5265 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 5266 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5267 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 5268 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5269 | } |
| 5270 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5271 | /* helper function for file defrag and space balancing. This |
| 5272 | * forces readahead on a given range of bytes in an inode |
| 5273 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5274 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5275 | struct file_ra_state *ra, struct file *file, |
| 5276 | pgoff_t offset, pgoff_t last_index) |
| 5277 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5278 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5279 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5280 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 5281 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5282 | } |
| 5283 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5284 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 5285 | { |
| 5286 | struct btrfs_inode *ei; |
| 5287 | |
| 5288 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 5289 | if (!ei) |
| 5290 | return NULL; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 5291 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5292 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5293 | ei->logged_trans = 0; |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5294 | ei->outstanding_extents = 0; |
| 5295 | ei->reserved_extents = 0; |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 5296 | ei->root = NULL; |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5297 | spin_lock_init(&ei->accounting_lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5298 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5299 | INIT_LIST_HEAD(&ei->i_orphan); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5300 | INIT_LIST_HEAD(&ei->ordered_operations); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5301 | return &ei->vfs_inode; |
| 5302 | } |
| 5303 | |
| 5304 | void btrfs_destroy_inode(struct inode *inode) |
| 5305 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5306 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5307 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5308 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5309 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 5310 | WARN_ON(inode->i_data.nrpages); |
| 5311 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5312 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 5313 | * This can happen where we create an inode, but somebody else also |
| 5314 | * created the same inode and we need to destroy the one we already |
| 5315 | * created. |
| 5316 | */ |
| 5317 | if (!root) |
| 5318 | goto free; |
| 5319 | |
| 5320 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5321 | * Make sure we're properly removed from the ordered operation |
| 5322 | * lists. |
| 5323 | */ |
| 5324 | smp_mb(); |
| 5325 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 5326 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 5327 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 5328 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 5329 | } |
| 5330 | |
| 5331 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5332 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5333 | printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", |
| 5334 | inode->i_ino); |
| 5335 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5336 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5337 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5338 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5339 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5340 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 5341 | if (!ordered) |
| 5342 | break; |
| 5343 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5344 | printk(KERN_ERR "btrfs found ordered " |
| 5345 | "extent %llu %llu on inode cleanup\n", |
| 5346 | (unsigned long long)ordered->file_offset, |
| 5347 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5348 | btrfs_remove_ordered_extent(inode, ordered); |
| 5349 | btrfs_put_ordered_extent(ordered); |
| 5350 | btrfs_put_ordered_extent(ordered); |
| 5351 | } |
| 5352 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5353 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5354 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 5355 | free: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5356 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 5357 | } |
| 5358 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5359 | void btrfs_drop_inode(struct inode *inode) |
| 5360 | { |
| 5361 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5362 | |
| 5363 | if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0) |
| 5364 | generic_delete_inode(inode); |
| 5365 | else |
| 5366 | generic_drop_inode(inode); |
| 5367 | } |
| 5368 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5369 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5370 | { |
| 5371 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 5372 | |
| 5373 | inode_init_once(&ei->vfs_inode); |
| 5374 | } |
| 5375 | |
| 5376 | void btrfs_destroy_cachep(void) |
| 5377 | { |
| 5378 | if (btrfs_inode_cachep) |
| 5379 | kmem_cache_destroy(btrfs_inode_cachep); |
| 5380 | if (btrfs_trans_handle_cachep) |
| 5381 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 5382 | if (btrfs_transaction_cachep) |
| 5383 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5384 | if (btrfs_path_cachep) |
| 5385 | kmem_cache_destroy(btrfs_path_cachep); |
| 5386 | } |
| 5387 | |
| 5388 | int btrfs_init_cachep(void) |
| 5389 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5390 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 5391 | sizeof(struct btrfs_inode), 0, |
| 5392 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5393 | if (!btrfs_inode_cachep) |
| 5394 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5395 | |
| 5396 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 5397 | sizeof(struct btrfs_trans_handle), 0, |
| 5398 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5399 | if (!btrfs_trans_handle_cachep) |
| 5400 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5401 | |
| 5402 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 5403 | sizeof(struct btrfs_transaction), 0, |
| 5404 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5405 | if (!btrfs_transaction_cachep) |
| 5406 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5407 | |
| 5408 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 5409 | sizeof(struct btrfs_path), 0, |
| 5410 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5411 | if (!btrfs_path_cachep) |
| 5412 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5413 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5414 | return 0; |
| 5415 | fail: |
| 5416 | btrfs_destroy_cachep(); |
| 5417 | return -ENOMEM; |
| 5418 | } |
| 5419 | |
| 5420 | static int btrfs_getattr(struct vfsmount *mnt, |
| 5421 | struct dentry *dentry, struct kstat *stat) |
| 5422 | { |
| 5423 | struct inode *inode = dentry->d_inode; |
| 5424 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5425 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 5426 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5427 | stat->blocks = (inode_get_bytes(inode) + |
| 5428 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5429 | return 0; |
| 5430 | } |
| 5431 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5432 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 5433 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5434 | { |
| 5435 | struct btrfs_trans_handle *trans; |
| 5436 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5437 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5438 | struct inode *new_inode = new_dentry->d_inode; |
| 5439 | struct inode *old_inode = old_dentry->d_inode; |
| 5440 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5441 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5442 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5443 | int ret; |
| 5444 | |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5445 | if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5446 | return -EPERM; |
| 5447 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5448 | /* we only allow rename subvolume link between subvolumes */ |
| 5449 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5450 | return -EXDEV; |
| 5451 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5452 | if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 5453 | (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) |
| 5454 | return -ENOTEMPTY; |
| 5455 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5456 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5457 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5458 | return -ENOTEMPTY; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 5459 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5460 | /* |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5461 | * We want to reserve the absolute worst case amount of items. So if |
| 5462 | * both inodes are subvols and we need to unlink them then that would |
| 5463 | * require 4 item modifications, but if they are both normal inodes it |
| 5464 | * would require 5 item modifications, so we'll assume their normal |
| 5465 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 5466 | * should cover the worst case number of items we'll modify. |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5467 | */ |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5468 | ret = btrfs_reserve_metadata_space(root, 11); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5469 | if (ret) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5470 | return ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5471 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5472 | /* |
| 5473 | * we're using rename to replace one file with another. |
| 5474 | * and the replacement file is large. Start IO on it now so |
| 5475 | * we don't add too much work to the end of the transaction |
| 5476 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 5477 | 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] | 5478 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 5479 | filemap_flush(old_inode->i_mapping); |
| 5480 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5481 | /* close the racy window with snapshot create/destroy ioctl */ |
| 5482 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 5483 | down_read(&root->fs_info->subvol_sem); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5484 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5485 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5486 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5487 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5488 | if (dest != root) |
| 5489 | btrfs_record_root_in_trans(trans, dest); |
| 5490 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5491 | ret = btrfs_set_inode_index(new_dir, &index); |
| 5492 | if (ret) |
| 5493 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5494 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5495 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5496 | /* force full log commit if subvolume involved. */ |
| 5497 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 5498 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5499 | ret = btrfs_insert_inode_ref(trans, dest, |
| 5500 | new_dentry->d_name.name, |
| 5501 | new_dentry->d_name.len, |
| 5502 | old_inode->i_ino, |
| 5503 | new_dir->i_ino, index); |
| 5504 | if (ret) |
| 5505 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5506 | /* |
| 5507 | * this is an ugly little race, but the rename is required |
| 5508 | * to make sure that if we crash, the inode is either at the |
| 5509 | * old name or the new one. pinning the log transaction lets |
| 5510 | * us make sure we don't allow a log commit to come in after |
| 5511 | * we unlink the name but before we add the new name back in. |
| 5512 | */ |
| 5513 | btrfs_pin_log_trans(root); |
| 5514 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5515 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5516 | * make sure the inode gets flushed if it is replacing |
| 5517 | * something. |
| 5518 | */ |
| 5519 | if (new_inode && new_inode->i_size && |
| 5520 | old_inode && S_ISREG(old_inode->i_mode)) { |
| 5521 | btrfs_add_ordered_operation(trans, root, old_inode); |
| 5522 | } |
| 5523 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5524 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 5525 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 5526 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5527 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5528 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 5529 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 5530 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5531 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5532 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 5533 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 5534 | old_dentry->d_name.name, |
| 5535 | old_dentry->d_name.len); |
| 5536 | } else { |
| 5537 | btrfs_inc_nlink(old_dentry->d_inode); |
| 5538 | ret = btrfs_unlink_inode(trans, root, old_dir, |
| 5539 | old_dentry->d_inode, |
| 5540 | old_dentry->d_name.name, |
| 5541 | old_dentry->d_name.len); |
| 5542 | } |
| 5543 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5544 | |
| 5545 | if (new_inode) { |
| 5546 | new_inode->i_ctime = CURRENT_TIME; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5547 | if (unlikely(new_inode->i_ino == |
| 5548 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 5549 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 5550 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 5551 | root_objectid, |
| 5552 | new_dentry->d_name.name, |
| 5553 | new_dentry->d_name.len); |
| 5554 | BUG_ON(new_inode->i_nlink == 0); |
| 5555 | } else { |
| 5556 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 5557 | new_dentry->d_inode, |
| 5558 | new_dentry->d_name.name, |
| 5559 | new_dentry->d_name.len); |
| 5560 | } |
| 5561 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5562 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5563 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5564 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5565 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5566 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5567 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5568 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 5569 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5570 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5571 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5572 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5573 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 5574 | btrfs_log_new_name(trans, old_inode, old_dir, |
| 5575 | new_dentry->d_parent); |
| 5576 | btrfs_end_log_trans(root); |
| 5577 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5578 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 5579 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5580 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5581 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 5582 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5583 | |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5584 | btrfs_unreserve_metadata_space(root, 11); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5585 | return ret; |
| 5586 | } |
| 5587 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5588 | /* |
| 5589 | * some fairly slow code that needs optimization. This walks the list |
| 5590 | * of all the inodes with pending delalloc and forces them to disk. |
| 5591 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 5592 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5593 | { |
| 5594 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 5595 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5596 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5597 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5598 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 5599 | return -EROFS; |
| 5600 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5601 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5602 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5603 | binode = list_entry(head->next, struct btrfs_inode, |
| 5604 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5605 | inode = igrab(&binode->vfs_inode); |
| 5606 | if (!inode) |
| 5607 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5608 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5609 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5610 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 5611 | if (delay_iput) |
| 5612 | btrfs_add_delayed_iput(inode); |
| 5613 | else |
| 5614 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5615 | } |
| 5616 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5617 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5618 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5619 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5620 | |
| 5621 | /* the filemap_flush will queue IO into the worker threads, but |
| 5622 | * we have to make sure the IO is actually started and that |
| 5623 | * ordered extents get created before we return |
| 5624 | */ |
| 5625 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5626 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5627 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5628 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5629 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 5630 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5631 | } |
| 5632 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5633 | return 0; |
| 5634 | } |
| 5635 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5636 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 5637 | const char *symname) |
| 5638 | { |
| 5639 | struct btrfs_trans_handle *trans; |
| 5640 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5641 | struct btrfs_path *path; |
| 5642 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5643 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5644 | int err; |
| 5645 | int drop_inode = 0; |
| 5646 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5647 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5648 | int name_len; |
| 5649 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5650 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5651 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5652 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5653 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5654 | |
| 5655 | name_len = strlen(symname) + 1; |
| 5656 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 5657 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5658 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5659 | /* |
| 5660 | * 2 items for inode item and ref |
| 5661 | * 2 items for dir items |
| 5662 | * 1 item for xattr if selinux is on |
| 5663 | */ |
| 5664 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5665 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5666 | return err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5667 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5668 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5669 | if (!trans) |
| 5670 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5671 | btrfs_set_trans_block_group(trans, dir); |
| 5672 | |
| 5673 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 5674 | if (err) { |
| 5675 | err = -ENOSPC; |
| 5676 | goto out_unlock; |
| 5677 | } |
| 5678 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5679 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5680 | dentry->d_name.len, |
| 5681 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5682 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 5683 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5684 | err = PTR_ERR(inode); |
| 5685 | if (IS_ERR(inode)) |
| 5686 | goto out_unlock; |
| 5687 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 5688 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5689 | if (err) { |
| 5690 | drop_inode = 1; |
| 5691 | goto out_unlock; |
| 5692 | } |
| 5693 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5694 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5695 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5696 | if (err) |
| 5697 | drop_inode = 1; |
| 5698 | else { |
| 5699 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 5700 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5701 | inode->i_fop = &btrfs_file_operations; |
| 5702 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5703 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5704 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5705 | btrfs_update_inode_block_group(trans, inode); |
| 5706 | btrfs_update_inode_block_group(trans, dir); |
| 5707 | if (drop_inode) |
| 5708 | goto out_unlock; |
| 5709 | |
| 5710 | path = btrfs_alloc_path(); |
| 5711 | BUG_ON(!path); |
| 5712 | key.objectid = inode->i_ino; |
| 5713 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5714 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 5715 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 5716 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 5717 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5718 | if (err) { |
| 5719 | drop_inode = 1; |
| 5720 | goto out_unlock; |
| 5721 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5722 | leaf = path->nodes[0]; |
| 5723 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 5724 | struct btrfs_file_extent_item); |
| 5725 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 5726 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5727 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5728 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 5729 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 5730 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 5731 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 5732 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5733 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5734 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 5735 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5736 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5737 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5738 | inode->i_op = &btrfs_symlink_inode_operations; |
| 5739 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 5740 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5741 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5742 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5743 | err = btrfs_update_inode(trans, root, inode); |
| 5744 | if (err) |
| 5745 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5746 | |
| 5747 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5748 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 5749 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5750 | out_fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5751 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5752 | if (drop_inode) { |
| 5753 | inode_dec_link_count(inode); |
| 5754 | iput(inode); |
| 5755 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5756 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5757 | return err; |
| 5758 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 5759 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5760 | static int prealloc_file_range(struct inode *inode, u64 start, u64 end, |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 5761 | u64 alloc_hint, int mode, loff_t actual_len) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5762 | { |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5763 | struct btrfs_trans_handle *trans; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5764 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5765 | struct btrfs_key ins; |
| 5766 | u64 alloc_size; |
| 5767 | u64 cur_offset = start; |
| 5768 | u64 num_bytes = end - start; |
| 5769 | int ret = 0; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 5770 | u64 i_size; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5771 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5772 | while (num_bytes > 0) { |
| 5773 | alloc_size = min(num_bytes, root->fs_info->max_extent); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5774 | |
Chris Mason | 3a1abec | 2009-12-17 15:47:17 -0500 | [diff] [blame] | 5775 | trans = btrfs_start_transaction(root, 1); |
| 5776 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5777 | ret = btrfs_reserve_extent(trans, root, alloc_size, |
| 5778 | root->sectorsize, 0, alloc_hint, |
| 5779 | (u64)-1, &ins, 1); |
| 5780 | if (ret) { |
| 5781 | WARN_ON(1); |
Chris Mason | 3a1abec | 2009-12-17 15:47:17 -0500 | [diff] [blame] | 5782 | goto stop_trans; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5783 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5784 | |
| 5785 | ret = btrfs_reserve_metadata_space(root, 3); |
| 5786 | if (ret) { |
| 5787 | btrfs_free_reserved_extent(root, ins.objectid, |
| 5788 | ins.offset); |
Chris Mason | 3a1abec | 2009-12-17 15:47:17 -0500 | [diff] [blame] | 5789 | goto stop_trans; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5792 | ret = insert_reserved_file_extent(trans, inode, |
| 5793 | cur_offset, ins.objectid, |
| 5794 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 5795 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5796 | BTRFS_FILE_EXTENT_PREALLOC); |
| 5797 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 5798 | btrfs_drop_extent_cache(inode, cur_offset, |
| 5799 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5800 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5801 | num_bytes -= ins.offset; |
| 5802 | cur_offset += ins.offset; |
| 5803 | alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5804 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5805 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5806 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5807 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Aneesh Kumar K.V | 23b5c50 | 2010-02-04 11:33:03 -0500 | [diff] [blame] | 5808 | (actual_len > inode->i_size) && |
| 5809 | (cur_offset > inode->i_size)) { |
| 5810 | |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 5811 | if (cur_offset > actual_len) |
| 5812 | i_size = actual_len; |
| 5813 | else |
| 5814 | i_size = cur_offset; |
| 5815 | i_size_write(inode, i_size); |
| 5816 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5817 | } |
| 5818 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5819 | ret = btrfs_update_inode(trans, root, inode); |
| 5820 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5821 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5822 | btrfs_end_transaction(trans, root); |
| 5823 | btrfs_unreserve_metadata_space(root, 3); |
| 5824 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5825 | return ret; |
Chris Mason | 3a1abec | 2009-12-17 15:47:17 -0500 | [diff] [blame] | 5826 | |
| 5827 | stop_trans: |
| 5828 | btrfs_end_transaction(trans, root); |
| 5829 | return ret; |
| 5830 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | static long btrfs_fallocate(struct inode *inode, int mode, |
| 5834 | loff_t offset, loff_t len) |
| 5835 | { |
| 5836 | u64 cur_offset; |
| 5837 | u64 last_byte; |
| 5838 | u64 alloc_start; |
| 5839 | u64 alloc_end; |
| 5840 | u64 alloc_hint = 0; |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5841 | u64 locked_end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5842 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 5843 | struct extent_map *em; |
| 5844 | int ret; |
| 5845 | |
| 5846 | alloc_start = offset & ~mask; |
| 5847 | alloc_end = (offset + len + mask) & ~mask; |
| 5848 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5849 | /* |
| 5850 | * wait for ordered IO before we have any locks. We'll loop again |
| 5851 | * below with the locks held. |
| 5852 | */ |
| 5853 | btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start); |
| 5854 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5855 | mutex_lock(&inode->i_mutex); |
| 5856 | if (alloc_start > inode->i_size) { |
| 5857 | ret = btrfs_cont_expand(inode, alloc_start); |
| 5858 | if (ret) |
| 5859 | goto out; |
| 5860 | } |
| 5861 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5862 | ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode, |
Josef Bacik | a970b0a | 2009-06-27 21:07:34 -0400 | [diff] [blame] | 5863 | alloc_end - alloc_start); |
| 5864 | if (ret) |
| 5865 | goto out; |
| 5866 | |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5867 | locked_end = alloc_end - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5868 | while (1) { |
| 5869 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5870 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5871 | /* the extent lock is ordered inside the running |
| 5872 | * transaction |
| 5873 | */ |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5874 | lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
| 5875 | GFP_NOFS); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5876 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 5877 | alloc_end - 1); |
| 5878 | if (ordered && |
| 5879 | ordered->file_offset + ordered->len > alloc_start && |
| 5880 | ordered->file_offset < alloc_end) { |
| 5881 | btrfs_put_ordered_extent(ordered); |
| 5882 | unlock_extent(&BTRFS_I(inode)->io_tree, |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5883 | alloc_start, locked_end, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5884 | /* |
| 5885 | * we can't wait on the range with the transaction |
| 5886 | * running or with the extent lock held |
| 5887 | */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5888 | btrfs_wait_ordered_range(inode, alloc_start, |
| 5889 | alloc_end - alloc_start); |
| 5890 | } else { |
| 5891 | if (ordered) |
| 5892 | btrfs_put_ordered_extent(ordered); |
| 5893 | break; |
| 5894 | } |
| 5895 | } |
| 5896 | |
| 5897 | cur_offset = alloc_start; |
| 5898 | while (1) { |
| 5899 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 5900 | alloc_end - cur_offset, 0); |
| 5901 | BUG_ON(IS_ERR(em) || !em); |
| 5902 | last_byte = min(extent_map_end(em), alloc_end); |
| 5903 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5904 | if (em->block_start == EXTENT_MAP_HOLE || |
| 5905 | (cur_offset >= inode->i_size && |
| 5906 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5907 | ret = prealloc_file_range(inode, |
| 5908 | cur_offset, last_byte, |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 5909 | alloc_hint, mode, offset+len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5910 | if (ret < 0) { |
| 5911 | free_extent_map(em); |
| 5912 | break; |
| 5913 | } |
| 5914 | } |
| 5915 | if (em->block_start <= EXTENT_MAP_LAST_BYTE) |
| 5916 | alloc_hint = em->block_start; |
| 5917 | free_extent_map(em); |
| 5918 | |
| 5919 | cur_offset = last_byte; |
| 5920 | if (cur_offset >= alloc_end) { |
| 5921 | ret = 0; |
| 5922 | break; |
| 5923 | } |
| 5924 | } |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5925 | unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5926 | GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5927 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5928 | btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode, |
| 5929 | alloc_end - alloc_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5930 | out: |
| 5931 | mutex_unlock(&inode->i_mutex); |
| 5932 | return ret; |
| 5933 | } |
| 5934 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5935 | static int btrfs_set_page_dirty(struct page *page) |
| 5936 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5937 | return __set_page_dirty_nobuffers(page); |
| 5938 | } |
| 5939 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5940 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5941 | { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5942 | if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5943 | return -EACCES; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5944 | return generic_permission(inode, mask, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5945 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5946 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 5947 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5948 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5949 | .lookup = btrfs_lookup, |
| 5950 | .create = btrfs_create, |
| 5951 | .unlink = btrfs_unlink, |
| 5952 | .link = btrfs_link, |
| 5953 | .mkdir = btrfs_mkdir, |
| 5954 | .rmdir = btrfs_rmdir, |
| 5955 | .rename = btrfs_rename, |
| 5956 | .symlink = btrfs_symlink, |
| 5957 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5958 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5959 | .setxattr = btrfs_setxattr, |
| 5960 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5961 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5962 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5963 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5964 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 5965 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5966 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5967 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5968 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5969 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 5970 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5971 | .llseek = generic_file_llseek, |
| 5972 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 5973 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 5974 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5975 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 5976 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5977 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 5978 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5979 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5980 | }; |
| 5981 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5982 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5983 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 5984 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5985 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5986 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5987 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 5988 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5989 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 5990 | .set_bit_hook = btrfs_set_bit_hook, |
| 5991 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5992 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 5993 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5994 | }; |
| 5995 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 5996 | /* |
| 5997 | * btrfs doesn't support the bmap operation because swapfiles |
| 5998 | * use bmap to make a mapping of extents in the file. They assume |
| 5999 | * these extents won't change over the life of the file and they |
| 6000 | * use the bmap result to do IO directly to the drive. |
| 6001 | * |
| 6002 | * the btrfs bmap call would return logical addresses that aren't |
| 6003 | * suitable for IO and they also will change frequently as COW |
| 6004 | * operations happen. So, swapfile + btrfs == corruption. |
| 6005 | * |
| 6006 | * For now we're avoiding this by dropping bmap. |
| 6007 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 6008 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6009 | .readpage = btrfs_readpage, |
| 6010 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6011 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6012 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6013 | .sync_page = block_sync_page, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6014 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6015 | .invalidatepage = btrfs_invalidatepage, |
| 6016 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6017 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 6018 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6019 | }; |
| 6020 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 6021 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6022 | .readpage = btrfs_readpage, |
| 6023 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 6024 | .invalidatepage = btrfs_invalidatepage, |
| 6025 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6026 | }; |
| 6027 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6028 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6029 | .truncate = btrfs_truncate, |
| 6030 | .getattr = btrfs_getattr, |
| 6031 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6032 | .setxattr = btrfs_setxattr, |
| 6033 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 6034 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6035 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6036 | .permission = btrfs_permission, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6037 | .fallocate = btrfs_fallocate, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6038 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6039 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6040 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6041 | .getattr = btrfs_getattr, |
| 6042 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6043 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6044 | .setxattr = btrfs_setxattr, |
| 6045 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6046 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6047 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6048 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6049 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6050 | .readlink = generic_readlink, |
| 6051 | .follow_link = page_follow_link_light, |
| 6052 | .put_link = page_put_link, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6053 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 6054 | .setxattr = btrfs_setxattr, |
| 6055 | .getxattr = btrfs_getxattr, |
| 6056 | .listxattr = btrfs_listxattr, |
| 6057 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6058 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6059 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 6060 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6061 | .d_delete = btrfs_dentry_delete, |
| 6062 | }; |