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> |
| 29 | #include <linux/smp_lock.h> |
| 30 | #include <linux/backing-dev.h> |
| 31 | #include <linux/mpage.h> |
| 32 | #include <linux/swap.h> |
| 33 | #include <linux/writeback.h> |
| 34 | #include <linux/statfs.h> |
| 35 | #include <linux/compat.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 36 | #include <linux/bit_spinlock.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 37 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 38 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 39 | #include <linux/falloc.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 40 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 41 | #include "ctree.h" |
| 42 | #include "disk-io.h" |
| 43 | #include "transaction.h" |
| 44 | #include "btrfs_inode.h" |
| 45 | #include "ioctl.h" |
| 46 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 47 | #include "volumes.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 48 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 49 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 50 | #include "tree-log.h" |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 51 | #include "ref-cache.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 52 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 53 | #include "locking.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 54 | |
| 55 | struct btrfs_iget_args { |
| 56 | u64 ino; |
| 57 | struct btrfs_root *root; |
| 58 | }; |
| 59 | |
| 60 | static struct inode_operations btrfs_dir_inode_operations; |
| 61 | static struct inode_operations btrfs_symlink_inode_operations; |
| 62 | static struct inode_operations btrfs_dir_ro_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 63 | static struct inode_operations btrfs_special_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 64 | static struct inode_operations btrfs_file_inode_operations; |
| 65 | static struct address_space_operations btrfs_aops; |
| 66 | static struct address_space_operations btrfs_symlink_aops; |
| 67 | static struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 68 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 69 | |
| 70 | static struct kmem_cache *btrfs_inode_cachep; |
| 71 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 72 | struct kmem_cache *btrfs_transaction_cachep; |
| 73 | struct kmem_cache *btrfs_bit_radix_cachep; |
| 74 | struct kmem_cache *btrfs_path_cachep; |
| 75 | |
| 76 | #define S_SHIFT 12 |
| 77 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 78 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 79 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 80 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 81 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 82 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 83 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 84 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 85 | }; |
| 86 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 87 | static void btrfs_truncate(struct inode *inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 88 | 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] | 89 | static noinline int cow_file_range(struct inode *inode, |
| 90 | struct page *locked_page, |
| 91 | u64 start, u64 end, int *page_started, |
| 92 | unsigned long *nr_written, int unlock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 93 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 94 | static int btrfs_init_inode_security(struct inode *inode, struct inode *dir) |
| 95 | { |
| 96 | int err; |
| 97 | |
| 98 | err = btrfs_init_acl(inode, dir); |
| 99 | if (!err) |
| 100 | err = btrfs_xattr_security_init(inode, dir); |
| 101 | return err; |
| 102 | } |
| 103 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 104 | /* |
| 105 | * a very lame attempt at stopping writes when the FS is 85% full. There |
| 106 | * are countless ways this is incorrect, but it is better than nothing. |
| 107 | */ |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 108 | int btrfs_check_free_space(struct btrfs_root *root, u64 num_required, |
| 109 | int for_del) |
| 110 | { |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 111 | u64 total; |
| 112 | u64 used; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 113 | u64 thresh; |
| 114 | int ret = 0; |
| 115 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 116 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 117 | total = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 118 | used = btrfs_super_bytes_used(&root->fs_info->super_copy); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 119 | if (for_del) |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 120 | thresh = total * 90; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 121 | else |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 122 | thresh = total * 85; |
| 123 | |
| 124 | do_div(thresh, 100); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 125 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 126 | if (used + root->fs_info->delalloc_bytes + num_required > thresh) |
| 127 | ret = -ENOSPC; |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 128 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 129 | return ret; |
| 130 | } |
| 131 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 132 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 133 | * this does all the hard work for inserting an inline extent into |
| 134 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 135 | * no overlapping inline items exist in the btree |
| 136 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 137 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 138 | struct btrfs_root *root, struct inode *inode, |
| 139 | u64 start, size_t size, size_t compressed_size, |
| 140 | struct page **compressed_pages) |
| 141 | { |
| 142 | struct btrfs_key key; |
| 143 | struct btrfs_path *path; |
| 144 | struct extent_buffer *leaf; |
| 145 | struct page *page = NULL; |
| 146 | char *kaddr; |
| 147 | unsigned long ptr; |
| 148 | struct btrfs_file_extent_item *ei; |
| 149 | int err = 0; |
| 150 | int ret; |
| 151 | size_t cur_size = size; |
| 152 | size_t datasize; |
| 153 | unsigned long offset; |
| 154 | int use_compress = 0; |
| 155 | |
| 156 | if (compressed_size && compressed_pages) { |
| 157 | use_compress = 1; |
| 158 | cur_size = compressed_size; |
| 159 | } |
| 160 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 161 | path = btrfs_alloc_path(); |
| 162 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 163 | return -ENOMEM; |
| 164 | |
| 165 | btrfs_set_trans_block_group(trans, inode); |
| 166 | |
| 167 | key.objectid = inode->i_ino; |
| 168 | key.offset = start; |
| 169 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 170 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 171 | |
| 172 | inode_add_bytes(inode, size); |
| 173 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 174 | datasize); |
| 175 | BUG_ON(ret); |
| 176 | if (ret) { |
| 177 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 178 | goto fail; |
| 179 | } |
| 180 | leaf = path->nodes[0]; |
| 181 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 182 | struct btrfs_file_extent_item); |
| 183 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 184 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 185 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 186 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 187 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 188 | ptr = btrfs_file_extent_inline_start(ei); |
| 189 | |
| 190 | if (use_compress) { |
| 191 | struct page *cpage; |
| 192 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 193 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 194 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 195 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 196 | PAGE_CACHE_SIZE); |
| 197 | |
| 198 | kaddr = kmap(cpage); |
| 199 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
| 200 | kunmap(cpage); |
| 201 | |
| 202 | i++; |
| 203 | ptr += cur_size; |
| 204 | compressed_size -= cur_size; |
| 205 | } |
| 206 | btrfs_set_file_extent_compression(leaf, ei, |
| 207 | BTRFS_COMPRESS_ZLIB); |
| 208 | } else { |
| 209 | page = find_get_page(inode->i_mapping, |
| 210 | start >> PAGE_CACHE_SHIFT); |
| 211 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 212 | kaddr = kmap_atomic(page, KM_USER0); |
| 213 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 214 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
| 215 | kunmap_atomic(kaddr, KM_USER0); |
| 216 | page_cache_release(page); |
| 217 | } |
| 218 | btrfs_mark_buffer_dirty(leaf); |
| 219 | btrfs_free_path(path); |
| 220 | |
| 221 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
| 222 | btrfs_update_inode(trans, root, inode); |
| 223 | return 0; |
| 224 | fail: |
| 225 | btrfs_free_path(path); |
| 226 | return err; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /* |
| 231 | * conditionally insert an inline extent into the file. This |
| 232 | * does the checks required to make sure the data is small enough |
| 233 | * to fit as an inline extent. |
| 234 | */ |
| 235 | static int cow_file_range_inline(struct btrfs_trans_handle *trans, |
| 236 | struct btrfs_root *root, |
| 237 | struct inode *inode, u64 start, u64 end, |
| 238 | size_t compressed_size, |
| 239 | struct page **compressed_pages) |
| 240 | { |
| 241 | u64 isize = i_size_read(inode); |
| 242 | u64 actual_end = min(end + 1, isize); |
| 243 | u64 inline_len = actual_end - start; |
| 244 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 245 | ~((u64)root->sectorsize - 1); |
| 246 | u64 hint_byte; |
| 247 | u64 data_len = inline_len; |
| 248 | int ret; |
| 249 | |
| 250 | if (compressed_size) |
| 251 | data_len = compressed_size; |
| 252 | |
| 253 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 254 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 255 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 256 | (!compressed_size && |
| 257 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 258 | end + 1 < isize || |
| 259 | data_len > root->fs_info->max_inline) { |
| 260 | return 1; |
| 261 | } |
| 262 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 263 | ret = btrfs_drop_extents(trans, root, inode, start, |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 264 | aligned_end, start, &hint_byte); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 265 | BUG_ON(ret); |
| 266 | |
| 267 | if (isize > actual_end) |
| 268 | inline_len = min_t(u64, isize, actual_end); |
| 269 | ret = insert_inline_extent(trans, root, inode, start, |
| 270 | inline_len, compressed_size, |
| 271 | compressed_pages); |
| 272 | BUG_ON(ret); |
| 273 | btrfs_drop_extent_cache(inode, start, aligned_end, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 277 | struct async_extent { |
| 278 | u64 start; |
| 279 | u64 ram_size; |
| 280 | u64 compressed_size; |
| 281 | struct page **pages; |
| 282 | unsigned long nr_pages; |
| 283 | struct list_head list; |
| 284 | }; |
| 285 | |
| 286 | struct async_cow { |
| 287 | struct inode *inode; |
| 288 | struct btrfs_root *root; |
| 289 | struct page *locked_page; |
| 290 | u64 start; |
| 291 | u64 end; |
| 292 | struct list_head extents; |
| 293 | struct btrfs_work work; |
| 294 | }; |
| 295 | |
| 296 | static noinline int add_async_extent(struct async_cow *cow, |
| 297 | u64 start, u64 ram_size, |
| 298 | u64 compressed_size, |
| 299 | struct page **pages, |
| 300 | unsigned long nr_pages) |
| 301 | { |
| 302 | struct async_extent *async_extent; |
| 303 | |
| 304 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
| 305 | async_extent->start = start; |
| 306 | async_extent->ram_size = ram_size; |
| 307 | async_extent->compressed_size = compressed_size; |
| 308 | async_extent->pages = pages; |
| 309 | async_extent->nr_pages = nr_pages; |
| 310 | list_add_tail(&async_extent->list, &cow->extents); |
| 311 | return 0; |
| 312 | } |
| 313 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 314 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 315 | * we create compressed extents in two phases. The first |
| 316 | * phase compresses a range of pages that have already been |
| 317 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 318 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 319 | * This is done inside an ordered work queue, and the compression |
| 320 | * is spread across many cpus. The actual IO submission is step |
| 321 | * two, and the ordered work queue takes care of making sure that |
| 322 | * happens in the same order things were put onto the queue by |
| 323 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 324 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 325 | * If this code finds it can't get good compression, it puts an |
| 326 | * entry onto the work queue to write the uncompressed bytes. This |
| 327 | * makes sure that both compressed inodes and uncompressed inodes |
| 328 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 329 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 330 | static noinline int compress_file_range(struct inode *inode, |
| 331 | struct page *locked_page, |
| 332 | u64 start, u64 end, |
| 333 | struct async_cow *async_cow, |
| 334 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 335 | { |
| 336 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 337 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 338 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 339 | u64 orig_start; |
| 340 | u64 disk_num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 341 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 342 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 343 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 344 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 345 | struct page **pages = NULL; |
| 346 | unsigned long nr_pages; |
| 347 | unsigned long nr_pages_ret = 0; |
| 348 | unsigned long total_compressed = 0; |
| 349 | unsigned long total_in = 0; |
| 350 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 351 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 352 | int i; |
| 353 | int will_compress; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 354 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 355 | orig_start = start; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 356 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 357 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 358 | again: |
| 359 | will_compress = 0; |
| 360 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 361 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 362 | |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 363 | /* |
| 364 | * we don't want to send crud past the end of i_size through |
| 365 | * compression, that's just a waste of CPU time. So, if the |
| 366 | * end of the file is before the start of our current |
| 367 | * requested range of bytes, we bail out to the uncompressed |
| 368 | * cleanup code that can deal with all of this. |
| 369 | * |
| 370 | * It isn't really the fastest way to fix things, but this is a |
| 371 | * very uncommon corner. |
| 372 | */ |
| 373 | if (actual_end <= start) |
| 374 | goto cleanup_and_bail_uncompressed; |
| 375 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 376 | total_compressed = actual_end - start; |
| 377 | |
| 378 | /* we want to make sure that amount of ram required to uncompress |
| 379 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 380 | * of a compressed extent to 128k. This is a crucial number |
| 381 | * because it also controls how easily we can spread reads across |
| 382 | * cpus for decompression. |
| 383 | * |
| 384 | * We also want to make sure the amount of IO required to do |
| 385 | * a random read is reasonably small, so we limit the size of |
| 386 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 387 | */ |
| 388 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 389 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 390 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 391 | disk_num_bytes = num_bytes; |
| 392 | total_in = 0; |
| 393 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 394 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 395 | /* |
| 396 | * we do compression for mount -o compress and when the |
| 397 | * inode has not been flagged as nocompress. This flag can |
| 398 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 399 | */ |
| 400 | if (!btrfs_test_flag(inode, NOCOMPRESS) && |
| 401 | btrfs_test_opt(root, COMPRESS)) { |
| 402 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 403 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 404 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 405 | ret = btrfs_zlib_compress_pages(inode->i_mapping, start, |
| 406 | total_compressed, pages, |
| 407 | nr_pages, &nr_pages_ret, |
| 408 | &total_in, |
| 409 | &total_compressed, |
| 410 | max_compressed); |
| 411 | |
| 412 | if (!ret) { |
| 413 | unsigned long offset = total_compressed & |
| 414 | (PAGE_CACHE_SIZE - 1); |
| 415 | struct page *page = pages[nr_pages_ret - 1]; |
| 416 | char *kaddr; |
| 417 | |
| 418 | /* zero the tail end of the last page, we might be |
| 419 | * sending it down to disk |
| 420 | */ |
| 421 | if (offset) { |
| 422 | kaddr = kmap_atomic(page, KM_USER0); |
| 423 | memset(kaddr + offset, 0, |
| 424 | PAGE_CACHE_SIZE - offset); |
| 425 | kunmap_atomic(kaddr, KM_USER0); |
| 426 | } |
| 427 | will_compress = 1; |
| 428 | } |
| 429 | } |
| 430 | if (start == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 431 | trans = btrfs_join_transaction(root, 1); |
| 432 | BUG_ON(!trans); |
| 433 | btrfs_set_trans_block_group(trans, inode); |
| 434 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 435 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 436 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 437 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 438 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 439 | */ |
| 440 | ret = cow_file_range_inline(trans, root, inode, |
| 441 | start, end, 0, NULL); |
| 442 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 443 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 444 | ret = cow_file_range_inline(trans, root, inode, |
| 445 | start, end, |
| 446 | total_compressed, pages); |
| 447 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 448 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 449 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 450 | /* |
| 451 | * inline extent creation worked, we don't need |
| 452 | * to create any more async work items. Unlock |
| 453 | * and free up our temp pages. |
| 454 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 455 | extent_clear_unlock_delalloc(inode, |
| 456 | &BTRFS_I(inode)->io_tree, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 457 | start, end, NULL, 1, 0, |
| 458 | 0, 1, 1, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 459 | ret = 0; |
| 460 | goto free_pages_out; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if (will_compress) { |
| 465 | /* |
| 466 | * we aren't doing an inline extent round the compressed size |
| 467 | * up to a block size boundary so the allocator does sane |
| 468 | * things |
| 469 | */ |
| 470 | total_compressed = (total_compressed + blocksize - 1) & |
| 471 | ~(blocksize - 1); |
| 472 | |
| 473 | /* |
| 474 | * one last check to make sure the compression is really a |
| 475 | * win, compare the page count read with the blocks on disk |
| 476 | */ |
| 477 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 478 | ~(PAGE_CACHE_SIZE - 1); |
| 479 | if (total_compressed >= total_in) { |
| 480 | will_compress = 0; |
| 481 | } else { |
| 482 | disk_num_bytes = total_compressed; |
| 483 | num_bytes = total_in; |
| 484 | } |
| 485 | } |
| 486 | if (!will_compress && pages) { |
| 487 | /* |
| 488 | * the compression code ran but failed to make things smaller, |
| 489 | * free any pages it allocated and our page pointer array |
| 490 | */ |
| 491 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 492 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 493 | page_cache_release(pages[i]); |
| 494 | } |
| 495 | kfree(pages); |
| 496 | pages = NULL; |
| 497 | total_compressed = 0; |
| 498 | nr_pages_ret = 0; |
| 499 | |
| 500 | /* flag the file so we don't compress in the future */ |
| 501 | btrfs_set_flag(inode, NOCOMPRESS); |
| 502 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 503 | if (will_compress) { |
| 504 | *num_added += 1; |
| 505 | |
| 506 | /* the async work queues will take care of doing actual |
| 507 | * allocation on disk for these compressed pages, |
| 508 | * and will submit them to the elevator. |
| 509 | */ |
| 510 | add_async_extent(async_cow, start, num_bytes, |
| 511 | total_compressed, pages, nr_pages_ret); |
| 512 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 513 | if (start + num_bytes < end && start + num_bytes < actual_end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 514 | start += num_bytes; |
| 515 | pages = NULL; |
| 516 | cond_resched(); |
| 517 | goto again; |
| 518 | } |
| 519 | } else { |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 520 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 521 | /* |
| 522 | * No compression, but we still need to write the pages in |
| 523 | * the file we've been given so far. redirty the locked |
| 524 | * page if it corresponds to our extent and set things up |
| 525 | * for the async work queue to run cow_file_range to do |
| 526 | * the normal delalloc dance |
| 527 | */ |
| 528 | if (page_offset(locked_page) >= start && |
| 529 | page_offset(locked_page) <= end) { |
| 530 | __set_page_dirty_nobuffers(locked_page); |
| 531 | /* unlocked later on in the async handlers */ |
| 532 | } |
| 533 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0); |
| 534 | *num_added += 1; |
| 535 | } |
| 536 | |
| 537 | out: |
| 538 | return 0; |
| 539 | |
| 540 | free_pages_out: |
| 541 | for (i = 0; i < nr_pages_ret; i++) { |
| 542 | WARN_ON(pages[i]->mapping); |
| 543 | page_cache_release(pages[i]); |
| 544 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 545 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 546 | |
| 547 | goto out; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * phase two of compressed writeback. This is the ordered portion |
| 552 | * of the code, which only gets called in the order the work was |
| 553 | * queued. We walk all the async extents created by compress_file_range |
| 554 | * and send them down to the disk. |
| 555 | */ |
| 556 | static noinline int submit_compressed_extents(struct inode *inode, |
| 557 | struct async_cow *async_cow) |
| 558 | { |
| 559 | struct async_extent *async_extent; |
| 560 | u64 alloc_hint = 0; |
| 561 | struct btrfs_trans_handle *trans; |
| 562 | struct btrfs_key ins; |
| 563 | struct extent_map *em; |
| 564 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 565 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 566 | struct extent_io_tree *io_tree; |
| 567 | int ret; |
| 568 | |
| 569 | if (list_empty(&async_cow->extents)) |
| 570 | return 0; |
| 571 | |
| 572 | trans = btrfs_join_transaction(root, 1); |
| 573 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 574 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 575 | async_extent = list_entry(async_cow->extents.next, |
| 576 | struct async_extent, list); |
| 577 | list_del(&async_extent->list); |
| 578 | |
| 579 | io_tree = &BTRFS_I(inode)->io_tree; |
| 580 | |
| 581 | /* did the compression code fall back to uncompressed IO? */ |
| 582 | if (!async_extent->pages) { |
| 583 | int page_started = 0; |
| 584 | unsigned long nr_written = 0; |
| 585 | |
| 586 | lock_extent(io_tree, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 587 | async_extent->start + |
| 588 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 589 | |
| 590 | /* allocate blocks */ |
| 591 | cow_file_range(inode, async_cow->locked_page, |
| 592 | async_extent->start, |
| 593 | async_extent->start + |
| 594 | async_extent->ram_size - 1, |
| 595 | &page_started, &nr_written, 0); |
| 596 | |
| 597 | /* |
| 598 | * if page_started, cow_file_range inserted an |
| 599 | * inline extent and took care of all the unlocking |
| 600 | * and IO for us. Otherwise, we need to submit |
| 601 | * all those pages down to the drive. |
| 602 | */ |
| 603 | if (!page_started) |
| 604 | extent_write_locked_range(io_tree, |
| 605 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 606 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 607 | async_extent->ram_size - 1, |
| 608 | btrfs_get_extent, |
| 609 | WB_SYNC_ALL); |
| 610 | kfree(async_extent); |
| 611 | cond_resched(); |
| 612 | continue; |
| 613 | } |
| 614 | |
| 615 | lock_extent(io_tree, async_extent->start, |
| 616 | async_extent->start + async_extent->ram_size - 1, |
| 617 | GFP_NOFS); |
| 618 | /* |
| 619 | * here we're doing allocation and writeback of the |
| 620 | * compressed pages |
| 621 | */ |
| 622 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 623 | async_extent->start + |
| 624 | async_extent->ram_size - 1, 0); |
| 625 | |
| 626 | ret = btrfs_reserve_extent(trans, root, |
| 627 | async_extent->compressed_size, |
| 628 | async_extent->compressed_size, |
| 629 | 0, alloc_hint, |
| 630 | (u64)-1, &ins, 1); |
| 631 | BUG_ON(ret); |
| 632 | em = alloc_extent_map(GFP_NOFS); |
| 633 | em->start = async_extent->start; |
| 634 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 635 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 636 | |
| 637 | em->block_start = ins.objectid; |
| 638 | em->block_len = ins.offset; |
| 639 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 640 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 641 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 642 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 643 | while (1) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 644 | spin_lock(&em_tree->lock); |
| 645 | ret = add_extent_mapping(em_tree, em); |
| 646 | spin_unlock(&em_tree->lock); |
| 647 | if (ret != -EEXIST) { |
| 648 | free_extent_map(em); |
| 649 | break; |
| 650 | } |
| 651 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 652 | async_extent->start + |
| 653 | async_extent->ram_size - 1, 0); |
| 654 | } |
| 655 | |
| 656 | ret = btrfs_add_ordered_extent(inode, async_extent->start, |
| 657 | ins.objectid, |
| 658 | async_extent->ram_size, |
| 659 | ins.offset, |
| 660 | BTRFS_ORDERED_COMPRESSED); |
| 661 | BUG_ON(ret); |
| 662 | |
| 663 | btrfs_end_transaction(trans, root); |
| 664 | |
| 665 | /* |
| 666 | * clear dirty, set writeback and unlock the pages. |
| 667 | */ |
| 668 | extent_clear_unlock_delalloc(inode, |
| 669 | &BTRFS_I(inode)->io_tree, |
| 670 | async_extent->start, |
| 671 | async_extent->start + |
| 672 | async_extent->ram_size - 1, |
| 673 | NULL, 1, 1, 0, 1, 1, 0); |
| 674 | |
| 675 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 676 | async_extent->start, |
| 677 | async_extent->ram_size, |
| 678 | ins.objectid, |
| 679 | ins.offset, async_extent->pages, |
| 680 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 681 | |
| 682 | BUG_ON(ret); |
| 683 | trans = btrfs_join_transaction(root, 1); |
| 684 | alloc_hint = ins.objectid + ins.offset; |
| 685 | kfree(async_extent); |
| 686 | cond_resched(); |
| 687 | } |
| 688 | |
| 689 | btrfs_end_transaction(trans, root); |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * when extent_io.c finds a delayed allocation range in the file, |
| 695 | * the call backs end up in this code. The basic idea is to |
| 696 | * allocate extents on disk for the range, and create ordered data structs |
| 697 | * in ram to track those extents. |
| 698 | * |
| 699 | * locked_page is the page that writepage had locked already. We use |
| 700 | * it to make sure we don't do extra locks or unlocks. |
| 701 | * |
| 702 | * *page_started is set to one if we unlock locked_page and do everything |
| 703 | * required to start IO on it. It may be clean and already done with |
| 704 | * IO when we return. |
| 705 | */ |
| 706 | static noinline int cow_file_range(struct inode *inode, |
| 707 | struct page *locked_page, |
| 708 | u64 start, u64 end, int *page_started, |
| 709 | unsigned long *nr_written, |
| 710 | int unlock) |
| 711 | { |
| 712 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 713 | struct btrfs_trans_handle *trans; |
| 714 | u64 alloc_hint = 0; |
| 715 | u64 num_bytes; |
| 716 | unsigned long ram_size; |
| 717 | u64 disk_num_bytes; |
| 718 | u64 cur_alloc_size; |
| 719 | u64 blocksize = root->sectorsize; |
| 720 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 721 | u64 isize = i_size_read(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 722 | struct btrfs_key ins; |
| 723 | struct extent_map *em; |
| 724 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 725 | int ret = 0; |
| 726 | |
| 727 | trans = btrfs_join_transaction(root, 1); |
| 728 | BUG_ON(!trans); |
| 729 | btrfs_set_trans_block_group(trans, inode); |
| 730 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 731 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 732 | |
| 733 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 734 | num_bytes = max(blocksize, num_bytes); |
| 735 | disk_num_bytes = num_bytes; |
| 736 | ret = 0; |
| 737 | |
| 738 | if (start == 0) { |
| 739 | /* lets try to make an inline extent */ |
| 740 | ret = cow_file_range_inline(trans, root, inode, |
| 741 | start, end, 0, NULL); |
| 742 | if (ret == 0) { |
| 743 | extent_clear_unlock_delalloc(inode, |
| 744 | &BTRFS_I(inode)->io_tree, |
| 745 | start, end, NULL, 1, 1, |
| 746 | 1, 1, 1, 1); |
| 747 | *nr_written = *nr_written + |
| 748 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 749 | *page_started = 1; |
| 750 | ret = 0; |
| 751 | goto out; |
| 752 | } |
| 753 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 754 | |
| 755 | BUG_ON(disk_num_bytes > |
| 756 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 757 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 758 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 759 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 760 | while (disk_num_bytes > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 761 | cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 762 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 763 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 764 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 765 | BUG_ON(ret); |
| 766 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 767 | em = alloc_extent_map(GFP_NOFS); |
| 768 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 769 | em->orig_start = em->start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 770 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 771 | ram_size = ins.offset; |
| 772 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 773 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 774 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 775 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 776 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 777 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 778 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 779 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 780 | spin_lock(&em_tree->lock); |
| 781 | ret = add_extent_mapping(em_tree, em); |
| 782 | spin_unlock(&em_tree->lock); |
| 783 | if (ret != -EEXIST) { |
| 784 | free_extent_map(em); |
| 785 | break; |
| 786 | } |
| 787 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 788 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 789 | } |
| 790 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 791 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 792 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 793 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 794 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 795 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 796 | if (root->root_key.objectid == |
| 797 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 798 | ret = btrfs_reloc_clone_csums(inode, start, |
| 799 | cur_alloc_size); |
| 800 | BUG_ON(ret); |
| 801 | } |
| 802 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 803 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 804 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 805 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 806 | /* we're not doing compressed IO, don't unlock the first |
| 807 | * page (which the caller expects to stay locked), don't |
| 808 | * clear any dirty bits and don't set any writeback bits |
| 809 | */ |
| 810 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 811 | start, start + ram_size - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 812 | locked_page, unlock, 1, |
| 813 | 1, 0, 0, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 814 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 815 | num_bytes -= cur_alloc_size; |
| 816 | alloc_hint = ins.objectid + ins.offset; |
| 817 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 818 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 819 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 820 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 821 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 822 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 823 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 824 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 825 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 826 | /* |
| 827 | * work queue call back to started compression on a file and pages |
| 828 | */ |
| 829 | static noinline void async_cow_start(struct btrfs_work *work) |
| 830 | { |
| 831 | struct async_cow *async_cow; |
| 832 | int num_added = 0; |
| 833 | async_cow = container_of(work, struct async_cow, work); |
| 834 | |
| 835 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 836 | async_cow->start, async_cow->end, async_cow, |
| 837 | &num_added); |
| 838 | if (num_added == 0) |
| 839 | async_cow->inode = NULL; |
| 840 | } |
| 841 | |
| 842 | /* |
| 843 | * work queue call back to submit previously compressed pages |
| 844 | */ |
| 845 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 846 | { |
| 847 | struct async_cow *async_cow; |
| 848 | struct btrfs_root *root; |
| 849 | unsigned long nr_pages; |
| 850 | |
| 851 | async_cow = container_of(work, struct async_cow, work); |
| 852 | |
| 853 | root = async_cow->root; |
| 854 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 855 | PAGE_CACHE_SHIFT; |
| 856 | |
| 857 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 858 | |
| 859 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 860 | 5 * 1042 * 1024 && |
| 861 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 862 | wake_up(&root->fs_info->async_submit_wait); |
| 863 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 864 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 865 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 866 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 867 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 868 | static noinline void async_cow_free(struct btrfs_work *work) |
| 869 | { |
| 870 | struct async_cow *async_cow; |
| 871 | async_cow = container_of(work, struct async_cow, work); |
| 872 | kfree(async_cow); |
| 873 | } |
| 874 | |
| 875 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 876 | u64 start, u64 end, int *page_started, |
| 877 | unsigned long *nr_written) |
| 878 | { |
| 879 | struct async_cow *async_cow; |
| 880 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 881 | unsigned long nr_pages; |
| 882 | u64 cur_end; |
| 883 | int limit = 10 * 1024 * 1042; |
| 884 | |
| 885 | if (!btrfs_test_opt(root, COMPRESS)) { |
| 886 | return cow_file_range(inode, locked_page, start, end, |
| 887 | page_started, nr_written, 1); |
| 888 | } |
| 889 | |
| 890 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED | |
| 891 | EXTENT_DELALLOC, 1, 0, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 892 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 893 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
| 894 | async_cow->inode = inode; |
| 895 | async_cow->root = root; |
| 896 | async_cow->locked_page = locked_page; |
| 897 | async_cow->start = start; |
| 898 | |
| 899 | if (btrfs_test_flag(inode, NOCOMPRESS)) |
| 900 | cur_end = end; |
| 901 | else |
| 902 | cur_end = min(end, start + 512 * 1024 - 1); |
| 903 | |
| 904 | async_cow->end = cur_end; |
| 905 | INIT_LIST_HEAD(&async_cow->extents); |
| 906 | |
| 907 | async_cow->work.func = async_cow_start; |
| 908 | async_cow->work.ordered_func = async_cow_submit; |
| 909 | async_cow->work.ordered_free = async_cow_free; |
| 910 | async_cow->work.flags = 0; |
| 911 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 912 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 913 | PAGE_CACHE_SHIFT; |
| 914 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 915 | |
| 916 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 917 | &async_cow->work); |
| 918 | |
| 919 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 920 | wait_event(root->fs_info->async_submit_wait, |
| 921 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 922 | limit)); |
| 923 | } |
| 924 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 925 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 926 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 927 | wait_event(root->fs_info->async_submit_wait, |
| 928 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 929 | 0)); |
| 930 | } |
| 931 | |
| 932 | *nr_written += nr_pages; |
| 933 | start = cur_end + 1; |
| 934 | } |
| 935 | *page_started = 1; |
| 936 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 937 | } |
| 938 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 939 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 940 | u64 bytenr, u64 num_bytes) |
| 941 | { |
| 942 | int ret; |
| 943 | struct btrfs_ordered_sum *sums; |
| 944 | LIST_HEAD(list); |
| 945 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 946 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
| 947 | bytenr + num_bytes - 1, &list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 948 | if (ret == 0 && list_empty(&list)) |
| 949 | return 0; |
| 950 | |
| 951 | while (!list_empty(&list)) { |
| 952 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 953 | list_del(&sums->list); |
| 954 | kfree(sums); |
| 955 | } |
| 956 | return 1; |
| 957 | } |
| 958 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 959 | /* |
| 960 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 961 | * of the extents that exist in the file, and COWs the file as required. |
| 962 | * |
| 963 | * If no cow copies or snapshots exist, we write directly to the existing |
| 964 | * blocks on disk |
| 965 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 966 | static int run_delalloc_nocow(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 967 | u64 start, u64 end, int *page_started, int force, |
| 968 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 969 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 970 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 971 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 972 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 973 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 974 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 975 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 976 | u64 cow_start; |
| 977 | u64 cur_offset; |
| 978 | u64 extent_end; |
| 979 | u64 disk_bytenr; |
| 980 | u64 num_bytes; |
| 981 | int extent_type; |
| 982 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 983 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 984 | int nocow; |
| 985 | int check_prev = 1; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 986 | |
| 987 | path = btrfs_alloc_path(); |
| 988 | BUG_ON(!path); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 989 | trans = btrfs_join_transaction(root, 1); |
| 990 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 991 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 992 | cow_start = (u64)-1; |
| 993 | cur_offset = start; |
| 994 | while (1) { |
| 995 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 996 | cur_offset, 0); |
| 997 | BUG_ON(ret < 0); |
| 998 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 999 | leaf = path->nodes[0]; |
| 1000 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1001 | path->slots[0] - 1); |
| 1002 | if (found_key.objectid == inode->i_ino && |
| 1003 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1004 | path->slots[0]--; |
| 1005 | } |
| 1006 | check_prev = 0; |
| 1007 | next_slot: |
| 1008 | leaf = path->nodes[0]; |
| 1009 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1010 | ret = btrfs_next_leaf(root, path); |
| 1011 | if (ret < 0) |
| 1012 | BUG_ON(1); |
| 1013 | if (ret > 0) |
| 1014 | break; |
| 1015 | leaf = path->nodes[0]; |
| 1016 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1017 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1018 | nocow = 0; |
| 1019 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1020 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1021 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1022 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1023 | if (found_key.objectid > inode->i_ino || |
| 1024 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1025 | found_key.offset > end) |
| 1026 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1027 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1028 | if (found_key.offset > cur_offset) { |
| 1029 | extent_end = found_key.offset; |
| 1030 | goto out_check; |
| 1031 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1032 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1033 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1034 | struct btrfs_file_extent_item); |
| 1035 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1036 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1037 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1038 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1039 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 1040 | extent_end = found_key.offset + |
| 1041 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1042 | if (extent_end <= start) { |
| 1043 | path->slots[0]++; |
| 1044 | goto next_slot; |
| 1045 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1046 | if (disk_bytenr == 0) |
| 1047 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1048 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1049 | btrfs_file_extent_encryption(leaf, fi) || |
| 1050 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1051 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1052 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1053 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1054 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1055 | goto out_check; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1056 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
| 1057 | disk_bytenr)) |
| 1058 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1059 | disk_bytenr += btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1060 | disk_bytenr += cur_offset - found_key.offset; |
| 1061 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1062 | /* |
| 1063 | * force cow if csum exists in the range. |
| 1064 | * this ensure that csum for a given extent are |
| 1065 | * either valid or do not exist. |
| 1066 | */ |
| 1067 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1068 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1069 | nocow = 1; |
| 1070 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1071 | extent_end = found_key.offset + |
| 1072 | btrfs_file_extent_inline_len(leaf, fi); |
| 1073 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1074 | } else { |
| 1075 | BUG_ON(1); |
| 1076 | } |
| 1077 | out_check: |
| 1078 | if (extent_end <= start) { |
| 1079 | path->slots[0]++; |
| 1080 | goto next_slot; |
| 1081 | } |
| 1082 | if (!nocow) { |
| 1083 | if (cow_start == (u64)-1) |
| 1084 | cow_start = cur_offset; |
| 1085 | cur_offset = extent_end; |
| 1086 | if (cur_offset > end) |
| 1087 | break; |
| 1088 | path->slots[0]++; |
| 1089 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1090 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1091 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1092 | btrfs_release_path(root, path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1093 | if (cow_start != (u64)-1) { |
| 1094 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1095 | found_key.offset - 1, page_started, |
| 1096 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1097 | BUG_ON(ret); |
| 1098 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1099 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1100 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1101 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1102 | struct extent_map *em; |
| 1103 | struct extent_map_tree *em_tree; |
| 1104 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 1105 | em = alloc_extent_map(GFP_NOFS); |
| 1106 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1107 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1108 | em->len = num_bytes; |
| 1109 | em->block_len = num_bytes; |
| 1110 | em->block_start = disk_bytenr; |
| 1111 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1112 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1113 | while (1) { |
| 1114 | spin_lock(&em_tree->lock); |
| 1115 | ret = add_extent_mapping(em_tree, em); |
| 1116 | spin_unlock(&em_tree->lock); |
| 1117 | if (ret != -EEXIST) { |
| 1118 | free_extent_map(em); |
| 1119 | break; |
| 1120 | } |
| 1121 | btrfs_drop_extent_cache(inode, em->start, |
| 1122 | em->start + em->len - 1, 0); |
| 1123 | } |
| 1124 | type = BTRFS_ORDERED_PREALLOC; |
| 1125 | } else { |
| 1126 | type = BTRFS_ORDERED_NOCOW; |
| 1127 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1128 | |
| 1129 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1130 | num_bytes, num_bytes, type); |
| 1131 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1132 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1133 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 1134 | cur_offset, cur_offset + num_bytes - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1135 | locked_page, 1, 1, 1, 0, 0, 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1136 | cur_offset = extent_end; |
| 1137 | if (cur_offset > end) |
| 1138 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1139 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1140 | btrfs_release_path(root, path); |
| 1141 | |
| 1142 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1143 | cow_start = cur_offset; |
| 1144 | if (cow_start != (u64)-1) { |
| 1145 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1146 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1147 | BUG_ON(ret); |
| 1148 | } |
| 1149 | |
| 1150 | ret = btrfs_end_transaction(trans, root); |
| 1151 | BUG_ON(ret); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1152 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1153 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1154 | } |
| 1155 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1156 | /* |
| 1157 | * extent_io.c call back to do delayed allocation processing |
| 1158 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1159 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1160 | u64 start, u64 end, int *page_started, |
| 1161 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1162 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1163 | int ret; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1164 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1165 | if (btrfs_test_flag(inode, NODATACOW)) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1166 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1167 | page_started, 1, nr_written); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1168 | else if (btrfs_test_flag(inode, PREALLOC)) |
| 1169 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1170 | page_started, 0, nr_written); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1171 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1172 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1173 | page_started, nr_written); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1174 | |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1175 | return ret; |
| 1176 | } |
| 1177 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1178 | /* |
| 1179 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1180 | * bytes in this file, and to maintain the list of inodes that |
| 1181 | * have pending delalloc work to be done. |
| 1182 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1183 | 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] | 1184 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1185 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1186 | /* |
| 1187 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1188 | * but in this case, we are only testeing for the DELALLOC |
| 1189 | * bit, which is only set or cleared with irqs on |
| 1190 | */ |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1191 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1192 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1193 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1194 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1195 | root->fs_info->delalloc_bytes += end - start + 1; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1196 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1197 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1198 | &root->fs_info->delalloc_inodes); |
| 1199 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1200 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1201 | } |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1205 | /* |
| 1206 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1207 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1208 | static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1209 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1210 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1211 | /* |
| 1212 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1213 | * but in this case, we are only testeing for the DELALLOC |
| 1214 | * bit, which is only set or cleared with irqs on |
| 1215 | */ |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1216 | if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1217 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1218 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1219 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1220 | if (end - start + 1 > root->fs_info->delalloc_bytes) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1221 | printk(KERN_INFO "btrfs warning: delalloc account " |
| 1222 | "%llu %llu\n", |
| 1223 | (unsigned long long)end - start + 1, |
| 1224 | (unsigned long long) |
| 1225 | root->fs_info->delalloc_bytes); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1226 | root->fs_info->delalloc_bytes = 0; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1227 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1228 | } else { |
| 1229 | root->fs_info->delalloc_bytes -= end - start + 1; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1230 | BTRFS_I(inode)->delalloc_bytes -= end - start + 1; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1231 | } |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1232 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
| 1233 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1234 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1235 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1236 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1237 | } |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1241 | /* |
| 1242 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1243 | * we don't create bios that span stripes or chunks |
| 1244 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1245 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1246 | size_t size, struct bio *bio, |
| 1247 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1248 | { |
| 1249 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1250 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1251 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1252 | u64 length = 0; |
| 1253 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1254 | int ret; |
| 1255 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1256 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1257 | return 0; |
| 1258 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1259 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1260 | map_tree = &root->fs_info->mapping_tree; |
| 1261 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1262 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1263 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1264 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1265 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1266 | return 1; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1270 | /* |
| 1271 | * in order to insert checksums into the metadata in large chunks, |
| 1272 | * we wait until bio submission time. All the pages in the bio are |
| 1273 | * checksummed and sums are attached onto the ordered extent record. |
| 1274 | * |
| 1275 | * At IO completion time the cums attached on the ordered extent record |
| 1276 | * are inserted into the btree |
| 1277 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1278 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1279 | struct bio *bio, int mirror_num, |
| 1280 | unsigned long bio_flags) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1281 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1282 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1283 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1284 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1285 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1286 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1287 | return 0; |
| 1288 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1289 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1290 | /* |
| 1291 | * in order to insert checksums into the metadata in large chunks, |
| 1292 | * we wait until bio submission time. All the pages in the bio are |
| 1293 | * checksummed and sums are attached onto the ordered extent record. |
| 1294 | * |
| 1295 | * At IO completion time the cums attached on the ordered extent record |
| 1296 | * are inserted into the btree |
| 1297 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1298 | 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] | 1299 | int mirror_num, unsigned long bio_flags) |
| 1300 | { |
| 1301 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1302 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1303 | } |
| 1304 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1305 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1306 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1307 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1308 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1309 | 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] | 1310 | int mirror_num, unsigned long bio_flags) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1311 | { |
| 1312 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1313 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1314 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1315 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1316 | skip_sum = btrfs_test_flag(inode, NODATASUM); |
| 1317 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1318 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 1319 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1320 | |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1321 | if (!(rw & (1 << BIO_RW))) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1322 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1323 | return btrfs_submit_compressed_read(inode, bio, |
| 1324 | mirror_num, bio_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1325 | } else if (!skip_sum) |
| 1326 | btrfs_lookup_bio_sums(root, inode, bio, NULL); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1327 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1328 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1329 | /* csum items have already been cloned */ |
| 1330 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1331 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1332 | /* we're doing a write, do the async checksumming */ |
| 1333 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1334 | inode, rw, bio, mirror_num, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1335 | bio_flags, __btrfs_submit_bio_start, |
| 1336 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1337 | } |
| 1338 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1339 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1340 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1341 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1342 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1343 | /* |
| 1344 | * given a list of ordered sums record them in the inode. This happens |
| 1345 | * at IO completion time based on sums calculated at bio submission time. |
| 1346 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1347 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1348 | struct inode *inode, u64 file_offset, |
| 1349 | struct list_head *list) |
| 1350 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1351 | struct btrfs_ordered_sum *sum; |
| 1352 | |
| 1353 | btrfs_set_trans_block_group(trans, inode); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1354 | |
| 1355 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1356 | btrfs_csum_file_blocks(trans, |
| 1357 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1358 | } |
| 1359 | return 0; |
| 1360 | } |
| 1361 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1362 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end) |
| 1363 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1364 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1365 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1366 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
| 1367 | GFP_NOFS); |
| 1368 | } |
| 1369 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1370 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1371 | struct btrfs_writepage_fixup { |
| 1372 | struct page *page; |
| 1373 | struct btrfs_work work; |
| 1374 | }; |
| 1375 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1376 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1377 | { |
| 1378 | struct btrfs_writepage_fixup *fixup; |
| 1379 | struct btrfs_ordered_extent *ordered; |
| 1380 | struct page *page; |
| 1381 | struct inode *inode; |
| 1382 | u64 page_start; |
| 1383 | u64 page_end; |
| 1384 | |
| 1385 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1386 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1387 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1388 | lock_page(page); |
| 1389 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1390 | ClearPageChecked(page); |
| 1391 | goto out_page; |
| 1392 | } |
| 1393 | |
| 1394 | inode = page->mapping->host; |
| 1395 | page_start = page_offset(page); |
| 1396 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1397 | |
| 1398 | 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] | 1399 | |
| 1400 | /* already ordered? We're done */ |
| 1401 | if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1402 | EXTENT_ORDERED, 0)) { |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1403 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1407 | if (ordered) { |
| 1408 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, |
| 1409 | page_end, GFP_NOFS); |
| 1410 | unlock_page(page); |
| 1411 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1412 | goto again; |
| 1413 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1414 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1415 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1416 | ClearPageChecked(page); |
| 1417 | out: |
| 1418 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); |
| 1419 | out_page: |
| 1420 | unlock_page(page); |
| 1421 | page_cache_release(page); |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * There are a few paths in the higher layers of the kernel that directly |
| 1426 | * set the page dirty bit without asking the filesystem if it is a |
| 1427 | * good idea. This causes problems because we want to make sure COW |
| 1428 | * properly happens and the data=ordered rules are followed. |
| 1429 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1430 | * 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] | 1431 | * hasn't been properly setup for IO. We kick off an async process |
| 1432 | * to fix it up. The async helper will wait for ordered extents, set |
| 1433 | * the delalloc bit and make it safe to write the page. |
| 1434 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1435 | 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] | 1436 | { |
| 1437 | struct inode *inode = page->mapping->host; |
| 1438 | struct btrfs_writepage_fixup *fixup; |
| 1439 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1440 | int ret; |
| 1441 | |
| 1442 | ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1443 | EXTENT_ORDERED, 0); |
| 1444 | if (ret) |
| 1445 | return 0; |
| 1446 | |
| 1447 | if (PageChecked(page)) |
| 1448 | return -EAGAIN; |
| 1449 | |
| 1450 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1451 | if (!fixup) |
| 1452 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1453 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1454 | SetPageChecked(page); |
| 1455 | page_cache_get(page); |
| 1456 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1457 | fixup->page = page; |
| 1458 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1459 | return -EAGAIN; |
| 1460 | } |
| 1461 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1462 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1463 | struct inode *inode, u64 file_pos, |
| 1464 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1465 | u64 num_bytes, u64 ram_bytes, |
| 1466 | u8 compression, u8 encryption, |
| 1467 | u16 other_encoding, int extent_type) |
| 1468 | { |
| 1469 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1470 | struct btrfs_file_extent_item *fi; |
| 1471 | struct btrfs_path *path; |
| 1472 | struct extent_buffer *leaf; |
| 1473 | struct btrfs_key ins; |
| 1474 | u64 hint; |
| 1475 | int ret; |
| 1476 | |
| 1477 | path = btrfs_alloc_path(); |
| 1478 | BUG_ON(!path); |
| 1479 | |
| 1480 | ret = btrfs_drop_extents(trans, root, inode, file_pos, |
| 1481 | file_pos + num_bytes, file_pos, &hint); |
| 1482 | BUG_ON(ret); |
| 1483 | |
| 1484 | ins.objectid = inode->i_ino; |
| 1485 | ins.offset = file_pos; |
| 1486 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1487 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1488 | BUG_ON(ret); |
| 1489 | leaf = path->nodes[0]; |
| 1490 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1491 | struct btrfs_file_extent_item); |
| 1492 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1493 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1494 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1495 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1496 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1497 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1498 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1499 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1500 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1501 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
| 1502 | btrfs_mark_buffer_dirty(leaf); |
| 1503 | |
| 1504 | inode_add_bytes(inode, num_bytes); |
| 1505 | btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0); |
| 1506 | |
| 1507 | ins.objectid = disk_bytenr; |
| 1508 | ins.offset = disk_num_bytes; |
| 1509 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 1510 | ret = btrfs_alloc_reserved_extent(trans, root, leaf->start, |
| 1511 | root->root_key.objectid, |
| 1512 | trans->transid, inode->i_ino, &ins); |
| 1513 | BUG_ON(ret); |
| 1514 | |
| 1515 | btrfs_free_path(path); |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1519 | /* as ordered data IO finishes, this gets called so we can finish |
| 1520 | * an ordered extent if the range of bytes in the file it covers are |
| 1521 | * fully written. |
| 1522 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1523 | 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] | 1524 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1525 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1526 | struct btrfs_trans_handle *trans; |
| 1527 | struct btrfs_ordered_extent *ordered_extent; |
| 1528 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1529 | int compressed = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1530 | int ret; |
| 1531 | |
| 1532 | ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1533 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1534 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1535 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 1536 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1537 | |
| 1538 | ordered_extent = btrfs_lookup_ordered_extent(inode, start); |
| 1539 | BUG_ON(!ordered_extent); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1540 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) |
| 1541 | goto nocow; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1542 | |
| 1543 | lock_extent(io_tree, ordered_extent->file_offset, |
| 1544 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1545 | GFP_NOFS); |
| 1546 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1547 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1548 | compressed = 1; |
| 1549 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 1550 | BUG_ON(compressed); |
| 1551 | ret = btrfs_mark_extent_written(trans, root, inode, |
| 1552 | ordered_extent->file_offset, |
| 1553 | ordered_extent->file_offset + |
| 1554 | ordered_extent->len); |
| 1555 | BUG_ON(ret); |
| 1556 | } else { |
| 1557 | ret = insert_reserved_file_extent(trans, inode, |
| 1558 | ordered_extent->file_offset, |
| 1559 | ordered_extent->start, |
| 1560 | ordered_extent->disk_len, |
| 1561 | ordered_extent->len, |
| 1562 | ordered_extent->len, |
| 1563 | compressed, 0, 0, |
| 1564 | BTRFS_FILE_EXTENT_REG); |
| 1565 | BUG_ON(ret); |
| 1566 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1567 | unlock_extent(io_tree, ordered_extent->file_offset, |
| 1568 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1569 | GFP_NOFS); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1570 | nocow: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1571 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1572 | &ordered_extent->list); |
| 1573 | |
Chris Mason | 3435302 | 2008-09-23 20:19:49 -0400 | [diff] [blame] | 1574 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 1575 | btrfs_ordered_update_i_size(inode, ordered_extent); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1576 | btrfs_update_inode(trans, root, inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1577 | btrfs_remove_ordered_extent(inode, ordered_extent); |
Chris Mason | 3435302 | 2008-09-23 20:19:49 -0400 | [diff] [blame] | 1578 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 1579 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1580 | /* once for us */ |
| 1581 | btrfs_put_ordered_extent(ordered_extent); |
| 1582 | /* once for the tree */ |
| 1583 | btrfs_put_ordered_extent(ordered_extent); |
| 1584 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1585 | btrfs_end_transaction(trans, root); |
| 1586 | return 0; |
| 1587 | } |
| 1588 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1589 | 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] | 1590 | struct extent_state *state, int uptodate) |
| 1591 | { |
| 1592 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1593 | } |
| 1594 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1595 | /* |
| 1596 | * When IO fails, either with EIO or csum verification fails, we |
| 1597 | * try other mirrors that might have a good copy of the data. This |
| 1598 | * io_failure_record is used to record state as we go through all the |
| 1599 | * mirrors. If another mirror has good data, the page is set up to date |
| 1600 | * and things continue. If a good mirror can't be found, the original |
| 1601 | * bio end_io callback is called to indicate things have failed. |
| 1602 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1603 | struct io_failure_record { |
| 1604 | struct page *page; |
| 1605 | u64 start; |
| 1606 | u64 len; |
| 1607 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1608 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1609 | int last_mirror; |
| 1610 | }; |
| 1611 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1612 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1613 | struct page *page, u64 start, u64 end, |
| 1614 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1615 | { |
| 1616 | struct io_failure_record *failrec = NULL; |
| 1617 | u64 private; |
| 1618 | struct extent_map *em; |
| 1619 | struct inode *inode = page->mapping->host; |
| 1620 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1621 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1622 | struct bio *bio; |
| 1623 | int num_copies; |
| 1624 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1625 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1626 | u64 logical; |
| 1627 | |
| 1628 | ret = get_state_private(failure_tree, start, &private); |
| 1629 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1630 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1631 | if (!failrec) |
| 1632 | return -ENOMEM; |
| 1633 | failrec->start = start; |
| 1634 | failrec->len = end - start + 1; |
| 1635 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1636 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1637 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1638 | spin_lock(&em_tree->lock); |
| 1639 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1640 | if (em->start > start || em->start + em->len < start) { |
| 1641 | free_extent_map(em); |
| 1642 | em = NULL; |
| 1643 | } |
| 1644 | spin_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1645 | |
| 1646 | if (!em || IS_ERR(em)) { |
| 1647 | kfree(failrec); |
| 1648 | return -EIO; |
| 1649 | } |
| 1650 | logical = start - em->start; |
| 1651 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1652 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1653 | logical = em->block_start; |
| 1654 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 1655 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1656 | failrec->logical = logical; |
| 1657 | free_extent_map(em); |
| 1658 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1659 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1660 | set_state_private(failure_tree, start, |
| 1661 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1662 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1663 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1664 | } |
| 1665 | num_copies = btrfs_num_copies( |
| 1666 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1667 | failrec->logical, failrec->len); |
| 1668 | failrec->last_mirror++; |
| 1669 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1670 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1671 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1672 | failrec->start, |
| 1673 | EXTENT_LOCKED); |
| 1674 | if (state && state->start != failrec->start) |
| 1675 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1676 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1677 | } |
| 1678 | if (!state || failrec->last_mirror > num_copies) { |
| 1679 | set_state_private(failure_tree, failrec->start, 0); |
| 1680 | clear_extent_bits(failure_tree, failrec->start, |
| 1681 | failrec->start + failrec->len - 1, |
| 1682 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1683 | kfree(failrec); |
| 1684 | return -EIO; |
| 1685 | } |
| 1686 | bio = bio_alloc(GFP_NOFS, 1); |
| 1687 | bio->bi_private = state; |
| 1688 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1689 | bio->bi_sector = failrec->logical >> 9; |
| 1690 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1691 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1692 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1693 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1694 | if (failed_bio->bi_rw & (1 << BIO_RW)) |
| 1695 | rw = WRITE; |
| 1696 | else |
| 1697 | rw = READ; |
| 1698 | |
| 1699 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1700 | failrec->last_mirror, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1701 | failrec->bio_flags); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1702 | return 0; |
| 1703 | } |
| 1704 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1705 | /* |
| 1706 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1707 | * to see if we need to process or clean up an io_failure_record |
| 1708 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1709 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1710 | { |
| 1711 | u64 private; |
| 1712 | u64 private_failure; |
| 1713 | struct io_failure_record *failure; |
| 1714 | int ret; |
| 1715 | |
| 1716 | private = 0; |
| 1717 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 1718 | (u64)-1, 1, EXTENT_DIRTY)) { |
| 1719 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1720 | start, &private_failure); |
| 1721 | if (ret == 0) { |
| 1722 | failure = (struct io_failure_record *)(unsigned long) |
| 1723 | private_failure; |
| 1724 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1725 | failure->start, 0); |
| 1726 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1727 | failure->start, |
| 1728 | failure->start + failure->len - 1, |
| 1729 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1730 | GFP_NOFS); |
| 1731 | kfree(failure); |
| 1732 | } |
| 1733 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1734 | return 0; |
| 1735 | } |
| 1736 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1737 | /* |
| 1738 | * when reads are done, we need to check csums to verify the data is correct |
| 1739 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1740 | * the io_failure_record routines to find good copies |
| 1741 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1742 | 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] | 1743 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1744 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1745 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1746 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1747 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1748 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1749 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1750 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1751 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1752 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1753 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1754 | if (PageChecked(page)) { |
| 1755 | ClearPageChecked(page); |
| 1756 | goto good; |
| 1757 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1758 | if (btrfs_test_flag(inode, NODATASUM)) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1759 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1760 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1761 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 1762 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) { |
| 1763 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1764 | GFP_NOFS); |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1768 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1769 | private = state->private; |
| 1770 | ret = 0; |
| 1771 | } else { |
| 1772 | ret = get_state_private(io_tree, start, &private); |
| 1773 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1774 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1775 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1776 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1777 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1778 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1779 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1780 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1781 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1782 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1783 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1784 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1785 | /* if the io failure tree for this inode is non-empty, |
| 1786 | * check to see if we've recovered from a failed IO |
| 1787 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1788 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1789 | return 0; |
| 1790 | |
| 1791 | zeroit: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1792 | printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " |
| 1793 | "private %llu\n", page->mapping->host->i_ino, |
| 1794 | (unsigned long long)start, csum, |
| 1795 | (unsigned long long)private); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1796 | memset(kaddr + offset, 1, end - start + 1); |
| 1797 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1798 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1799 | if (private == 0) |
| 1800 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1801 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1802 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1803 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1804 | /* |
| 1805 | * This creates an orphan entry for the given inode in case something goes |
| 1806 | * wrong in the middle of an unlink/truncate. |
| 1807 | */ |
| 1808 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 1809 | { |
| 1810 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1811 | int ret = 0; |
| 1812 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1813 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1814 | |
| 1815 | /* already on the orphan list, we're good */ |
| 1816 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1817 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1818 | return 0; |
| 1819 | } |
| 1820 | |
| 1821 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 1822 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1823 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1824 | |
| 1825 | /* |
| 1826 | * insert an orphan item to track this unlinked/truncated file |
| 1827 | */ |
| 1828 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 1829 | |
| 1830 | return ret; |
| 1831 | } |
| 1832 | |
| 1833 | /* |
| 1834 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 1835 | * item for this particular inode. |
| 1836 | */ |
| 1837 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 1838 | { |
| 1839 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1840 | int ret = 0; |
| 1841 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1842 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1843 | |
| 1844 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1845 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1846 | return 0; |
| 1847 | } |
| 1848 | |
| 1849 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 1850 | if (!trans) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1851 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1852 | return 0; |
| 1853 | } |
| 1854 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1855 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1856 | |
| 1857 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 1858 | |
| 1859 | return ret; |
| 1860 | } |
| 1861 | |
| 1862 | /* |
| 1863 | * this cleans up any orphans that may be left on the list from the last use |
| 1864 | * of this root. |
| 1865 | */ |
| 1866 | void btrfs_orphan_cleanup(struct btrfs_root *root) |
| 1867 | { |
| 1868 | struct btrfs_path *path; |
| 1869 | struct extent_buffer *leaf; |
| 1870 | struct btrfs_item *item; |
| 1871 | struct btrfs_key key, found_key; |
| 1872 | struct btrfs_trans_handle *trans; |
| 1873 | struct inode *inode; |
| 1874 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 1875 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1876 | path = btrfs_alloc_path(); |
| 1877 | if (!path) |
| 1878 | return; |
| 1879 | path->reada = -1; |
| 1880 | |
| 1881 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 1882 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 1883 | key.offset = (u64)-1; |
| 1884 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1885 | |
| 1886 | while (1) { |
| 1887 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1888 | if (ret < 0) { |
| 1889 | printk(KERN_ERR "Error searching slot for orphan: %d" |
| 1890 | "\n", ret); |
| 1891 | break; |
| 1892 | } |
| 1893 | |
| 1894 | /* |
| 1895 | * if ret == 0 means we found what we were searching for, which |
| 1896 | * is weird, but possible, so only screw with path if we didnt |
| 1897 | * find the key and see if we have stuff that matches |
| 1898 | */ |
| 1899 | if (ret > 0) { |
| 1900 | if (path->slots[0] == 0) |
| 1901 | break; |
| 1902 | path->slots[0]--; |
| 1903 | } |
| 1904 | |
| 1905 | /* pull out the item */ |
| 1906 | leaf = path->nodes[0]; |
| 1907 | item = btrfs_item_nr(leaf, path->slots[0]); |
| 1908 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1909 | |
| 1910 | /* make sure the item matches what we want */ |
| 1911 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 1912 | break; |
| 1913 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 1914 | break; |
| 1915 | |
| 1916 | /* release the path since we're done with it */ |
| 1917 | btrfs_release_path(root, path); |
| 1918 | |
| 1919 | /* |
| 1920 | * this is where we are basically btrfs_lookup, without the |
| 1921 | * crossing root thing. we store the inode number in the |
| 1922 | * offset of the orphan item. |
| 1923 | */ |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1924 | inode = btrfs_iget_locked(root->fs_info->sb, |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1925 | found_key.offset, root); |
| 1926 | if (!inode) |
| 1927 | break; |
| 1928 | |
| 1929 | if (inode->i_state & I_NEW) { |
| 1930 | BTRFS_I(inode)->root = root; |
| 1931 | |
| 1932 | /* have to set the location manually */ |
| 1933 | BTRFS_I(inode)->location.objectid = inode->i_ino; |
| 1934 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 1935 | BTRFS_I(inode)->location.offset = 0; |
| 1936 | |
| 1937 | btrfs_read_locked_inode(inode); |
| 1938 | unlock_new_inode(inode); |
| 1939 | } |
| 1940 | |
| 1941 | /* |
| 1942 | * add this inode to the orphan list so btrfs_orphan_del does |
| 1943 | * the proper thing when we hit it |
| 1944 | */ |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1945 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1946 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1947 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1948 | |
| 1949 | /* |
| 1950 | * if this is a bad inode, means we actually succeeded in |
| 1951 | * removing the inode, but not the orphan record, which means |
| 1952 | * we need to manually delete the orphan since iput will just |
| 1953 | * do a destroy_inode |
| 1954 | */ |
| 1955 | if (is_bad_inode(inode)) { |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1956 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1957 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1958 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1959 | iput(inode); |
| 1960 | continue; |
| 1961 | } |
| 1962 | |
| 1963 | /* if we have links, this was a truncate, lets do that */ |
| 1964 | if (inode->i_nlink) { |
| 1965 | nr_truncate++; |
| 1966 | btrfs_truncate(inode); |
| 1967 | } else { |
| 1968 | nr_unlink++; |
| 1969 | } |
| 1970 | |
| 1971 | /* this will do delete_inode and everything for us */ |
| 1972 | iput(inode); |
| 1973 | } |
| 1974 | |
| 1975 | if (nr_unlink) |
| 1976 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 1977 | if (nr_truncate) |
| 1978 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
| 1979 | |
| 1980 | btrfs_free_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1981 | } |
| 1982 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1983 | /* |
| 1984 | * read an inode from the btree into the in-memory inode |
| 1985 | */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1986 | void btrfs_read_locked_inode(struct inode *inode) |
| 1987 | { |
| 1988 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1989 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1990 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1991 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1992 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1993 | struct btrfs_key location; |
| 1994 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1995 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1996 | int ret; |
| 1997 | |
| 1998 | path = btrfs_alloc_path(); |
| 1999 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2000 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2001 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2002 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2003 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2004 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2005 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2006 | leaf = path->nodes[0]; |
| 2007 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2008 | struct btrfs_inode_item); |
| 2009 | |
| 2010 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2011 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2012 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2013 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2014 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2015 | |
| 2016 | tspec = btrfs_inode_atime(inode_item); |
| 2017 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2018 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2019 | |
| 2020 | tspec = btrfs_inode_mtime(inode_item); |
| 2021 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2022 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2023 | |
| 2024 | tspec = btrfs_inode_ctime(inode_item); |
| 2025 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2026 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2027 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2028 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2029 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2030 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2031 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2032 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2033 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2034 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2035 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2036 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2037 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2038 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2039 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2040 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2041 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2042 | btrfs_free_path(path); |
| 2043 | inode_item = NULL; |
| 2044 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2045 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2046 | case S_IFREG: |
| 2047 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2048 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2049 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2050 | inode->i_fop = &btrfs_file_operations; |
| 2051 | inode->i_op = &btrfs_file_inode_operations; |
| 2052 | break; |
| 2053 | case S_IFDIR: |
| 2054 | inode->i_fop = &btrfs_dir_file_operations; |
| 2055 | if (root == root->fs_info->tree_root) |
| 2056 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2057 | else |
| 2058 | inode->i_op = &btrfs_dir_inode_operations; |
| 2059 | break; |
| 2060 | case S_IFLNK: |
| 2061 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2062 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2063 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2064 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2065 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2066 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2067 | init_special_inode(inode, inode->i_mode, rdev); |
| 2068 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2069 | } |
| 2070 | return; |
| 2071 | |
| 2072 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2073 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2074 | make_bad_inode(inode); |
| 2075 | } |
| 2076 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2077 | /* |
| 2078 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2079 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2080 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2081 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2082 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2083 | struct inode *inode) |
| 2084 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2085 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2086 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2087 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2088 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2089 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2090 | |
| 2091 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2092 | inode->i_atime.tv_sec); |
| 2093 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2094 | inode->i_atime.tv_nsec); |
| 2095 | |
| 2096 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2097 | inode->i_mtime.tv_sec); |
| 2098 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2099 | inode->i_mtime.tv_nsec); |
| 2100 | |
| 2101 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2102 | inode->i_ctime.tv_sec); |
| 2103 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2104 | inode->i_ctime.tv_nsec); |
| 2105 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2106 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2107 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2108 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2109 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2110 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2111 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2112 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2113 | } |
| 2114 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2115 | /* |
| 2116 | * copy everything in the in-memory inode into the btree. |
| 2117 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2118 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2119 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2120 | { |
| 2121 | struct btrfs_inode_item *inode_item; |
| 2122 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2123 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2124 | int ret; |
| 2125 | |
| 2126 | path = btrfs_alloc_path(); |
| 2127 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2128 | ret = btrfs_lookup_inode(trans, root, path, |
| 2129 | &BTRFS_I(inode)->location, 1); |
| 2130 | if (ret) { |
| 2131 | if (ret > 0) |
| 2132 | ret = -ENOENT; |
| 2133 | goto failed; |
| 2134 | } |
| 2135 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2136 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2137 | leaf = path->nodes[0]; |
| 2138 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2139 | struct btrfs_inode_item); |
| 2140 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2141 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2142 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2143 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2144 | ret = 0; |
| 2145 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2146 | btrfs_free_path(path); |
| 2147 | return ret; |
| 2148 | } |
| 2149 | |
| 2150 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2151 | /* |
| 2152 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2153 | * recovery code. It remove a link in a directory with a given name, and |
| 2154 | * also drops the back refs in the inode to the directory |
| 2155 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2156 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2157 | struct btrfs_root *root, |
| 2158 | struct inode *dir, struct inode *inode, |
| 2159 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2160 | { |
| 2161 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2162 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2163 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2164 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2165 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2166 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2167 | |
| 2168 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2169 | if (!path) { |
| 2170 | ret = -ENOMEM; |
| 2171 | goto err; |
| 2172 | } |
| 2173 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2174 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2175 | name, name_len, -1); |
| 2176 | if (IS_ERR(di)) { |
| 2177 | ret = PTR_ERR(di); |
| 2178 | goto err; |
| 2179 | } |
| 2180 | if (!di) { |
| 2181 | ret = -ENOENT; |
| 2182 | goto err; |
| 2183 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2184 | leaf = path->nodes[0]; |
| 2185 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2186 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2187 | if (ret) |
| 2188 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2189 | btrfs_release_path(root, path); |
| 2190 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2191 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2192 | inode->i_ino, |
| 2193 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2194 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2195 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2196 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2197 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2198 | goto err; |
| 2199 | } |
| 2200 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2201 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2202 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2203 | if (IS_ERR(di)) { |
| 2204 | ret = PTR_ERR(di); |
| 2205 | goto err; |
| 2206 | } |
| 2207 | if (!di) { |
| 2208 | ret = -ENOENT; |
| 2209 | goto err; |
| 2210 | } |
| 2211 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2212 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2213 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2214 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 2215 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2216 | BUG_ON(ret != 0 && ret != -ENOENT); |
| 2217 | if (ret != -ENOENT) |
| 2218 | BTRFS_I(dir)->log_dirty_trans = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2219 | |
| 2220 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2221 | dir, index); |
| 2222 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2223 | err: |
| 2224 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2225 | if (ret) |
| 2226 | goto out; |
| 2227 | |
| 2228 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2229 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2230 | btrfs_update_inode(trans, root, dir); |
| 2231 | btrfs_drop_nlink(inode); |
| 2232 | ret = btrfs_update_inode(trans, root, inode); |
| 2233 | dir->i_sb->s_dirt = 1; |
| 2234 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2235 | return ret; |
| 2236 | } |
| 2237 | |
| 2238 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2239 | { |
| 2240 | struct btrfs_root *root; |
| 2241 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2242 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2243 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2244 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2245 | |
| 2246 | root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2247 | |
| 2248 | ret = btrfs_check_free_space(root, 1, 1); |
| 2249 | if (ret) |
| 2250 | goto fail; |
| 2251 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2252 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2253 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2254 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2255 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2256 | dentry->d_name.name, dentry->d_name.len); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2257 | |
| 2258 | if (inode->i_nlink == 0) |
| 2259 | ret = btrfs_orphan_add(trans, inode); |
| 2260 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2261 | nr = trans->blocks_used; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2262 | |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2263 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2264 | fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2265 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2266 | return ret; |
| 2267 | } |
| 2268 | |
| 2269 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 2270 | { |
| 2271 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2272 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2273 | int ret; |
| 2274 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2275 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2276 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2277 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 2278 | /* |
| 2279 | * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir |
| 2280 | * the root of a subvolume or snapshot |
| 2281 | */ |
| 2282 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
| 2283 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 2284 | return -ENOTEMPTY; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2285 | } |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 2286 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2287 | ret = btrfs_check_free_space(root, 1, 1); |
| 2288 | if (ret) |
| 2289 | goto fail; |
| 2290 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2291 | trans = btrfs_start_transaction(root, 1); |
| 2292 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2293 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2294 | err = btrfs_orphan_add(trans, inode); |
| 2295 | if (err) |
| 2296 | goto fail_trans; |
| 2297 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2298 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2299 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2300 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2301 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2302 | btrfs_i_size_write(inode, 0); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2303 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2304 | fail_trans: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2305 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2306 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2307 | fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2308 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2309 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2310 | if (ret && !err) |
| 2311 | err = ret; |
| 2312 | return err; |
| 2313 | } |
| 2314 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2315 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2316 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2317 | * when truncating bytes in a file, it is possible to avoid reading |
| 2318 | * the leaves that contain only checksum items. This can be the |
| 2319 | * majority of the IO required to delete a large file, but it must |
| 2320 | * be done carefully. |
| 2321 | * |
| 2322 | * The keys in the level just above the leaves are checked to make sure |
| 2323 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 2324 | * after the new size. |
| 2325 | * |
| 2326 | * Then the key for the next leaf is checked to make sure it also has |
| 2327 | * a checksum item for the same file. If it does, we know our target leaf |
| 2328 | * contains only checksum items, and it can be safely freed without reading |
| 2329 | * it. |
| 2330 | * |
| 2331 | * This is just an optimization targeted at large files. It may do |
| 2332 | * nothing. It will return 0 unless things went badly. |
| 2333 | */ |
| 2334 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 2335 | struct btrfs_root *root, |
| 2336 | struct btrfs_path *path, |
| 2337 | struct inode *inode, u64 new_size) |
| 2338 | { |
| 2339 | struct btrfs_key key; |
| 2340 | int ret; |
| 2341 | int nritems; |
| 2342 | struct btrfs_key found_key; |
| 2343 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2344 | struct btrfs_leaf_ref *ref; |
| 2345 | u64 leaf_gen; |
| 2346 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2347 | |
| 2348 | path->lowest_level = 1; |
| 2349 | key.objectid = inode->i_ino; |
| 2350 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 2351 | key.offset = new_size; |
| 2352 | again: |
| 2353 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2354 | if (ret < 0) |
| 2355 | goto out; |
| 2356 | |
| 2357 | if (path->nodes[1] == NULL) { |
| 2358 | ret = 0; |
| 2359 | goto out; |
| 2360 | } |
| 2361 | ret = 0; |
| 2362 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 2363 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 2364 | |
| 2365 | if (!nritems) |
| 2366 | goto out; |
| 2367 | |
| 2368 | if (path->slots[1] >= nritems) |
| 2369 | goto next_node; |
| 2370 | |
| 2371 | /* did we find a key greater than anything we want to delete? */ |
| 2372 | if (found_key.objectid > inode->i_ino || |
| 2373 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 2374 | goto out; |
| 2375 | |
| 2376 | /* we check the next key in the node to make sure the leave contains |
| 2377 | * only checksum items. This comparison doesn't work if our |
| 2378 | * leaf is the last one in the node |
| 2379 | */ |
| 2380 | if (path->slots[1] + 1 >= nritems) { |
| 2381 | next_node: |
| 2382 | /* search forward from the last key in the node, this |
| 2383 | * will bring us into the next node in the tree |
| 2384 | */ |
| 2385 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 2386 | |
| 2387 | /* unlikely, but we inc below, so check to be safe */ |
| 2388 | if (found_key.offset == (u64)-1) |
| 2389 | goto out; |
| 2390 | |
| 2391 | /* search_forward needs a path with locks held, do the |
| 2392 | * search again for the original key. It is possible |
| 2393 | * this will race with a balance and return a path that |
| 2394 | * we could modify, but this drop is just an optimization |
| 2395 | * and is allowed to miss some leaves. |
| 2396 | */ |
| 2397 | btrfs_release_path(root, path); |
| 2398 | found_key.offset++; |
| 2399 | |
| 2400 | /* setup a max key for search_forward */ |
| 2401 | other_key.offset = (u64)-1; |
| 2402 | other_key.type = key.type; |
| 2403 | other_key.objectid = key.objectid; |
| 2404 | |
| 2405 | path->keep_locks = 1; |
| 2406 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 2407 | path, 0, 0); |
| 2408 | path->keep_locks = 0; |
| 2409 | if (ret || found_key.objectid != key.objectid || |
| 2410 | found_key.type != key.type) { |
| 2411 | ret = 0; |
| 2412 | goto out; |
| 2413 | } |
| 2414 | |
| 2415 | key.offset = found_key.offset; |
| 2416 | btrfs_release_path(root, path); |
| 2417 | cond_resched(); |
| 2418 | goto again; |
| 2419 | } |
| 2420 | |
| 2421 | /* we know there's one more slot after us in the tree, |
| 2422 | * read that key so we can verify it is also a checksum item |
| 2423 | */ |
| 2424 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 2425 | |
| 2426 | if (found_key.objectid < inode->i_ino) |
| 2427 | goto next_key; |
| 2428 | |
| 2429 | if (found_key.type != key.type || found_key.offset < new_size) |
| 2430 | goto next_key; |
| 2431 | |
| 2432 | /* |
| 2433 | * if the key for the next leaf isn't a csum key from this objectid, |
| 2434 | * we can't be sure there aren't good items inside this leaf. |
| 2435 | * Bail out |
| 2436 | */ |
| 2437 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 2438 | goto out; |
| 2439 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2440 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 2441 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2442 | /* |
| 2443 | * it is safe to delete this leaf, it contains only |
| 2444 | * csum items from this inode at an offset >= new_size |
| 2445 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2446 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2447 | BUG_ON(ret); |
| 2448 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2449 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 2450 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 2451 | if (ref) { |
| 2452 | ref->root_gen = root->root_key.offset; |
| 2453 | ref->bytenr = leaf_start; |
| 2454 | ref->owner = 0; |
| 2455 | ref->generation = leaf_gen; |
| 2456 | ref->nritems = 0; |
| 2457 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2458 | btrfs_sort_leaf_ref(ref); |
| 2459 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2460 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 2461 | WARN_ON(ret); |
| 2462 | btrfs_free_leaf_ref(root, ref); |
| 2463 | } else { |
| 2464 | WARN_ON(1); |
| 2465 | } |
| 2466 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2467 | next_key: |
| 2468 | btrfs_release_path(root, path); |
| 2469 | |
| 2470 | if (other_key.objectid == inode->i_ino && |
| 2471 | other_key.type == key.type && other_key.offset > key.offset) { |
| 2472 | key.offset = other_key.offset; |
| 2473 | cond_resched(); |
| 2474 | goto again; |
| 2475 | } |
| 2476 | ret = 0; |
| 2477 | out: |
| 2478 | /* fixup any changes we've made to the path */ |
| 2479 | path->lowest_level = 0; |
| 2480 | path->keep_locks = 0; |
| 2481 | btrfs_release_path(root, path); |
| 2482 | return ret; |
| 2483 | } |
| 2484 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2485 | #endif |
| 2486 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2487 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2488 | * this can truncate away extent items, csum items and directory items. |
| 2489 | * 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] | 2490 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2491 | * |
| 2492 | * csum items that cross the new i_size are truncated to the new size |
| 2493 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2494 | * |
| 2495 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 2496 | * 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] | 2497 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2498 | noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 2499 | struct btrfs_root *root, |
| 2500 | struct inode *inode, |
| 2501 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2502 | { |
| 2503 | int ret; |
| 2504 | struct btrfs_path *path; |
| 2505 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2506 | struct btrfs_key found_key; |
Chris Mason | 06d9a8d | 2009-02-04 09:30:58 -0500 | [diff] [blame] | 2507 | u32 found_type = (u8)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2508 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2509 | struct btrfs_file_extent_item *fi; |
| 2510 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2511 | u64 extent_num_bytes = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2512 | u64 item_end = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2513 | u64 root_gen = 0; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 2514 | u64 root_owner = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2515 | int found_extent; |
| 2516 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2517 | int pending_del_nr = 0; |
| 2518 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2519 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2520 | int encoding; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2521 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2522 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2523 | if (root->ref_cows) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2524 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2525 | path = btrfs_alloc_path(); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2526 | path->reada = -1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2527 | BUG_ON(!path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2528 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2529 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 2530 | key.objectid = inode->i_ino; |
| 2531 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2532 | key.type = (u8)-1; |
| 2533 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2534 | search_again: |
| 2535 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2536 | if (ret < 0) |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2537 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2538 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2539 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2540 | /* there are no items in the tree for us to truncate, we're |
| 2541 | * done |
| 2542 | */ |
| 2543 | if (path->slots[0] == 0) { |
| 2544 | ret = 0; |
| 2545 | goto error; |
| 2546 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2547 | path->slots[0]--; |
| 2548 | } |
| 2549 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2550 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2551 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2552 | leaf = path->nodes[0]; |
| 2553 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2554 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2555 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2556 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2557 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2558 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2559 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2560 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2561 | break; |
| 2562 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2563 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2564 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2565 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2566 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2567 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2568 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 2569 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 2570 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 2571 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2572 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2573 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2574 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2575 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2576 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2577 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2578 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 2579 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2580 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2581 | if (item_end < new_size) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2582 | if (found_type == BTRFS_DIR_ITEM_KEY) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2583 | found_type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2584 | else if (found_type == BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2585 | found_type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2586 | else if (found_type == BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2587 | found_type = BTRFS_XATTR_ITEM_KEY; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2588 | else if (found_type == BTRFS_XATTR_ITEM_KEY) |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2589 | found_type = BTRFS_INODE_REF_KEY; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2590 | else if (found_type) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2591 | found_type--; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2592 | else |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2593 | break; |
Yan | a61721d | 2007-09-17 11:08:38 -0400 | [diff] [blame] | 2594 | btrfs_set_key_type(&key, found_type); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2595 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2596 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2597 | if (found_key.offset >= new_size) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2598 | del_item = 1; |
| 2599 | else |
| 2600 | del_item = 0; |
| 2601 | found_extent = 0; |
| 2602 | |
| 2603 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2604 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 2605 | goto delete; |
| 2606 | |
| 2607 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2608 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2609 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2610 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2611 | u64 orig_num_bytes = |
| 2612 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2613 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2614 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 2615 | extent_num_bytes = extent_num_bytes & |
| 2616 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2617 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2618 | extent_num_bytes); |
| 2619 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2620 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2621 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2622 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2623 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2624 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2625 | extent_num_bytes = |
| 2626 | btrfs_file_extent_disk_num_bytes(leaf, |
| 2627 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2628 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2629 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2630 | if (extent_start != 0) { |
| 2631 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2632 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2633 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2634 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2635 | root_gen = btrfs_header_generation(leaf); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 2636 | root_owner = btrfs_header_owner(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2637 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2638 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2639 | /* |
| 2640 | * we can't truncate inline items that have had |
| 2641 | * special encodings |
| 2642 | */ |
| 2643 | if (!del_item && |
| 2644 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 2645 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 2646 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2647 | u32 size = new_size - found_key.offset; |
| 2648 | |
| 2649 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2650 | inode_sub_bytes(inode, item_end + 1 - |
| 2651 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2652 | } |
| 2653 | size = |
| 2654 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2655 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2656 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2657 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2658 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2659 | inode_sub_bytes(inode, item_end + 1 - |
| 2660 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2661 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2662 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2663 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2664 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2665 | if (!pending_del_nr) { |
| 2666 | /* no pending yet, add ourselves */ |
| 2667 | pending_del_slot = path->slots[0]; |
| 2668 | pending_del_nr = 1; |
| 2669 | } else if (pending_del_nr && |
| 2670 | path->slots[0] + 1 == pending_del_slot) { |
| 2671 | /* hop on the pending chunk */ |
| 2672 | pending_del_nr++; |
| 2673 | pending_del_slot = path->slots[0]; |
| 2674 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2675 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2676 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2677 | } else { |
| 2678 | break; |
| 2679 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2680 | if (found_extent) { |
| 2681 | ret = btrfs_free_extent(trans, root, extent_start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2682 | extent_num_bytes, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2683 | leaf->start, root_owner, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2684 | root_gen, inode->i_ino, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2685 | BUG_ON(ret); |
| 2686 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2687 | next: |
| 2688 | if (path->slots[0] == 0) { |
| 2689 | if (pending_del_nr) |
| 2690 | goto del_pending; |
| 2691 | btrfs_release_path(root, path); |
Chris Mason | 06d9a8d | 2009-02-04 09:30:58 -0500 | [diff] [blame] | 2692 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 2693 | break; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2694 | goto search_again; |
| 2695 | } |
| 2696 | |
| 2697 | path->slots[0]--; |
| 2698 | if (pending_del_nr && |
| 2699 | path->slots[0] + 1 != pending_del_slot) { |
| 2700 | struct btrfs_key debug; |
| 2701 | del_pending: |
| 2702 | btrfs_item_key_to_cpu(path->nodes[0], &debug, |
| 2703 | pending_del_slot); |
| 2704 | ret = btrfs_del_items(trans, root, path, |
| 2705 | pending_del_slot, |
| 2706 | pending_del_nr); |
| 2707 | BUG_ON(ret); |
| 2708 | pending_del_nr = 0; |
| 2709 | btrfs_release_path(root, path); |
Chris Mason | 06d9a8d | 2009-02-04 09:30:58 -0500 | [diff] [blame] | 2710 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 2711 | break; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2712 | goto search_again; |
| 2713 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2714 | } |
| 2715 | ret = 0; |
| 2716 | error: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2717 | if (pending_del_nr) { |
| 2718 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 2719 | pending_del_nr); |
| 2720 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2721 | btrfs_free_path(path); |
| 2722 | inode->i_sb->s_dirt = 1; |
| 2723 | return ret; |
| 2724 | } |
| 2725 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2726 | /* |
| 2727 | * taken from block_truncate_page, but does cow as it zeros out |
| 2728 | * any bytes left in the last page in the file. |
| 2729 | */ |
| 2730 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 2731 | { |
| 2732 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2733 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2734 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 2735 | struct btrfs_ordered_extent *ordered; |
| 2736 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2737 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2738 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 2739 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 2740 | struct page *page; |
| 2741 | int ret = 0; |
| 2742 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2743 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2744 | |
| 2745 | if ((offset & (blocksize - 1)) == 0) |
| 2746 | goto out; |
| 2747 | |
| 2748 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2749 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2750 | page = grab_cache_page(mapping, index); |
| 2751 | if (!page) |
| 2752 | goto out; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2753 | |
| 2754 | page_start = page_offset(page); |
| 2755 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 2756 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2757 | if (!PageUptodate(page)) { |
| 2758 | ret = btrfs_readpage(NULL, page); |
| 2759 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2760 | if (page->mapping != mapping) { |
| 2761 | unlock_page(page); |
| 2762 | page_cache_release(page); |
| 2763 | goto again; |
| 2764 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2765 | if (!PageUptodate(page)) { |
| 2766 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 2767 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2768 | } |
| 2769 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2770 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2771 | |
| 2772 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 2773 | set_page_extent_mapped(page); |
| 2774 | |
| 2775 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 2776 | if (ordered) { |
| 2777 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 2778 | unlock_page(page); |
| 2779 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 2780 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2781 | btrfs_put_ordered_extent(ordered); |
| 2782 | goto again; |
| 2783 | } |
| 2784 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2785 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2786 | ret = 0; |
| 2787 | if (offset != PAGE_CACHE_SIZE) { |
| 2788 | kaddr = kmap(page); |
| 2789 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 2790 | flush_dcache_page(page); |
| 2791 | kunmap(page); |
| 2792 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2793 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2794 | set_page_dirty(page); |
| 2795 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2796 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 2797 | out_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2798 | unlock_page(page); |
| 2799 | page_cache_release(page); |
| 2800 | out: |
| 2801 | return ret; |
| 2802 | } |
| 2803 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 2804 | int btrfs_cont_expand(struct inode *inode, loff_t size) |
| 2805 | { |
| 2806 | struct btrfs_trans_handle *trans; |
| 2807 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2808 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 2809 | struct extent_map *em; |
| 2810 | u64 mask = root->sectorsize - 1; |
| 2811 | u64 hole_start = (inode->i_size + mask) & ~mask; |
| 2812 | u64 block_end = (size + mask) & ~mask; |
| 2813 | u64 last_byte; |
| 2814 | u64 cur_offset; |
| 2815 | u64 hole_size; |
| 2816 | int err; |
| 2817 | |
| 2818 | if (size <= hole_start) |
| 2819 | return 0; |
| 2820 | |
| 2821 | err = btrfs_check_free_space(root, 1, 0); |
| 2822 | if (err) |
| 2823 | return err; |
| 2824 | |
| 2825 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 2826 | |
| 2827 | while (1) { |
| 2828 | struct btrfs_ordered_extent *ordered; |
| 2829 | btrfs_wait_ordered_range(inode, hole_start, |
| 2830 | block_end - hole_start); |
| 2831 | lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 2832 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 2833 | if (!ordered) |
| 2834 | break; |
| 2835 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 2836 | btrfs_put_ordered_extent(ordered); |
| 2837 | } |
| 2838 | |
| 2839 | trans = btrfs_start_transaction(root, 1); |
| 2840 | btrfs_set_trans_block_group(trans, inode); |
| 2841 | |
| 2842 | cur_offset = hole_start; |
| 2843 | while (1) { |
| 2844 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 2845 | block_end - cur_offset, 0); |
| 2846 | BUG_ON(IS_ERR(em) || !em); |
| 2847 | last_byte = min(extent_map_end(em), block_end); |
| 2848 | last_byte = (last_byte + mask) & ~mask; |
| 2849 | if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2850 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 2851 | hole_size = last_byte - cur_offset; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2852 | err = btrfs_drop_extents(trans, root, inode, |
| 2853 | cur_offset, |
| 2854 | cur_offset + hole_size, |
| 2855 | cur_offset, &hint_byte); |
| 2856 | if (err) |
| 2857 | break; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 2858 | err = btrfs_insert_file_extent(trans, root, |
| 2859 | inode->i_ino, cur_offset, 0, |
| 2860 | 0, hole_size, 0, hole_size, |
| 2861 | 0, 0, 0); |
| 2862 | btrfs_drop_extent_cache(inode, hole_start, |
| 2863 | last_byte - 1, 0); |
| 2864 | } |
| 2865 | free_extent_map(em); |
| 2866 | cur_offset = last_byte; |
| 2867 | if (err || cur_offset >= block_end) |
| 2868 | break; |
| 2869 | } |
| 2870 | |
| 2871 | btrfs_end_transaction(trans, root); |
| 2872 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 2873 | return err; |
| 2874 | } |
| 2875 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2876 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 2877 | { |
| 2878 | struct inode *inode = dentry->d_inode; |
| 2879 | int err; |
| 2880 | |
| 2881 | err = inode_change_ok(inode, attr); |
| 2882 | if (err) |
| 2883 | return err; |
| 2884 | |
| 2885 | if (S_ISREG(inode->i_mode) && |
| 2886 | attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 2887 | err = btrfs_cont_expand(inode, attr->ia_size); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2888 | if (err) |
| 2889 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2890 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 2891 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2892 | err = inode_setattr(inode, attr); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 2893 | |
| 2894 | if (!err && ((attr->ia_valid & ATTR_MODE))) |
| 2895 | err = btrfs_acl_chmod(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2896 | return err; |
| 2897 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 2898 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2899 | void btrfs_delete_inode(struct inode *inode) |
| 2900 | { |
| 2901 | struct btrfs_trans_handle *trans; |
| 2902 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2903 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2904 | int ret; |
| 2905 | |
| 2906 | truncate_inode_pages(&inode->i_data, 0); |
| 2907 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2908 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2909 | goto no_delete; |
| 2910 | } |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2911 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2912 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2913 | btrfs_i_size_write(inode, 0); |
Yan Zheng | 180591b | 2009-01-06 09:58:06 -0500 | [diff] [blame] | 2914 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2915 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2916 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2917 | ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2918 | if (ret) { |
| 2919 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2920 | goto no_delete_lock; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | btrfs_orphan_del(trans, inode); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2924 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2925 | nr = trans->blocks_used; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2926 | clear_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2927 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2928 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2929 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2930 | return; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2931 | |
| 2932 | no_delete_lock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2933 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2934 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2935 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2936 | no_delete: |
| 2937 | clear_inode(inode); |
| 2938 | } |
| 2939 | |
| 2940 | /* |
| 2941 | * this returns the key found in the dir entry in the location pointer. |
| 2942 | * If no dir entries were found, location->objectid is 0. |
| 2943 | */ |
| 2944 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 2945 | struct btrfs_key *location) |
| 2946 | { |
| 2947 | const char *name = dentry->d_name.name; |
| 2948 | int namelen = dentry->d_name.len; |
| 2949 | struct btrfs_dir_item *di; |
| 2950 | struct btrfs_path *path; |
| 2951 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 2952 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2953 | |
| 2954 | path = btrfs_alloc_path(); |
| 2955 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2956 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2957 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 2958 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 2959 | if (IS_ERR(di)) |
| 2960 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2961 | |
| 2962 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2963 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2964 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2965 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2966 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2967 | btrfs_free_path(path); |
| 2968 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2969 | out_err: |
| 2970 | location->objectid = 0; |
| 2971 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | /* |
| 2975 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 2976 | * needs to be changed to reflect the root directory of the tree root. This |
| 2977 | * is kind of like crossing a mount point. |
| 2978 | */ |
| 2979 | static int fixup_tree_root_location(struct btrfs_root *root, |
| 2980 | struct btrfs_key *location, |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2981 | struct btrfs_root **sub_root, |
| 2982 | struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2983 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2984 | struct btrfs_root_item *ri; |
| 2985 | |
| 2986 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) |
| 2987 | return 0; |
| 2988 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 2989 | return 0; |
| 2990 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2991 | *sub_root = btrfs_read_fs_root(root->fs_info, location, |
| 2992 | dentry->d_name.name, |
| 2993 | dentry->d_name.len); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2994 | if (IS_ERR(*sub_root)) |
| 2995 | return PTR_ERR(*sub_root); |
| 2996 | |
| 2997 | ri = &(*sub_root)->root_item; |
| 2998 | location->objectid = btrfs_root_dirid(ri); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2999 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 3000 | location->offset = 0; |
| 3001 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3002 | return 0; |
| 3003 | } |
| 3004 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3005 | static noinline void init_btrfs_i(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3006 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3007 | struct btrfs_inode *bi = BTRFS_I(inode); |
| 3008 | |
| 3009 | bi->i_acl = NULL; |
| 3010 | bi->i_default_acl = NULL; |
| 3011 | |
| 3012 | bi->generation = 0; |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 3013 | bi->sequence = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3014 | bi->last_trans = 0; |
| 3015 | bi->logged_trans = 0; |
| 3016 | bi->delalloc_bytes = 0; |
| 3017 | bi->disk_i_size = 0; |
| 3018 | bi->flags = 0; |
| 3019 | bi->index_cnt = (u64)-1; |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 3020 | bi->log_dirty_trans = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3021 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 3022 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3023 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3024 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
| 3025 | inode->i_mapping, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3026 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 3027 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 3028 | mutex_init(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3029 | mutex_init(&BTRFS_I(inode)->log_mutex); |
| 3030 | } |
| 3031 | |
| 3032 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 3033 | { |
| 3034 | struct btrfs_iget_args *args = p; |
| 3035 | inode->i_ino = args->ino; |
| 3036 | init_btrfs_i(inode); |
| 3037 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3038 | return 0; |
| 3039 | } |
| 3040 | |
| 3041 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 3042 | { |
| 3043 | struct btrfs_iget_args *args = opaque; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3044 | return args->ino == inode->i_ino && |
| 3045 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3046 | } |
| 3047 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3048 | struct inode *btrfs_ilookup(struct super_block *s, u64 objectid, |
| 3049 | struct btrfs_root *root, int wait) |
| 3050 | { |
| 3051 | struct inode *inode; |
| 3052 | struct btrfs_iget_args args; |
| 3053 | args.ino = objectid; |
| 3054 | args.root = root; |
| 3055 | |
| 3056 | if (wait) { |
| 3057 | inode = ilookup5(s, objectid, btrfs_find_actor, |
| 3058 | (void *)&args); |
| 3059 | } else { |
| 3060 | inode = ilookup5_nowait(s, objectid, btrfs_find_actor, |
| 3061 | (void *)&args); |
| 3062 | } |
| 3063 | return inode; |
| 3064 | } |
| 3065 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3066 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
| 3067 | struct btrfs_root *root) |
| 3068 | { |
| 3069 | struct inode *inode; |
| 3070 | struct btrfs_iget_args args; |
| 3071 | args.ino = objectid; |
| 3072 | args.root = root; |
| 3073 | |
| 3074 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 3075 | btrfs_init_locked_inode, |
| 3076 | (void *)&args); |
| 3077 | return inode; |
| 3078 | } |
| 3079 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3080 | /* Get an inode object given its location and corresponding root. |
| 3081 | * Returns in *is_new if the inode was read from disk |
| 3082 | */ |
| 3083 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
| 3084 | struct btrfs_root *root, int *is_new) |
| 3085 | { |
| 3086 | struct inode *inode; |
| 3087 | |
| 3088 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 3089 | if (!inode) |
| 3090 | return ERR_PTR(-EACCES); |
| 3091 | |
| 3092 | if (inode->i_state & I_NEW) { |
| 3093 | BTRFS_I(inode)->root = root; |
| 3094 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 3095 | btrfs_read_locked_inode(inode); |
| 3096 | unlock_new_inode(inode); |
| 3097 | if (is_new) |
| 3098 | *is_new = 1; |
| 3099 | } else { |
| 3100 | if (is_new) |
| 3101 | *is_new = 0; |
| 3102 | } |
| 3103 | |
| 3104 | return inode; |
| 3105 | } |
| 3106 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3107 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3108 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3109 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3110 | struct btrfs_inode *bi = BTRFS_I(dir); |
| 3111 | struct btrfs_root *root = bi->root; |
| 3112 | struct btrfs_root *sub_root = root; |
| 3113 | struct btrfs_key location; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3114 | int ret, new; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3115 | |
| 3116 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 3117 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3118 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3119 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3120 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3121 | if (ret < 0) |
| 3122 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3123 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3124 | inode = NULL; |
| 3125 | if (location.objectid) { |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3126 | ret = fixup_tree_root_location(root, &location, &sub_root, |
| 3127 | dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3128 | if (ret < 0) |
| 3129 | return ERR_PTR(ret); |
| 3130 | if (ret > 0) |
| 3131 | return ERR_PTR(-ENOENT); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3132 | inode = btrfs_iget(dir->i_sb, &location, sub_root, &new); |
| 3133 | if (IS_ERR(inode)) |
| 3134 | return ERR_CAST(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3135 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3136 | return inode; |
| 3137 | } |
| 3138 | |
| 3139 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 3140 | struct nameidata *nd) |
| 3141 | { |
| 3142 | struct inode *inode; |
| 3143 | |
| 3144 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 3145 | return ERR_PTR(-ENAMETOOLONG); |
| 3146 | |
| 3147 | inode = btrfs_lookup_dentry(dir, dentry); |
| 3148 | if (IS_ERR(inode)) |
| 3149 | return ERR_CAST(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3150 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3151 | return d_splice_alias(inode, dentry); |
| 3152 | } |
| 3153 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3154 | static unsigned char btrfs_filetype_table[] = { |
| 3155 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 3156 | }; |
| 3157 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 3158 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 3159 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3160 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 3161 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3162 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3163 | struct btrfs_item *item; |
| 3164 | struct btrfs_dir_item *di; |
| 3165 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3166 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3167 | struct btrfs_path *path; |
| 3168 | int ret; |
| 3169 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3170 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3171 | int slot; |
| 3172 | int advance; |
| 3173 | unsigned char d_type; |
| 3174 | int over = 0; |
| 3175 | u32 di_cur; |
| 3176 | u32 di_total; |
| 3177 | u32 di_len; |
| 3178 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3179 | char tmp_name[32]; |
| 3180 | char *name_ptr; |
| 3181 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3182 | |
| 3183 | /* FIXME, use a real flag for deciding about the key type */ |
| 3184 | if (root->fs_info->tree_root == root) |
| 3185 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3186 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3187 | /* special case for "." */ |
| 3188 | if (filp->f_pos == 0) { |
| 3189 | over = filldir(dirent, ".", 1, |
| 3190 | 1, inode->i_ino, |
| 3191 | DT_DIR); |
| 3192 | if (over) |
| 3193 | return 0; |
| 3194 | filp->f_pos = 1; |
| 3195 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3196 | /* special case for .., just use the back ref */ |
| 3197 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3198 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3199 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3200 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3201 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3202 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3203 | filp->f_pos = 2; |
| 3204 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3205 | path = btrfs_alloc_path(); |
| 3206 | path->reada = 2; |
| 3207 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3208 | btrfs_set_key_type(&key, key_type); |
| 3209 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3210 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3211 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3212 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3213 | if (ret < 0) |
| 3214 | goto err; |
| 3215 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3216 | |
| 3217 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3218 | leaf = path->nodes[0]; |
| 3219 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3220 | slot = path->slots[0]; |
| 3221 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3222 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3223 | ret = btrfs_next_leaf(root, path); |
| 3224 | if (ret) |
| 3225 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3226 | leaf = path->nodes[0]; |
| 3227 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3228 | slot = path->slots[0]; |
| 3229 | } else { |
| 3230 | slot++; |
| 3231 | path->slots[0]++; |
| 3232 | } |
| 3233 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3234 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3235 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3236 | item = btrfs_item_nr(leaf, slot); |
| 3237 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3238 | |
| 3239 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3240 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3241 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3242 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3243 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3244 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3245 | |
| 3246 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3247 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3248 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 3249 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3250 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3251 | |
| 3252 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3253 | struct btrfs_key location; |
| 3254 | |
| 3255 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3256 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3257 | name_ptr = tmp_name; |
| 3258 | } else { |
| 3259 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3260 | if (!name_ptr) { |
| 3261 | ret = -ENOMEM; |
| 3262 | goto err; |
| 3263 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3264 | } |
| 3265 | read_extent_buffer(leaf, name_ptr, |
| 3266 | (unsigned long)(di + 1), name_len); |
| 3267 | |
| 3268 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 3269 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3270 | |
| 3271 | /* is this a reference to our own snapshot? If so |
| 3272 | * skip it |
| 3273 | */ |
| 3274 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 3275 | location.objectid == root->root_key.objectid) { |
| 3276 | over = 0; |
| 3277 | goto skip; |
| 3278 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3279 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3280 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3281 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3282 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3283 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3284 | if (name_ptr != tmp_name) |
| 3285 | kfree(name_ptr); |
| 3286 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3287 | if (over) |
| 3288 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 3289 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3290 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3291 | di_cur += di_len; |
| 3292 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 3293 | } |
| 3294 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3295 | |
| 3296 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 3297 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Chris Mason | 89f135d | 2009-01-28 15:34:27 -0500 | [diff] [blame] | 3298 | filp->f_pos = INT_LIMIT(off_t); |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 3299 | else |
| 3300 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3301 | nopos: |
| 3302 | ret = 0; |
| 3303 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3304 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3305 | return ret; |
| 3306 | } |
| 3307 | |
| 3308 | int btrfs_write_inode(struct inode *inode, int wait) |
| 3309 | { |
| 3310 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3311 | struct btrfs_trans_handle *trans; |
| 3312 | int ret = 0; |
| 3313 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3314 | if (root->fs_info->btree_inode == inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 3315 | return 0; |
| 3316 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3317 | if (wait) { |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 3318 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3319 | btrfs_set_trans_block_group(trans, inode); |
| 3320 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3321 | } |
| 3322 | return ret; |
| 3323 | } |
| 3324 | |
| 3325 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3326 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3327 | * inode changes. But, it is most likely to find the inode in cache. |
| 3328 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 3329 | * to keep or drop this code. |
| 3330 | */ |
| 3331 | void btrfs_dirty_inode(struct inode *inode) |
| 3332 | { |
| 3333 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3334 | struct btrfs_trans_handle *trans; |
| 3335 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 3336 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3337 | btrfs_set_trans_block_group(trans, inode); |
| 3338 | btrfs_update_inode(trans, root, inode); |
| 3339 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3340 | } |
| 3341 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3342 | /* |
| 3343 | * find the highest existing sequence number in a directory |
| 3344 | * and then set the in-memory index_cnt variable to reflect |
| 3345 | * free sequence numbers |
| 3346 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3347 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 3348 | { |
| 3349 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3350 | struct btrfs_key key, found_key; |
| 3351 | struct btrfs_path *path; |
| 3352 | struct extent_buffer *leaf; |
| 3353 | int ret; |
| 3354 | |
| 3355 | key.objectid = inode->i_ino; |
| 3356 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 3357 | key.offset = (u64)-1; |
| 3358 | |
| 3359 | path = btrfs_alloc_path(); |
| 3360 | if (!path) |
| 3361 | return -ENOMEM; |
| 3362 | |
| 3363 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3364 | if (ret < 0) |
| 3365 | goto out; |
| 3366 | /* FIXME: we should be able to handle this */ |
| 3367 | if (ret == 0) |
| 3368 | goto out; |
| 3369 | ret = 0; |
| 3370 | |
| 3371 | /* |
| 3372 | * MAGIC NUMBER EXPLANATION: |
| 3373 | * since we search a directory based on f_pos we have to start at 2 |
| 3374 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 3375 | * else has to start at 2 |
| 3376 | */ |
| 3377 | if (path->slots[0] == 0) { |
| 3378 | BTRFS_I(inode)->index_cnt = 2; |
| 3379 | goto out; |
| 3380 | } |
| 3381 | |
| 3382 | path->slots[0]--; |
| 3383 | |
| 3384 | leaf = path->nodes[0]; |
| 3385 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3386 | |
| 3387 | if (found_key.objectid != inode->i_ino || |
| 3388 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 3389 | BTRFS_I(inode)->index_cnt = 2; |
| 3390 | goto out; |
| 3391 | } |
| 3392 | |
| 3393 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 3394 | out: |
| 3395 | btrfs_free_path(path); |
| 3396 | return ret; |
| 3397 | } |
| 3398 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3399 | /* |
| 3400 | * helper to find a free sequence number in a given directory. This current |
| 3401 | * code is very simple, later versions will do smarter things in the btree |
| 3402 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3403 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3404 | { |
| 3405 | int ret = 0; |
| 3406 | |
| 3407 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 3408 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3409 | if (ret) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3410 | return ret; |
| 3411 | } |
| 3412 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3413 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3414 | BTRFS_I(dir)->index_cnt++; |
| 3415 | |
| 3416 | return ret; |
| 3417 | } |
| 3418 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3419 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 3420 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3421 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3422 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3423 | u64 ref_objectid, u64 objectid, |
| 3424 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3425 | { |
| 3426 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3427 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3428 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3429 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3430 | struct btrfs_inode_ref *ref; |
| 3431 | struct btrfs_key key[2]; |
| 3432 | u32 sizes[2]; |
| 3433 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3434 | int ret; |
| 3435 | int owner; |
| 3436 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3437 | path = btrfs_alloc_path(); |
| 3438 | BUG_ON(!path); |
| 3439 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3440 | inode = new_inode(root->fs_info->sb); |
| 3441 | if (!inode) |
| 3442 | return ERR_PTR(-ENOMEM); |
| 3443 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3444 | if (dir) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3445 | ret = btrfs_set_inode_index(dir, index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3446 | if (ret) |
| 3447 | return ERR_PTR(ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3448 | } |
| 3449 | /* |
| 3450 | * index_cnt is ignored for everything but a dir, |
| 3451 | * btrfs_get_inode_index_count has an explanation for the magic |
| 3452 | * number |
| 3453 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3454 | init_btrfs_i(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3455 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3456 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3457 | BTRFS_I(inode)->generation = trans->transid; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3458 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3459 | if (mode & S_IFDIR) |
| 3460 | owner = 0; |
| 3461 | else |
| 3462 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3463 | BTRFS_I(inode)->block_group = |
| 3464 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3465 | if ((mode & S_IFREG)) { |
| 3466 | if (btrfs_test_opt(root, NODATASUM)) |
| 3467 | btrfs_set_flag(inode, NODATASUM); |
| 3468 | if (btrfs_test_opt(root, NODATACOW)) |
| 3469 | btrfs_set_flag(inode, NODATACOW); |
| 3470 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3471 | |
| 3472 | key[0].objectid = objectid; |
| 3473 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 3474 | key[0].offset = 0; |
| 3475 | |
| 3476 | key[1].objectid = objectid; |
| 3477 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 3478 | key[1].offset = ref_objectid; |
| 3479 | |
| 3480 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 3481 | sizes[1] = name_len + sizeof(*ref); |
| 3482 | |
| 3483 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 3484 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3485 | goto fail; |
| 3486 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3487 | if (objectid > root->highest_inode) |
| 3488 | root->highest_inode = objectid; |
| 3489 | |
Chris Mason | 79683f2 | 2008-11-19 22:00:53 -0500 | [diff] [blame] | 3490 | inode->i_uid = current_fsuid(); |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 3491 | |
Chris Mason | 42f15d7 | 2009-02-06 11:35:57 -0500 | [diff] [blame] | 3492 | if (dir && (dir->i_mode & S_ISGID)) { |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 3493 | inode->i_gid = dir->i_gid; |
| 3494 | if (S_ISDIR(mode)) |
| 3495 | mode |= S_ISGID; |
| 3496 | } else |
| 3497 | inode->i_gid = current_fsgid(); |
| 3498 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3499 | inode->i_mode = mode; |
| 3500 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3501 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3502 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3503 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3504 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3505 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3506 | |
| 3507 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 3508 | struct btrfs_inode_ref); |
| 3509 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3510 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3511 | ptr = (unsigned long)(ref + 1); |
| 3512 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 3513 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3514 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3515 | btrfs_free_path(path); |
| 3516 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3517 | location = &BTRFS_I(inode)->location; |
| 3518 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3519 | location->offset = 0; |
| 3520 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 3521 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3522 | insert_inode_hash(inode); |
| 3523 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3524 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3525 | if (dir) |
| 3526 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3527 | btrfs_free_path(path); |
| 3528 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 3532 | { |
| 3533 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 3534 | } |
| 3535 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3536 | /* |
| 3537 | * utility function to add 'inode' into 'parent_inode' with |
| 3538 | * a give name and a given sequence number. |
| 3539 | * if 'add_backref' is true, also insert a backref from the |
| 3540 | * inode to the parent directory. |
| 3541 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3542 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 3543 | struct inode *parent_inode, struct inode *inode, |
| 3544 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3545 | { |
| 3546 | int ret; |
| 3547 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3548 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3549 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3550 | key.objectid = inode->i_ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3551 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 3552 | key.offset = 0; |
| 3553 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3554 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 3555 | parent_inode->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3556 | &key, btrfs_inode_type(inode), |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3557 | index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3558 | if (ret == 0) { |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3559 | if (add_backref) { |
| 3560 | ret = btrfs_insert_inode_ref(trans, root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3561 | name, name_len, |
| 3562 | inode->i_ino, |
| 3563 | parent_inode->i_ino, |
| 3564 | index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3565 | } |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3566 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3567 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 3568 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3569 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3570 | } |
| 3571 | return ret; |
| 3572 | } |
| 3573 | |
| 3574 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3575 | struct dentry *dentry, struct inode *inode, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3576 | int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3577 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3578 | int err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 3579 | inode, dentry->d_name.name, |
| 3580 | dentry->d_name.len, backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3581 | if (!err) { |
| 3582 | d_instantiate(dentry, inode); |
| 3583 | return 0; |
| 3584 | } |
| 3585 | if (err > 0) |
| 3586 | err = -EEXIST; |
| 3587 | return err; |
| 3588 | } |
| 3589 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3590 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 3591 | int mode, dev_t rdev) |
| 3592 | { |
| 3593 | struct btrfs_trans_handle *trans; |
| 3594 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3595 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3596 | int err; |
| 3597 | int drop_inode = 0; |
| 3598 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3599 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3600 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3601 | |
| 3602 | if (!new_valid_dev(rdev)) |
| 3603 | return -EINVAL; |
| 3604 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3605 | err = btrfs_check_free_space(root, 1, 0); |
| 3606 | if (err) |
| 3607 | goto fail; |
| 3608 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3609 | trans = btrfs_start_transaction(root, 1); |
| 3610 | btrfs_set_trans_block_group(trans, dir); |
| 3611 | |
| 3612 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 3613 | if (err) { |
| 3614 | err = -ENOSPC; |
| 3615 | goto out_unlock; |
| 3616 | } |
| 3617 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3618 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3619 | dentry->d_name.len, |
| 3620 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3621 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3622 | err = PTR_ERR(inode); |
| 3623 | if (IS_ERR(inode)) |
| 3624 | goto out_unlock; |
| 3625 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3626 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3627 | if (err) { |
| 3628 | drop_inode = 1; |
| 3629 | goto out_unlock; |
| 3630 | } |
| 3631 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3632 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3633 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3634 | if (err) |
| 3635 | drop_inode = 1; |
| 3636 | else { |
| 3637 | inode->i_op = &btrfs_special_inode_operations; |
| 3638 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 3639 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3640 | } |
| 3641 | dir->i_sb->s_dirt = 1; |
| 3642 | btrfs_update_inode_block_group(trans, inode); |
| 3643 | btrfs_update_inode_block_group(trans, dir); |
| 3644 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3645 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3646 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3647 | fail: |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3648 | if (drop_inode) { |
| 3649 | inode_dec_link_count(inode); |
| 3650 | iput(inode); |
| 3651 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3652 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3653 | return err; |
| 3654 | } |
| 3655 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3656 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 3657 | int mode, struct nameidata *nd) |
| 3658 | { |
| 3659 | struct btrfs_trans_handle *trans; |
| 3660 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3661 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3662 | int err; |
| 3663 | int drop_inode = 0; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3664 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3665 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3666 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3667 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3668 | err = btrfs_check_free_space(root, 1, 0); |
| 3669 | if (err) |
| 3670 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3671 | trans = btrfs_start_transaction(root, 1); |
| 3672 | btrfs_set_trans_block_group(trans, dir); |
| 3673 | |
| 3674 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 3675 | if (err) { |
| 3676 | err = -ENOSPC; |
| 3677 | goto out_unlock; |
| 3678 | } |
| 3679 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3680 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3681 | dentry->d_name.len, |
| 3682 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3683 | objectid, BTRFS_I(dir)->block_group, mode, |
| 3684 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3685 | err = PTR_ERR(inode); |
| 3686 | if (IS_ERR(inode)) |
| 3687 | goto out_unlock; |
| 3688 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3689 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3690 | if (err) { |
| 3691 | drop_inode = 1; |
| 3692 | goto out_unlock; |
| 3693 | } |
| 3694 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3695 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3696 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3697 | if (err) |
| 3698 | drop_inode = 1; |
| 3699 | else { |
| 3700 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3701 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3702 | inode->i_fop = &btrfs_file_operations; |
| 3703 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3704 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3705 | } |
| 3706 | dir->i_sb->s_dirt = 1; |
| 3707 | btrfs_update_inode_block_group(trans, inode); |
| 3708 | btrfs_update_inode_block_group(trans, dir); |
| 3709 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3710 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 3711 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3712 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3713 | if (drop_inode) { |
| 3714 | inode_dec_link_count(inode); |
| 3715 | iput(inode); |
| 3716 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3717 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3718 | return err; |
| 3719 | } |
| 3720 | |
| 3721 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 3722 | struct dentry *dentry) |
| 3723 | { |
| 3724 | struct btrfs_trans_handle *trans; |
| 3725 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 3726 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3727 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3728 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3729 | int err; |
| 3730 | int drop_inode = 0; |
| 3731 | |
| 3732 | if (inode->i_nlink == 0) |
| 3733 | return -ENOENT; |
| 3734 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3735 | btrfs_inc_nlink(inode); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3736 | err = btrfs_check_free_space(root, 1, 0); |
| 3737 | if (err) |
| 3738 | goto fail; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3739 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3740 | if (err) |
| 3741 | goto fail; |
| 3742 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3743 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3744 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3745 | btrfs_set_trans_block_group(trans, dir); |
| 3746 | atomic_inc(&inode->i_count); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3747 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3748 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3749 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3750 | if (err) |
| 3751 | drop_inode = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3752 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3753 | dir->i_sb->s_dirt = 1; |
| 3754 | btrfs_update_inode_block_group(trans, dir); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3755 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3756 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3757 | if (err) |
| 3758 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3759 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3760 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 3761 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3762 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3763 | if (drop_inode) { |
| 3764 | inode_dec_link_count(inode); |
| 3765 | iput(inode); |
| 3766 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3767 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3768 | return err; |
| 3769 | } |
| 3770 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3771 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 3772 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 3773 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3774 | struct btrfs_trans_handle *trans; |
| 3775 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 3776 | int err = 0; |
| 3777 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 3778 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3779 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3780 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3781 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3782 | err = btrfs_check_free_space(root, 1, 0); |
| 3783 | if (err) |
| 3784 | goto out_unlock; |
| 3785 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3786 | trans = btrfs_start_transaction(root, 1); |
| 3787 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3788 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3789 | if (IS_ERR(trans)) { |
| 3790 | err = PTR_ERR(trans); |
| 3791 | goto out_unlock; |
| 3792 | } |
| 3793 | |
| 3794 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 3795 | if (err) { |
| 3796 | err = -ENOSPC; |
| 3797 | goto out_unlock; |
| 3798 | } |
| 3799 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3800 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3801 | dentry->d_name.len, |
| 3802 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3803 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 3804 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3805 | if (IS_ERR(inode)) { |
| 3806 | err = PTR_ERR(inode); |
| 3807 | goto out_fail; |
| 3808 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3809 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3810 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3811 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3812 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3813 | if (err) |
| 3814 | goto out_fail; |
| 3815 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3816 | inode->i_op = &btrfs_dir_inode_operations; |
| 3817 | inode->i_fop = &btrfs_dir_file_operations; |
| 3818 | btrfs_set_trans_block_group(trans, inode); |
| 3819 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3820 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3821 | err = btrfs_update_inode(trans, root, inode); |
| 3822 | if (err) |
| 3823 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3824 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3825 | err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 3826 | inode, dentry->d_name.name, |
| 3827 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3828 | if (err) |
| 3829 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3830 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3831 | d_instantiate(dentry, inode); |
| 3832 | drop_on_err = 0; |
| 3833 | dir->i_sb->s_dirt = 1; |
| 3834 | btrfs_update_inode_block_group(trans, inode); |
| 3835 | btrfs_update_inode_block_group(trans, dir); |
| 3836 | |
| 3837 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3838 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 3839 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3840 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3841 | out_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3842 | if (drop_on_err) |
| 3843 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3844 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3845 | return err; |
| 3846 | } |
| 3847 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3848 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 3849 | * and an extent that you want to insert, deal with overlap and insert |
| 3850 | * the new extent into the tree. |
| 3851 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3852 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 3853 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3854 | struct extent_map *em, |
| 3855 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3856 | { |
| 3857 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3858 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3859 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 3860 | start_diff = map_start - em->start; |
| 3861 | em->start = map_start; |
| 3862 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3863 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 3864 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3865 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3866 | em->block_len -= start_diff; |
| 3867 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3868 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3869 | } |
| 3870 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3871 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 3872 | struct inode *inode, struct page *page, |
| 3873 | size_t pg_offset, u64 extent_offset, |
| 3874 | struct btrfs_file_extent_item *item) |
| 3875 | { |
| 3876 | int ret; |
| 3877 | struct extent_buffer *leaf = path->nodes[0]; |
| 3878 | char *tmp; |
| 3879 | size_t max_size; |
| 3880 | unsigned long inline_size; |
| 3881 | unsigned long ptr; |
| 3882 | |
| 3883 | WARN_ON(pg_offset != 0); |
| 3884 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 3885 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 3886 | btrfs_item_nr(leaf, path->slots[0])); |
| 3887 | tmp = kmalloc(inline_size, GFP_NOFS); |
| 3888 | ptr = btrfs_file_extent_inline_start(item); |
| 3889 | |
| 3890 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 3891 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 3892 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3893 | ret = btrfs_zlib_decompress(tmp, page, extent_offset, |
| 3894 | inline_size, max_size); |
| 3895 | if (ret) { |
| 3896 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 3897 | unsigned long copy_size = min_t(u64, |
| 3898 | PAGE_CACHE_SIZE - pg_offset, |
| 3899 | max_size - extent_offset); |
| 3900 | memset(kaddr + pg_offset, 0, copy_size); |
| 3901 | kunmap_atomic(kaddr, KM_USER0); |
| 3902 | } |
| 3903 | kfree(tmp); |
| 3904 | return 0; |
| 3905 | } |
| 3906 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3907 | /* |
| 3908 | * 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] | 3909 | * the ugly parts come from merging extents from the disk with the in-ram |
| 3910 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3911 | * where the in-ram extents might be locked pending data=ordered completion. |
| 3912 | * |
| 3913 | * This also copies inline extents directly into the page. |
| 3914 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3915 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3916 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3917 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3918 | int create) |
| 3919 | { |
| 3920 | int ret; |
| 3921 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3922 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3923 | u64 extent_start = 0; |
| 3924 | u64 extent_end = 0; |
| 3925 | u64 objectid = inode->i_ino; |
| 3926 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 3927 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3928 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3929 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3930 | struct extent_buffer *leaf; |
| 3931 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3932 | struct extent_map *em = NULL; |
| 3933 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3934 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3935 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3936 | int compressed; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3937 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3938 | again: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3939 | spin_lock(&em_tree->lock); |
| 3940 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3941 | if (em) |
| 3942 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3943 | spin_unlock(&em_tree->lock); |
| 3944 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3945 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 3946 | if (em->start > start || em->start + em->len <= start) |
| 3947 | free_extent_map(em); |
| 3948 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3949 | free_extent_map(em); |
| 3950 | else |
| 3951 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3952 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3953 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3954 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3955 | err = -ENOMEM; |
| 3956 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3957 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3958 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3959 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 3960 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3961 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3962 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 3963 | |
| 3964 | if (!path) { |
| 3965 | path = btrfs_alloc_path(); |
| 3966 | BUG_ON(!path); |
| 3967 | } |
| 3968 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3969 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 3970 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3971 | if (ret < 0) { |
| 3972 | err = ret; |
| 3973 | goto out; |
| 3974 | } |
| 3975 | |
| 3976 | if (ret != 0) { |
| 3977 | if (path->slots[0] == 0) |
| 3978 | goto not_found; |
| 3979 | path->slots[0]--; |
| 3980 | } |
| 3981 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3982 | leaf = path->nodes[0]; |
| 3983 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3984 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3985 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3986 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3987 | found_type = btrfs_key_type(&found_key); |
| 3988 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3989 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 3990 | goto not_found; |
| 3991 | } |
| 3992 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3993 | found_type = btrfs_file_extent_type(leaf, item); |
| 3994 | extent_start = found_key.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3995 | compressed = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3996 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 3997 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3998 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3999 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4000 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 4001 | size_t size; |
| 4002 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4003 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 4004 | ~((u64)root->sectorsize - 1); |
| 4005 | } |
| 4006 | |
| 4007 | if (start >= extent_end) { |
| 4008 | path->slots[0]++; |
| 4009 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 4010 | ret = btrfs_next_leaf(root, path); |
| 4011 | if (ret < 0) { |
| 4012 | err = ret; |
| 4013 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4014 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4015 | if (ret > 0) |
| 4016 | goto not_found; |
| 4017 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4018 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4019 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4020 | if (found_key.objectid != objectid || |
| 4021 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 4022 | goto not_found; |
| 4023 | if (start + len <= found_key.offset) |
| 4024 | goto not_found; |
| 4025 | em->start = start; |
| 4026 | em->len = found_key.offset - start; |
| 4027 | goto not_found_em; |
| 4028 | } |
| 4029 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4030 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4031 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4032 | em->start = extent_start; |
| 4033 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4034 | em->orig_start = extent_start - |
| 4035 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4036 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 4037 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4038 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4039 | goto insert; |
| 4040 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4041 | if (compressed) { |
| 4042 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 4043 | em->block_start = bytenr; |
| 4044 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 4045 | item); |
| 4046 | } else { |
| 4047 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 4048 | em->block_start = bytenr; |
| 4049 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4050 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 4051 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4052 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4053 | goto insert; |
| 4054 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4055 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4056 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4057 | size_t size; |
| 4058 | size_t extent_offset; |
| 4059 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4060 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4061 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4062 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4063 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4064 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4065 | goto out; |
| 4066 | } |
| 4067 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4068 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4069 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4070 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4071 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4072 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4073 | em->len = (copy_size + root->sectorsize - 1) & |
| 4074 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4075 | em->orig_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4076 | if (compressed) |
| 4077 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4078 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4079 | if (create == 0 && !PageUptodate(page)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4080 | if (btrfs_file_extent_compression(leaf, item) == |
| 4081 | BTRFS_COMPRESS_ZLIB) { |
| 4082 | ret = uncompress_inline(path, inode, page, |
| 4083 | pg_offset, |
| 4084 | extent_offset, item); |
| 4085 | BUG_ON(ret); |
| 4086 | } else { |
| 4087 | map = kmap(page); |
| 4088 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 4089 | copy_size); |
| 4090 | kunmap(page); |
| 4091 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4092 | flush_dcache_page(page); |
| 4093 | } else if (create && PageUptodate(page)) { |
| 4094 | if (!trans) { |
| 4095 | kunmap(page); |
| 4096 | free_extent_map(em); |
| 4097 | em = NULL; |
| 4098 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4099 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4100 | goto again; |
| 4101 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4102 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4103 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4104 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4105 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4106 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4107 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4108 | set_extent_uptodate(io_tree, em->start, |
| 4109 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4110 | goto insert; |
| 4111 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4112 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4113 | WARN_ON(1); |
| 4114 | } |
| 4115 | not_found: |
| 4116 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4117 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4118 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4119 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4120 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4121 | insert: |
| 4122 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4123 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4124 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 4125 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 4126 | (unsigned long long)em->len, |
| 4127 | (unsigned long long)start, |
| 4128 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4129 | err = -EIO; |
| 4130 | goto out; |
| 4131 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4132 | |
| 4133 | err = 0; |
| 4134 | spin_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4135 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4136 | /* it is possible that someone inserted the extent into the tree |
| 4137 | * while we had the lock dropped. It is also possible that |
| 4138 | * an overlapping map exists in the tree |
| 4139 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4140 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4141 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4142 | |
| 4143 | ret = 0; |
| 4144 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4145 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4146 | if (existing && (existing->start > start || |
| 4147 | existing->start + existing->len <= start)) { |
| 4148 | free_extent_map(existing); |
| 4149 | existing = NULL; |
| 4150 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4151 | if (!existing) { |
| 4152 | existing = lookup_extent_mapping(em_tree, em->start, |
| 4153 | em->len); |
| 4154 | if (existing) { |
| 4155 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4156 | em, start, |
| 4157 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4158 | free_extent_map(existing); |
| 4159 | if (err) { |
| 4160 | free_extent_map(em); |
| 4161 | em = NULL; |
| 4162 | } |
| 4163 | } else { |
| 4164 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4165 | free_extent_map(em); |
| 4166 | em = NULL; |
| 4167 | } |
| 4168 | } else { |
| 4169 | free_extent_map(em); |
| 4170 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4171 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4172 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4173 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4174 | spin_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4175 | out: |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4176 | if (path) |
| 4177 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4178 | if (trans) { |
| 4179 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4180 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4181 | err = ret; |
| 4182 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4183 | if (err) { |
| 4184 | free_extent_map(em); |
| 4185 | WARN_ON(1); |
| 4186 | return ERR_PTR(err); |
| 4187 | } |
| 4188 | return em; |
| 4189 | } |
| 4190 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4191 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 4192 | const struct iovec *iov, loff_t offset, |
| 4193 | unsigned long nr_segs) |
| 4194 | { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4195 | return -EINVAL; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4196 | } |
| 4197 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4198 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4199 | __u64 start, __u64 len) |
| 4200 | { |
| 4201 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent); |
| 4202 | } |
| 4203 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4204 | int btrfs_readpage(struct file *file, struct page *page) |
| 4205 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4206 | struct extent_io_tree *tree; |
| 4207 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4208 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4209 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4210 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4211 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 4212 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4213 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4214 | |
| 4215 | |
| 4216 | if (current->flags & PF_MEMALLOC) { |
| 4217 | redirty_page_for_writepage(wbc, page); |
| 4218 | unlock_page(page); |
| 4219 | return 0; |
| 4220 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4221 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4222 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 4223 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4224 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4225 | int btrfs_writepages(struct address_space *mapping, |
| 4226 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 4227 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4228 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4229 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4230 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 4231 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 4232 | } |
| 4233 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 4234 | static int |
| 4235 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 4236 | struct list_head *pages, unsigned nr_pages) |
| 4237 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4238 | struct extent_io_tree *tree; |
| 4239 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 4240 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 4241 | btrfs_get_extent); |
| 4242 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4243 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4244 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4245 | struct extent_io_tree *tree; |
| 4246 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4247 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4248 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4249 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 4250 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4251 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4252 | if (ret == 1) { |
| 4253 | ClearPagePrivate(page); |
| 4254 | set_page_private(page, 0); |
| 4255 | page_cache_release(page); |
| 4256 | } |
| 4257 | return ret; |
| 4258 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4259 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4260 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 4261 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 4262 | if (PageWriteback(page) || PageDirty(page)) |
| 4263 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 4264 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4265 | } |
| 4266 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4267 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 4268 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4269 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4270 | struct btrfs_ordered_extent *ordered; |
| 4271 | u64 page_start = page_offset(page); |
| 4272 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4273 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4274 | wait_on_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4275 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4276 | if (offset) { |
| 4277 | btrfs_releasepage(page, GFP_NOFS); |
| 4278 | return; |
| 4279 | } |
| 4280 | |
| 4281 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 4282 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 4283 | page_offset(page)); |
| 4284 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4285 | /* |
| 4286 | * IO on this page will never be started, so we need |
| 4287 | * to account for any ordered extents now |
| 4288 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4289 | clear_extent_bit(tree, page_start, page_end, |
| 4290 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4291 | EXTENT_LOCKED, 1, 0, GFP_NOFS); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 4292 | btrfs_finish_ordered_io(page->mapping->host, |
| 4293 | page_start, page_end); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4294 | btrfs_put_ordered_extent(ordered); |
| 4295 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 4296 | } |
| 4297 | clear_extent_bit(tree, page_start, page_end, |
| 4298 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4299 | EXTENT_ORDERED, |
| 4300 | 1, 1, GFP_NOFS); |
| 4301 | __btrfs_releasepage(page, GFP_NOFS); |
| 4302 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 4303 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 4304 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 4305 | ClearPagePrivate(page); |
| 4306 | set_page_private(page, 0); |
| 4307 | page_cache_release(page); |
| 4308 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4309 | } |
| 4310 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4311 | /* |
| 4312 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 4313 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 4314 | * be careful to check for EOF conditions here. We set the page up correctly |
| 4315 | * for a written page which means we get ENOSPC checking when writing into |
| 4316 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 4317 | * support these features. |
| 4318 | * |
| 4319 | * We are not allowed to take the i_mutex here so we have to play games to |
| 4320 | * protect against truncate races as the page could now be beyond EOF. Because |
| 4321 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 4322 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 4323 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 4324 | * unlock the page. |
| 4325 | */ |
| 4326 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page) |
| 4327 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4328 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4329 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4330 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4331 | struct btrfs_ordered_extent *ordered; |
| 4332 | char *kaddr; |
| 4333 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4334 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4335 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4336 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4337 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4338 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4339 | ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4340 | if (ret) |
| 4341 | goto out; |
| 4342 | |
| 4343 | ret = -EINVAL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4344 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4345 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4346 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4347 | page_start = page_offset(page); |
| 4348 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4349 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4350 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4351 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4352 | /* page got truncated out from underneath us */ |
| 4353 | goto out_unlock; |
| 4354 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4355 | wait_on_page_writeback(page); |
| 4356 | |
| 4357 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 4358 | set_page_extent_mapped(page); |
| 4359 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4360 | /* |
| 4361 | * we can't set the delalloc bits if there are pending ordered |
| 4362 | * extents. Drop our locks and wait for them to finish |
| 4363 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4364 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4365 | if (ordered) { |
| 4366 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 4367 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4368 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4369 | btrfs_put_ordered_extent(ordered); |
| 4370 | goto again; |
| 4371 | } |
| 4372 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 4373 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4374 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4375 | |
| 4376 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4377 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4378 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4379 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4380 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4381 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4382 | if (zero_start != PAGE_CACHE_SIZE) { |
| 4383 | kaddr = kmap(page); |
| 4384 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 4385 | flush_dcache_page(page); |
| 4386 | kunmap(page); |
| 4387 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4388 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4389 | set_page_dirty(page); |
| 4390 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4391 | |
| 4392 | out_unlock: |
| 4393 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4394 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4395 | return ret; |
| 4396 | } |
| 4397 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4398 | static void btrfs_truncate(struct inode *inode) |
| 4399 | { |
| 4400 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4401 | int ret; |
| 4402 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4403 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4404 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4405 | |
| 4406 | if (!S_ISREG(inode->i_mode)) |
| 4407 | return; |
| 4408 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 4409 | return; |
| 4410 | |
| 4411 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 4412 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4413 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4414 | trans = btrfs_start_transaction(root, 1); |
| 4415 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4416 | btrfs_i_size_write(inode, inode->i_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4417 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4418 | ret = btrfs_orphan_add(trans, inode); |
| 4419 | if (ret) |
| 4420 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4421 | /* FIXME, add redo link to tree so we don't leak on crash */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4422 | ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4423 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4424 | btrfs_update_inode(trans, root, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4425 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4426 | ret = btrfs_orphan_del(trans, inode); |
| 4427 | BUG_ON(ret); |
| 4428 | |
| 4429 | out: |
| 4430 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4431 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4432 | BUG_ON(ret); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4433 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4434 | } |
| 4435 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 4436 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4437 | * create a new subvolume directory/inode (helper for the ioctl). |
| 4438 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4439 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
| 4440 | struct btrfs_root *new_root, struct dentry *dentry, |
| 4441 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4442 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4443 | struct inode *inode; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 4444 | int error; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4445 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4446 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4447 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4448 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4449 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4450 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4451 | inode->i_op = &btrfs_dir_inode_operations; |
| 4452 | inode->i_fop = &btrfs_dir_file_operations; |
| 4453 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4454 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4455 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 4456 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 4457 | error = btrfs_update_inode(trans, new_root, inode); |
| 4458 | if (error) |
| 4459 | return error; |
| 4460 | |
| 4461 | d_instantiate(dentry, inode); |
| 4462 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4463 | } |
| 4464 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4465 | /* helper function for file defrag and space balancing. This |
| 4466 | * forces readahead on a given range of bytes in an inode |
| 4467 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4468 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4469 | struct file_ra_state *ra, struct file *file, |
| 4470 | pgoff_t offset, pgoff_t last_index) |
| 4471 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 4472 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4473 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4474 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 4475 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4476 | } |
| 4477 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4478 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 4479 | { |
| 4480 | struct btrfs_inode *ei; |
| 4481 | |
| 4482 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 4483 | if (!ei) |
| 4484 | return NULL; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 4485 | ei->last_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4486 | ei->logged_trans = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4487 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4488 | ei->i_acl = BTRFS_ACL_NOT_CACHED; |
| 4489 | ei->i_default_acl = BTRFS_ACL_NOT_CACHED; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4490 | INIT_LIST_HEAD(&ei->i_orphan); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4491 | return &ei->vfs_inode; |
| 4492 | } |
| 4493 | |
| 4494 | void btrfs_destroy_inode(struct inode *inode) |
| 4495 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4496 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4497 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 4498 | WARN_ON(inode->i_data.nrpages); |
| 4499 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4500 | if (BTRFS_I(inode)->i_acl && |
| 4501 | BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED) |
| 4502 | posix_acl_release(BTRFS_I(inode)->i_acl); |
| 4503 | if (BTRFS_I(inode)->i_default_acl && |
| 4504 | BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED) |
| 4505 | posix_acl_release(BTRFS_I(inode)->i_default_acl); |
| 4506 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 4507 | spin_lock(&BTRFS_I(inode)->root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4508 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 4509 | printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan" |
| 4510 | " list\n", inode->i_ino); |
| 4511 | dump_stack(); |
| 4512 | } |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 4513 | spin_unlock(&BTRFS_I(inode)->root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4514 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4515 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4516 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 4517 | if (!ordered) |
| 4518 | break; |
| 4519 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4520 | printk(KERN_ERR "btrfs found ordered " |
| 4521 | "extent %llu %llu on inode cleanup\n", |
| 4522 | (unsigned long long)ordered->file_offset, |
| 4523 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4524 | btrfs_remove_ordered_extent(inode, ordered); |
| 4525 | btrfs_put_ordered_extent(ordered); |
| 4526 | btrfs_put_ordered_extent(ordered); |
| 4527 | } |
| 4528 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 4529 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4530 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 4531 | } |
| 4532 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 4533 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4534 | { |
| 4535 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 4536 | |
| 4537 | inode_init_once(&ei->vfs_inode); |
| 4538 | } |
| 4539 | |
| 4540 | void btrfs_destroy_cachep(void) |
| 4541 | { |
| 4542 | if (btrfs_inode_cachep) |
| 4543 | kmem_cache_destroy(btrfs_inode_cachep); |
| 4544 | if (btrfs_trans_handle_cachep) |
| 4545 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 4546 | if (btrfs_transaction_cachep) |
| 4547 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 4548 | if (btrfs_bit_radix_cachep) |
| 4549 | kmem_cache_destroy(btrfs_bit_radix_cachep); |
| 4550 | if (btrfs_path_cachep) |
| 4551 | kmem_cache_destroy(btrfs_path_cachep); |
| 4552 | } |
| 4553 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4554 | struct kmem_cache *btrfs_cache_create(const char *name, size_t size, |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 4555 | unsigned long extra_flags, |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 4556 | void (*ctor)(void *)) |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 4557 | { |
| 4558 | return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT | |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 4559 | SLAB_MEM_SPREAD | extra_flags), ctor); |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 4560 | } |
| 4561 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4562 | int btrfs_init_cachep(void) |
| 4563 | { |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4564 | btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache", |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 4565 | sizeof(struct btrfs_inode), |
| 4566 | 0, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4567 | if (!btrfs_inode_cachep) |
| 4568 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4569 | btrfs_trans_handle_cachep = |
| 4570 | btrfs_cache_create("btrfs_trans_handle_cache", |
| 4571 | sizeof(struct btrfs_trans_handle), |
| 4572 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4573 | if (!btrfs_trans_handle_cachep) |
| 4574 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4575 | btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache", |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4576 | sizeof(struct btrfs_transaction), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 4577 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4578 | if (!btrfs_transaction_cachep) |
| 4579 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4580 | btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache", |
Yan | 2322358 | 2007-09-17 11:08:52 -0400 | [diff] [blame] | 4581 | sizeof(struct btrfs_path), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 4582 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4583 | if (!btrfs_path_cachep) |
| 4584 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4585 | btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256, |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 4586 | SLAB_DESTROY_BY_RCU, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4587 | if (!btrfs_bit_radix_cachep) |
| 4588 | goto fail; |
| 4589 | return 0; |
| 4590 | fail: |
| 4591 | btrfs_destroy_cachep(); |
| 4592 | return -ENOMEM; |
| 4593 | } |
| 4594 | |
| 4595 | static int btrfs_getattr(struct vfsmount *mnt, |
| 4596 | struct dentry *dentry, struct kstat *stat) |
| 4597 | { |
| 4598 | struct inode *inode = dentry->d_inode; |
| 4599 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 4600 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 4601 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4602 | stat->blocks = (inode_get_bytes(inode) + |
| 4603 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4604 | return 0; |
| 4605 | } |
| 4606 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4607 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 4608 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4609 | { |
| 4610 | struct btrfs_trans_handle *trans; |
| 4611 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 4612 | struct inode *new_inode = new_dentry->d_inode; |
| 4613 | struct inode *old_inode = old_dentry->d_inode; |
| 4614 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4615 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4616 | int ret; |
| 4617 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 4618 | /* we're not allowed to rename between subvolumes */ |
| 4619 | if (BTRFS_I(old_inode)->root->root_key.objectid != |
| 4620 | BTRFS_I(new_dir)->root->root_key.objectid) |
| 4621 | return -EXDEV; |
| 4622 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4623 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
| 4624 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { |
| 4625 | return -ENOTEMPTY; |
| 4626 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4627 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 4628 | /* to rename a snapshot or subvolume, we need to juggle the |
| 4629 | * backrefs. This isn't coded yet |
| 4630 | */ |
| 4631 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 4632 | return -EXDEV; |
| 4633 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4634 | ret = btrfs_check_free_space(root, 1, 0); |
| 4635 | if (ret) |
| 4636 | goto out_unlock; |
| 4637 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4638 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4639 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4640 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4641 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4642 | btrfs_inc_nlink(old_dentry->d_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4643 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 4644 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 4645 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4646 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4647 | ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode, |
| 4648 | old_dentry->d_name.name, |
| 4649 | old_dentry->d_name.len); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4650 | if (ret) |
| 4651 | goto out_fail; |
| 4652 | |
| 4653 | if (new_inode) { |
| 4654 | new_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4655 | ret = btrfs_unlink_inode(trans, root, new_dir, |
| 4656 | new_dentry->d_inode, |
| 4657 | new_dentry->d_name.name, |
| 4658 | new_dentry->d_name.len); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4659 | if (ret) |
| 4660 | goto out_fail; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4661 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4662 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4663 | if (ret) |
| 4664 | goto out_fail; |
| 4665 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4666 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4667 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4668 | ret = btrfs_set_inode_index(new_dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4669 | if (ret) |
| 4670 | goto out_fail; |
| 4671 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4672 | ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode, |
| 4673 | old_inode, new_dentry->d_name.name, |
| 4674 | new_dentry->d_name.len, 1, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4675 | if (ret) |
| 4676 | goto out_fail; |
| 4677 | |
| 4678 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4679 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4680 | out_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4681 | return ret; |
| 4682 | } |
| 4683 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4684 | /* |
| 4685 | * some fairly slow code that needs optimization. This walks the list |
| 4686 | * of all the inodes with pending delalloc and forces them to disk. |
| 4687 | */ |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 4688 | int btrfs_start_delalloc_inodes(struct btrfs_root *root) |
| 4689 | { |
| 4690 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 4691 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 4692 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 4693 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 4694 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 4695 | return -EROFS; |
| 4696 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 4697 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4698 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 4699 | binode = list_entry(head->next, struct btrfs_inode, |
| 4700 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 4701 | inode = igrab(&binode->vfs_inode); |
| 4702 | if (!inode) |
| 4703 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 4704 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 4705 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 4706 | filemap_flush(inode->i_mapping); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 4707 | iput(inode); |
| 4708 | } |
| 4709 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 4710 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 4711 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 4712 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 4713 | |
| 4714 | /* the filemap_flush will queue IO into the worker threads, but |
| 4715 | * we have to make sure the IO is actually started and that |
| 4716 | * ordered extents get created before we return |
| 4717 | */ |
| 4718 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4719 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4720 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 4721 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4722 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 4723 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 4724 | } |
| 4725 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 4726 | return 0; |
| 4727 | } |
| 4728 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4729 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 4730 | const char *symname) |
| 4731 | { |
| 4732 | struct btrfs_trans_handle *trans; |
| 4733 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4734 | struct btrfs_path *path; |
| 4735 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4736 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4737 | int err; |
| 4738 | int drop_inode = 0; |
| 4739 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4740 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4741 | int name_len; |
| 4742 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4743 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4744 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4745 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4746 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4747 | |
| 4748 | name_len = strlen(symname) + 1; |
| 4749 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 4750 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4751 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4752 | err = btrfs_check_free_space(root, 1, 0); |
| 4753 | if (err) |
| 4754 | goto out_fail; |
| 4755 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4756 | trans = btrfs_start_transaction(root, 1); |
| 4757 | btrfs_set_trans_block_group(trans, dir); |
| 4758 | |
| 4759 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4760 | if (err) { |
| 4761 | err = -ENOSPC; |
| 4762 | goto out_unlock; |
| 4763 | } |
| 4764 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4765 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4766 | dentry->d_name.len, |
| 4767 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4768 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 4769 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4770 | err = PTR_ERR(inode); |
| 4771 | if (IS_ERR(inode)) |
| 4772 | goto out_unlock; |
| 4773 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 4774 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4775 | if (err) { |
| 4776 | drop_inode = 1; |
| 4777 | goto out_unlock; |
| 4778 | } |
| 4779 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4780 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4781 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4782 | if (err) |
| 4783 | drop_inode = 1; |
| 4784 | else { |
| 4785 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4786 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4787 | inode->i_fop = &btrfs_file_operations; |
| 4788 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4789 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4790 | } |
| 4791 | dir->i_sb->s_dirt = 1; |
| 4792 | btrfs_update_inode_block_group(trans, inode); |
| 4793 | btrfs_update_inode_block_group(trans, dir); |
| 4794 | if (drop_inode) |
| 4795 | goto out_unlock; |
| 4796 | |
| 4797 | path = btrfs_alloc_path(); |
| 4798 | BUG_ON(!path); |
| 4799 | key.objectid = inode->i_ino; |
| 4800 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4801 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 4802 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 4803 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 4804 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4805 | if (err) { |
| 4806 | drop_inode = 1; |
| 4807 | goto out_unlock; |
| 4808 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4809 | leaf = path->nodes[0]; |
| 4810 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 4811 | struct btrfs_file_extent_item); |
| 4812 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 4813 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4814 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4815 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 4816 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 4817 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 4818 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 4819 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4820 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4821 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 4822 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4823 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4824 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4825 | inode->i_op = &btrfs_symlink_inode_operations; |
| 4826 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4827 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4828 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4829 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4830 | err = btrfs_update_inode(trans, root, inode); |
| 4831 | if (err) |
| 4832 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4833 | |
| 4834 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4835 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4836 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4837 | out_fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4838 | if (drop_inode) { |
| 4839 | inode_dec_link_count(inode); |
| 4840 | iput(inode); |
| 4841 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4842 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4843 | return err; |
| 4844 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4845 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4846 | static int prealloc_file_range(struct inode *inode, u64 start, u64 end, |
| 4847 | u64 alloc_hint, int mode) |
| 4848 | { |
| 4849 | struct btrfs_trans_handle *trans; |
| 4850 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4851 | struct btrfs_key ins; |
| 4852 | u64 alloc_size; |
| 4853 | u64 cur_offset = start; |
| 4854 | u64 num_bytes = end - start; |
| 4855 | int ret = 0; |
| 4856 | |
| 4857 | trans = btrfs_join_transaction(root, 1); |
| 4858 | BUG_ON(!trans); |
| 4859 | btrfs_set_trans_block_group(trans, inode); |
| 4860 | |
| 4861 | while (num_bytes > 0) { |
| 4862 | alloc_size = min(num_bytes, root->fs_info->max_extent); |
| 4863 | ret = btrfs_reserve_extent(trans, root, alloc_size, |
| 4864 | root->sectorsize, 0, alloc_hint, |
| 4865 | (u64)-1, &ins, 1); |
| 4866 | if (ret) { |
| 4867 | WARN_ON(1); |
| 4868 | goto out; |
| 4869 | } |
| 4870 | ret = insert_reserved_file_extent(trans, inode, |
| 4871 | cur_offset, ins.objectid, |
| 4872 | ins.offset, ins.offset, |
| 4873 | ins.offset, 0, 0, 0, |
| 4874 | BTRFS_FILE_EXTENT_PREALLOC); |
| 4875 | BUG_ON(ret); |
| 4876 | num_bytes -= ins.offset; |
| 4877 | cur_offset += ins.offset; |
| 4878 | alloc_hint = ins.objectid + ins.offset; |
| 4879 | } |
| 4880 | out: |
| 4881 | if (cur_offset > start) { |
| 4882 | inode->i_ctime = CURRENT_TIME; |
| 4883 | btrfs_set_flag(inode, PREALLOC); |
| 4884 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
| 4885 | cur_offset > i_size_read(inode)) |
| 4886 | btrfs_i_size_write(inode, cur_offset); |
| 4887 | ret = btrfs_update_inode(trans, root, inode); |
| 4888 | BUG_ON(ret); |
| 4889 | } |
| 4890 | |
| 4891 | btrfs_end_transaction(trans, root); |
| 4892 | return ret; |
| 4893 | } |
| 4894 | |
| 4895 | static long btrfs_fallocate(struct inode *inode, int mode, |
| 4896 | loff_t offset, loff_t len) |
| 4897 | { |
| 4898 | u64 cur_offset; |
| 4899 | u64 last_byte; |
| 4900 | u64 alloc_start; |
| 4901 | u64 alloc_end; |
| 4902 | u64 alloc_hint = 0; |
| 4903 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 4904 | struct extent_map *em; |
| 4905 | int ret; |
| 4906 | |
| 4907 | alloc_start = offset & ~mask; |
| 4908 | alloc_end = (offset + len + mask) & ~mask; |
| 4909 | |
| 4910 | mutex_lock(&inode->i_mutex); |
| 4911 | if (alloc_start > inode->i_size) { |
| 4912 | ret = btrfs_cont_expand(inode, alloc_start); |
| 4913 | if (ret) |
| 4914 | goto out; |
| 4915 | } |
| 4916 | |
| 4917 | while (1) { |
| 4918 | struct btrfs_ordered_extent *ordered; |
| 4919 | lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, |
| 4920 | alloc_end - 1, GFP_NOFS); |
| 4921 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 4922 | alloc_end - 1); |
| 4923 | if (ordered && |
| 4924 | ordered->file_offset + ordered->len > alloc_start && |
| 4925 | ordered->file_offset < alloc_end) { |
| 4926 | btrfs_put_ordered_extent(ordered); |
| 4927 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 4928 | alloc_start, alloc_end - 1, GFP_NOFS); |
| 4929 | btrfs_wait_ordered_range(inode, alloc_start, |
| 4930 | alloc_end - alloc_start); |
| 4931 | } else { |
| 4932 | if (ordered) |
| 4933 | btrfs_put_ordered_extent(ordered); |
| 4934 | break; |
| 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | cur_offset = alloc_start; |
| 4939 | while (1) { |
| 4940 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4941 | alloc_end - cur_offset, 0); |
| 4942 | BUG_ON(IS_ERR(em) || !em); |
| 4943 | last_byte = min(extent_map_end(em), alloc_end); |
| 4944 | last_byte = (last_byte + mask) & ~mask; |
| 4945 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 4946 | ret = prealloc_file_range(inode, cur_offset, |
| 4947 | last_byte, alloc_hint, mode); |
| 4948 | if (ret < 0) { |
| 4949 | free_extent_map(em); |
| 4950 | break; |
| 4951 | } |
| 4952 | } |
| 4953 | if (em->block_start <= EXTENT_MAP_LAST_BYTE) |
| 4954 | alloc_hint = em->block_start; |
| 4955 | free_extent_map(em); |
| 4956 | |
| 4957 | cur_offset = last_byte; |
| 4958 | if (cur_offset >= alloc_end) { |
| 4959 | ret = 0; |
| 4960 | break; |
| 4961 | } |
| 4962 | } |
| 4963 | unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1, |
| 4964 | GFP_NOFS); |
| 4965 | out: |
| 4966 | mutex_unlock(&inode->i_mutex); |
| 4967 | return ret; |
| 4968 | } |
| 4969 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4970 | static int btrfs_set_page_dirty(struct page *page) |
| 4971 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4972 | return __set_page_dirty_nobuffers(page); |
| 4973 | } |
| 4974 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 4975 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 4976 | { |
| 4977 | if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE)) |
| 4978 | return -EACCES; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4979 | return generic_permission(inode, mask, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 4980 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4981 | |
| 4982 | static struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 4983 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4984 | .lookup = btrfs_lookup, |
| 4985 | .create = btrfs_create, |
| 4986 | .unlink = btrfs_unlink, |
| 4987 | .link = btrfs_link, |
| 4988 | .mkdir = btrfs_mkdir, |
| 4989 | .rmdir = btrfs_rmdir, |
| 4990 | .rename = btrfs_rename, |
| 4991 | .symlink = btrfs_symlink, |
| 4992 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4993 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 4994 | .setxattr = btrfs_setxattr, |
| 4995 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4996 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 4997 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 4998 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4999 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5000 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
| 5001 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5002 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5003 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5004 | static struct file_operations btrfs_dir_file_operations = { |
| 5005 | .llseek = generic_file_llseek, |
| 5006 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 5007 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 5008 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5009 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 5010 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5011 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 5012 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5013 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5014 | }; |
| 5015 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5016 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5017 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 5018 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5019 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5020 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5021 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 5022 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5023 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 5024 | .set_bit_hook = btrfs_set_bit_hook, |
| 5025 | .clear_bit_hook = btrfs_clear_bit_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5026 | }; |
| 5027 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 5028 | /* |
| 5029 | * btrfs doesn't support the bmap operation because swapfiles |
| 5030 | * use bmap to make a mapping of extents in the file. They assume |
| 5031 | * these extents won't change over the life of the file and they |
| 5032 | * use the bmap result to do IO directly to the drive. |
| 5033 | * |
| 5034 | * the btrfs bmap call would return logical addresses that aren't |
| 5035 | * suitable for IO and they also will change frequently as COW |
| 5036 | * operations happen. So, swapfile + btrfs == corruption. |
| 5037 | * |
| 5038 | * For now we're avoiding this by dropping bmap. |
| 5039 | */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5040 | static struct address_space_operations btrfs_aops = { |
| 5041 | .readpage = btrfs_readpage, |
| 5042 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 5043 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 5044 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5045 | .sync_page = block_sync_page, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 5046 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5047 | .invalidatepage = btrfs_invalidatepage, |
| 5048 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5049 | .set_page_dirty = btrfs_set_page_dirty, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5050 | }; |
| 5051 | |
| 5052 | static struct address_space_operations btrfs_symlink_aops = { |
| 5053 | .readpage = btrfs_readpage, |
| 5054 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 5055 | .invalidatepage = btrfs_invalidatepage, |
| 5056 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5057 | }; |
| 5058 | |
| 5059 | static struct inode_operations btrfs_file_inode_operations = { |
| 5060 | .truncate = btrfs_truncate, |
| 5061 | .getattr = btrfs_getattr, |
| 5062 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5063 | .setxattr = btrfs_setxattr, |
| 5064 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5065 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5066 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5067 | .permission = btrfs_permission, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5068 | .fallocate = btrfs_fallocate, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5069 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5070 | }; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5071 | static struct inode_operations btrfs_special_inode_operations = { |
| 5072 | .getattr = btrfs_getattr, |
| 5073 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5074 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5075 | .setxattr = btrfs_setxattr, |
| 5076 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5077 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5078 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5079 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5080 | static struct inode_operations btrfs_symlink_inode_operations = { |
| 5081 | .readlink = generic_readlink, |
| 5082 | .follow_link = page_follow_link_light, |
| 5083 | .put_link = page_put_link, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5084 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 5085 | .setxattr = btrfs_setxattr, |
| 5086 | .getxattr = btrfs_getxattr, |
| 5087 | .listxattr = btrfs_listxattr, |
| 5088 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5089 | }; |