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