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 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 91 | static int btrfs_init_inode_security(struct inode *inode, struct inode *dir) |
| 92 | { |
| 93 | int err; |
| 94 | |
| 95 | err = btrfs_init_acl(inode, dir); |
| 96 | if (!err) |
| 97 | err = btrfs_xattr_security_init(inode, dir); |
| 98 | return err; |
| 99 | } |
| 100 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 101 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 102 | * this does all the hard work for inserting an inline extent into |
| 103 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 104 | * no overlapping inline items exist in the btree |
| 105 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 106 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 107 | struct btrfs_root *root, struct inode *inode, |
| 108 | u64 start, size_t size, size_t compressed_size, |
| 109 | struct page **compressed_pages) |
| 110 | { |
| 111 | struct btrfs_key key; |
| 112 | struct btrfs_path *path; |
| 113 | struct extent_buffer *leaf; |
| 114 | struct page *page = NULL; |
| 115 | char *kaddr; |
| 116 | unsigned long ptr; |
| 117 | struct btrfs_file_extent_item *ei; |
| 118 | int err = 0; |
| 119 | int ret; |
| 120 | size_t cur_size = size; |
| 121 | size_t datasize; |
| 122 | unsigned long offset; |
| 123 | int use_compress = 0; |
| 124 | |
| 125 | if (compressed_size && compressed_pages) { |
| 126 | use_compress = 1; |
| 127 | cur_size = compressed_size; |
| 128 | } |
| 129 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 130 | path = btrfs_alloc_path(); |
| 131 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 132 | return -ENOMEM; |
| 133 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 134 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 135 | btrfs_set_trans_block_group(trans, inode); |
| 136 | |
| 137 | key.objectid = inode->i_ino; |
| 138 | key.offset = start; |
| 139 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 140 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 141 | |
| 142 | inode_add_bytes(inode, size); |
| 143 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 144 | datasize); |
| 145 | BUG_ON(ret); |
| 146 | if (ret) { |
| 147 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 148 | goto fail; |
| 149 | } |
| 150 | leaf = path->nodes[0]; |
| 151 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 152 | struct btrfs_file_extent_item); |
| 153 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 154 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 155 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 156 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 157 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 158 | ptr = btrfs_file_extent_inline_start(ei); |
| 159 | |
| 160 | if (use_compress) { |
| 161 | struct page *cpage; |
| 162 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 163 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 164 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 165 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | PAGE_CACHE_SIZE); |
| 167 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 168 | kaddr = kmap_atomic(cpage, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 169 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 170 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | |
| 172 | i++; |
| 173 | ptr += cur_size; |
| 174 | compressed_size -= cur_size; |
| 175 | } |
| 176 | btrfs_set_file_extent_compression(leaf, ei, |
| 177 | BTRFS_COMPRESS_ZLIB); |
| 178 | } else { |
| 179 | page = find_get_page(inode->i_mapping, |
| 180 | start >> PAGE_CACHE_SHIFT); |
| 181 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 182 | kaddr = kmap_atomic(page, KM_USER0); |
| 183 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 184 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
| 185 | kunmap_atomic(kaddr, KM_USER0); |
| 186 | page_cache_release(page); |
| 187 | } |
| 188 | btrfs_mark_buffer_dirty(leaf); |
| 189 | btrfs_free_path(path); |
| 190 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 191 | /* |
| 192 | * we're an inline extent, so nobody can |
| 193 | * extend the file past i_size without locking |
| 194 | * a page we already have locked. |
| 195 | * |
| 196 | * We must do any isize and inode updates |
| 197 | * before we unlock the pages. Otherwise we |
| 198 | * could end up racing with unlink. |
| 199 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 200 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
| 201 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 202 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 203 | return 0; |
| 204 | fail: |
| 205 | btrfs_free_path(path); |
| 206 | return err; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /* |
| 211 | * conditionally insert an inline extent into the file. This |
| 212 | * does the checks required to make sure the data is small enough |
| 213 | * to fit as an inline extent. |
| 214 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 215 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 216 | struct btrfs_root *root, |
| 217 | struct inode *inode, u64 start, u64 end, |
| 218 | size_t compressed_size, |
| 219 | struct page **compressed_pages) |
| 220 | { |
| 221 | u64 isize = i_size_read(inode); |
| 222 | u64 actual_end = min(end + 1, isize); |
| 223 | u64 inline_len = actual_end - start; |
| 224 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 225 | ~((u64)root->sectorsize - 1); |
| 226 | u64 hint_byte; |
| 227 | u64 data_len = inline_len; |
| 228 | int ret; |
| 229 | |
| 230 | if (compressed_size) |
| 231 | data_len = compressed_size; |
| 232 | |
| 233 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 234 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 235 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 236 | (!compressed_size && |
| 237 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 238 | end + 1 < isize || |
| 239 | data_len > root->fs_info->max_inline) { |
| 240 | return 1; |
| 241 | } |
| 242 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 243 | ret = btrfs_drop_extents(trans, inode, start, aligned_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 244 | &hint_byte, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 245 | BUG_ON(ret); |
| 246 | |
| 247 | if (isize > actual_end) |
| 248 | inline_len = min_t(u64, isize, actual_end); |
| 249 | ret = insert_inline_extent(trans, root, inode, start, |
| 250 | inline_len, compressed_size, |
| 251 | compressed_pages); |
| 252 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 253 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 257 | struct async_extent { |
| 258 | u64 start; |
| 259 | u64 ram_size; |
| 260 | u64 compressed_size; |
| 261 | struct page **pages; |
| 262 | unsigned long nr_pages; |
| 263 | struct list_head list; |
| 264 | }; |
| 265 | |
| 266 | struct async_cow { |
| 267 | struct inode *inode; |
| 268 | struct btrfs_root *root; |
| 269 | struct page *locked_page; |
| 270 | u64 start; |
| 271 | u64 end; |
| 272 | struct list_head extents; |
| 273 | struct btrfs_work work; |
| 274 | }; |
| 275 | |
| 276 | static noinline int add_async_extent(struct async_cow *cow, |
| 277 | u64 start, u64 ram_size, |
| 278 | u64 compressed_size, |
| 279 | struct page **pages, |
| 280 | unsigned long nr_pages) |
| 281 | { |
| 282 | struct async_extent *async_extent; |
| 283 | |
| 284 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
| 285 | async_extent->start = start; |
| 286 | async_extent->ram_size = ram_size; |
| 287 | async_extent->compressed_size = compressed_size; |
| 288 | async_extent->pages = pages; |
| 289 | async_extent->nr_pages = nr_pages; |
| 290 | list_add_tail(&async_extent->list, &cow->extents); |
| 291 | return 0; |
| 292 | } |
| 293 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 294 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 295 | * we create compressed extents in two phases. The first |
| 296 | * phase compresses a range of pages that have already been |
| 297 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 298 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 299 | * This is done inside an ordered work queue, and the compression |
| 300 | * is spread across many cpus. The actual IO submission is step |
| 301 | * two, and the ordered work queue takes care of making sure that |
| 302 | * happens in the same order things were put onto the queue by |
| 303 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 304 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 305 | * If this code finds it can't get good compression, it puts an |
| 306 | * entry onto the work queue to write the uncompressed bytes. This |
| 307 | * makes sure that both compressed inodes and uncompressed inodes |
| 308 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 309 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 310 | static noinline int compress_file_range(struct inode *inode, |
| 311 | struct page *locked_page, |
| 312 | u64 start, u64 end, |
| 313 | struct async_cow *async_cow, |
| 314 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 315 | { |
| 316 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 317 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 318 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 319 | u64 orig_start; |
| 320 | u64 disk_num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 321 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 322 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 323 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 324 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 325 | struct page **pages = NULL; |
| 326 | unsigned long nr_pages; |
| 327 | unsigned long nr_pages_ret = 0; |
| 328 | unsigned long total_compressed = 0; |
| 329 | unsigned long total_in = 0; |
| 330 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 331 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 332 | int i; |
| 333 | int will_compress; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 334 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 335 | orig_start = start; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 336 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 337 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 338 | again: |
| 339 | will_compress = 0; |
| 340 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 341 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 342 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 343 | /* |
| 344 | * we don't want to send crud past the end of i_size through |
| 345 | * compression, that's just a waste of CPU time. So, if the |
| 346 | * end of the file is before the start of our current |
| 347 | * requested range of bytes, we bail out to the uncompressed |
| 348 | * cleanup code that can deal with all of this. |
| 349 | * |
| 350 | * It isn't really the fastest way to fix things, but this is a |
| 351 | * very uncommon corner. |
| 352 | */ |
| 353 | if (actual_end <= start) |
| 354 | goto cleanup_and_bail_uncompressed; |
| 355 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 356 | total_compressed = actual_end - start; |
| 357 | |
| 358 | /* we want to make sure that amount of ram required to uncompress |
| 359 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 360 | * of a compressed extent to 128k. This is a crucial number |
| 361 | * because it also controls how easily we can spread reads across |
| 362 | * cpus for decompression. |
| 363 | * |
| 364 | * We also want to make sure the amount of IO required to do |
| 365 | * a random read is reasonably small, so we limit the size of |
| 366 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 367 | */ |
| 368 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 369 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 370 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 371 | disk_num_bytes = num_bytes; |
| 372 | total_in = 0; |
| 373 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 374 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 375 | /* |
| 376 | * we do compression for mount -o compress and when the |
| 377 | * inode has not been flagged as nocompress. This flag can |
| 378 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 379 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 380 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 381 | btrfs_test_opt(root, COMPRESS)) { |
| 382 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 383 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 384 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 385 | ret = btrfs_zlib_compress_pages(inode->i_mapping, start, |
| 386 | total_compressed, pages, |
| 387 | nr_pages, &nr_pages_ret, |
| 388 | &total_in, |
| 389 | &total_compressed, |
| 390 | max_compressed); |
| 391 | |
| 392 | if (!ret) { |
| 393 | unsigned long offset = total_compressed & |
| 394 | (PAGE_CACHE_SIZE - 1); |
| 395 | struct page *page = pages[nr_pages_ret - 1]; |
| 396 | char *kaddr; |
| 397 | |
| 398 | /* zero the tail end of the last page, we might be |
| 399 | * sending it down to disk |
| 400 | */ |
| 401 | if (offset) { |
| 402 | kaddr = kmap_atomic(page, KM_USER0); |
| 403 | memset(kaddr + offset, 0, |
| 404 | PAGE_CACHE_SIZE - offset); |
| 405 | kunmap_atomic(kaddr, KM_USER0); |
| 406 | } |
| 407 | will_compress = 1; |
| 408 | } |
| 409 | } |
| 410 | if (start == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 411 | trans = btrfs_join_transaction(root, 1); |
| 412 | BUG_ON(!trans); |
| 413 | btrfs_set_trans_block_group(trans, inode); |
| 414 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 415 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 416 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 417 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 418 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 419 | */ |
| 420 | ret = cow_file_range_inline(trans, root, inode, |
| 421 | start, end, 0, NULL); |
| 422 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 423 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 424 | ret = cow_file_range_inline(trans, root, inode, |
| 425 | start, end, |
| 426 | total_compressed, pages); |
| 427 | } |
| 428 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 429 | /* |
| 430 | * inline extent creation worked, we don't need |
| 431 | * to create any more async work items. Unlock |
| 432 | * and free up our temp pages. |
| 433 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 434 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 435 | &BTRFS_I(inode)->io_tree, |
| 436 | start, end, NULL, |
| 437 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 438 | EXTENT_CLEAR_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 439 | EXTENT_CLEAR_ACCOUNTING | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 440 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 441 | |
| 442 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 443 | goto free_pages_out; |
| 444 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 445 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | if (will_compress) { |
| 449 | /* |
| 450 | * we aren't doing an inline extent round the compressed size |
| 451 | * up to a block size boundary so the allocator does sane |
| 452 | * things |
| 453 | */ |
| 454 | total_compressed = (total_compressed + blocksize - 1) & |
| 455 | ~(blocksize - 1); |
| 456 | |
| 457 | /* |
| 458 | * one last check to make sure the compression is really a |
| 459 | * win, compare the page count read with the blocks on disk |
| 460 | */ |
| 461 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 462 | ~(PAGE_CACHE_SIZE - 1); |
| 463 | if (total_compressed >= total_in) { |
| 464 | will_compress = 0; |
| 465 | } else { |
| 466 | disk_num_bytes = total_compressed; |
| 467 | num_bytes = total_in; |
| 468 | } |
| 469 | } |
| 470 | if (!will_compress && pages) { |
| 471 | /* |
| 472 | * the compression code ran but failed to make things smaller, |
| 473 | * free any pages it allocated and our page pointer array |
| 474 | */ |
| 475 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 476 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 477 | page_cache_release(pages[i]); |
| 478 | } |
| 479 | kfree(pages); |
| 480 | pages = NULL; |
| 481 | total_compressed = 0; |
| 482 | nr_pages_ret = 0; |
| 483 | |
| 484 | /* flag the file so we don't compress in the future */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 485 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 486 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 487 | if (will_compress) { |
| 488 | *num_added += 1; |
| 489 | |
| 490 | /* the async work queues will take care of doing actual |
| 491 | * allocation on disk for these compressed pages, |
| 492 | * and will submit them to the elevator. |
| 493 | */ |
| 494 | add_async_extent(async_cow, start, num_bytes, |
| 495 | total_compressed, pages, nr_pages_ret); |
| 496 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 497 | if (start + num_bytes < end && start + num_bytes < actual_end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 498 | start += num_bytes; |
| 499 | pages = NULL; |
| 500 | cond_resched(); |
| 501 | goto again; |
| 502 | } |
| 503 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 504 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 505 | /* |
| 506 | * No compression, but we still need to write the pages in |
| 507 | * the file we've been given so far. redirty the locked |
| 508 | * page if it corresponds to our extent and set things up |
| 509 | * for the async work queue to run cow_file_range to do |
| 510 | * the normal delalloc dance |
| 511 | */ |
| 512 | if (page_offset(locked_page) >= start && |
| 513 | page_offset(locked_page) <= end) { |
| 514 | __set_page_dirty_nobuffers(locked_page); |
| 515 | /* unlocked later on in the async handlers */ |
| 516 | } |
| 517 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0); |
| 518 | *num_added += 1; |
| 519 | } |
| 520 | |
| 521 | out: |
| 522 | return 0; |
| 523 | |
| 524 | free_pages_out: |
| 525 | for (i = 0; i < nr_pages_ret; i++) { |
| 526 | WARN_ON(pages[i]->mapping); |
| 527 | page_cache_release(pages[i]); |
| 528 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 529 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 530 | |
| 531 | goto out; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * phase two of compressed writeback. This is the ordered portion |
| 536 | * of the code, which only gets called in the order the work was |
| 537 | * queued. We walk all the async extents created by compress_file_range |
| 538 | * and send them down to the disk. |
| 539 | */ |
| 540 | static noinline int submit_compressed_extents(struct inode *inode, |
| 541 | struct async_cow *async_cow) |
| 542 | { |
| 543 | struct async_extent *async_extent; |
| 544 | u64 alloc_hint = 0; |
| 545 | struct btrfs_trans_handle *trans; |
| 546 | struct btrfs_key ins; |
| 547 | struct extent_map *em; |
| 548 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 549 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 550 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 551 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 552 | |
| 553 | if (list_empty(&async_cow->extents)) |
| 554 | return 0; |
| 555 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 556 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 557 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 558 | async_extent = list_entry(async_cow->extents.next, |
| 559 | struct async_extent, list); |
| 560 | list_del(&async_extent->list); |
| 561 | |
| 562 | io_tree = &BTRFS_I(inode)->io_tree; |
| 563 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 564 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 565 | /* did the compression code fall back to uncompressed IO? */ |
| 566 | if (!async_extent->pages) { |
| 567 | int page_started = 0; |
| 568 | unsigned long nr_written = 0; |
| 569 | |
| 570 | lock_extent(io_tree, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 571 | async_extent->start + |
| 572 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 573 | |
| 574 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 575 | ret = cow_file_range(inode, async_cow->locked_page, |
| 576 | async_extent->start, |
| 577 | async_extent->start + |
| 578 | async_extent->ram_size - 1, |
| 579 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 580 | |
| 581 | /* |
| 582 | * if page_started, cow_file_range inserted an |
| 583 | * inline extent and took care of all the unlocking |
| 584 | * and IO for us. Otherwise, we need to submit |
| 585 | * all those pages down to the drive. |
| 586 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 587 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 588 | extent_write_locked_range(io_tree, |
| 589 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 590 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 591 | async_extent->ram_size - 1, |
| 592 | btrfs_get_extent, |
| 593 | WB_SYNC_ALL); |
| 594 | kfree(async_extent); |
| 595 | cond_resched(); |
| 596 | continue; |
| 597 | } |
| 598 | |
| 599 | lock_extent(io_tree, async_extent->start, |
| 600 | async_extent->start + async_extent->ram_size - 1, |
| 601 | GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 602 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 603 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 604 | ret = btrfs_reserve_extent(trans, root, |
| 605 | async_extent->compressed_size, |
| 606 | async_extent->compressed_size, |
| 607 | 0, alloc_hint, |
| 608 | (u64)-1, &ins, 1); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 609 | btrfs_end_transaction(trans, root); |
| 610 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 611 | if (ret) { |
| 612 | int i; |
| 613 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 614 | WARN_ON(async_extent->pages[i]->mapping); |
| 615 | page_cache_release(async_extent->pages[i]); |
| 616 | } |
| 617 | kfree(async_extent->pages); |
| 618 | async_extent->nr_pages = 0; |
| 619 | async_extent->pages = NULL; |
| 620 | unlock_extent(io_tree, async_extent->start, |
| 621 | async_extent->start + |
| 622 | async_extent->ram_size - 1, GFP_NOFS); |
| 623 | goto retry; |
| 624 | } |
| 625 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 626 | /* |
| 627 | * here we're doing allocation and writeback of the |
| 628 | * compressed pages |
| 629 | */ |
| 630 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 631 | async_extent->start + |
| 632 | async_extent->ram_size - 1, 0); |
| 633 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 634 | em = alloc_extent_map(GFP_NOFS); |
| 635 | em->start = async_extent->start; |
| 636 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 637 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 638 | |
| 639 | em->block_start = ins.objectid; |
| 640 | em->block_len = ins.offset; |
| 641 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 642 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 643 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 644 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 645 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 646 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 647 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 648 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 649 | if (ret != -EEXIST) { |
| 650 | free_extent_map(em); |
| 651 | break; |
| 652 | } |
| 653 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 654 | async_extent->start + |
| 655 | async_extent->ram_size - 1, 0); |
| 656 | } |
| 657 | |
| 658 | ret = btrfs_add_ordered_extent(inode, async_extent->start, |
| 659 | ins.objectid, |
| 660 | async_extent->ram_size, |
| 661 | ins.offset, |
| 662 | BTRFS_ORDERED_COMPRESSED); |
| 663 | BUG_ON(ret); |
| 664 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 665 | /* |
| 666 | * clear dirty, set writeback and unlock the pages. |
| 667 | */ |
| 668 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 669 | &BTRFS_I(inode)->io_tree, |
| 670 | async_extent->start, |
| 671 | async_extent->start + |
| 672 | async_extent->ram_size - 1, |
| 673 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 674 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 675 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 676 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 677 | |
| 678 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 679 | async_extent->start, |
| 680 | async_extent->ram_size, |
| 681 | ins.objectid, |
| 682 | ins.offset, async_extent->pages, |
| 683 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 684 | |
| 685 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 686 | alloc_hint = ins.objectid + ins.offset; |
| 687 | kfree(async_extent); |
| 688 | cond_resched(); |
| 689 | } |
| 690 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * when extent_io.c finds a delayed allocation range in the file, |
| 696 | * the call backs end up in this code. The basic idea is to |
| 697 | * allocate extents on disk for the range, and create ordered data structs |
| 698 | * in ram to track those extents. |
| 699 | * |
| 700 | * locked_page is the page that writepage had locked already. We use |
| 701 | * it to make sure we don't do extra locks or unlocks. |
| 702 | * |
| 703 | * *page_started is set to one if we unlock locked_page and do everything |
| 704 | * required to start IO on it. It may be clean and already done with |
| 705 | * IO when we return. |
| 706 | */ |
| 707 | static noinline int cow_file_range(struct inode *inode, |
| 708 | struct page *locked_page, |
| 709 | u64 start, u64 end, int *page_started, |
| 710 | unsigned long *nr_written, |
| 711 | int unlock) |
| 712 | { |
| 713 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 714 | struct btrfs_trans_handle *trans; |
| 715 | u64 alloc_hint = 0; |
| 716 | u64 num_bytes; |
| 717 | unsigned long ram_size; |
| 718 | u64 disk_num_bytes; |
| 719 | u64 cur_alloc_size; |
| 720 | u64 blocksize = root->sectorsize; |
| 721 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 722 | u64 isize = i_size_read(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 723 | struct btrfs_key ins; |
| 724 | struct extent_map *em; |
| 725 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 726 | int ret = 0; |
| 727 | |
| 728 | trans = btrfs_join_transaction(root, 1); |
| 729 | BUG_ON(!trans); |
| 730 | btrfs_set_trans_block_group(trans, inode); |
| 731 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 732 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 733 | |
| 734 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 735 | num_bytes = max(blocksize, num_bytes); |
| 736 | disk_num_bytes = num_bytes; |
| 737 | ret = 0; |
| 738 | |
| 739 | if (start == 0) { |
| 740 | /* lets try to make an inline extent */ |
| 741 | ret = cow_file_range_inline(trans, root, inode, |
| 742 | start, end, 0, NULL); |
| 743 | if (ret == 0) { |
| 744 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 745 | &BTRFS_I(inode)->io_tree, |
| 746 | start, end, NULL, |
| 747 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 748 | EXTENT_CLEAR_UNLOCK | |
| 749 | EXTENT_CLEAR_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 750 | EXTENT_CLEAR_ACCOUNTING | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 751 | EXTENT_CLEAR_DIRTY | |
| 752 | EXTENT_SET_WRITEBACK | |
| 753 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 754 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 755 | *nr_written = *nr_written + |
| 756 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 757 | *page_started = 1; |
| 758 | ret = 0; |
| 759 | goto out; |
| 760 | } |
| 761 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 762 | |
| 763 | BUG_ON(disk_num_bytes > |
| 764 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 765 | |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 766 | |
| 767 | read_lock(&BTRFS_I(inode)->extent_tree.lock); |
| 768 | em = search_extent_mapping(&BTRFS_I(inode)->extent_tree, |
| 769 | start, num_bytes); |
| 770 | if (em) { |
Josef Bacik | 6346c93 | 2009-11-10 21:23:47 -0500 | [diff] [blame] | 771 | /* |
| 772 | * if block start isn't an actual block number then find the |
| 773 | * first block in this inode and use that as a hint. If that |
| 774 | * block is also bogus then just don't worry about it. |
| 775 | */ |
| 776 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 777 | free_extent_map(em); |
| 778 | em = search_extent_mapping(em_tree, 0, 0); |
| 779 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 780 | alloc_hint = em->block_start; |
| 781 | if (em) |
| 782 | free_extent_map(em); |
| 783 | } else { |
| 784 | alloc_hint = em->block_start; |
| 785 | free_extent_map(em); |
| 786 | } |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 787 | } |
| 788 | read_unlock(&BTRFS_I(inode)->extent_tree.lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 789 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 790 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 791 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 792 | unsigned long op; |
| 793 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 794 | cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 795 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 796 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 797 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 798 | BUG_ON(ret); |
| 799 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 800 | em = alloc_extent_map(GFP_NOFS); |
| 801 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 802 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 803 | ram_size = ins.offset; |
| 804 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 805 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 806 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 807 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 808 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 809 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 810 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 811 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 812 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 813 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 814 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 815 | if (ret != -EEXIST) { |
| 816 | free_extent_map(em); |
| 817 | break; |
| 818 | } |
| 819 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 820 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 821 | } |
| 822 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 823 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 824 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 825 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 826 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 827 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 828 | if (root->root_key.objectid == |
| 829 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 830 | ret = btrfs_reloc_clone_csums(inode, start, |
| 831 | cur_alloc_size); |
| 832 | BUG_ON(ret); |
| 833 | } |
| 834 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 835 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 836 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 837 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 838 | /* we're not doing compressed IO, don't unlock the first |
| 839 | * page (which the caller expects to stay locked), don't |
| 840 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 841 | * |
| 842 | * Do set the Private2 bit so we know this page was properly |
| 843 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 844 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 845 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 846 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 847 | EXTENT_SET_PRIVATE2; |
| 848 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 849 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 850 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 851 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 852 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 853 | num_bytes -= cur_alloc_size; |
| 854 | alloc_hint = ins.objectid + ins.offset; |
| 855 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 856 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 857 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 858 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 859 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 860 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 861 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 862 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 863 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 864 | /* |
| 865 | * work queue call back to started compression on a file and pages |
| 866 | */ |
| 867 | static noinline void async_cow_start(struct btrfs_work *work) |
| 868 | { |
| 869 | struct async_cow *async_cow; |
| 870 | int num_added = 0; |
| 871 | async_cow = container_of(work, struct async_cow, work); |
| 872 | |
| 873 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 874 | async_cow->start, async_cow->end, async_cow, |
| 875 | &num_added); |
| 876 | if (num_added == 0) |
| 877 | async_cow->inode = NULL; |
| 878 | } |
| 879 | |
| 880 | /* |
| 881 | * work queue call back to submit previously compressed pages |
| 882 | */ |
| 883 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 884 | { |
| 885 | struct async_cow *async_cow; |
| 886 | struct btrfs_root *root; |
| 887 | unsigned long nr_pages; |
| 888 | |
| 889 | async_cow = container_of(work, struct async_cow, work); |
| 890 | |
| 891 | root = async_cow->root; |
| 892 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 893 | PAGE_CACHE_SHIFT; |
| 894 | |
| 895 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 896 | |
| 897 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 898 | 5 * 1042 * 1024 && |
| 899 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 900 | wake_up(&root->fs_info->async_submit_wait); |
| 901 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 902 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 903 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 904 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 905 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 906 | static noinline void async_cow_free(struct btrfs_work *work) |
| 907 | { |
| 908 | struct async_cow *async_cow; |
| 909 | async_cow = container_of(work, struct async_cow, work); |
| 910 | kfree(async_cow); |
| 911 | } |
| 912 | |
| 913 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 914 | u64 start, u64 end, int *page_started, |
| 915 | unsigned long *nr_written) |
| 916 | { |
| 917 | struct async_cow *async_cow; |
| 918 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 919 | unsigned long nr_pages; |
| 920 | u64 cur_end; |
| 921 | int limit = 10 * 1024 * 1042; |
| 922 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 923 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 924 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 925 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 926 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
| 927 | async_cow->inode = inode; |
| 928 | async_cow->root = root; |
| 929 | async_cow->locked_page = locked_page; |
| 930 | async_cow->start = start; |
| 931 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 932 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 933 | cur_end = end; |
| 934 | else |
| 935 | cur_end = min(end, start + 512 * 1024 - 1); |
| 936 | |
| 937 | async_cow->end = cur_end; |
| 938 | INIT_LIST_HEAD(&async_cow->extents); |
| 939 | |
| 940 | async_cow->work.func = async_cow_start; |
| 941 | async_cow->work.ordered_func = async_cow_submit; |
| 942 | async_cow->work.ordered_free = async_cow_free; |
| 943 | async_cow->work.flags = 0; |
| 944 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 945 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 946 | PAGE_CACHE_SHIFT; |
| 947 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 948 | |
| 949 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 950 | &async_cow->work); |
| 951 | |
| 952 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 953 | wait_event(root->fs_info->async_submit_wait, |
| 954 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 955 | limit)); |
| 956 | } |
| 957 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 958 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 959 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 960 | wait_event(root->fs_info->async_submit_wait, |
| 961 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 962 | 0)); |
| 963 | } |
| 964 | |
| 965 | *nr_written += nr_pages; |
| 966 | start = cur_end + 1; |
| 967 | } |
| 968 | *page_started = 1; |
| 969 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 970 | } |
| 971 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 972 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 973 | u64 bytenr, u64 num_bytes) |
| 974 | { |
| 975 | int ret; |
| 976 | struct btrfs_ordered_sum *sums; |
| 977 | LIST_HEAD(list); |
| 978 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 979 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
| 980 | bytenr + num_bytes - 1, &list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 981 | if (ret == 0 && list_empty(&list)) |
| 982 | return 0; |
| 983 | |
| 984 | while (!list_empty(&list)) { |
| 985 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 986 | list_del(&sums->list); |
| 987 | kfree(sums); |
| 988 | } |
| 989 | return 1; |
| 990 | } |
| 991 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 992 | /* |
| 993 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 994 | * of the extents that exist in the file, and COWs the file as required. |
| 995 | * |
| 996 | * If no cow copies or snapshots exist, we write directly to the existing |
| 997 | * blocks on disk |
| 998 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 999 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1000 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1001 | u64 start, u64 end, int *page_started, int force, |
| 1002 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1003 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1004 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1005 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1006 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1007 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1008 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1009 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1010 | u64 cow_start; |
| 1011 | u64 cur_offset; |
| 1012 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1013 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1014 | u64 disk_bytenr; |
| 1015 | u64 num_bytes; |
| 1016 | int extent_type; |
| 1017 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1018 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1019 | int nocow; |
| 1020 | int check_prev = 1; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1021 | |
| 1022 | path = btrfs_alloc_path(); |
| 1023 | BUG_ON(!path); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1024 | trans = btrfs_join_transaction(root, 1); |
| 1025 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1026 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1027 | cow_start = (u64)-1; |
| 1028 | cur_offset = start; |
| 1029 | while (1) { |
| 1030 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 1031 | cur_offset, 0); |
| 1032 | BUG_ON(ret < 0); |
| 1033 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1034 | leaf = path->nodes[0]; |
| 1035 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1036 | path->slots[0] - 1); |
| 1037 | if (found_key.objectid == inode->i_ino && |
| 1038 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1039 | path->slots[0]--; |
| 1040 | } |
| 1041 | check_prev = 0; |
| 1042 | next_slot: |
| 1043 | leaf = path->nodes[0]; |
| 1044 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1045 | ret = btrfs_next_leaf(root, path); |
| 1046 | if (ret < 0) |
| 1047 | BUG_ON(1); |
| 1048 | if (ret > 0) |
| 1049 | break; |
| 1050 | leaf = path->nodes[0]; |
| 1051 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1052 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1053 | nocow = 0; |
| 1054 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1055 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1056 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
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 | if (found_key.objectid > inode->i_ino || |
| 1059 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1060 | found_key.offset > end) |
| 1061 | break; |
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.offset > cur_offset) { |
| 1064 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1065 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1066 | goto out_check; |
| 1067 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1068 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1069 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1070 | struct btrfs_file_extent_item); |
| 1071 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1072 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1073 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1074 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1075 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1076 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1077 | extent_end = found_key.offset + |
| 1078 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1079 | if (extent_end <= start) { |
| 1080 | path->slots[0]++; |
| 1081 | goto next_slot; |
| 1082 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1083 | if (disk_bytenr == 0) |
| 1084 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1085 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1086 | btrfs_file_extent_encryption(leaf, fi) || |
| 1087 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1088 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1089 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1090 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1091 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1092 | goto out_check; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1093 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1094 | found_key.offset - |
| 1095 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1096 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1097 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1098 | disk_bytenr += cur_offset - found_key.offset; |
| 1099 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1100 | /* |
| 1101 | * force cow if csum exists in the range. |
| 1102 | * this ensure that csum for a given extent are |
| 1103 | * either valid or do not exist. |
| 1104 | */ |
| 1105 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1106 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1107 | nocow = 1; |
| 1108 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1109 | extent_end = found_key.offset + |
| 1110 | btrfs_file_extent_inline_len(leaf, fi); |
| 1111 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1112 | } else { |
| 1113 | BUG_ON(1); |
| 1114 | } |
| 1115 | out_check: |
| 1116 | if (extent_end <= start) { |
| 1117 | path->slots[0]++; |
| 1118 | goto next_slot; |
| 1119 | } |
| 1120 | if (!nocow) { |
| 1121 | if (cow_start == (u64)-1) |
| 1122 | cow_start = cur_offset; |
| 1123 | cur_offset = extent_end; |
| 1124 | if (cur_offset > end) |
| 1125 | break; |
| 1126 | path->slots[0]++; |
| 1127 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1128 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1129 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1130 | btrfs_release_path(root, path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1131 | if (cow_start != (u64)-1) { |
| 1132 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1133 | found_key.offset - 1, page_started, |
| 1134 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1135 | BUG_ON(ret); |
| 1136 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1137 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1138 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1139 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1140 | struct extent_map *em; |
| 1141 | struct extent_map_tree *em_tree; |
| 1142 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 1143 | em = alloc_extent_map(GFP_NOFS); |
| 1144 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1145 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1146 | em->len = num_bytes; |
| 1147 | em->block_len = num_bytes; |
| 1148 | em->block_start = disk_bytenr; |
| 1149 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1150 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1151 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1152 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1153 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1154 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1155 | if (ret != -EEXIST) { |
| 1156 | free_extent_map(em); |
| 1157 | break; |
| 1158 | } |
| 1159 | btrfs_drop_extent_cache(inode, em->start, |
| 1160 | em->start + em->len - 1, 0); |
| 1161 | } |
| 1162 | type = BTRFS_ORDERED_PREALLOC; |
| 1163 | } else { |
| 1164 | type = BTRFS_ORDERED_NOCOW; |
| 1165 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1166 | |
| 1167 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1168 | num_bytes, num_bytes, type); |
| 1169 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1170 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1171 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1172 | cur_offset, cur_offset + num_bytes - 1, |
| 1173 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1174 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1175 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1176 | cur_offset = extent_end; |
| 1177 | if (cur_offset > end) |
| 1178 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1179 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1180 | btrfs_release_path(root, path); |
| 1181 | |
| 1182 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1183 | cow_start = cur_offset; |
| 1184 | if (cow_start != (u64)-1) { |
| 1185 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1186 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1187 | BUG_ON(ret); |
| 1188 | } |
| 1189 | |
| 1190 | ret = btrfs_end_transaction(trans, root); |
| 1191 | BUG_ON(ret); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1192 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1193 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1194 | } |
| 1195 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1196 | /* |
| 1197 | * extent_io.c call back to do delayed allocation processing |
| 1198 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1199 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1200 | u64 start, u64 end, int *page_started, |
| 1201 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1202 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1203 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1204 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1205 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1206 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1207 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1208 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1209 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1210 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1211 | page_started, 0, nr_written); |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1212 | else if (!btrfs_test_opt(root, COMPRESS)) |
| 1213 | ret = cow_file_range(inode, locked_page, start, end, |
| 1214 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1215 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1216 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1217 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1218 | return ret; |
| 1219 | } |
| 1220 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1221 | static int btrfs_split_extent_hook(struct inode *inode, |
| 1222 | struct extent_state *orig, u64 split) |
| 1223 | { |
| 1224 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1225 | u64 size; |
| 1226 | |
| 1227 | if (!(orig->state & EXTENT_DELALLOC)) |
| 1228 | return 0; |
| 1229 | |
| 1230 | size = orig->end - orig->start + 1; |
| 1231 | if (size > root->fs_info->max_extent) { |
| 1232 | u64 num_extents; |
| 1233 | u64 new_size; |
| 1234 | |
| 1235 | new_size = orig->end - split + 1; |
| 1236 | num_extents = div64_u64(size + root->fs_info->max_extent - 1, |
| 1237 | root->fs_info->max_extent); |
| 1238 | |
| 1239 | /* |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1240 | * if we break a large extent up then leave oustanding_extents |
| 1241 | * be, since we've already accounted for the large extent. |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1242 | */ |
| 1243 | if (div64_u64(new_size + root->fs_info->max_extent - 1, |
| 1244 | root->fs_info->max_extent) < num_extents) |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1248 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1249 | BTRFS_I(inode)->outstanding_extents++; |
| 1250 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1251 | |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1257 | * extents so we can keep track of new extents that are just merged onto old |
| 1258 | * extents, such as when we are doing sequential writes, so we can properly |
| 1259 | * account for the metadata space we'll need. |
| 1260 | */ |
| 1261 | static int btrfs_merge_extent_hook(struct inode *inode, |
| 1262 | struct extent_state *new, |
| 1263 | struct extent_state *other) |
| 1264 | { |
| 1265 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1266 | u64 new_size, old_size; |
| 1267 | u64 num_extents; |
| 1268 | |
| 1269 | /* not delalloc, ignore it */ |
| 1270 | if (!(other->state & EXTENT_DELALLOC)) |
| 1271 | return 0; |
| 1272 | |
| 1273 | old_size = other->end - other->start + 1; |
| 1274 | if (new->start < other->start) |
| 1275 | new_size = other->end - new->start + 1; |
| 1276 | else |
| 1277 | new_size = new->end - other->start + 1; |
| 1278 | |
| 1279 | /* we're not bigger than the max, unreserve the space and go */ |
| 1280 | if (new_size <= root->fs_info->max_extent) { |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1281 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1282 | BTRFS_I(inode)->outstanding_extents--; |
| 1283 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * If we grew by another max_extent, just return, we want to keep that |
| 1289 | * reserved amount. |
| 1290 | */ |
| 1291 | num_extents = div64_u64(old_size + root->fs_info->max_extent - 1, |
| 1292 | root->fs_info->max_extent); |
| 1293 | if (div64_u64(new_size + root->fs_info->max_extent - 1, |
| 1294 | root->fs_info->max_extent) > num_extents) |
| 1295 | return 0; |
| 1296 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1297 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1298 | BTRFS_I(inode)->outstanding_extents--; |
| 1299 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1300 | |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1304 | /* |
| 1305 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1306 | * bytes in this file, and to maintain the list of inodes that |
| 1307 | * have pending delalloc work to be done. |
| 1308 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1309 | 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] | 1310 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1311 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1312 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1313 | /* |
| 1314 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1315 | * but in this case, we are only testeing for the DELALLOC |
| 1316 | * bit, which is only set or cleared with irqs on |
| 1317 | */ |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1318 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1319 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1320 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1321 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1322 | BTRFS_I(inode)->outstanding_extents++; |
| 1323 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1324 | btrfs_delalloc_reserve_space(root, inode, end - start + 1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1325 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1326 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1327 | root->fs_info->delalloc_bytes += end - start + 1; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1328 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1329 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1330 | &root->fs_info->delalloc_inodes); |
| 1331 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1332 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1333 | } |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1337 | /* |
| 1338 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1339 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1340 | static int btrfs_clear_bit_hook(struct inode *inode, |
| 1341 | struct extent_state *state, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1342 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1343 | /* |
| 1344 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1345 | * but in this case, we are only testeing for the DELALLOC |
| 1346 | * bit, which is only set or cleared with irqs on |
| 1347 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1348 | if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1349 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1350 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1351 | if (bits & EXTENT_DO_ACCOUNTING) { |
| 1352 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 1353 | BTRFS_I(inode)->outstanding_extents--; |
| 1354 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
| 1355 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
| 1356 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1357 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1358 | spin_lock(&root->fs_info->delalloc_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1359 | if (state->end - state->start + 1 > |
| 1360 | root->fs_info->delalloc_bytes) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1361 | printk(KERN_INFO "btrfs warning: delalloc account " |
| 1362 | "%llu %llu\n", |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1363 | (unsigned long long) |
| 1364 | state->end - state->start + 1, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1365 | (unsigned long long) |
| 1366 | root->fs_info->delalloc_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1367 | btrfs_delalloc_free_space(root, inode, (u64)-1); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1368 | root->fs_info->delalloc_bytes = 0; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1369 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1370 | } else { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1371 | btrfs_delalloc_free_space(root, inode, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1372 | state->end - |
| 1373 | state->start + 1); |
| 1374 | root->fs_info->delalloc_bytes -= state->end - |
| 1375 | state->start + 1; |
| 1376 | BTRFS_I(inode)->delalloc_bytes -= state->end - |
| 1377 | state->start + 1; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1378 | } |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1379 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
| 1380 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1381 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1382 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1383 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1384 | } |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1388 | /* |
| 1389 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1390 | * we don't create bios that span stripes or chunks |
| 1391 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1392 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1393 | size_t size, struct bio *bio, |
| 1394 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1395 | { |
| 1396 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1397 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1398 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1399 | u64 length = 0; |
| 1400 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1401 | int ret; |
| 1402 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1403 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1404 | return 0; |
| 1405 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1406 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1407 | map_tree = &root->fs_info->mapping_tree; |
| 1408 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1409 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1410 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1411 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1412 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1413 | return 1; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1414 | return 0; |
| 1415 | } |
| 1416 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1417 | /* |
| 1418 | * in order to insert checksums into the metadata in large chunks, |
| 1419 | * we wait until bio submission time. All the pages in the bio are |
| 1420 | * checksummed and sums are attached onto the ordered extent record. |
| 1421 | * |
| 1422 | * At IO completion time the cums attached on the ordered extent record |
| 1423 | * are inserted into the btree |
| 1424 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1425 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1426 | struct bio *bio, int mirror_num, |
| 1427 | unsigned long bio_flags) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1428 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1429 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1430 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1431 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1432 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1433 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1434 | return 0; |
| 1435 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1436 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1437 | /* |
| 1438 | * in order to insert checksums into the metadata in large chunks, |
| 1439 | * we wait until bio submission time. All the pages in the bio are |
| 1440 | * checksummed and sums are attached onto the ordered extent record. |
| 1441 | * |
| 1442 | * At IO completion time the cums attached on the ordered extent record |
| 1443 | * are inserted into the btree |
| 1444 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1445 | 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] | 1446 | int mirror_num, unsigned long bio_flags) |
| 1447 | { |
| 1448 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1449 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1450 | } |
| 1451 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1452 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1453 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1454 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1455 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1456 | 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] | 1457 | int mirror_num, unsigned long bio_flags) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1458 | { |
| 1459 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1460 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1461 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1462 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1463 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1464 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1465 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 1466 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1467 | |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1468 | if (!(rw & (1 << BIO_RW))) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1469 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1470 | return btrfs_submit_compressed_read(inode, bio, |
| 1471 | mirror_num, bio_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1472 | } else if (!skip_sum) |
| 1473 | btrfs_lookup_bio_sums(root, inode, bio, NULL); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1474 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1475 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1476 | /* csum items have already been cloned */ |
| 1477 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1478 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1479 | /* we're doing a write, do the async checksumming */ |
| 1480 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1481 | inode, rw, bio, mirror_num, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1482 | bio_flags, __btrfs_submit_bio_start, |
| 1483 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1484 | } |
| 1485 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1486 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1487 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1488 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1489 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1490 | /* |
| 1491 | * given a list of ordered sums record them in the inode. This happens |
| 1492 | * at IO completion time based on sums calculated at bio submission time. |
| 1493 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1494 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1495 | struct inode *inode, u64 file_offset, |
| 1496 | struct list_head *list) |
| 1497 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1498 | struct btrfs_ordered_sum *sum; |
| 1499 | |
| 1500 | btrfs_set_trans_block_group(trans, inode); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1501 | |
| 1502 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1503 | btrfs_csum_file_blocks(trans, |
| 1504 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1505 | } |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1509 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end) |
| 1510 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1511 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1512 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1513 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
| 1514 | GFP_NOFS); |
| 1515 | } |
| 1516 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1517 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1518 | struct btrfs_writepage_fixup { |
| 1519 | struct page *page; |
| 1520 | struct btrfs_work work; |
| 1521 | }; |
| 1522 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1523 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1524 | { |
| 1525 | struct btrfs_writepage_fixup *fixup; |
| 1526 | struct btrfs_ordered_extent *ordered; |
| 1527 | struct page *page; |
| 1528 | struct inode *inode; |
| 1529 | u64 page_start; |
| 1530 | u64 page_end; |
| 1531 | |
| 1532 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1533 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1534 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1535 | lock_page(page); |
| 1536 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1537 | ClearPageChecked(page); |
| 1538 | goto out_page; |
| 1539 | } |
| 1540 | |
| 1541 | inode = page->mapping->host; |
| 1542 | page_start = page_offset(page); |
| 1543 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1544 | |
| 1545 | 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] | 1546 | |
| 1547 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1548 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1549 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1550 | |
| 1551 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1552 | if (ordered) { |
| 1553 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, |
| 1554 | page_end, GFP_NOFS); |
| 1555 | unlock_page(page); |
| 1556 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1557 | goto again; |
| 1558 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1559 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1560 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1561 | ClearPageChecked(page); |
| 1562 | out: |
| 1563 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); |
| 1564 | out_page: |
| 1565 | unlock_page(page); |
| 1566 | page_cache_release(page); |
| 1567 | } |
| 1568 | |
| 1569 | /* |
| 1570 | * There are a few paths in the higher layers of the kernel that directly |
| 1571 | * set the page dirty bit without asking the filesystem if it is a |
| 1572 | * good idea. This causes problems because we want to make sure COW |
| 1573 | * properly happens and the data=ordered rules are followed. |
| 1574 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1575 | * 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] | 1576 | * hasn't been properly setup for IO. We kick off an async process |
| 1577 | * to fix it up. The async helper will wait for ordered extents, set |
| 1578 | * the delalloc bit and make it safe to write the page. |
| 1579 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1580 | 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] | 1581 | { |
| 1582 | struct inode *inode = page->mapping->host; |
| 1583 | struct btrfs_writepage_fixup *fixup; |
| 1584 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1585 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1586 | /* this page is properly in the ordered list */ |
| 1587 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1588 | return 0; |
| 1589 | |
| 1590 | if (PageChecked(page)) |
| 1591 | return -EAGAIN; |
| 1592 | |
| 1593 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1594 | if (!fixup) |
| 1595 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1596 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1597 | SetPageChecked(page); |
| 1598 | page_cache_get(page); |
| 1599 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1600 | fixup->page = page; |
| 1601 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1602 | return -EAGAIN; |
| 1603 | } |
| 1604 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1605 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1606 | struct inode *inode, u64 file_pos, |
| 1607 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1608 | u64 num_bytes, u64 ram_bytes, |
| 1609 | u8 compression, u8 encryption, |
| 1610 | u16 other_encoding, int extent_type) |
| 1611 | { |
| 1612 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1613 | struct btrfs_file_extent_item *fi; |
| 1614 | struct btrfs_path *path; |
| 1615 | struct extent_buffer *leaf; |
| 1616 | struct btrfs_key ins; |
| 1617 | u64 hint; |
| 1618 | int ret; |
| 1619 | |
| 1620 | path = btrfs_alloc_path(); |
| 1621 | BUG_ON(!path); |
| 1622 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1623 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1624 | |
| 1625 | /* |
| 1626 | * we may be replacing one extent in the tree with another. |
| 1627 | * The new extent is pinned in the extent map, and we don't want |
| 1628 | * to drop it from the cache until it is completely in the btree. |
| 1629 | * |
| 1630 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1631 | * the caller is expected to unpin it and allow it to be merged |
| 1632 | * with the others. |
| 1633 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1634 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1635 | &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1636 | BUG_ON(ret); |
| 1637 | |
| 1638 | ins.objectid = inode->i_ino; |
| 1639 | ins.offset = file_pos; |
| 1640 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1641 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1642 | BUG_ON(ret); |
| 1643 | leaf = path->nodes[0]; |
| 1644 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1645 | struct btrfs_file_extent_item); |
| 1646 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1647 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1648 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1649 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1650 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1651 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1652 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1653 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1654 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1655 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1656 | |
| 1657 | btrfs_unlock_up_safe(path, 1); |
| 1658 | btrfs_set_lock_blocking(leaf); |
| 1659 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1660 | btrfs_mark_buffer_dirty(leaf); |
| 1661 | |
| 1662 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1663 | |
| 1664 | ins.objectid = disk_bytenr; |
| 1665 | ins.offset = disk_num_bytes; |
| 1666 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1667 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1668 | root->root_key.objectid, |
| 1669 | inode->i_ino, file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1670 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1671 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1672 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1673 | return 0; |
| 1674 | } |
| 1675 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1676 | /* |
| 1677 | * helper function for btrfs_finish_ordered_io, this |
| 1678 | * just reads in some of the csum leaves to prime them into ram |
| 1679 | * before we start the transaction. It limits the amount of btree |
| 1680 | * reads required while inside the transaction. |
| 1681 | */ |
| 1682 | static noinline void reada_csum(struct btrfs_root *root, |
| 1683 | struct btrfs_path *path, |
| 1684 | struct btrfs_ordered_extent *ordered_extent) |
| 1685 | { |
| 1686 | struct btrfs_ordered_sum *sum; |
| 1687 | u64 bytenr; |
| 1688 | |
| 1689 | sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum, |
| 1690 | list); |
| 1691 | bytenr = sum->sums[0].bytenr; |
| 1692 | |
| 1693 | /* |
| 1694 | * we don't care about the results, the point of this search is |
| 1695 | * just to get the btree leaves into ram |
| 1696 | */ |
| 1697 | btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0); |
| 1698 | } |
| 1699 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1700 | /* as ordered data IO finishes, this gets called so we can finish |
| 1701 | * an ordered extent if the range of bytes in the file it covers are |
| 1702 | * fully written. |
| 1703 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1704 | 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] | 1705 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1706 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1707 | struct btrfs_trans_handle *trans; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1708 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1709 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1710 | struct btrfs_path *path; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1711 | int compressed = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1712 | int ret; |
| 1713 | |
| 1714 | ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1715 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1716 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1717 | |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1718 | /* |
| 1719 | * before we join the transaction, try to do some of our IO. |
| 1720 | * This will limit the amount of IO that we have to do with |
| 1721 | * the transaction running. We're unlikely to need to do any |
| 1722 | * IO if the file extents are new, the disk_i_size checks |
| 1723 | * covers the most common case. |
| 1724 | */ |
| 1725 | if (start < BTRFS_I(inode)->disk_i_size) { |
| 1726 | path = btrfs_alloc_path(); |
| 1727 | if (path) { |
| 1728 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 1729 | inode->i_ino, |
| 1730 | start, 0); |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1731 | ordered_extent = btrfs_lookup_ordered_extent(inode, |
| 1732 | start); |
| 1733 | if (!list_empty(&ordered_extent->list)) { |
| 1734 | btrfs_release_path(root, path); |
| 1735 | reada_csum(root, path, ordered_extent); |
| 1736 | } |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1737 | btrfs_free_path(path); |
| 1738 | } |
| 1739 | } |
| 1740 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1741 | if (!ordered_extent) |
| 1742 | ordered_extent = btrfs_lookup_ordered_extent(inode, start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1743 | BUG_ON(!ordered_extent); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1744 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
| 1745 | BUG_ON(!list_empty(&ordered_extent->list)); |
| 1746 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1747 | if (!ret) { |
| 1748 | trans = btrfs_join_transaction(root, 1); |
| 1749 | ret = btrfs_update_inode(trans, root, inode); |
| 1750 | BUG_ON(ret); |
| 1751 | btrfs_end_transaction(trans, root); |
| 1752 | } |
| 1753 | goto out; |
| 1754 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1755 | |
| 1756 | lock_extent(io_tree, ordered_extent->file_offset, |
| 1757 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1758 | GFP_NOFS); |
| 1759 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1760 | trans = btrfs_join_transaction(root, 1); |
| 1761 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1762 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1763 | compressed = 1; |
| 1764 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 1765 | BUG_ON(compressed); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1766 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1767 | ordered_extent->file_offset, |
| 1768 | ordered_extent->file_offset + |
| 1769 | ordered_extent->len); |
| 1770 | BUG_ON(ret); |
| 1771 | } else { |
| 1772 | ret = insert_reserved_file_extent(trans, inode, |
| 1773 | ordered_extent->file_offset, |
| 1774 | ordered_extent->start, |
| 1775 | ordered_extent->disk_len, |
| 1776 | ordered_extent->len, |
| 1777 | ordered_extent->len, |
| 1778 | compressed, 0, 0, |
| 1779 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1780 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1781 | ordered_extent->file_offset, |
| 1782 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1783 | BUG_ON(ret); |
| 1784 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1785 | unlock_extent(io_tree, ordered_extent->file_offset, |
| 1786 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1787 | GFP_NOFS); |
| 1788 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1789 | &ordered_extent->list); |
| 1790 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1791 | /* this also removes the ordered extent from the tree */ |
| 1792 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1793 | ret = btrfs_update_inode(trans, root, inode); |
| 1794 | BUG_ON(ret); |
| 1795 | btrfs_end_transaction(trans, root); |
| 1796 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1797 | /* once for us */ |
| 1798 | btrfs_put_ordered_extent(ordered_extent); |
| 1799 | /* once for the tree */ |
| 1800 | btrfs_put_ordered_extent(ordered_extent); |
| 1801 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1802 | return 0; |
| 1803 | } |
| 1804 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1805 | 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] | 1806 | struct extent_state *state, int uptodate) |
| 1807 | { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1808 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1809 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1810 | } |
| 1811 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1812 | /* |
| 1813 | * When IO fails, either with EIO or csum verification fails, we |
| 1814 | * try other mirrors that might have a good copy of the data. This |
| 1815 | * io_failure_record is used to record state as we go through all the |
| 1816 | * mirrors. If another mirror has good data, the page is set up to date |
| 1817 | * and things continue. If a good mirror can't be found, the original |
| 1818 | * bio end_io callback is called to indicate things have failed. |
| 1819 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1820 | struct io_failure_record { |
| 1821 | struct page *page; |
| 1822 | u64 start; |
| 1823 | u64 len; |
| 1824 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1825 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1826 | int last_mirror; |
| 1827 | }; |
| 1828 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1829 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1830 | struct page *page, u64 start, u64 end, |
| 1831 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1832 | { |
| 1833 | struct io_failure_record *failrec = NULL; |
| 1834 | u64 private; |
| 1835 | struct extent_map *em; |
| 1836 | struct inode *inode = page->mapping->host; |
| 1837 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1838 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1839 | struct bio *bio; |
| 1840 | int num_copies; |
| 1841 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1842 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1843 | u64 logical; |
| 1844 | |
| 1845 | ret = get_state_private(failure_tree, start, &private); |
| 1846 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1847 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1848 | if (!failrec) |
| 1849 | return -ENOMEM; |
| 1850 | failrec->start = start; |
| 1851 | failrec->len = end - start + 1; |
| 1852 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1853 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1854 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1855 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1856 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1857 | if (em->start > start || em->start + em->len < start) { |
| 1858 | free_extent_map(em); |
| 1859 | em = NULL; |
| 1860 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1861 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1862 | |
| 1863 | if (!em || IS_ERR(em)) { |
| 1864 | kfree(failrec); |
| 1865 | return -EIO; |
| 1866 | } |
| 1867 | logical = start - em->start; |
| 1868 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1869 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1870 | logical = em->block_start; |
| 1871 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 1872 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1873 | failrec->logical = logical; |
| 1874 | free_extent_map(em); |
| 1875 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1876 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1877 | set_state_private(failure_tree, start, |
| 1878 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1879 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1880 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1881 | } |
| 1882 | num_copies = btrfs_num_copies( |
| 1883 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1884 | failrec->logical, failrec->len); |
| 1885 | failrec->last_mirror++; |
| 1886 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1887 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1888 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1889 | failrec->start, |
| 1890 | EXTENT_LOCKED); |
| 1891 | if (state && state->start != failrec->start) |
| 1892 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1893 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1894 | } |
| 1895 | if (!state || failrec->last_mirror > num_copies) { |
| 1896 | set_state_private(failure_tree, failrec->start, 0); |
| 1897 | clear_extent_bits(failure_tree, failrec->start, |
| 1898 | failrec->start + failrec->len - 1, |
| 1899 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1900 | kfree(failrec); |
| 1901 | return -EIO; |
| 1902 | } |
| 1903 | bio = bio_alloc(GFP_NOFS, 1); |
| 1904 | bio->bi_private = state; |
| 1905 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1906 | bio->bi_sector = failrec->logical >> 9; |
| 1907 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1908 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1909 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1910 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1911 | if (failed_bio->bi_rw & (1 << BIO_RW)) |
| 1912 | rw = WRITE; |
| 1913 | else |
| 1914 | rw = READ; |
| 1915 | |
| 1916 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1917 | failrec->last_mirror, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1918 | failrec->bio_flags); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1919 | return 0; |
| 1920 | } |
| 1921 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1922 | /* |
| 1923 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1924 | * to see if we need to process or clean up an io_failure_record |
| 1925 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1926 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1927 | { |
| 1928 | u64 private; |
| 1929 | u64 private_failure; |
| 1930 | struct io_failure_record *failure; |
| 1931 | int ret; |
| 1932 | |
| 1933 | private = 0; |
| 1934 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 1935 | (u64)-1, 1, EXTENT_DIRTY)) { |
| 1936 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1937 | start, &private_failure); |
| 1938 | if (ret == 0) { |
| 1939 | failure = (struct io_failure_record *)(unsigned long) |
| 1940 | private_failure; |
| 1941 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1942 | failure->start, 0); |
| 1943 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1944 | failure->start, |
| 1945 | failure->start + failure->len - 1, |
| 1946 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1947 | GFP_NOFS); |
| 1948 | kfree(failure); |
| 1949 | } |
| 1950 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1951 | return 0; |
| 1952 | } |
| 1953 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1954 | /* |
| 1955 | * when reads are done, we need to check csums to verify the data is correct |
| 1956 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1957 | * the io_failure_record routines to find good copies |
| 1958 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1959 | 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] | 1960 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1961 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1962 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1963 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1964 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1965 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1966 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1967 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1968 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1969 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1970 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1971 | if (PageChecked(page)) { |
| 1972 | ClearPageChecked(page); |
| 1973 | goto good; |
| 1974 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1975 | |
| 1976 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1977 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1978 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1979 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1980 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1981 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1982 | GFP_NOFS); |
| 1983 | return 0; |
| 1984 | } |
| 1985 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1986 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1987 | private = state->private; |
| 1988 | ret = 0; |
| 1989 | } else { |
| 1990 | ret = get_state_private(io_tree, start, &private); |
| 1991 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1992 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1993 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1994 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1995 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1996 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1997 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1998 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1999 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2000 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2001 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2002 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2003 | /* if the io failure tree for this inode is non-empty, |
| 2004 | * check to see if we've recovered from a failed IO |
| 2005 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2006 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2007 | return 0; |
| 2008 | |
| 2009 | zeroit: |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 2010 | if (printk_ratelimit()) { |
| 2011 | printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " |
| 2012 | "private %llu\n", page->mapping->host->i_ino, |
| 2013 | (unsigned long long)start, csum, |
| 2014 | (unsigned long long)private); |
| 2015 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2016 | memset(kaddr + offset, 1, end - start + 1); |
| 2017 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2018 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2019 | if (private == 0) |
| 2020 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2021 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2022 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2023 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2024 | /* |
| 2025 | * This creates an orphan entry for the given inode in case something goes |
| 2026 | * wrong in the middle of an unlink/truncate. |
| 2027 | */ |
| 2028 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2029 | { |
| 2030 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2031 | int ret = 0; |
| 2032 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2033 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2034 | |
| 2035 | /* already on the orphan list, we're good */ |
| 2036 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2037 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2038 | return 0; |
| 2039 | } |
| 2040 | |
| 2041 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2042 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2043 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2044 | |
| 2045 | /* |
| 2046 | * insert an orphan item to track this unlinked/truncated file |
| 2047 | */ |
| 2048 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 2049 | |
| 2050 | return ret; |
| 2051 | } |
| 2052 | |
| 2053 | /* |
| 2054 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2055 | * item for this particular inode. |
| 2056 | */ |
| 2057 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2058 | { |
| 2059 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2060 | int ret = 0; |
| 2061 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2062 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2063 | |
| 2064 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2065 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2066 | return 0; |
| 2067 | } |
| 2068 | |
| 2069 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2070 | if (!trans) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2071 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2072 | return 0; |
| 2073 | } |
| 2074 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2075 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2076 | |
| 2077 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 2078 | |
| 2079 | return ret; |
| 2080 | } |
| 2081 | |
| 2082 | /* |
| 2083 | * this cleans up any orphans that may be left on the list from the last use |
| 2084 | * of this root. |
| 2085 | */ |
| 2086 | void btrfs_orphan_cleanup(struct btrfs_root *root) |
| 2087 | { |
| 2088 | struct btrfs_path *path; |
| 2089 | struct extent_buffer *leaf; |
| 2090 | struct btrfs_item *item; |
| 2091 | struct btrfs_key key, found_key; |
| 2092 | struct btrfs_trans_handle *trans; |
| 2093 | struct inode *inode; |
| 2094 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2095 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2096 | if (!xchg(&root->clean_orphans, 0)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2097 | return; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2098 | |
| 2099 | path = btrfs_alloc_path(); |
| 2100 | BUG_ON(!path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2101 | path->reada = -1; |
| 2102 | |
| 2103 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2104 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2105 | key.offset = (u64)-1; |
| 2106 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2107 | while (1) { |
| 2108 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2109 | if (ret < 0) { |
| 2110 | printk(KERN_ERR "Error searching slot for orphan: %d" |
| 2111 | "\n", ret); |
| 2112 | break; |
| 2113 | } |
| 2114 | |
| 2115 | /* |
| 2116 | * if ret == 0 means we found what we were searching for, which |
| 2117 | * is weird, but possible, so only screw with path if we didnt |
| 2118 | * find the key and see if we have stuff that matches |
| 2119 | */ |
| 2120 | if (ret > 0) { |
| 2121 | if (path->slots[0] == 0) |
| 2122 | break; |
| 2123 | path->slots[0]--; |
| 2124 | } |
| 2125 | |
| 2126 | /* pull out the item */ |
| 2127 | leaf = path->nodes[0]; |
| 2128 | item = btrfs_item_nr(leaf, path->slots[0]); |
| 2129 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2130 | |
| 2131 | /* make sure the item matches what we want */ |
| 2132 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2133 | break; |
| 2134 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2135 | break; |
| 2136 | |
| 2137 | /* release the path since we're done with it */ |
| 2138 | btrfs_release_path(root, path); |
| 2139 | |
| 2140 | /* |
| 2141 | * this is where we are basically btrfs_lookup, without the |
| 2142 | * crossing root thing. we store the inode number in the |
| 2143 | * offset of the orphan item. |
| 2144 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2145 | found_key.objectid = found_key.offset; |
| 2146 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2147 | found_key.offset = 0; |
| 2148 | inode = btrfs_iget(root->fs_info->sb, &found_key, root); |
| 2149 | if (IS_ERR(inode)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2150 | break; |
| 2151 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2152 | /* |
| 2153 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2154 | * the proper thing when we hit it |
| 2155 | */ |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2156 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2157 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2158 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2159 | |
| 2160 | /* |
| 2161 | * if this is a bad inode, means we actually succeeded in |
| 2162 | * removing the inode, but not the orphan record, which means |
| 2163 | * we need to manually delete the orphan since iput will just |
| 2164 | * do a destroy_inode |
| 2165 | */ |
| 2166 | if (is_bad_inode(inode)) { |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2167 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2168 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2169 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2170 | iput(inode); |
| 2171 | continue; |
| 2172 | } |
| 2173 | |
| 2174 | /* if we have links, this was a truncate, lets do that */ |
| 2175 | if (inode->i_nlink) { |
| 2176 | nr_truncate++; |
| 2177 | btrfs_truncate(inode); |
| 2178 | } else { |
| 2179 | nr_unlink++; |
| 2180 | } |
| 2181 | |
| 2182 | /* this will do delete_inode and everything for us */ |
| 2183 | iput(inode); |
| 2184 | } |
| 2185 | |
| 2186 | if (nr_unlink) |
| 2187 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2188 | if (nr_truncate) |
| 2189 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
| 2190 | |
| 2191 | btrfs_free_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2192 | } |
| 2193 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2194 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2195 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2196 | * don't find any xattrs, we know there can't be any acls. |
| 2197 | * |
| 2198 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2199 | */ |
| 2200 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2201 | int slot, u64 objectid) |
| 2202 | { |
| 2203 | u32 nritems = btrfs_header_nritems(leaf); |
| 2204 | struct btrfs_key found_key; |
| 2205 | int scanned = 0; |
| 2206 | |
| 2207 | slot++; |
| 2208 | while (slot < nritems) { |
| 2209 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2210 | |
| 2211 | /* we found a different objectid, there must not be acls */ |
| 2212 | if (found_key.objectid != objectid) |
| 2213 | return 0; |
| 2214 | |
| 2215 | /* we found an xattr, assume we've got an acl */ |
| 2216 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2217 | return 1; |
| 2218 | |
| 2219 | /* |
| 2220 | * we found a key greater than an xattr key, there can't |
| 2221 | * be any acls later on |
| 2222 | */ |
| 2223 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2224 | return 0; |
| 2225 | |
| 2226 | slot++; |
| 2227 | scanned++; |
| 2228 | |
| 2229 | /* |
| 2230 | * it goes inode, inode backrefs, xattrs, extents, |
| 2231 | * so if there are a ton of hard links to an inode there can |
| 2232 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2233 | * this is just an optimization |
| 2234 | */ |
| 2235 | if (scanned >= 8) |
| 2236 | break; |
| 2237 | } |
| 2238 | /* we hit the end of the leaf before we found an xattr or |
| 2239 | * something larger than an xattr. We have to assume the inode |
| 2240 | * has acls |
| 2241 | */ |
| 2242 | return 1; |
| 2243 | } |
| 2244 | |
| 2245 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2246 | * read an inode from the btree into the in-memory inode |
| 2247 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2248 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2249 | { |
| 2250 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2251 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2252 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2253 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2254 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2255 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2256 | int maybe_acls; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2257 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2258 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2259 | int ret; |
| 2260 | |
| 2261 | path = btrfs_alloc_path(); |
| 2262 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2263 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2264 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2265 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2266 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2267 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2268 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2269 | leaf = path->nodes[0]; |
| 2270 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2271 | struct btrfs_inode_item); |
| 2272 | |
| 2273 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2274 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2275 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2276 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2277 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2278 | |
| 2279 | tspec = btrfs_inode_atime(inode_item); |
| 2280 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2281 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2282 | |
| 2283 | tspec = btrfs_inode_mtime(inode_item); |
| 2284 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2285 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2286 | |
| 2287 | tspec = btrfs_inode_ctime(inode_item); |
| 2288 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2289 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2290 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2291 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2292 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2293 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2294 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2295 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2296 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2297 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2298 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2299 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2300 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2301 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2302 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2303 | /* |
| 2304 | * try to precache a NULL acl entry for files that don't have |
| 2305 | * any xattrs or acls |
| 2306 | */ |
| 2307 | 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] | 2308 | if (!maybe_acls) |
| 2309 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2310 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2311 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2312 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2313 | btrfs_free_path(path); |
| 2314 | inode_item = NULL; |
| 2315 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2316 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2317 | case S_IFREG: |
| 2318 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2319 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2320 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2321 | inode->i_fop = &btrfs_file_operations; |
| 2322 | inode->i_op = &btrfs_file_inode_operations; |
| 2323 | break; |
| 2324 | case S_IFDIR: |
| 2325 | inode->i_fop = &btrfs_dir_file_operations; |
| 2326 | if (root == root->fs_info->tree_root) |
| 2327 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2328 | else |
| 2329 | inode->i_op = &btrfs_dir_inode_operations; |
| 2330 | break; |
| 2331 | case S_IFLNK: |
| 2332 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2333 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2334 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2335 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2336 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2337 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2338 | init_special_inode(inode, inode->i_mode, rdev); |
| 2339 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2340 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2341 | |
| 2342 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2343 | return; |
| 2344 | |
| 2345 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2346 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2347 | make_bad_inode(inode); |
| 2348 | } |
| 2349 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2350 | /* |
| 2351 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2352 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2353 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2354 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2355 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2356 | struct inode *inode) |
| 2357 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2358 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2359 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2360 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2361 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2362 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2363 | |
| 2364 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2365 | inode->i_atime.tv_sec); |
| 2366 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2367 | inode->i_atime.tv_nsec); |
| 2368 | |
| 2369 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2370 | inode->i_mtime.tv_sec); |
| 2371 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2372 | inode->i_mtime.tv_nsec); |
| 2373 | |
| 2374 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2375 | inode->i_ctime.tv_sec); |
| 2376 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2377 | inode->i_ctime.tv_nsec); |
| 2378 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2379 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2380 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2381 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2382 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2383 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2384 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2385 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2386 | } |
| 2387 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2388 | /* |
| 2389 | * copy everything in the in-memory inode into the btree. |
| 2390 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2391 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2392 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2393 | { |
| 2394 | struct btrfs_inode_item *inode_item; |
| 2395 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2396 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2397 | int ret; |
| 2398 | |
| 2399 | path = btrfs_alloc_path(); |
| 2400 | BUG_ON(!path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2401 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2402 | ret = btrfs_lookup_inode(trans, root, path, |
| 2403 | &BTRFS_I(inode)->location, 1); |
| 2404 | if (ret) { |
| 2405 | if (ret > 0) |
| 2406 | ret = -ENOENT; |
| 2407 | goto failed; |
| 2408 | } |
| 2409 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2410 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2411 | leaf = path->nodes[0]; |
| 2412 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2413 | struct btrfs_inode_item); |
| 2414 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2415 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2416 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2417 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2418 | ret = 0; |
| 2419 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2420 | btrfs_free_path(path); |
| 2421 | return ret; |
| 2422 | } |
| 2423 | |
| 2424 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2425 | /* |
| 2426 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2427 | * recovery code. It remove a link in a directory with a given name, and |
| 2428 | * also drops the back refs in the inode to the directory |
| 2429 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2430 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2431 | struct btrfs_root *root, |
| 2432 | struct inode *dir, struct inode *inode, |
| 2433 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2434 | { |
| 2435 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2436 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2437 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2438 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2439 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2440 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2441 | |
| 2442 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2443 | if (!path) { |
| 2444 | ret = -ENOMEM; |
| 2445 | goto err; |
| 2446 | } |
| 2447 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2448 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2449 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2450 | name, name_len, -1); |
| 2451 | if (IS_ERR(di)) { |
| 2452 | ret = PTR_ERR(di); |
| 2453 | goto err; |
| 2454 | } |
| 2455 | if (!di) { |
| 2456 | ret = -ENOENT; |
| 2457 | goto err; |
| 2458 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2459 | leaf = path->nodes[0]; |
| 2460 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2461 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2462 | if (ret) |
| 2463 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2464 | btrfs_release_path(root, path); |
| 2465 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2466 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2467 | inode->i_ino, |
| 2468 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2469 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2470 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2471 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2472 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2473 | goto err; |
| 2474 | } |
| 2475 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2476 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2477 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2478 | if (IS_ERR(di)) { |
| 2479 | ret = PTR_ERR(di); |
| 2480 | goto err; |
| 2481 | } |
| 2482 | if (!di) { |
| 2483 | ret = -ENOENT; |
| 2484 | goto err; |
| 2485 | } |
| 2486 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2487 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2488 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2489 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 2490 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2491 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2492 | |
| 2493 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2494 | dir, index); |
| 2495 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2496 | err: |
| 2497 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2498 | if (ret) |
| 2499 | goto out; |
| 2500 | |
| 2501 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2502 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2503 | btrfs_update_inode(trans, root, dir); |
| 2504 | btrfs_drop_nlink(inode); |
| 2505 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2506 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2507 | return ret; |
| 2508 | } |
| 2509 | |
| 2510 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2511 | { |
| 2512 | struct btrfs_root *root; |
| 2513 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2514 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2515 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2516 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2517 | |
| 2518 | root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2519 | |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2520 | /* |
| 2521 | * 5 items for unlink inode |
| 2522 | * 1 for orphan |
| 2523 | */ |
| 2524 | ret = btrfs_reserve_metadata_space(root, 6); |
| 2525 | if (ret) |
| 2526 | return ret; |
| 2527 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2528 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2529 | if (IS_ERR(trans)) { |
| 2530 | btrfs_unreserve_metadata_space(root, 6); |
| 2531 | return PTR_ERR(trans); |
| 2532 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2533 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2534 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2535 | |
| 2536 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2537 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2538 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2539 | dentry->d_name.name, dentry->d_name.len); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2540 | |
| 2541 | if (inode->i_nlink == 0) |
| 2542 | ret = btrfs_orphan_add(trans, inode); |
| 2543 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2544 | nr = trans->blocks_used; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2545 | |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2546 | btrfs_end_transaction_throttle(trans, root); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2547 | btrfs_unreserve_metadata_space(root, 6); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2548 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2549 | return ret; |
| 2550 | } |
| 2551 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2552 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2553 | struct btrfs_root *root, |
| 2554 | struct inode *dir, u64 objectid, |
| 2555 | const char *name, int name_len) |
| 2556 | { |
| 2557 | struct btrfs_path *path; |
| 2558 | struct extent_buffer *leaf; |
| 2559 | struct btrfs_dir_item *di; |
| 2560 | struct btrfs_key key; |
| 2561 | u64 index; |
| 2562 | int ret; |
| 2563 | |
| 2564 | path = btrfs_alloc_path(); |
| 2565 | if (!path) |
| 2566 | return -ENOMEM; |
| 2567 | |
| 2568 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2569 | name, name_len, -1); |
| 2570 | BUG_ON(!di || IS_ERR(di)); |
| 2571 | |
| 2572 | leaf = path->nodes[0]; |
| 2573 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2574 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2575 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2576 | BUG_ON(ret); |
| 2577 | btrfs_release_path(root, path); |
| 2578 | |
| 2579 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2580 | objectid, root->root_key.objectid, |
| 2581 | dir->i_ino, &index, name, name_len); |
| 2582 | if (ret < 0) { |
| 2583 | BUG_ON(ret != -ENOENT); |
| 2584 | di = btrfs_search_dir_index_item(root, path, dir->i_ino, |
| 2585 | name, name_len); |
| 2586 | BUG_ON(!di || IS_ERR(di)); |
| 2587 | |
| 2588 | leaf = path->nodes[0]; |
| 2589 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2590 | btrfs_release_path(root, path); |
| 2591 | index = key.offset; |
| 2592 | } |
| 2593 | |
| 2594 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 2595 | index, name, name_len, -1); |
| 2596 | BUG_ON(!di || IS_ERR(di)); |
| 2597 | |
| 2598 | leaf = path->nodes[0]; |
| 2599 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2600 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2601 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2602 | BUG_ON(ret); |
| 2603 | btrfs_release_path(root, path); |
| 2604 | |
| 2605 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2606 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2607 | ret = btrfs_update_inode(trans, root, dir); |
| 2608 | BUG_ON(ret); |
| 2609 | dir->i_sb->s_dirt = 1; |
| 2610 | |
| 2611 | btrfs_free_path(path); |
| 2612 | return 0; |
| 2613 | } |
| 2614 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2615 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 2616 | { |
| 2617 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2618 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2619 | int ret; |
| 2620 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2621 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2622 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2623 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 2624 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2625 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 2626 | return -ENOTEMPTY; |
| 2627 | |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2628 | ret = btrfs_reserve_metadata_space(root, 5); |
| 2629 | if (ret) |
| 2630 | return ret; |
| 2631 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2632 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2633 | if (IS_ERR(trans)) { |
| 2634 | btrfs_unreserve_metadata_space(root, 5); |
| 2635 | return PTR_ERR(trans); |
| 2636 | } |
| 2637 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2638 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2639 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2640 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 2641 | err = btrfs_unlink_subvol(trans, root, dir, |
| 2642 | BTRFS_I(inode)->location.objectid, |
| 2643 | dentry->d_name.name, |
| 2644 | dentry->d_name.len); |
| 2645 | goto out; |
| 2646 | } |
| 2647 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2648 | err = btrfs_orphan_add(trans, inode); |
| 2649 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2650 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2651 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2652 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2653 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2654 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2655 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2656 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2657 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2658 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2659 | ret = btrfs_end_transaction_throttle(trans, root); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2660 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2661 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2662 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2663 | if (ret && !err) |
| 2664 | err = ret; |
| 2665 | return err; |
| 2666 | } |
| 2667 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2668 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2669 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2670 | * when truncating bytes in a file, it is possible to avoid reading |
| 2671 | * the leaves that contain only checksum items. This can be the |
| 2672 | * majority of the IO required to delete a large file, but it must |
| 2673 | * be done carefully. |
| 2674 | * |
| 2675 | * The keys in the level just above the leaves are checked to make sure |
| 2676 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 2677 | * after the new size. |
| 2678 | * |
| 2679 | * Then the key for the next leaf is checked to make sure it also has |
| 2680 | * a checksum item for the same file. If it does, we know our target leaf |
| 2681 | * contains only checksum items, and it can be safely freed without reading |
| 2682 | * it. |
| 2683 | * |
| 2684 | * This is just an optimization targeted at large files. It may do |
| 2685 | * nothing. It will return 0 unless things went badly. |
| 2686 | */ |
| 2687 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 2688 | struct btrfs_root *root, |
| 2689 | struct btrfs_path *path, |
| 2690 | struct inode *inode, u64 new_size) |
| 2691 | { |
| 2692 | struct btrfs_key key; |
| 2693 | int ret; |
| 2694 | int nritems; |
| 2695 | struct btrfs_key found_key; |
| 2696 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2697 | struct btrfs_leaf_ref *ref; |
| 2698 | u64 leaf_gen; |
| 2699 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2700 | |
| 2701 | path->lowest_level = 1; |
| 2702 | key.objectid = inode->i_ino; |
| 2703 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 2704 | key.offset = new_size; |
| 2705 | again: |
| 2706 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2707 | if (ret < 0) |
| 2708 | goto out; |
| 2709 | |
| 2710 | if (path->nodes[1] == NULL) { |
| 2711 | ret = 0; |
| 2712 | goto out; |
| 2713 | } |
| 2714 | ret = 0; |
| 2715 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 2716 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 2717 | |
| 2718 | if (!nritems) |
| 2719 | goto out; |
| 2720 | |
| 2721 | if (path->slots[1] >= nritems) |
| 2722 | goto next_node; |
| 2723 | |
| 2724 | /* did we find a key greater than anything we want to delete? */ |
| 2725 | if (found_key.objectid > inode->i_ino || |
| 2726 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 2727 | goto out; |
| 2728 | |
| 2729 | /* we check the next key in the node to make sure the leave contains |
| 2730 | * only checksum items. This comparison doesn't work if our |
| 2731 | * leaf is the last one in the node |
| 2732 | */ |
| 2733 | if (path->slots[1] + 1 >= nritems) { |
| 2734 | next_node: |
| 2735 | /* search forward from the last key in the node, this |
| 2736 | * will bring us into the next node in the tree |
| 2737 | */ |
| 2738 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 2739 | |
| 2740 | /* unlikely, but we inc below, so check to be safe */ |
| 2741 | if (found_key.offset == (u64)-1) |
| 2742 | goto out; |
| 2743 | |
| 2744 | /* search_forward needs a path with locks held, do the |
| 2745 | * search again for the original key. It is possible |
| 2746 | * this will race with a balance and return a path that |
| 2747 | * we could modify, but this drop is just an optimization |
| 2748 | * and is allowed to miss some leaves. |
| 2749 | */ |
| 2750 | btrfs_release_path(root, path); |
| 2751 | found_key.offset++; |
| 2752 | |
| 2753 | /* setup a max key for search_forward */ |
| 2754 | other_key.offset = (u64)-1; |
| 2755 | other_key.type = key.type; |
| 2756 | other_key.objectid = key.objectid; |
| 2757 | |
| 2758 | path->keep_locks = 1; |
| 2759 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 2760 | path, 0, 0); |
| 2761 | path->keep_locks = 0; |
| 2762 | if (ret || found_key.objectid != key.objectid || |
| 2763 | found_key.type != key.type) { |
| 2764 | ret = 0; |
| 2765 | goto out; |
| 2766 | } |
| 2767 | |
| 2768 | key.offset = found_key.offset; |
| 2769 | btrfs_release_path(root, path); |
| 2770 | cond_resched(); |
| 2771 | goto again; |
| 2772 | } |
| 2773 | |
| 2774 | /* we know there's one more slot after us in the tree, |
| 2775 | * read that key so we can verify it is also a checksum item |
| 2776 | */ |
| 2777 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 2778 | |
| 2779 | if (found_key.objectid < inode->i_ino) |
| 2780 | goto next_key; |
| 2781 | |
| 2782 | if (found_key.type != key.type || found_key.offset < new_size) |
| 2783 | goto next_key; |
| 2784 | |
| 2785 | /* |
| 2786 | * if the key for the next leaf isn't a csum key from this objectid, |
| 2787 | * we can't be sure there aren't good items inside this leaf. |
| 2788 | * Bail out |
| 2789 | */ |
| 2790 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 2791 | goto out; |
| 2792 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2793 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 2794 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2795 | /* |
| 2796 | * it is safe to delete this leaf, it contains only |
| 2797 | * csum items from this inode at an offset >= new_size |
| 2798 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2799 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2800 | BUG_ON(ret); |
| 2801 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2802 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 2803 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 2804 | if (ref) { |
| 2805 | ref->root_gen = root->root_key.offset; |
| 2806 | ref->bytenr = leaf_start; |
| 2807 | ref->owner = 0; |
| 2808 | ref->generation = leaf_gen; |
| 2809 | ref->nritems = 0; |
| 2810 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2811 | btrfs_sort_leaf_ref(ref); |
| 2812 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2813 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 2814 | WARN_ON(ret); |
| 2815 | btrfs_free_leaf_ref(root, ref); |
| 2816 | } else { |
| 2817 | WARN_ON(1); |
| 2818 | } |
| 2819 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2820 | next_key: |
| 2821 | btrfs_release_path(root, path); |
| 2822 | |
| 2823 | if (other_key.objectid == inode->i_ino && |
| 2824 | other_key.type == key.type && other_key.offset > key.offset) { |
| 2825 | key.offset = other_key.offset; |
| 2826 | cond_resched(); |
| 2827 | goto again; |
| 2828 | } |
| 2829 | ret = 0; |
| 2830 | out: |
| 2831 | /* fixup any changes we've made to the path */ |
| 2832 | path->lowest_level = 0; |
| 2833 | path->keep_locks = 0; |
| 2834 | btrfs_release_path(root, path); |
| 2835 | return ret; |
| 2836 | } |
| 2837 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2838 | #endif |
| 2839 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2840 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2841 | * this can truncate away extent items, csum items and directory items. |
| 2842 | * 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] | 2843 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2844 | * |
| 2845 | * csum items that cross the new i_size are truncated to the new size |
| 2846 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2847 | * |
| 2848 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 2849 | * 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] | 2850 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 2851 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 2852 | struct btrfs_root *root, |
| 2853 | struct inode *inode, |
| 2854 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2855 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2856 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2857 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2858 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 2859 | struct btrfs_key key; |
| 2860 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2861 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2862 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2863 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2864 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 2865 | u64 mask = root->sectorsize - 1; |
| 2866 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2867 | int found_extent; |
| 2868 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2869 | int pending_del_nr = 0; |
| 2870 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2871 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2872 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 2873 | int ret; |
| 2874 | int err = 0; |
| 2875 | |
| 2876 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2877 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2878 | if (root->ref_cows) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2879 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 2880 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2881 | path = btrfs_alloc_path(); |
| 2882 | BUG_ON(!path); |
Julia Lawall | 33c17ad | 2009-07-22 16:49:01 -0400 | [diff] [blame] | 2883 | path->reada = -1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2884 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2885 | key.objectid = inode->i_ino; |
| 2886 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2887 | key.type = (u8)-1; |
| 2888 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2889 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2890 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2891 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 2892 | if (ret < 0) { |
| 2893 | err = ret; |
| 2894 | goto out; |
| 2895 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2896 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2897 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2898 | /* there are no items in the tree for us to truncate, we're |
| 2899 | * done |
| 2900 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 2901 | if (path->slots[0] == 0) |
| 2902 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2903 | path->slots[0]--; |
| 2904 | } |
| 2905 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2906 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2907 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2908 | leaf = path->nodes[0]; |
| 2909 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2910 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2911 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2912 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2913 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2914 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2915 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2916 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2917 | break; |
| 2918 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2919 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2920 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2921 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2922 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2923 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2924 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 2925 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 2926 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 2927 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2928 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2929 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2930 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2931 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2932 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2933 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2934 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 2935 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2936 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 2937 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2938 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 2939 | } else { |
| 2940 | if (item_end < new_size) |
| 2941 | break; |
| 2942 | if (found_key.offset >= new_size) |
| 2943 | del_item = 1; |
| 2944 | else |
| 2945 | del_item = 0; |
| 2946 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2947 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2948 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2949 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 2950 | goto delete; |
| 2951 | |
| 2952 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2953 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2954 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2955 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2956 | u64 orig_num_bytes = |
| 2957 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2958 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2959 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 2960 | extent_num_bytes = extent_num_bytes & |
| 2961 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2962 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2963 | extent_num_bytes); |
| 2964 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2965 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2966 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2967 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2968 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2969 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2970 | extent_num_bytes = |
| 2971 | btrfs_file_extent_disk_num_bytes(leaf, |
| 2972 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2973 | extent_offset = found_key.offset - |
| 2974 | btrfs_file_extent_offset(leaf, fi); |
| 2975 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2976 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2977 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2978 | if (extent_start != 0) { |
| 2979 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2980 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2981 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2982 | } |
| 2983 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2984 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2985 | /* |
| 2986 | * we can't truncate inline items that have had |
| 2987 | * special encodings |
| 2988 | */ |
| 2989 | if (!del_item && |
| 2990 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 2991 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 2992 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2993 | u32 size = new_size - found_key.offset; |
| 2994 | |
| 2995 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2996 | inode_sub_bytes(inode, item_end + 1 - |
| 2997 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2998 | } |
| 2999 | size = |
| 3000 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3001 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3002 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3003 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3004 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3005 | inode_sub_bytes(inode, item_end + 1 - |
| 3006 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3007 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3008 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3009 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3010 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3011 | if (!pending_del_nr) { |
| 3012 | /* no pending yet, add ourselves */ |
| 3013 | pending_del_slot = path->slots[0]; |
| 3014 | pending_del_nr = 1; |
| 3015 | } else if (pending_del_nr && |
| 3016 | path->slots[0] + 1 == pending_del_slot) { |
| 3017 | /* hop on the pending chunk */ |
| 3018 | pending_del_nr++; |
| 3019 | pending_del_slot = path->slots[0]; |
| 3020 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3021 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3022 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3023 | } else { |
| 3024 | break; |
| 3025 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3026 | if (found_extent && root->ref_cows) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3027 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3028 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3029 | extent_num_bytes, 0, |
| 3030 | btrfs_header_owner(leaf), |
| 3031 | inode->i_ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3032 | BUG_ON(ret); |
| 3033 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3034 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3035 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3036 | break; |
| 3037 | |
| 3038 | if (path->slots[0] == 0 || |
| 3039 | path->slots[0] != pending_del_slot) { |
| 3040 | if (root->ref_cows) { |
| 3041 | err = -EAGAIN; |
| 3042 | goto out; |
| 3043 | } |
| 3044 | if (pending_del_nr) { |
| 3045 | ret = btrfs_del_items(trans, root, path, |
| 3046 | pending_del_slot, |
| 3047 | pending_del_nr); |
| 3048 | BUG_ON(ret); |
| 3049 | pending_del_nr = 0; |
| 3050 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3051 | btrfs_release_path(root, path); |
| 3052 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3053 | } else { |
| 3054 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3055 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3056 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3057 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3058 | if (pending_del_nr) { |
| 3059 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3060 | pending_del_nr); |
| 3061 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3062 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3063 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3064 | } |
| 3065 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3066 | /* |
| 3067 | * taken from block_truncate_page, but does cow as it zeros out |
| 3068 | * any bytes left in the last page in the file. |
| 3069 | */ |
| 3070 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3071 | { |
| 3072 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3073 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3074 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3075 | struct btrfs_ordered_extent *ordered; |
| 3076 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3077 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3078 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3079 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3080 | struct page *page; |
| 3081 | int ret = 0; |
| 3082 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3083 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3084 | |
| 3085 | if ((offset & (blocksize - 1)) == 0) |
| 3086 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3087 | ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE); |
| 3088 | if (ret) |
| 3089 | goto out; |
| 3090 | |
| 3091 | ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1); |
| 3092 | if (ret) |
| 3093 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3094 | |
| 3095 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3096 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3097 | page = grab_cache_page(mapping, index); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3098 | if (!page) { |
| 3099 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
| 3100 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3101 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3102 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3103 | |
| 3104 | page_start = page_offset(page); |
| 3105 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3106 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3107 | if (!PageUptodate(page)) { |
| 3108 | ret = btrfs_readpage(NULL, page); |
| 3109 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3110 | if (page->mapping != mapping) { |
| 3111 | unlock_page(page); |
| 3112 | page_cache_release(page); |
| 3113 | goto again; |
| 3114 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3115 | if (!PageUptodate(page)) { |
| 3116 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3117 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3118 | } |
| 3119 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3120 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3121 | |
| 3122 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3123 | set_page_extent_mapped(page); |
| 3124 | |
| 3125 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3126 | if (ordered) { |
| 3127 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3128 | unlock_page(page); |
| 3129 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3130 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3131 | btrfs_put_ordered_extent(ordered); |
| 3132 | goto again; |
| 3133 | } |
| 3134 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3135 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 3136 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
| 3137 | GFP_NOFS); |
| 3138 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3139 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end); |
| 3140 | if (ret) { |
| 3141 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3142 | goto out_unlock; |
| 3143 | } |
| 3144 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3145 | ret = 0; |
| 3146 | if (offset != PAGE_CACHE_SIZE) { |
| 3147 | kaddr = kmap(page); |
| 3148 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3149 | flush_dcache_page(page); |
| 3150 | kunmap(page); |
| 3151 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3152 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3153 | set_page_dirty(page); |
| 3154 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3155 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3156 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3157 | if (ret) |
| 3158 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
| 3159 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3160 | unlock_page(page); |
| 3161 | page_cache_release(page); |
| 3162 | out: |
| 3163 | return ret; |
| 3164 | } |
| 3165 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3166 | int btrfs_cont_expand(struct inode *inode, loff_t size) |
| 3167 | { |
| 3168 | struct btrfs_trans_handle *trans; |
| 3169 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3170 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3171 | struct extent_map *em; |
| 3172 | u64 mask = root->sectorsize - 1; |
| 3173 | u64 hole_start = (inode->i_size + mask) & ~mask; |
| 3174 | u64 block_end = (size + mask) & ~mask; |
| 3175 | u64 last_byte; |
| 3176 | u64 cur_offset; |
| 3177 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3178 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3179 | |
| 3180 | if (size <= hole_start) |
| 3181 | return 0; |
| 3182 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3183 | while (1) { |
| 3184 | struct btrfs_ordered_extent *ordered; |
| 3185 | btrfs_wait_ordered_range(inode, hole_start, |
| 3186 | block_end - hole_start); |
| 3187 | lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3188 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3189 | if (!ordered) |
| 3190 | break; |
| 3191 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3192 | btrfs_put_ordered_extent(ordered); |
| 3193 | } |
| 3194 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3195 | cur_offset = hole_start; |
| 3196 | while (1) { |
| 3197 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3198 | block_end - cur_offset, 0); |
| 3199 | BUG_ON(IS_ERR(em) || !em); |
| 3200 | last_byte = min(extent_map_end(em), block_end); |
| 3201 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3202 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3203 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3204 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3205 | |
| 3206 | err = btrfs_reserve_metadata_space(root, 2); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3207 | if (err) |
| 3208 | break; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3209 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3210 | trans = btrfs_start_transaction(root, 1); |
| 3211 | btrfs_set_trans_block_group(trans, inode); |
| 3212 | |
| 3213 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3214 | cur_offset + hole_size, |
| 3215 | &hint_byte, 1); |
| 3216 | BUG_ON(err); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3217 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3218 | err = btrfs_insert_file_extent(trans, root, |
| 3219 | inode->i_ino, cur_offset, 0, |
| 3220 | 0, hole_size, 0, hole_size, |
| 3221 | 0, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3222 | BUG_ON(err); |
| 3223 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3224 | btrfs_drop_extent_cache(inode, hole_start, |
| 3225 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3226 | |
| 3227 | btrfs_end_transaction(trans, root); |
| 3228 | btrfs_unreserve_metadata_space(root, 2); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3229 | } |
| 3230 | free_extent_map(em); |
| 3231 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3232 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3233 | break; |
| 3234 | } |
| 3235 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3236 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3237 | return err; |
| 3238 | } |
| 3239 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3240 | static int btrfs_setattr_size(struct inode *inode, struct iattr *attr) |
| 3241 | { |
| 3242 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3243 | struct btrfs_trans_handle *trans; |
| 3244 | unsigned long nr; |
| 3245 | int ret; |
| 3246 | |
| 3247 | if (attr->ia_size == inode->i_size) |
| 3248 | return 0; |
| 3249 | |
| 3250 | if (attr->ia_size > inode->i_size) { |
| 3251 | unsigned long limit; |
| 3252 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
| 3253 | if (attr->ia_size > inode->i_sb->s_maxbytes) |
| 3254 | return -EFBIG; |
| 3255 | if (limit != RLIM_INFINITY && attr->ia_size > limit) { |
| 3256 | send_sig(SIGXFSZ, current, 0); |
| 3257 | return -EFBIG; |
| 3258 | } |
| 3259 | } |
| 3260 | |
| 3261 | ret = btrfs_reserve_metadata_space(root, 1); |
| 3262 | if (ret) |
| 3263 | return ret; |
| 3264 | |
| 3265 | trans = btrfs_start_transaction(root, 1); |
| 3266 | btrfs_set_trans_block_group(trans, inode); |
| 3267 | |
| 3268 | ret = btrfs_orphan_add(trans, inode); |
| 3269 | BUG_ON(ret); |
| 3270 | |
| 3271 | nr = trans->blocks_used; |
| 3272 | btrfs_end_transaction(trans, root); |
| 3273 | btrfs_unreserve_metadata_space(root, 1); |
| 3274 | btrfs_btree_balance_dirty(root, nr); |
| 3275 | |
| 3276 | if (attr->ia_size > inode->i_size) { |
| 3277 | ret = btrfs_cont_expand(inode, attr->ia_size); |
| 3278 | if (ret) { |
| 3279 | btrfs_truncate(inode); |
| 3280 | return ret; |
| 3281 | } |
| 3282 | |
| 3283 | i_size_write(inode, attr->ia_size); |
| 3284 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
| 3285 | |
| 3286 | trans = btrfs_start_transaction(root, 1); |
| 3287 | btrfs_set_trans_block_group(trans, inode); |
| 3288 | |
| 3289 | ret = btrfs_update_inode(trans, root, inode); |
| 3290 | BUG_ON(ret); |
| 3291 | if (inode->i_nlink > 0) { |
| 3292 | ret = btrfs_orphan_del(trans, inode); |
| 3293 | BUG_ON(ret); |
| 3294 | } |
| 3295 | nr = trans->blocks_used; |
| 3296 | btrfs_end_transaction(trans, root); |
| 3297 | btrfs_btree_balance_dirty(root, nr); |
| 3298 | return 0; |
| 3299 | } |
| 3300 | |
| 3301 | /* |
| 3302 | * We're truncating a file that used to have good data down to |
| 3303 | * zero. Make sure it gets into the ordered flush list so that |
| 3304 | * any new writes get down to disk quickly. |
| 3305 | */ |
| 3306 | if (attr->ia_size == 0) |
| 3307 | BTRFS_I(inode)->ordered_data_close = 1; |
| 3308 | |
| 3309 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3310 | ret = vmtruncate(inode, attr->ia_size); |
| 3311 | BUG_ON(ret); |
| 3312 | |
| 3313 | return 0; |
| 3314 | } |
| 3315 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3316 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3317 | { |
| 3318 | struct inode *inode = dentry->d_inode; |
| 3319 | int err; |
| 3320 | |
| 3321 | err = inode_change_ok(inode, attr); |
| 3322 | if (err) |
| 3323 | return err; |
| 3324 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3325 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3326 | err = btrfs_setattr_size(inode, attr); |
| 3327 | if (err) |
| 3328 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3329 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3330 | attr->ia_valid &= ~ATTR_SIZE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3331 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3332 | if (attr->ia_valid) |
| 3333 | err = inode_setattr(inode, attr); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3334 | |
| 3335 | if (!err && ((attr->ia_valid & ATTR_MODE))) |
| 3336 | err = btrfs_acl_chmod(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3337 | return err; |
| 3338 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3339 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3340 | void btrfs_delete_inode(struct inode *inode) |
| 3341 | { |
| 3342 | struct btrfs_trans_handle *trans; |
| 3343 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3344 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3345 | int ret; |
| 3346 | |
| 3347 | truncate_inode_pages(&inode->i_data, 0); |
| 3348 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3349 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3350 | goto no_delete; |
| 3351 | } |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3352 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3353 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3354 | if (root->fs_info->log_root_recovering) { |
| 3355 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3356 | goto no_delete; |
| 3357 | } |
| 3358 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3359 | if (inode->i_nlink > 0) { |
| 3360 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3361 | goto no_delete; |
| 3362 | } |
| 3363 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3364 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3365 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3366 | while (1) { |
| 3367 | trans = btrfs_start_transaction(root, 1); |
| 3368 | btrfs_set_trans_block_group(trans, inode); |
| 3369 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
| 3370 | |
| 3371 | if (ret != -EAGAIN) |
| 3372 | break; |
| 3373 | |
| 3374 | nr = trans->blocks_used; |
| 3375 | btrfs_end_transaction(trans, root); |
| 3376 | trans = NULL; |
| 3377 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3378 | } |
| 3379 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3380 | if (ret == 0) { |
| 3381 | ret = btrfs_orphan_del(trans, inode); |
| 3382 | BUG_ON(ret); |
| 3383 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3384 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3385 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3386 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3387 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3388 | no_delete: |
| 3389 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 3390 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | /* |
| 3394 | * this returns the key found in the dir entry in the location pointer. |
| 3395 | * If no dir entries were found, location->objectid is 0. |
| 3396 | */ |
| 3397 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3398 | struct btrfs_key *location) |
| 3399 | { |
| 3400 | const char *name = dentry->d_name.name; |
| 3401 | int namelen = dentry->d_name.len; |
| 3402 | struct btrfs_dir_item *di; |
| 3403 | struct btrfs_path *path; |
| 3404 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3405 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3406 | |
| 3407 | path = btrfs_alloc_path(); |
| 3408 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3409 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3410 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 3411 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3412 | if (IS_ERR(di)) |
| 3413 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3414 | |
| 3415 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3416 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3417 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3418 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3419 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3420 | btrfs_free_path(path); |
| 3421 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3422 | out_err: |
| 3423 | location->objectid = 0; |
| 3424 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | /* |
| 3428 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3429 | * needs to be changed to reflect the root directory of the tree root. This |
| 3430 | * is kind of like crossing a mount point. |
| 3431 | */ |
| 3432 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3433 | struct inode *dir, |
| 3434 | struct dentry *dentry, |
| 3435 | struct btrfs_key *location, |
| 3436 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3437 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3438 | struct btrfs_path *path; |
| 3439 | struct btrfs_root *new_root; |
| 3440 | struct btrfs_root_ref *ref; |
| 3441 | struct extent_buffer *leaf; |
| 3442 | int ret; |
| 3443 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3444 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3445 | path = btrfs_alloc_path(); |
| 3446 | if (!path) { |
| 3447 | err = -ENOMEM; |
| 3448 | goto out; |
| 3449 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3450 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3451 | err = -ENOENT; |
| 3452 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3453 | BTRFS_I(dir)->root->root_key.objectid, |
| 3454 | location->objectid); |
| 3455 | if (ret) { |
| 3456 | if (ret < 0) |
| 3457 | err = ret; |
| 3458 | goto out; |
| 3459 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3460 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3461 | leaf = path->nodes[0]; |
| 3462 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 3463 | if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || |
| 3464 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3465 | goto out; |
| 3466 | |
| 3467 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3468 | (unsigned long)(ref + 1), |
| 3469 | dentry->d_name.len); |
| 3470 | if (ret) |
| 3471 | goto out; |
| 3472 | |
| 3473 | btrfs_release_path(root->fs_info->tree_root, path); |
| 3474 | |
| 3475 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3476 | if (IS_ERR(new_root)) { |
| 3477 | err = PTR_ERR(new_root); |
| 3478 | goto out; |
| 3479 | } |
| 3480 | |
| 3481 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3482 | err = -ENOENT; |
| 3483 | goto out; |
| 3484 | } |
| 3485 | |
| 3486 | *sub_root = new_root; |
| 3487 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3488 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3489 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3490 | err = 0; |
| 3491 | out: |
| 3492 | btrfs_free_path(path); |
| 3493 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3494 | } |
| 3495 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3496 | static void inode_tree_add(struct inode *inode) |
| 3497 | { |
| 3498 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3499 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3500 | struct rb_node **p; |
| 3501 | struct rb_node *parent; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3502 | again: |
| 3503 | p = &root->inode_tree.rb_node; |
| 3504 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3505 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3506 | if (hlist_unhashed(&inode->i_hash)) |
| 3507 | return; |
| 3508 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3509 | spin_lock(&root->inode_lock); |
| 3510 | while (*p) { |
| 3511 | parent = *p; |
| 3512 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3513 | |
| 3514 | if (inode->i_ino < entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3515 | p = &parent->rb_left; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3516 | else if (inode->i_ino > entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3517 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3518 | else { |
| 3519 | WARN_ON(!(entry->vfs_inode.i_state & |
| 3520 | (I_WILL_FREE | I_FREEING | I_CLEAR))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3521 | rb_erase(parent, &root->inode_tree); |
| 3522 | RB_CLEAR_NODE(parent); |
| 3523 | spin_unlock(&root->inode_lock); |
| 3524 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3525 | } |
| 3526 | } |
| 3527 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3528 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3529 | spin_unlock(&root->inode_lock); |
| 3530 | } |
| 3531 | |
| 3532 | static void inode_tree_del(struct inode *inode) |
| 3533 | { |
| 3534 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3535 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3536 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3537 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3538 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3539 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3540 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3541 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3542 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3543 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3544 | |
| 3545 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
| 3546 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3547 | spin_lock(&root->inode_lock); |
| 3548 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3549 | spin_unlock(&root->inode_lock); |
| 3550 | if (empty) |
| 3551 | btrfs_add_dead_root(root); |
| 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 3556 | { |
| 3557 | struct rb_node *node; |
| 3558 | struct rb_node *prev; |
| 3559 | struct btrfs_inode *entry; |
| 3560 | struct inode *inode; |
| 3561 | u64 objectid = 0; |
| 3562 | |
| 3563 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3564 | |
| 3565 | spin_lock(&root->inode_lock); |
| 3566 | again: |
| 3567 | node = root->inode_tree.rb_node; |
| 3568 | prev = NULL; |
| 3569 | while (node) { |
| 3570 | prev = node; |
| 3571 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3572 | |
| 3573 | if (objectid < entry->vfs_inode.i_ino) |
| 3574 | node = node->rb_left; |
| 3575 | else if (objectid > entry->vfs_inode.i_ino) |
| 3576 | node = node->rb_right; |
| 3577 | else |
| 3578 | break; |
| 3579 | } |
| 3580 | if (!node) { |
| 3581 | while (prev) { |
| 3582 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
| 3583 | if (objectid <= entry->vfs_inode.i_ino) { |
| 3584 | node = prev; |
| 3585 | break; |
| 3586 | } |
| 3587 | prev = rb_next(prev); |
| 3588 | } |
| 3589 | } |
| 3590 | while (node) { |
| 3591 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3592 | objectid = entry->vfs_inode.i_ino + 1; |
| 3593 | inode = igrab(&entry->vfs_inode); |
| 3594 | if (inode) { |
| 3595 | spin_unlock(&root->inode_lock); |
| 3596 | if (atomic_read(&inode->i_count) > 1) |
| 3597 | d_prune_aliases(inode); |
| 3598 | /* |
| 3599 | * btrfs_drop_inode will remove it from |
| 3600 | * the inode cache when its usage count |
| 3601 | * hits zero. |
| 3602 | */ |
| 3603 | iput(inode); |
| 3604 | cond_resched(); |
| 3605 | spin_lock(&root->inode_lock); |
| 3606 | goto again; |
| 3607 | } |
| 3608 | |
| 3609 | if (cond_resched_lock(&root->inode_lock)) |
| 3610 | goto again; |
| 3611 | |
| 3612 | node = rb_next(node); |
| 3613 | } |
| 3614 | spin_unlock(&root->inode_lock); |
| 3615 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3616 | } |
| 3617 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3618 | static noinline void init_btrfs_i(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3619 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3620 | struct btrfs_inode *bi = BTRFS_I(inode); |
| 3621 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3622 | bi->generation = 0; |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 3623 | bi->sequence = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3624 | bi->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 3625 | bi->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3626 | bi->logged_trans = 0; |
| 3627 | bi->delalloc_bytes = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3628 | bi->reserved_bytes = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3629 | bi->disk_i_size = 0; |
| 3630 | bi->flags = 0; |
| 3631 | bi->index_cnt = (u64)-1; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3632 | bi->last_unlink_trans = 0; |
Chris Mason | 2757495 | 2009-05-14 13:10:02 -0400 | [diff] [blame] | 3633 | bi->ordered_data_close = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3634 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 3635 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3636 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3637 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
| 3638 | inode->i_mapping, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3639 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3640 | INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3641 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 3642 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3643 | mutex_init(&BTRFS_I(inode)->log_mutex); |
| 3644 | } |
| 3645 | |
| 3646 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 3647 | { |
| 3648 | struct btrfs_iget_args *args = p; |
| 3649 | inode->i_ino = args->ino; |
| 3650 | init_btrfs_i(inode); |
| 3651 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3652 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3653 | return 0; |
| 3654 | } |
| 3655 | |
| 3656 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 3657 | { |
| 3658 | struct btrfs_iget_args *args = opaque; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3659 | return args->ino == inode->i_ino && |
| 3660 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3661 | } |
| 3662 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3663 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 3664 | u64 objectid, |
| 3665 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3666 | { |
| 3667 | struct inode *inode; |
| 3668 | struct btrfs_iget_args args; |
| 3669 | args.ino = objectid; |
| 3670 | args.root = root; |
| 3671 | |
| 3672 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 3673 | btrfs_init_locked_inode, |
| 3674 | (void *)&args); |
| 3675 | return inode; |
| 3676 | } |
| 3677 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3678 | /* Get an inode object given its location and corresponding root. |
| 3679 | * Returns in *is_new if the inode was read from disk |
| 3680 | */ |
| 3681 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3682 | struct btrfs_root *root) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3683 | { |
| 3684 | struct inode *inode; |
| 3685 | |
| 3686 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 3687 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3688 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3689 | |
| 3690 | if (inode->i_state & I_NEW) { |
| 3691 | BTRFS_I(inode)->root = root; |
| 3692 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 3693 | btrfs_read_locked_inode(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3694 | |
| 3695 | inode_tree_add(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3696 | unlock_new_inode(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | return inode; |
| 3700 | } |
| 3701 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3702 | static struct inode *new_simple_dir(struct super_block *s, |
| 3703 | struct btrfs_key *key, |
| 3704 | struct btrfs_root *root) |
| 3705 | { |
| 3706 | struct inode *inode = new_inode(s); |
| 3707 | |
| 3708 | if (!inode) |
| 3709 | return ERR_PTR(-ENOMEM); |
| 3710 | |
| 3711 | init_btrfs_i(inode); |
| 3712 | |
| 3713 | BTRFS_I(inode)->root = root; |
| 3714 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 3715 | BTRFS_I(inode)->dummy_inode = 1; |
| 3716 | |
| 3717 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 3718 | inode->i_op = &simple_dir_inode_operations; |
| 3719 | inode->i_fop = &simple_dir_operations; |
| 3720 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 3721 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 3722 | |
| 3723 | return inode; |
| 3724 | } |
| 3725 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3726 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3727 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3728 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3729 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3730 | struct btrfs_root *sub_root = root; |
| 3731 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3732 | int index; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3733 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3734 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3735 | dentry->d_op = &btrfs_dentry_operations; |
| 3736 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3737 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 3738 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3739 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3740 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3741 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3742 | if (ret < 0) |
| 3743 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3744 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3745 | if (location.objectid == 0) |
| 3746 | return NULL; |
| 3747 | |
| 3748 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
| 3749 | inode = btrfs_iget(dir->i_sb, &location, root); |
| 3750 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3751 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3752 | |
| 3753 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 3754 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3755 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3756 | ret = fixup_tree_root_location(root, dir, dentry, |
| 3757 | &location, &sub_root); |
| 3758 | if (ret < 0) { |
| 3759 | if (ret != -ENOENT) |
| 3760 | inode = ERR_PTR(ret); |
| 3761 | else |
| 3762 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 3763 | } else { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3764 | inode = btrfs_iget(dir->i_sb, &location, sub_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3765 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3766 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 3767 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3768 | if (root != sub_root) { |
| 3769 | down_read(&root->fs_info->cleanup_work_sem); |
| 3770 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
| 3771 | btrfs_orphan_cleanup(sub_root); |
| 3772 | up_read(&root->fs_info->cleanup_work_sem); |
| 3773 | } |
| 3774 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3775 | return inode; |
| 3776 | } |
| 3777 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3778 | static int btrfs_dentry_delete(struct dentry *dentry) |
| 3779 | { |
| 3780 | struct btrfs_root *root; |
| 3781 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 3782 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 3783 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3784 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 3785 | if (dentry->d_inode) { |
| 3786 | root = BTRFS_I(dentry->d_inode)->root; |
| 3787 | if (btrfs_root_refs(&root->root_item) == 0) |
| 3788 | return 1; |
| 3789 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3790 | return 0; |
| 3791 | } |
| 3792 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3793 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 3794 | struct nameidata *nd) |
| 3795 | { |
| 3796 | struct inode *inode; |
| 3797 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3798 | inode = btrfs_lookup_dentry(dir, dentry); |
| 3799 | if (IS_ERR(inode)) |
| 3800 | return ERR_CAST(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3801 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3802 | return d_splice_alias(inode, dentry); |
| 3803 | } |
| 3804 | |
| 3805 | static unsigned char btrfs_filetype_table[] = { |
| 3806 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 3807 | }; |
| 3808 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 3809 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 3810 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3811 | { |
| 3812 | struct inode *inode = filp->f_dentry->d_inode; |
| 3813 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3814 | struct btrfs_item *item; |
| 3815 | struct btrfs_dir_item *di; |
| 3816 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3817 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3818 | struct btrfs_path *path; |
| 3819 | int ret; |
| 3820 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3821 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3822 | int slot; |
| 3823 | int advance; |
| 3824 | unsigned char d_type; |
| 3825 | int over = 0; |
| 3826 | u32 di_cur; |
| 3827 | u32 di_total; |
| 3828 | u32 di_len; |
| 3829 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3830 | char tmp_name[32]; |
| 3831 | char *name_ptr; |
| 3832 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3833 | |
| 3834 | /* FIXME, use a real flag for deciding about the key type */ |
| 3835 | if (root->fs_info->tree_root == root) |
| 3836 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3837 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3838 | /* special case for "." */ |
| 3839 | if (filp->f_pos == 0) { |
| 3840 | over = filldir(dirent, ".", 1, |
| 3841 | 1, inode->i_ino, |
| 3842 | DT_DIR); |
| 3843 | if (over) |
| 3844 | return 0; |
| 3845 | filp->f_pos = 1; |
| 3846 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3847 | /* special case for .., just use the back ref */ |
| 3848 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3849 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3850 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3851 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3852 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3853 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3854 | filp->f_pos = 2; |
| 3855 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3856 | path = btrfs_alloc_path(); |
| 3857 | path->reada = 2; |
| 3858 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3859 | btrfs_set_key_type(&key, key_type); |
| 3860 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3861 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3862 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3863 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3864 | if (ret < 0) |
| 3865 | goto err; |
| 3866 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3867 | |
| 3868 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3869 | leaf = path->nodes[0]; |
| 3870 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3871 | slot = path->slots[0]; |
| 3872 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3873 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3874 | ret = btrfs_next_leaf(root, path); |
| 3875 | if (ret) |
| 3876 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3877 | leaf = path->nodes[0]; |
| 3878 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3879 | slot = path->slots[0]; |
| 3880 | } else { |
| 3881 | slot++; |
| 3882 | path->slots[0]++; |
| 3883 | } |
| 3884 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3885 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3886 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3887 | item = btrfs_item_nr(leaf, slot); |
| 3888 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3889 | |
| 3890 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3891 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3892 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3893 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3894 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3895 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3896 | |
| 3897 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3898 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3899 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 3900 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3901 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3902 | |
| 3903 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3904 | struct btrfs_key location; |
| 3905 | |
| 3906 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3907 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3908 | name_ptr = tmp_name; |
| 3909 | } else { |
| 3910 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3911 | if (!name_ptr) { |
| 3912 | ret = -ENOMEM; |
| 3913 | goto err; |
| 3914 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3915 | } |
| 3916 | read_extent_buffer(leaf, name_ptr, |
| 3917 | (unsigned long)(di + 1), name_len); |
| 3918 | |
| 3919 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 3920 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3921 | |
| 3922 | /* is this a reference to our own snapshot? If so |
| 3923 | * skip it |
| 3924 | */ |
| 3925 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 3926 | location.objectid == root->root_key.objectid) { |
| 3927 | over = 0; |
| 3928 | goto skip; |
| 3929 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3930 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3931 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3932 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3933 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3934 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3935 | if (name_ptr != tmp_name) |
| 3936 | kfree(name_ptr); |
| 3937 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3938 | if (over) |
| 3939 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 3940 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3941 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3942 | di_cur += di_len; |
| 3943 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 3944 | } |
| 3945 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3946 | |
| 3947 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 3948 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Chris Mason | 89f135d | 2009-01-28 15:34:27 -0500 | [diff] [blame] | 3949 | filp->f_pos = INT_LIMIT(off_t); |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 3950 | else |
| 3951 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3952 | nopos: |
| 3953 | ret = 0; |
| 3954 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3955 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3956 | return ret; |
| 3957 | } |
| 3958 | |
| 3959 | int btrfs_write_inode(struct inode *inode, int wait) |
| 3960 | { |
| 3961 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3962 | struct btrfs_trans_handle *trans; |
| 3963 | int ret = 0; |
| 3964 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3965 | if (root->fs_info->btree_inode == inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 3966 | return 0; |
| 3967 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3968 | if (wait) { |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 3969 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3970 | btrfs_set_trans_block_group(trans, inode); |
| 3971 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3972 | } |
| 3973 | return ret; |
| 3974 | } |
| 3975 | |
| 3976 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3977 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3978 | * inode changes. But, it is most likely to find the inode in cache. |
| 3979 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 3980 | * to keep or drop this code. |
| 3981 | */ |
| 3982 | void btrfs_dirty_inode(struct inode *inode) |
| 3983 | { |
| 3984 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3985 | struct btrfs_trans_handle *trans; |
| 3986 | |
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 | btrfs_update_inode(trans, root, inode); |
| 3990 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3991 | } |
| 3992 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3993 | /* |
| 3994 | * find the highest existing sequence number in a directory |
| 3995 | * and then set the in-memory index_cnt variable to reflect |
| 3996 | * free sequence numbers |
| 3997 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3998 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 3999 | { |
| 4000 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4001 | struct btrfs_key key, found_key; |
| 4002 | struct btrfs_path *path; |
| 4003 | struct extent_buffer *leaf; |
| 4004 | int ret; |
| 4005 | |
| 4006 | key.objectid = inode->i_ino; |
| 4007 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4008 | key.offset = (u64)-1; |
| 4009 | |
| 4010 | path = btrfs_alloc_path(); |
| 4011 | if (!path) |
| 4012 | return -ENOMEM; |
| 4013 | |
| 4014 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4015 | if (ret < 0) |
| 4016 | goto out; |
| 4017 | /* FIXME: we should be able to handle this */ |
| 4018 | if (ret == 0) |
| 4019 | goto out; |
| 4020 | ret = 0; |
| 4021 | |
| 4022 | /* |
| 4023 | * MAGIC NUMBER EXPLANATION: |
| 4024 | * since we search a directory based on f_pos we have to start at 2 |
| 4025 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4026 | * else has to start at 2 |
| 4027 | */ |
| 4028 | if (path->slots[0] == 0) { |
| 4029 | BTRFS_I(inode)->index_cnt = 2; |
| 4030 | goto out; |
| 4031 | } |
| 4032 | |
| 4033 | path->slots[0]--; |
| 4034 | |
| 4035 | leaf = path->nodes[0]; |
| 4036 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4037 | |
| 4038 | if (found_key.objectid != inode->i_ino || |
| 4039 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4040 | BTRFS_I(inode)->index_cnt = 2; |
| 4041 | goto out; |
| 4042 | } |
| 4043 | |
| 4044 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4045 | out: |
| 4046 | btrfs_free_path(path); |
| 4047 | return ret; |
| 4048 | } |
| 4049 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4050 | /* |
| 4051 | * helper to find a free sequence number in a given directory. This current |
| 4052 | * code is very simple, later versions will do smarter things in the btree |
| 4053 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4054 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4055 | { |
| 4056 | int ret = 0; |
| 4057 | |
| 4058 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 4059 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4060 | if (ret) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4061 | return ret; |
| 4062 | } |
| 4063 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4064 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4065 | BTRFS_I(dir)->index_cnt++; |
| 4066 | |
| 4067 | return ret; |
| 4068 | } |
| 4069 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4070 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4071 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4072 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4073 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4074 | u64 ref_objectid, u64 objectid, |
| 4075 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4076 | { |
| 4077 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4078 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4079 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4080 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4081 | struct btrfs_inode_ref *ref; |
| 4082 | struct btrfs_key key[2]; |
| 4083 | u32 sizes[2]; |
| 4084 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4085 | int ret; |
| 4086 | int owner; |
| 4087 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4088 | path = btrfs_alloc_path(); |
| 4089 | BUG_ON(!path); |
| 4090 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4091 | inode = new_inode(root->fs_info->sb); |
| 4092 | if (!inode) |
| 4093 | return ERR_PTR(-ENOMEM); |
| 4094 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4095 | if (dir) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4096 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4097 | if (ret) { |
| 4098 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4099 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4100 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4101 | } |
| 4102 | /* |
| 4103 | * index_cnt is ignored for everything but a dir, |
| 4104 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4105 | * number |
| 4106 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4107 | init_btrfs_i(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4108 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4109 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4110 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4111 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4112 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4113 | if (mode & S_IFDIR) |
| 4114 | owner = 0; |
| 4115 | else |
| 4116 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4117 | BTRFS_I(inode)->block_group = |
| 4118 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4119 | |
| 4120 | key[0].objectid = objectid; |
| 4121 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4122 | key[0].offset = 0; |
| 4123 | |
| 4124 | key[1].objectid = objectid; |
| 4125 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4126 | key[1].offset = ref_objectid; |
| 4127 | |
| 4128 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4129 | sizes[1] = name_len + sizeof(*ref); |
| 4130 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4131 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4132 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4133 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4134 | goto fail; |
| 4135 | |
Chris Mason | 79683f2 | 2008-11-19 22:00:53 -0500 | [diff] [blame] | 4136 | inode->i_uid = current_fsuid(); |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 4137 | |
Chris Mason | 42f15d7 | 2009-02-06 11:35:57 -0500 | [diff] [blame] | 4138 | if (dir && (dir->i_mode & S_ISGID)) { |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 4139 | inode->i_gid = dir->i_gid; |
| 4140 | if (S_ISDIR(mode)) |
| 4141 | mode |= S_ISGID; |
| 4142 | } else |
| 4143 | inode->i_gid = current_fsgid(); |
| 4144 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4145 | inode->i_mode = mode; |
| 4146 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4147 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4148 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4149 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4150 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4151 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4152 | |
| 4153 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4154 | struct btrfs_inode_ref); |
| 4155 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4156 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4157 | ptr = (unsigned long)(ref + 1); |
| 4158 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4159 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4160 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4161 | btrfs_free_path(path); |
| 4162 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4163 | location = &BTRFS_I(inode)->location; |
| 4164 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4165 | location->offset = 0; |
| 4166 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4167 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4168 | btrfs_inherit_iflags(inode, dir); |
| 4169 | |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4170 | if ((mode & S_IFREG)) { |
| 4171 | if (btrfs_test_opt(root, NODATASUM)) |
| 4172 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 4173 | if (btrfs_test_opt(root, NODATACOW)) |
| 4174 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4175 | } |
| 4176 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4177 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4178 | inode_tree_add(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4179 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4180 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4181 | if (dir) |
| 4182 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4183 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4184 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4185 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4189 | { |
| 4190 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4191 | } |
| 4192 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4193 | /* |
| 4194 | * utility function to add 'inode' into 'parent_inode' with |
| 4195 | * a give name and a given sequence number. |
| 4196 | * if 'add_backref' is true, also insert a backref from the |
| 4197 | * inode to the parent directory. |
| 4198 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4199 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4200 | struct inode *parent_inode, struct inode *inode, |
| 4201 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4202 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4203 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4204 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4205 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4206 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4207 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4208 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4209 | } else { |
| 4210 | key.objectid = inode->i_ino; |
| 4211 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4212 | key.offset = 0; |
| 4213 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4214 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4215 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4216 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4217 | key.objectid, root->root_key.objectid, |
| 4218 | parent_inode->i_ino, |
| 4219 | index, name, name_len); |
| 4220 | } else if (add_backref) { |
| 4221 | ret = btrfs_insert_inode_ref(trans, root, |
| 4222 | name, name_len, inode->i_ino, |
| 4223 | parent_inode->i_ino, index); |
| 4224 | } |
| 4225 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4226 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4227 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4228 | parent_inode->i_ino, &key, |
| 4229 | btrfs_inode_type(inode), index); |
| 4230 | BUG_ON(ret); |
| 4231 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4232 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4233 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4234 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4235 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4236 | } |
| 4237 | return ret; |
| 4238 | } |
| 4239 | |
| 4240 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4241 | struct dentry *dentry, struct inode *inode, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4242 | int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4243 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4244 | int err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4245 | inode, dentry->d_name.name, |
| 4246 | dentry->d_name.len, backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4247 | if (!err) { |
| 4248 | d_instantiate(dentry, inode); |
| 4249 | return 0; |
| 4250 | } |
| 4251 | if (err > 0) |
| 4252 | err = -EEXIST; |
| 4253 | return err; |
| 4254 | } |
| 4255 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4256 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4257 | int mode, dev_t rdev) |
| 4258 | { |
| 4259 | struct btrfs_trans_handle *trans; |
| 4260 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4261 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4262 | int err; |
| 4263 | int drop_inode = 0; |
| 4264 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4265 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4266 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4267 | |
| 4268 | if (!new_valid_dev(rdev)) |
| 4269 | return -EINVAL; |
| 4270 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4271 | /* |
| 4272 | * 2 for inode item and ref |
| 4273 | * 2 for dir items |
| 4274 | * 1 for xattr if selinux is on |
| 4275 | */ |
| 4276 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4277 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4278 | return err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4279 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4280 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4281 | if (!trans) |
| 4282 | goto fail; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4283 | btrfs_set_trans_block_group(trans, dir); |
| 4284 | |
| 4285 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4286 | if (err) { |
| 4287 | err = -ENOSPC; |
| 4288 | goto out_unlock; |
| 4289 | } |
| 4290 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4291 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4292 | dentry->d_name.len, |
| 4293 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4294 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4295 | err = PTR_ERR(inode); |
| 4296 | if (IS_ERR(inode)) |
| 4297 | goto out_unlock; |
| 4298 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 4299 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4300 | if (err) { |
| 4301 | drop_inode = 1; |
| 4302 | goto out_unlock; |
| 4303 | } |
| 4304 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4305 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4306 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4307 | if (err) |
| 4308 | drop_inode = 1; |
| 4309 | else { |
| 4310 | inode->i_op = &btrfs_special_inode_operations; |
| 4311 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4312 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4313 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4314 | btrfs_update_inode_block_group(trans, inode); |
| 4315 | btrfs_update_inode_block_group(trans, dir); |
| 4316 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4317 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4318 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4319 | fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4320 | btrfs_unreserve_metadata_space(root, 5); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4321 | if (drop_inode) { |
| 4322 | inode_dec_link_count(inode); |
| 4323 | iput(inode); |
| 4324 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4325 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4326 | return err; |
| 4327 | } |
| 4328 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4329 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4330 | int mode, struct nameidata *nd) |
| 4331 | { |
| 4332 | struct btrfs_trans_handle *trans; |
| 4333 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4334 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4335 | int err; |
| 4336 | int drop_inode = 0; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4337 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4338 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4339 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4340 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4341 | /* |
| 4342 | * 2 for inode item and ref |
| 4343 | * 2 for dir items |
| 4344 | * 1 for xattr if selinux is on |
| 4345 | */ |
| 4346 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4347 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4348 | return err; |
| 4349 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4350 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4351 | if (!trans) |
| 4352 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4353 | btrfs_set_trans_block_group(trans, dir); |
| 4354 | |
| 4355 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4356 | if (err) { |
| 4357 | err = -ENOSPC; |
| 4358 | goto out_unlock; |
| 4359 | } |
| 4360 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4361 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4362 | dentry->d_name.len, |
| 4363 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4364 | objectid, BTRFS_I(dir)->block_group, mode, |
| 4365 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4366 | err = PTR_ERR(inode); |
| 4367 | if (IS_ERR(inode)) |
| 4368 | goto out_unlock; |
| 4369 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 4370 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4371 | if (err) { |
| 4372 | drop_inode = 1; |
| 4373 | goto out_unlock; |
| 4374 | } |
| 4375 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4376 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4377 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4378 | if (err) |
| 4379 | drop_inode = 1; |
| 4380 | else { |
| 4381 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4382 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4383 | inode->i_fop = &btrfs_file_operations; |
| 4384 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4385 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4386 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4387 | btrfs_update_inode_block_group(trans, inode); |
| 4388 | btrfs_update_inode_block_group(trans, dir); |
| 4389 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4390 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4391 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4392 | fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4393 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4394 | if (drop_inode) { |
| 4395 | inode_dec_link_count(inode); |
| 4396 | iput(inode); |
| 4397 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4398 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4399 | return err; |
| 4400 | } |
| 4401 | |
| 4402 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4403 | struct dentry *dentry) |
| 4404 | { |
| 4405 | struct btrfs_trans_handle *trans; |
| 4406 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4407 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4408 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4409 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4410 | int err; |
| 4411 | int drop_inode = 0; |
| 4412 | |
| 4413 | if (inode->i_nlink == 0) |
| 4414 | return -ENOENT; |
| 4415 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4416 | /* |
| 4417 | * 1 item for inode ref |
| 4418 | * 2 items for dir items |
| 4419 | */ |
| 4420 | err = btrfs_reserve_metadata_space(root, 3); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4421 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4422 | return err; |
| 4423 | |
| 4424 | btrfs_inc_nlink(inode); |
| 4425 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4426 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4427 | if (err) |
| 4428 | goto fail; |
| 4429 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4430 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4431 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4432 | btrfs_set_trans_block_group(trans, dir); |
| 4433 | atomic_inc(&inode->i_count); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4434 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4435 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4436 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4437 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4438 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4439 | } else { |
| 4440 | btrfs_update_inode_block_group(trans, dir); |
| 4441 | err = btrfs_update_inode(trans, root, inode); |
| 4442 | BUG_ON(err); |
| 4443 | btrfs_log_new_name(trans, inode, NULL, dentry->d_parent); |
| 4444 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4445 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4446 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4447 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4448 | fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4449 | btrfs_unreserve_metadata_space(root, 3); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4450 | if (drop_inode) { |
| 4451 | inode_dec_link_count(inode); |
| 4452 | iput(inode); |
| 4453 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4454 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4455 | return err; |
| 4456 | } |
| 4457 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4458 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4459 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4460 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4461 | struct btrfs_trans_handle *trans; |
| 4462 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4463 | int err = 0; |
| 4464 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4465 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4466 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4467 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4468 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4469 | /* |
| 4470 | * 2 items for inode and ref |
| 4471 | * 2 items for dir items |
| 4472 | * 1 for xattr if selinux is on |
| 4473 | */ |
| 4474 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4475 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4476 | return err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4477 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4478 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4479 | if (!trans) { |
| 4480 | err = -ENOMEM; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4481 | goto out_unlock; |
| 4482 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4483 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4484 | |
| 4485 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4486 | if (err) { |
| 4487 | err = -ENOSPC; |
| 4488 | goto out_unlock; |
| 4489 | } |
| 4490 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4491 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4492 | dentry->d_name.len, |
| 4493 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4494 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 4495 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4496 | if (IS_ERR(inode)) { |
| 4497 | err = PTR_ERR(inode); |
| 4498 | goto out_fail; |
| 4499 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4500 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4501 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4502 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 4503 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4504 | if (err) |
| 4505 | goto out_fail; |
| 4506 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4507 | inode->i_op = &btrfs_dir_inode_operations; |
| 4508 | inode->i_fop = &btrfs_dir_file_operations; |
| 4509 | btrfs_set_trans_block_group(trans, inode); |
| 4510 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4511 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4512 | err = btrfs_update_inode(trans, root, inode); |
| 4513 | if (err) |
| 4514 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4515 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4516 | err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4517 | inode, dentry->d_name.name, |
| 4518 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4519 | if (err) |
| 4520 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4521 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4522 | d_instantiate(dentry, inode); |
| 4523 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4524 | btrfs_update_inode_block_group(trans, inode); |
| 4525 | btrfs_update_inode_block_group(trans, dir); |
| 4526 | |
| 4527 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4528 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4529 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4530 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4531 | out_unlock: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4532 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4533 | if (drop_on_err) |
| 4534 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4535 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4536 | return err; |
| 4537 | } |
| 4538 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4539 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4540 | * and an extent that you want to insert, deal with overlap and insert |
| 4541 | * the new extent into the tree. |
| 4542 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4543 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4544 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4545 | struct extent_map *em, |
| 4546 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4547 | { |
| 4548 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4549 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4550 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4551 | start_diff = map_start - em->start; |
| 4552 | em->start = map_start; |
| 4553 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4554 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4555 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4556 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4557 | em->block_len -= start_diff; |
| 4558 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4559 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4560 | } |
| 4561 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4562 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4563 | struct inode *inode, struct page *page, |
| 4564 | size_t pg_offset, u64 extent_offset, |
| 4565 | struct btrfs_file_extent_item *item) |
| 4566 | { |
| 4567 | int ret; |
| 4568 | struct extent_buffer *leaf = path->nodes[0]; |
| 4569 | char *tmp; |
| 4570 | size_t max_size; |
| 4571 | unsigned long inline_size; |
| 4572 | unsigned long ptr; |
| 4573 | |
| 4574 | WARN_ON(pg_offset != 0); |
| 4575 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4576 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4577 | btrfs_item_nr(leaf, path->slots[0])); |
| 4578 | tmp = kmalloc(inline_size, GFP_NOFS); |
| 4579 | ptr = btrfs_file_extent_inline_start(item); |
| 4580 | |
| 4581 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4582 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 4583 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4584 | ret = btrfs_zlib_decompress(tmp, page, extent_offset, |
| 4585 | inline_size, max_size); |
| 4586 | if (ret) { |
| 4587 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 4588 | unsigned long copy_size = min_t(u64, |
| 4589 | PAGE_CACHE_SIZE - pg_offset, |
| 4590 | max_size - extent_offset); |
| 4591 | memset(kaddr + pg_offset, 0, copy_size); |
| 4592 | kunmap_atomic(kaddr, KM_USER0); |
| 4593 | } |
| 4594 | kfree(tmp); |
| 4595 | return 0; |
| 4596 | } |
| 4597 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4598 | /* |
| 4599 | * 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] | 4600 | * the ugly parts come from merging extents from the disk with the in-ram |
| 4601 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4602 | * where the in-ram extents might be locked pending data=ordered completion. |
| 4603 | * |
| 4604 | * This also copies inline extents directly into the page. |
| 4605 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4606 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4607 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4608 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4609 | int create) |
| 4610 | { |
| 4611 | int ret; |
| 4612 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4613 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4614 | u64 extent_start = 0; |
| 4615 | u64 extent_end = 0; |
| 4616 | u64 objectid = inode->i_ino; |
| 4617 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4618 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4619 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4620 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4621 | struct extent_buffer *leaf; |
| 4622 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4623 | struct extent_map *em = NULL; |
| 4624 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4625 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4626 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4627 | int compressed; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4628 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4629 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4630 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4631 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4632 | if (em) |
| 4633 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4634 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4635 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4636 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4637 | if (em->start > start || em->start + em->len <= start) |
| 4638 | free_extent_map(em); |
| 4639 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4640 | free_extent_map(em); |
| 4641 | else |
| 4642 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4643 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4644 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4645 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4646 | err = -ENOMEM; |
| 4647 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4648 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4649 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4650 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 4651 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4652 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4653 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4654 | |
| 4655 | if (!path) { |
| 4656 | path = btrfs_alloc_path(); |
| 4657 | BUG_ON(!path); |
| 4658 | } |
| 4659 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4660 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 4661 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4662 | if (ret < 0) { |
| 4663 | err = ret; |
| 4664 | goto out; |
| 4665 | } |
| 4666 | |
| 4667 | if (ret != 0) { |
| 4668 | if (path->slots[0] == 0) |
| 4669 | goto not_found; |
| 4670 | path->slots[0]--; |
| 4671 | } |
| 4672 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4673 | leaf = path->nodes[0]; |
| 4674 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4675 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4676 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4677 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4678 | found_type = btrfs_key_type(&found_key); |
| 4679 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4680 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 4681 | goto not_found; |
| 4682 | } |
| 4683 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4684 | found_type = btrfs_file_extent_type(leaf, item); |
| 4685 | extent_start = found_key.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4686 | compressed = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4687 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4688 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4689 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4690 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4691 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 4692 | size_t size; |
| 4693 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4694 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 4695 | ~((u64)root->sectorsize - 1); |
| 4696 | } |
| 4697 | |
| 4698 | if (start >= extent_end) { |
| 4699 | path->slots[0]++; |
| 4700 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 4701 | ret = btrfs_next_leaf(root, path); |
| 4702 | if (ret < 0) { |
| 4703 | err = ret; |
| 4704 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4705 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4706 | if (ret > 0) |
| 4707 | goto not_found; |
| 4708 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4709 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4710 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4711 | if (found_key.objectid != objectid || |
| 4712 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 4713 | goto not_found; |
| 4714 | if (start + len <= found_key.offset) |
| 4715 | goto not_found; |
| 4716 | em->start = start; |
| 4717 | em->len = found_key.offset - start; |
| 4718 | goto not_found_em; |
| 4719 | } |
| 4720 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4721 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4722 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4723 | em->start = extent_start; |
| 4724 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4725 | em->orig_start = extent_start - |
| 4726 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4727 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 4728 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4729 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4730 | goto insert; |
| 4731 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4732 | if (compressed) { |
| 4733 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 4734 | em->block_start = bytenr; |
| 4735 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 4736 | item); |
| 4737 | } else { |
| 4738 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 4739 | em->block_start = bytenr; |
| 4740 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4741 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 4742 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4743 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4744 | goto insert; |
| 4745 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4746 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4747 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4748 | size_t size; |
| 4749 | size_t extent_offset; |
| 4750 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4751 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4752 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4753 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4754 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4755 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4756 | goto out; |
| 4757 | } |
| 4758 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4759 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4760 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4761 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4762 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4763 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4764 | em->len = (copy_size + root->sectorsize - 1) & |
| 4765 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4766 | em->orig_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4767 | if (compressed) |
| 4768 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4769 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4770 | if (create == 0 && !PageUptodate(page)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4771 | if (btrfs_file_extent_compression(leaf, item) == |
| 4772 | BTRFS_COMPRESS_ZLIB) { |
| 4773 | ret = uncompress_inline(path, inode, page, |
| 4774 | pg_offset, |
| 4775 | extent_offset, item); |
| 4776 | BUG_ON(ret); |
| 4777 | } else { |
| 4778 | map = kmap(page); |
| 4779 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 4780 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 4781 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 4782 | memset(map + pg_offset + copy_size, 0, |
| 4783 | PAGE_CACHE_SIZE - pg_offset - |
| 4784 | copy_size); |
| 4785 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4786 | kunmap(page); |
| 4787 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4788 | flush_dcache_page(page); |
| 4789 | } else if (create && PageUptodate(page)) { |
| 4790 | if (!trans) { |
| 4791 | kunmap(page); |
| 4792 | free_extent_map(em); |
| 4793 | em = NULL; |
| 4794 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4795 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4796 | goto again; |
| 4797 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4798 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4799 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4800 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4801 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4802 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4803 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4804 | set_extent_uptodate(io_tree, em->start, |
| 4805 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4806 | goto insert; |
| 4807 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4808 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4809 | WARN_ON(1); |
| 4810 | } |
| 4811 | not_found: |
| 4812 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4813 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4814 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4815 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4816 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4817 | insert: |
| 4818 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4819 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4820 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 4821 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 4822 | (unsigned long long)em->len, |
| 4823 | (unsigned long long)start, |
| 4824 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4825 | err = -EIO; |
| 4826 | goto out; |
| 4827 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4828 | |
| 4829 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4830 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4831 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4832 | /* it is possible that someone inserted the extent into the tree |
| 4833 | * while we had the lock dropped. It is also possible that |
| 4834 | * an overlapping map exists in the tree |
| 4835 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4836 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4837 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4838 | |
| 4839 | ret = 0; |
| 4840 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4841 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4842 | if (existing && (existing->start > start || |
| 4843 | existing->start + existing->len <= start)) { |
| 4844 | free_extent_map(existing); |
| 4845 | existing = NULL; |
| 4846 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4847 | if (!existing) { |
| 4848 | existing = lookup_extent_mapping(em_tree, em->start, |
| 4849 | em->len); |
| 4850 | if (existing) { |
| 4851 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4852 | em, start, |
| 4853 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4854 | free_extent_map(existing); |
| 4855 | if (err) { |
| 4856 | free_extent_map(em); |
| 4857 | em = NULL; |
| 4858 | } |
| 4859 | } else { |
| 4860 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4861 | free_extent_map(em); |
| 4862 | em = NULL; |
| 4863 | } |
| 4864 | } else { |
| 4865 | free_extent_map(em); |
| 4866 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4867 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4868 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4869 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4870 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4871 | out: |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4872 | if (path) |
| 4873 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4874 | if (trans) { |
| 4875 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4876 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4877 | err = ret; |
| 4878 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4879 | if (err) { |
| 4880 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4881 | return ERR_PTR(err); |
| 4882 | } |
| 4883 | return em; |
| 4884 | } |
| 4885 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4886 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 4887 | const struct iovec *iov, loff_t offset, |
| 4888 | unsigned long nr_segs) |
| 4889 | { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4890 | return -EINVAL; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4891 | } |
| 4892 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4893 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4894 | __u64 start, __u64 len) |
| 4895 | { |
| 4896 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent); |
| 4897 | } |
| 4898 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4899 | int btrfs_readpage(struct file *file, struct page *page) |
| 4900 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4901 | struct extent_io_tree *tree; |
| 4902 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4903 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4904 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4905 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4906 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 4907 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4908 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4909 | |
| 4910 | |
| 4911 | if (current->flags & PF_MEMALLOC) { |
| 4912 | redirty_page_for_writepage(wbc, page); |
| 4913 | unlock_page(page); |
| 4914 | return 0; |
| 4915 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4916 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4917 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 4918 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4919 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4920 | int btrfs_writepages(struct address_space *mapping, |
| 4921 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 4922 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4923 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4924 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4925 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 4926 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 4927 | } |
| 4928 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 4929 | static int |
| 4930 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 4931 | struct list_head *pages, unsigned nr_pages) |
| 4932 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4933 | struct extent_io_tree *tree; |
| 4934 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 4935 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 4936 | btrfs_get_extent); |
| 4937 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4938 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4939 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4940 | struct extent_io_tree *tree; |
| 4941 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4942 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4943 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4944 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 4945 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4946 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4947 | if (ret == 1) { |
| 4948 | ClearPagePrivate(page); |
| 4949 | set_page_private(page, 0); |
| 4950 | page_cache_release(page); |
| 4951 | } |
| 4952 | return ret; |
| 4953 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4954 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4955 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 4956 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 4957 | if (PageWriteback(page) || PageDirty(page)) |
| 4958 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 4959 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4960 | } |
| 4961 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4962 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 4963 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4964 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4965 | struct btrfs_ordered_extent *ordered; |
| 4966 | u64 page_start = page_offset(page); |
| 4967 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4968 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4969 | |
| 4970 | /* |
| 4971 | * we have the page locked, so new writeback can't start, |
| 4972 | * and the dirty bit won't be cleared while we are here. |
| 4973 | * |
| 4974 | * Wait for IO on this page so that we can safely clear |
| 4975 | * the PagePrivate2 bit and do ordered accounting |
| 4976 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4977 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4978 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4979 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4980 | if (offset) { |
| 4981 | btrfs_releasepage(page, GFP_NOFS); |
| 4982 | return; |
| 4983 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4984 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 4985 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 4986 | page_offset(page)); |
| 4987 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4988 | /* |
| 4989 | * IO on this page will never be started, so we need |
| 4990 | * to account for any ordered extents now |
| 4991 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4992 | clear_extent_bit(tree, page_start, page_end, |
| 4993 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 4994 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
| 4995 | NULL, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4996 | /* |
| 4997 | * whoever cleared the private bit is responsible |
| 4998 | * for the finish_ordered_io |
| 4999 | */ |
| 5000 | if (TestClearPagePrivate2(page)) { |
| 5001 | btrfs_finish_ordered_io(page->mapping->host, |
| 5002 | page_start, page_end); |
| 5003 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5004 | btrfs_put_ordered_extent(ordered); |
| 5005 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 5006 | } |
| 5007 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5008 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 5009 | EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5010 | __btrfs_releasepage(page, GFP_NOFS); |
| 5011 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 5012 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 5013 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 5014 | ClearPagePrivate(page); |
| 5015 | set_page_private(page, 0); |
| 5016 | page_cache_release(page); |
| 5017 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5018 | } |
| 5019 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5020 | /* |
| 5021 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 5022 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 5023 | * be careful to check for EOF conditions here. We set the page up correctly |
| 5024 | * for a written page which means we get ENOSPC checking when writing into |
| 5025 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 5026 | * support these features. |
| 5027 | * |
| 5028 | * We are not allowed to take the i_mutex here so we have to play games to |
| 5029 | * protect against truncate races as the page could now be beyond EOF. Because |
| 5030 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 5031 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 5032 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 5033 | * unlock the page. |
| 5034 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5035 | 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] | 5036 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 5037 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 5038 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5039 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5040 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5041 | struct btrfs_ordered_extent *ordered; |
| 5042 | char *kaddr; |
| 5043 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5044 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5045 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5046 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5047 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5048 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5049 | ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5050 | if (ret) { |
| 5051 | if (ret == -ENOMEM) |
| 5052 | ret = VM_FAULT_OOM; |
| 5053 | else /* -ENOSPC, -EIO, etc */ |
| 5054 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5055 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5056 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5057 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5058 | ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1); |
| 5059 | if (ret) { |
| 5060 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
| 5061 | ret = VM_FAULT_SIGBUS; |
| 5062 | goto out; |
| 5063 | } |
| 5064 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 5065 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5066 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5067 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5068 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5069 | page_start = page_offset(page); |
| 5070 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5071 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5072 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5073 | (page_start >= size)) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5074 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5075 | /* page got truncated out from underneath us */ |
| 5076 | goto out_unlock; |
| 5077 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5078 | wait_on_page_writeback(page); |
| 5079 | |
| 5080 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5081 | set_page_extent_mapped(page); |
| 5082 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5083 | /* |
| 5084 | * we can't set the delalloc bits if there are pending ordered |
| 5085 | * extents. Drop our locks and wait for them to finish |
| 5086 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5087 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 5088 | if (ordered) { |
| 5089 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5090 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 5091 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5092 | btrfs_put_ordered_extent(ordered); |
| 5093 | goto again; |
| 5094 | } |
| 5095 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 5096 | /* |
| 5097 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 5098 | * if it was already dirty, so for space accounting reasons we need to |
| 5099 | * clear any delalloc bits for the range we are fixing to save. There |
| 5100 | * is probably a better way to do this, but for now keep consistent with |
| 5101 | * prepare_pages in the normal write path. |
| 5102 | */ |
| 5103 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5104 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
| 5105 | GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 5106 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5107 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end); |
| 5108 | if (ret) { |
| 5109 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5110 | ret = VM_FAULT_SIGBUS; |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 5111 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5112 | goto out_unlock; |
| 5113 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5114 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5115 | |
| 5116 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5117 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5118 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5119 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5120 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5121 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5122 | if (zero_start != PAGE_CACHE_SIZE) { |
| 5123 | kaddr = kmap(page); |
| 5124 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 5125 | flush_dcache_page(page); |
| 5126 | kunmap(page); |
| 5127 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 5128 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5129 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 5130 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5131 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5132 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 5133 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 5134 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5135 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5136 | |
| 5137 | out_unlock: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5138 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 5139 | if (!ret) |
| 5140 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5141 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5142 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 5143 | return ret; |
| 5144 | } |
| 5145 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5146 | static void btrfs_truncate(struct inode *inode) |
| 5147 | { |
| 5148 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5149 | int ret; |
| 5150 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5151 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5152 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5153 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 5154 | if (!S_ISREG(inode->i_mode)) { |
| 5155 | WARN_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5156 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 5157 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5158 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 5159 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 5160 | if (ret) |
| 5161 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 5162 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 5163 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 5164 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5165 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5166 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 5167 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5168 | |
| 5169 | /* |
| 5170 | * setattr is responsible for setting the ordered_data_close flag, |
| 5171 | * but that is only tested during the last file release. That |
| 5172 | * could happen well after the next commit, leaving a great big |
| 5173 | * window where new writes may get lost if someone chooses to write |
| 5174 | * to this file after truncating to zero |
| 5175 | * |
| 5176 | * The inode doesn't have any dirty data here, and so if we commit |
| 5177 | * this is a noop. If someone immediately starts writing to the inode |
| 5178 | * it is very likely we'll catch some of their writes in this |
| 5179 | * transaction, and the commit will find this file on the ordered |
| 5180 | * data list with good things to send down. |
| 5181 | * |
| 5182 | * This is a best effort solution, there is still a window where |
| 5183 | * using truncate to replace the contents of the file will |
| 5184 | * end up with a zero length file after a crash. |
| 5185 | */ |
| 5186 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 5187 | btrfs_add_ordered_operation(trans, root, inode); |
| 5188 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 5189 | while (1) { |
| 5190 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 5191 | inode->i_size, |
| 5192 | BTRFS_EXTENT_DATA_KEY); |
| 5193 | if (ret != -EAGAIN) |
| 5194 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5195 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 5196 | ret = btrfs_update_inode(trans, root, inode); |
| 5197 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5198 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 5199 | nr = trans->blocks_used; |
| 5200 | btrfs_end_transaction(trans, root); |
| 5201 | btrfs_btree_balance_dirty(root, nr); |
| 5202 | |
| 5203 | trans = btrfs_start_transaction(root, 1); |
| 5204 | btrfs_set_trans_block_group(trans, inode); |
| 5205 | } |
| 5206 | |
| 5207 | if (ret == 0 && inode->i_nlink > 0) { |
| 5208 | ret = btrfs_orphan_del(trans, inode); |
| 5209 | BUG_ON(ret); |
| 5210 | } |
| 5211 | |
| 5212 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5213 | BUG_ON(ret); |
| 5214 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5215 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5216 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5217 | BUG_ON(ret); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5218 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5219 | } |
| 5220 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 5221 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5222 | * create a new subvolume directory/inode (helper for the ioctl). |
| 5223 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5224 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5225 | struct btrfs_root *new_root, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5226 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5227 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5228 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5229 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5230 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5231 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5232 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5233 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5234 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5235 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5236 | inode->i_op = &btrfs_dir_inode_operations; |
| 5237 | inode->i_fop = &btrfs_dir_file_operations; |
| 5238 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5239 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5240 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 5241 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5242 | err = btrfs_update_inode(trans, new_root, inode); |
| 5243 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 5244 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5245 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 5246 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5247 | } |
| 5248 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5249 | /* helper function for file defrag and space balancing. This |
| 5250 | * forces readahead on a given range of bytes in an inode |
| 5251 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5252 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5253 | struct file_ra_state *ra, struct file *file, |
| 5254 | pgoff_t offset, pgoff_t last_index) |
| 5255 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5256 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5257 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5258 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 5259 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 5260 | } |
| 5261 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5262 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 5263 | { |
| 5264 | struct btrfs_inode *ei; |
| 5265 | |
| 5266 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 5267 | if (!ei) |
| 5268 | return NULL; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 5269 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 5270 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5271 | ei->logged_trans = 0; |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5272 | ei->outstanding_extents = 0; |
| 5273 | ei->reserved_extents = 0; |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 5274 | ei->root = NULL; |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 5275 | spin_lock_init(&ei->accounting_lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5276 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5277 | INIT_LIST_HEAD(&ei->i_orphan); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5278 | INIT_LIST_HEAD(&ei->ordered_operations); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5279 | return &ei->vfs_inode; |
| 5280 | } |
| 5281 | |
| 5282 | void btrfs_destroy_inode(struct inode *inode) |
| 5283 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5284 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5285 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5286 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5287 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 5288 | WARN_ON(inode->i_data.nrpages); |
| 5289 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5290 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 5291 | * This can happen where we create an inode, but somebody else also |
| 5292 | * created the same inode and we need to destroy the one we already |
| 5293 | * created. |
| 5294 | */ |
| 5295 | if (!root) |
| 5296 | goto free; |
| 5297 | |
| 5298 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5299 | * Make sure we're properly removed from the ordered operation |
| 5300 | * lists. |
| 5301 | */ |
| 5302 | smp_mb(); |
| 5303 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 5304 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 5305 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 5306 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 5307 | } |
| 5308 | |
| 5309 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5310 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame^] | 5311 | printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", |
| 5312 | inode->i_ino); |
| 5313 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5314 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5315 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5316 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5317 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5318 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 5319 | if (!ordered) |
| 5320 | break; |
| 5321 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5322 | printk(KERN_ERR "btrfs found ordered " |
| 5323 | "extent %llu %llu on inode cleanup\n", |
| 5324 | (unsigned long long)ordered->file_offset, |
| 5325 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5326 | btrfs_remove_ordered_extent(inode, ordered); |
| 5327 | btrfs_put_ordered_extent(ordered); |
| 5328 | btrfs_put_ordered_extent(ordered); |
| 5329 | } |
| 5330 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5331 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5332 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 5333 | free: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5334 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 5335 | } |
| 5336 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5337 | void btrfs_drop_inode(struct inode *inode) |
| 5338 | { |
| 5339 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5340 | |
| 5341 | if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0) |
| 5342 | generic_delete_inode(inode); |
| 5343 | else |
| 5344 | generic_drop_inode(inode); |
| 5345 | } |
| 5346 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5347 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5348 | { |
| 5349 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 5350 | |
| 5351 | inode_init_once(&ei->vfs_inode); |
| 5352 | } |
| 5353 | |
| 5354 | void btrfs_destroy_cachep(void) |
| 5355 | { |
| 5356 | if (btrfs_inode_cachep) |
| 5357 | kmem_cache_destroy(btrfs_inode_cachep); |
| 5358 | if (btrfs_trans_handle_cachep) |
| 5359 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 5360 | if (btrfs_transaction_cachep) |
| 5361 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5362 | if (btrfs_path_cachep) |
| 5363 | kmem_cache_destroy(btrfs_path_cachep); |
| 5364 | } |
| 5365 | |
| 5366 | int btrfs_init_cachep(void) |
| 5367 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5368 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 5369 | sizeof(struct btrfs_inode), 0, |
| 5370 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5371 | if (!btrfs_inode_cachep) |
| 5372 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5373 | |
| 5374 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 5375 | sizeof(struct btrfs_trans_handle), 0, |
| 5376 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5377 | if (!btrfs_trans_handle_cachep) |
| 5378 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5379 | |
| 5380 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 5381 | sizeof(struct btrfs_transaction), 0, |
| 5382 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5383 | if (!btrfs_transaction_cachep) |
| 5384 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5385 | |
| 5386 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 5387 | sizeof(struct btrfs_path), 0, |
| 5388 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5389 | if (!btrfs_path_cachep) |
| 5390 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 5391 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5392 | return 0; |
| 5393 | fail: |
| 5394 | btrfs_destroy_cachep(); |
| 5395 | return -ENOMEM; |
| 5396 | } |
| 5397 | |
| 5398 | static int btrfs_getattr(struct vfsmount *mnt, |
| 5399 | struct dentry *dentry, struct kstat *stat) |
| 5400 | { |
| 5401 | struct inode *inode = dentry->d_inode; |
| 5402 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5403 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 5404 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5405 | stat->blocks = (inode_get_bytes(inode) + |
| 5406 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5407 | return 0; |
| 5408 | } |
| 5409 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5410 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 5411 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5412 | { |
| 5413 | struct btrfs_trans_handle *trans; |
| 5414 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5415 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5416 | struct inode *new_inode = new_dentry->d_inode; |
| 5417 | struct inode *old_inode = old_dentry->d_inode; |
| 5418 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5419 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5420 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5421 | int ret; |
| 5422 | |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5423 | if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5424 | return -EPERM; |
| 5425 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5426 | /* we only allow rename subvolume link between subvolumes */ |
| 5427 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5428 | return -EXDEV; |
| 5429 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5430 | if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 5431 | (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) |
| 5432 | return -ENOTEMPTY; |
| 5433 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5434 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5435 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5436 | return -ENOTEMPTY; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 5437 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5438 | /* |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5439 | * We want to reserve the absolute worst case amount of items. So if |
| 5440 | * both inodes are subvols and we need to unlink them then that would |
| 5441 | * require 4 item modifications, but if they are both normal inodes it |
| 5442 | * would require 5 item modifications, so we'll assume their normal |
| 5443 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 5444 | * should cover the worst case number of items we'll modify. |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5445 | */ |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5446 | ret = btrfs_reserve_metadata_space(root, 11); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5447 | if (ret) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5448 | return ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5449 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5450 | /* |
| 5451 | * we're using rename to replace one file with another. |
| 5452 | * and the replacement file is large. Start IO on it now so |
| 5453 | * we don't add too much work to the end of the transaction |
| 5454 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 5455 | 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] | 5456 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 5457 | filemap_flush(old_inode->i_mapping); |
| 5458 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5459 | /* close the racy window with snapshot create/destroy ioctl */ |
| 5460 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 5461 | down_read(&root->fs_info->subvol_sem); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5462 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5463 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5464 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5465 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5466 | if (dest != root) |
| 5467 | btrfs_record_root_in_trans(trans, dest); |
| 5468 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5469 | ret = btrfs_set_inode_index(new_dir, &index); |
| 5470 | if (ret) |
| 5471 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5472 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5473 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5474 | /* force full log commit if subvolume involved. */ |
| 5475 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 5476 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5477 | ret = btrfs_insert_inode_ref(trans, dest, |
| 5478 | new_dentry->d_name.name, |
| 5479 | new_dentry->d_name.len, |
| 5480 | old_inode->i_ino, |
| 5481 | new_dir->i_ino, index); |
| 5482 | if (ret) |
| 5483 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5484 | /* |
| 5485 | * this is an ugly little race, but the rename is required |
| 5486 | * to make sure that if we crash, the inode is either at the |
| 5487 | * old name or the new one. pinning the log transaction lets |
| 5488 | * us make sure we don't allow a log commit to come in after |
| 5489 | * we unlink the name but before we add the new name back in. |
| 5490 | */ |
| 5491 | btrfs_pin_log_trans(root); |
| 5492 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5493 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5494 | * make sure the inode gets flushed if it is replacing |
| 5495 | * something. |
| 5496 | */ |
| 5497 | if (new_inode && new_inode->i_size && |
| 5498 | old_inode && S_ISREG(old_inode->i_mode)) { |
| 5499 | btrfs_add_ordered_operation(trans, root, old_inode); |
| 5500 | } |
| 5501 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5502 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 5503 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 5504 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5505 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5506 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 5507 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 5508 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5509 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5510 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 5511 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 5512 | old_dentry->d_name.name, |
| 5513 | old_dentry->d_name.len); |
| 5514 | } else { |
| 5515 | btrfs_inc_nlink(old_dentry->d_inode); |
| 5516 | ret = btrfs_unlink_inode(trans, root, old_dir, |
| 5517 | old_dentry->d_inode, |
| 5518 | old_dentry->d_name.name, |
| 5519 | old_dentry->d_name.len); |
| 5520 | } |
| 5521 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5522 | |
| 5523 | if (new_inode) { |
| 5524 | new_inode->i_ctime = CURRENT_TIME; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5525 | if (unlikely(new_inode->i_ino == |
| 5526 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 5527 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 5528 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 5529 | root_objectid, |
| 5530 | new_dentry->d_name.name, |
| 5531 | new_dentry->d_name.len); |
| 5532 | BUG_ON(new_inode->i_nlink == 0); |
| 5533 | } else { |
| 5534 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 5535 | new_dentry->d_inode, |
| 5536 | new_dentry->d_name.name, |
| 5537 | new_dentry->d_name.len); |
| 5538 | } |
| 5539 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5540 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5541 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5542 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5543 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5544 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5545 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5546 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 5547 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5548 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5549 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5550 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5551 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 5552 | btrfs_log_new_name(trans, old_inode, old_dir, |
| 5553 | new_dentry->d_parent); |
| 5554 | btrfs_end_log_trans(root); |
| 5555 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5556 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 5557 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5558 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5559 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 5560 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5561 | |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5562 | btrfs_unreserve_metadata_space(root, 11); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5563 | return ret; |
| 5564 | } |
| 5565 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5566 | /* |
| 5567 | * some fairly slow code that needs optimization. This walks the list |
| 5568 | * of all the inodes with pending delalloc and forces them to disk. |
| 5569 | */ |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5570 | int btrfs_start_delalloc_inodes(struct btrfs_root *root) |
| 5571 | { |
| 5572 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 5573 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5574 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5575 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5576 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 5577 | return -EROFS; |
| 5578 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5579 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5580 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5581 | binode = list_entry(head->next, struct btrfs_inode, |
| 5582 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5583 | inode = igrab(&binode->vfs_inode); |
| 5584 | if (!inode) |
| 5585 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5586 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5587 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5588 | filemap_flush(inode->i_mapping); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5589 | iput(inode); |
| 5590 | } |
| 5591 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5592 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5593 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5594 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5595 | |
| 5596 | /* the filemap_flush will queue IO into the worker threads, but |
| 5597 | * we have to make sure the IO is actually started and that |
| 5598 | * ordered extents get created before we return |
| 5599 | */ |
| 5600 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5601 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5602 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5603 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5604 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 5605 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5606 | } |
| 5607 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5608 | return 0; |
| 5609 | } |
| 5610 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5611 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 5612 | const char *symname) |
| 5613 | { |
| 5614 | struct btrfs_trans_handle *trans; |
| 5615 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5616 | struct btrfs_path *path; |
| 5617 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5618 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5619 | int err; |
| 5620 | int drop_inode = 0; |
| 5621 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5622 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5623 | int name_len; |
| 5624 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5625 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5626 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5627 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5628 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5629 | |
| 5630 | name_len = strlen(symname) + 1; |
| 5631 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 5632 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5633 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5634 | /* |
| 5635 | * 2 items for inode item and ref |
| 5636 | * 2 items for dir items |
| 5637 | * 1 item for xattr if selinux is on |
| 5638 | */ |
| 5639 | err = btrfs_reserve_metadata_space(root, 5); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5640 | if (err) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5641 | return err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5642 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5643 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5644 | if (!trans) |
| 5645 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5646 | btrfs_set_trans_block_group(trans, dir); |
| 5647 | |
| 5648 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 5649 | if (err) { |
| 5650 | err = -ENOSPC; |
| 5651 | goto out_unlock; |
| 5652 | } |
| 5653 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5654 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5655 | dentry->d_name.len, |
| 5656 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5657 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 5658 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5659 | err = PTR_ERR(inode); |
| 5660 | if (IS_ERR(inode)) |
| 5661 | goto out_unlock; |
| 5662 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 5663 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5664 | if (err) { |
| 5665 | drop_inode = 1; |
| 5666 | goto out_unlock; |
| 5667 | } |
| 5668 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5669 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5670 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5671 | if (err) |
| 5672 | drop_inode = 1; |
| 5673 | else { |
| 5674 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 5675 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5676 | inode->i_fop = &btrfs_file_operations; |
| 5677 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5678 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5679 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5680 | btrfs_update_inode_block_group(trans, inode); |
| 5681 | btrfs_update_inode_block_group(trans, dir); |
| 5682 | if (drop_inode) |
| 5683 | goto out_unlock; |
| 5684 | |
| 5685 | path = btrfs_alloc_path(); |
| 5686 | BUG_ON(!path); |
| 5687 | key.objectid = inode->i_ino; |
| 5688 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5689 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 5690 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 5691 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 5692 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5693 | if (err) { |
| 5694 | drop_inode = 1; |
| 5695 | goto out_unlock; |
| 5696 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5697 | leaf = path->nodes[0]; |
| 5698 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 5699 | struct btrfs_file_extent_item); |
| 5700 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 5701 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5702 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5703 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 5704 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 5705 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 5706 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 5707 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5708 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5709 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 5710 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5711 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5712 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5713 | inode->i_op = &btrfs_symlink_inode_operations; |
| 5714 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 5715 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5716 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5717 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5718 | err = btrfs_update_inode(trans, root, inode); |
| 5719 | if (err) |
| 5720 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5721 | |
| 5722 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5723 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 5724 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5725 | out_fail: |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5726 | btrfs_unreserve_metadata_space(root, 5); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5727 | if (drop_inode) { |
| 5728 | inode_dec_link_count(inode); |
| 5729 | iput(inode); |
| 5730 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5731 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5732 | return err; |
| 5733 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 5734 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5735 | static int prealloc_file_range(struct inode *inode, u64 start, u64 end, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 5736 | u64 alloc_hint, int mode) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5737 | { |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5738 | struct btrfs_trans_handle *trans; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5739 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5740 | struct btrfs_key ins; |
| 5741 | u64 alloc_size; |
| 5742 | u64 cur_offset = start; |
| 5743 | u64 num_bytes = end - start; |
| 5744 | int ret = 0; |
| 5745 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5746 | while (num_bytes > 0) { |
| 5747 | alloc_size = min(num_bytes, root->fs_info->max_extent); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5748 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5749 | ret = btrfs_reserve_extent(trans, root, alloc_size, |
| 5750 | root->sectorsize, 0, alloc_hint, |
| 5751 | (u64)-1, &ins, 1); |
| 5752 | if (ret) { |
| 5753 | WARN_ON(1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5754 | break; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5755 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5756 | |
| 5757 | ret = btrfs_reserve_metadata_space(root, 3); |
| 5758 | if (ret) { |
| 5759 | btrfs_free_reserved_extent(root, ins.objectid, |
| 5760 | ins.offset); |
| 5761 | break; |
| 5762 | } |
| 5763 | |
| 5764 | trans = btrfs_start_transaction(root, 1); |
| 5765 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5766 | ret = insert_reserved_file_extent(trans, inode, |
| 5767 | cur_offset, ins.objectid, |
| 5768 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 5769 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5770 | BTRFS_FILE_EXTENT_PREALLOC); |
| 5771 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 5772 | btrfs_drop_extent_cache(inode, cur_offset, |
| 5773 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5774 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5775 | num_bytes -= ins.offset; |
| 5776 | cur_offset += ins.offset; |
| 5777 | alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5778 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5779 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5780 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5781 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5782 | cur_offset > inode->i_size) { |
| 5783 | i_size_write(inode, cur_offset); |
| 5784 | btrfs_ordered_update_i_size(inode, cur_offset, NULL); |
| 5785 | } |
| 5786 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5787 | ret = btrfs_update_inode(trans, root, inode); |
| 5788 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5789 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5790 | btrfs_end_transaction(trans, root); |
| 5791 | btrfs_unreserve_metadata_space(root, 3); |
| 5792 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5793 | return ret; |
| 5794 | } |
| 5795 | |
| 5796 | static long btrfs_fallocate(struct inode *inode, int mode, |
| 5797 | loff_t offset, loff_t len) |
| 5798 | { |
| 5799 | u64 cur_offset; |
| 5800 | u64 last_byte; |
| 5801 | u64 alloc_start; |
| 5802 | u64 alloc_end; |
| 5803 | u64 alloc_hint = 0; |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5804 | u64 locked_end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5805 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 5806 | struct extent_map *em; |
| 5807 | int ret; |
| 5808 | |
| 5809 | alloc_start = offset & ~mask; |
| 5810 | alloc_end = (offset + len + mask) & ~mask; |
| 5811 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5812 | /* |
| 5813 | * wait for ordered IO before we have any locks. We'll loop again |
| 5814 | * below with the locks held. |
| 5815 | */ |
| 5816 | btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start); |
| 5817 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5818 | mutex_lock(&inode->i_mutex); |
| 5819 | if (alloc_start > inode->i_size) { |
| 5820 | ret = btrfs_cont_expand(inode, alloc_start); |
| 5821 | if (ret) |
| 5822 | goto out; |
| 5823 | } |
| 5824 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5825 | ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode, |
Josef Bacik | a970b0a | 2009-06-27 21:07:34 -0400 | [diff] [blame] | 5826 | alloc_end - alloc_start); |
| 5827 | if (ret) |
| 5828 | goto out; |
| 5829 | |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5830 | locked_end = alloc_end - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5831 | while (1) { |
| 5832 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5833 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5834 | /* the extent lock is ordered inside the running |
| 5835 | * transaction |
| 5836 | */ |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5837 | lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
| 5838 | GFP_NOFS); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5839 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 5840 | alloc_end - 1); |
| 5841 | if (ordered && |
| 5842 | ordered->file_offset + ordered->len > alloc_start && |
| 5843 | ordered->file_offset < alloc_end) { |
| 5844 | btrfs_put_ordered_extent(ordered); |
| 5845 | unlock_extent(&BTRFS_I(inode)->io_tree, |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5846 | alloc_start, locked_end, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5847 | /* |
| 5848 | * we can't wait on the range with the transaction |
| 5849 | * running or with the extent lock held |
| 5850 | */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5851 | btrfs_wait_ordered_range(inode, alloc_start, |
| 5852 | alloc_end - alloc_start); |
| 5853 | } else { |
| 5854 | if (ordered) |
| 5855 | btrfs_put_ordered_extent(ordered); |
| 5856 | break; |
| 5857 | } |
| 5858 | } |
| 5859 | |
| 5860 | cur_offset = alloc_start; |
| 5861 | while (1) { |
| 5862 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 5863 | alloc_end - cur_offset, 0); |
| 5864 | BUG_ON(IS_ERR(em) || !em); |
| 5865 | last_byte = min(extent_map_end(em), alloc_end); |
| 5866 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5867 | if (em->block_start == EXTENT_MAP_HOLE || |
| 5868 | (cur_offset >= inode->i_size && |
| 5869 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5870 | ret = prealloc_file_range(inode, |
| 5871 | cur_offset, last_byte, |
| 5872 | alloc_hint, mode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5873 | if (ret < 0) { |
| 5874 | free_extent_map(em); |
| 5875 | break; |
| 5876 | } |
| 5877 | } |
| 5878 | if (em->block_start <= EXTENT_MAP_LAST_BYTE) |
| 5879 | alloc_hint = em->block_start; |
| 5880 | free_extent_map(em); |
| 5881 | |
| 5882 | cur_offset = last_byte; |
| 5883 | if (cur_offset >= alloc_end) { |
| 5884 | ret = 0; |
| 5885 | break; |
| 5886 | } |
| 5887 | } |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5888 | unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5889 | GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5890 | |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 5891 | btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode, |
| 5892 | alloc_end - alloc_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5893 | out: |
| 5894 | mutex_unlock(&inode->i_mutex); |
| 5895 | return ret; |
| 5896 | } |
| 5897 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5898 | static int btrfs_set_page_dirty(struct page *page) |
| 5899 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5900 | return __set_page_dirty_nobuffers(page); |
| 5901 | } |
| 5902 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5903 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5904 | { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5905 | if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5906 | return -EACCES; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5907 | return generic_permission(inode, mask, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5908 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5909 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 5910 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5911 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5912 | .lookup = btrfs_lookup, |
| 5913 | .create = btrfs_create, |
| 5914 | .unlink = btrfs_unlink, |
| 5915 | .link = btrfs_link, |
| 5916 | .mkdir = btrfs_mkdir, |
| 5917 | .rmdir = btrfs_rmdir, |
| 5918 | .rename = btrfs_rename, |
| 5919 | .symlink = btrfs_symlink, |
| 5920 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5921 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5922 | .setxattr = btrfs_setxattr, |
| 5923 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5924 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5925 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5926 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5927 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 5928 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5929 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5930 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5931 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5932 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 5933 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5934 | .llseek = generic_file_llseek, |
| 5935 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 5936 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 5937 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5938 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 5939 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5940 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 5941 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5942 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5943 | }; |
| 5944 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5945 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5946 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 5947 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5948 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5949 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5950 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 5951 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5952 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 5953 | .set_bit_hook = btrfs_set_bit_hook, |
| 5954 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5955 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 5956 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5957 | }; |
| 5958 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 5959 | /* |
| 5960 | * btrfs doesn't support the bmap operation because swapfiles |
| 5961 | * use bmap to make a mapping of extents in the file. They assume |
| 5962 | * these extents won't change over the life of the file and they |
| 5963 | * use the bmap result to do IO directly to the drive. |
| 5964 | * |
| 5965 | * the btrfs bmap call would return logical addresses that aren't |
| 5966 | * suitable for IO and they also will change frequently as COW |
| 5967 | * operations happen. So, swapfile + btrfs == corruption. |
| 5968 | * |
| 5969 | * For now we're avoiding this by dropping bmap. |
| 5970 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 5971 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5972 | .readpage = btrfs_readpage, |
| 5973 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 5974 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 5975 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5976 | .sync_page = block_sync_page, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 5977 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5978 | .invalidatepage = btrfs_invalidatepage, |
| 5979 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5980 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 5981 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5982 | }; |
| 5983 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 5984 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5985 | .readpage = btrfs_readpage, |
| 5986 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 5987 | .invalidatepage = btrfs_invalidatepage, |
| 5988 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5989 | }; |
| 5990 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 5991 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5992 | .truncate = btrfs_truncate, |
| 5993 | .getattr = btrfs_getattr, |
| 5994 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5995 | .setxattr = btrfs_setxattr, |
| 5996 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5997 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5998 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5999 | .permission = btrfs_permission, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6000 | .fallocate = btrfs_fallocate, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6001 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6002 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6003 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6004 | .getattr = btrfs_getattr, |
| 6005 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6006 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6007 | .setxattr = btrfs_setxattr, |
| 6008 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6009 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 6010 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6011 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 6012 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6013 | .readlink = generic_readlink, |
| 6014 | .follow_link = page_follow_link_light, |
| 6015 | .put_link = page_put_link, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 6016 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 6017 | .setxattr = btrfs_setxattr, |
| 6018 | .getxattr = btrfs_getxattr, |
| 6019 | .listxattr = btrfs_listxattr, |
| 6020 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6021 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6022 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 6023 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6024 | .d_delete = btrfs_dentry_delete, |
| 6025 | }; |