Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 20 | #include <linux/bio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 22 | #include <linux/file.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 23 | #include <linux/fs.h> |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/string.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 29 | #include <linux/backing-dev.h> |
| 30 | #include <linux/mpage.h> |
| 31 | #include <linux/swap.h> |
| 32 | #include <linux/writeback.h> |
| 33 | #include <linux/statfs.h> |
| 34 | #include <linux/compat.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 35 | #include <linux/bit_spinlock.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 36 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 37 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 38 | #include <linux/falloc.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 39 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 40 | #include "ctree.h" |
| 41 | #include "disk-io.h" |
| 42 | #include "transaction.h" |
| 43 | #include "btrfs_inode.h" |
| 44 | #include "ioctl.h" |
| 45 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 46 | #include "volumes.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 47 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 48 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 49 | #include "tree-log.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 50 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 51 | #include "locking.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 52 | |
| 53 | struct btrfs_iget_args { |
| 54 | u64 ino; |
| 55 | struct btrfs_root *root; |
| 56 | }; |
| 57 | |
| 58 | static struct inode_operations btrfs_dir_inode_operations; |
| 59 | static struct inode_operations btrfs_symlink_inode_operations; |
| 60 | static struct inode_operations btrfs_dir_ro_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 61 | static struct inode_operations btrfs_special_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 62 | static struct inode_operations btrfs_file_inode_operations; |
| 63 | static struct address_space_operations btrfs_aops; |
| 64 | static struct address_space_operations btrfs_symlink_aops; |
| 65 | static struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 66 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 67 | |
| 68 | static struct kmem_cache *btrfs_inode_cachep; |
| 69 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 70 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 71 | struct kmem_cache *btrfs_path_cachep; |
| 72 | |
| 73 | #define S_SHIFT 12 |
| 74 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 75 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 76 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 77 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 78 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 79 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 80 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 81 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 82 | }; |
| 83 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 84 | static void btrfs_truncate(struct inode *inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 85 | static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 86 | static noinline int cow_file_range(struct inode *inode, |
| 87 | struct page *locked_page, |
| 88 | u64 start, u64 end, int *page_started, |
| 89 | unsigned long *nr_written, int unlock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 90 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 91 | static int btrfs_init_inode_security(struct inode *inode, struct inode *dir) |
| 92 | { |
| 93 | int err; |
| 94 | |
| 95 | err = btrfs_init_acl(inode, dir); |
| 96 | if (!err) |
| 97 | err = btrfs_xattr_security_init(inode, dir); |
| 98 | return err; |
| 99 | } |
| 100 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 101 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 102 | * this does all the hard work for inserting an inline extent into |
| 103 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 104 | * no overlapping inline items exist in the btree |
| 105 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 106 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 107 | struct btrfs_root *root, struct inode *inode, |
| 108 | u64 start, size_t size, size_t compressed_size, |
| 109 | struct page **compressed_pages) |
| 110 | { |
| 111 | struct btrfs_key key; |
| 112 | struct btrfs_path *path; |
| 113 | struct extent_buffer *leaf; |
| 114 | struct page *page = NULL; |
| 115 | char *kaddr; |
| 116 | unsigned long ptr; |
| 117 | struct btrfs_file_extent_item *ei; |
| 118 | int err = 0; |
| 119 | int ret; |
| 120 | size_t cur_size = size; |
| 121 | size_t datasize; |
| 122 | unsigned long offset; |
| 123 | int use_compress = 0; |
| 124 | |
| 125 | if (compressed_size && compressed_pages) { |
| 126 | use_compress = 1; |
| 127 | cur_size = compressed_size; |
| 128 | } |
| 129 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 130 | path = btrfs_alloc_path(); |
| 131 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 132 | return -ENOMEM; |
| 133 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 134 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 135 | btrfs_set_trans_block_group(trans, inode); |
| 136 | |
| 137 | key.objectid = inode->i_ino; |
| 138 | key.offset = start; |
| 139 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 140 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 141 | |
| 142 | inode_add_bytes(inode, size); |
| 143 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 144 | datasize); |
| 145 | BUG_ON(ret); |
| 146 | if (ret) { |
| 147 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 148 | goto fail; |
| 149 | } |
| 150 | leaf = path->nodes[0]; |
| 151 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 152 | struct btrfs_file_extent_item); |
| 153 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 154 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 155 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 156 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 157 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 158 | ptr = btrfs_file_extent_inline_start(ei); |
| 159 | |
| 160 | if (use_compress) { |
| 161 | struct page *cpage; |
| 162 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 163 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 164 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 165 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | PAGE_CACHE_SIZE); |
| 167 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 168 | kaddr = kmap_atomic(cpage, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 169 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 170 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | |
| 172 | i++; |
| 173 | ptr += cur_size; |
| 174 | compressed_size -= cur_size; |
| 175 | } |
| 176 | btrfs_set_file_extent_compression(leaf, ei, |
| 177 | BTRFS_COMPRESS_ZLIB); |
| 178 | } else { |
| 179 | page = find_get_page(inode->i_mapping, |
| 180 | start >> PAGE_CACHE_SHIFT); |
| 181 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 182 | kaddr = kmap_atomic(page, KM_USER0); |
| 183 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 184 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
| 185 | kunmap_atomic(kaddr, KM_USER0); |
| 186 | page_cache_release(page); |
| 187 | } |
| 188 | btrfs_mark_buffer_dirty(leaf); |
| 189 | btrfs_free_path(path); |
| 190 | |
| 191 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
| 192 | btrfs_update_inode(trans, root, inode); |
| 193 | return 0; |
| 194 | fail: |
| 195 | btrfs_free_path(path); |
| 196 | return err; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /* |
| 201 | * conditionally insert an inline extent into the file. This |
| 202 | * does the checks required to make sure the data is small enough |
| 203 | * to fit as an inline extent. |
| 204 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 205 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 206 | struct btrfs_root *root, |
| 207 | struct inode *inode, u64 start, u64 end, |
| 208 | size_t compressed_size, |
| 209 | struct page **compressed_pages) |
| 210 | { |
| 211 | u64 isize = i_size_read(inode); |
| 212 | u64 actual_end = min(end + 1, isize); |
| 213 | u64 inline_len = actual_end - start; |
| 214 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 215 | ~((u64)root->sectorsize - 1); |
| 216 | u64 hint_byte; |
| 217 | u64 data_len = inline_len; |
| 218 | int ret; |
| 219 | |
| 220 | if (compressed_size) |
| 221 | data_len = compressed_size; |
| 222 | |
| 223 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 224 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 225 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 226 | (!compressed_size && |
| 227 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 228 | end + 1 < isize || |
| 229 | data_len > root->fs_info->max_inline) { |
| 230 | return 1; |
| 231 | } |
| 232 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 233 | ret = btrfs_drop_extents(trans, root, inode, start, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 234 | aligned_end, aligned_end, start, |
| 235 | &hint_byte, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 236 | BUG_ON(ret); |
| 237 | |
| 238 | if (isize > actual_end) |
| 239 | inline_len = min_t(u64, isize, actual_end); |
| 240 | ret = insert_inline_extent(trans, root, inode, start, |
| 241 | inline_len, compressed_size, |
| 242 | compressed_pages); |
| 243 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 244 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 248 | struct async_extent { |
| 249 | u64 start; |
| 250 | u64 ram_size; |
| 251 | u64 compressed_size; |
| 252 | struct page **pages; |
| 253 | unsigned long nr_pages; |
| 254 | struct list_head list; |
| 255 | }; |
| 256 | |
| 257 | struct async_cow { |
| 258 | struct inode *inode; |
| 259 | struct btrfs_root *root; |
| 260 | struct page *locked_page; |
| 261 | u64 start; |
| 262 | u64 end; |
| 263 | struct list_head extents; |
| 264 | struct btrfs_work work; |
| 265 | }; |
| 266 | |
| 267 | static noinline int add_async_extent(struct async_cow *cow, |
| 268 | u64 start, u64 ram_size, |
| 269 | u64 compressed_size, |
| 270 | struct page **pages, |
| 271 | unsigned long nr_pages) |
| 272 | { |
| 273 | struct async_extent *async_extent; |
| 274 | |
| 275 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
| 276 | async_extent->start = start; |
| 277 | async_extent->ram_size = ram_size; |
| 278 | async_extent->compressed_size = compressed_size; |
| 279 | async_extent->pages = pages; |
| 280 | async_extent->nr_pages = nr_pages; |
| 281 | list_add_tail(&async_extent->list, &cow->extents); |
| 282 | return 0; |
| 283 | } |
| 284 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 285 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 286 | * we create compressed extents in two phases. The first |
| 287 | * phase compresses a range of pages that have already been |
| 288 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 289 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 290 | * This is done inside an ordered work queue, and the compression |
| 291 | * is spread across many cpus. The actual IO submission is step |
| 292 | * two, and the ordered work queue takes care of making sure that |
| 293 | * happens in the same order things were put onto the queue by |
| 294 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 295 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 296 | * If this code finds it can't get good compression, it puts an |
| 297 | * entry onto the work queue to write the uncompressed bytes. This |
| 298 | * makes sure that both compressed inodes and uncompressed inodes |
| 299 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 300 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 301 | static noinline int compress_file_range(struct inode *inode, |
| 302 | struct page *locked_page, |
| 303 | u64 start, u64 end, |
| 304 | struct async_cow *async_cow, |
| 305 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 306 | { |
| 307 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 308 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 309 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 310 | u64 orig_start; |
| 311 | u64 disk_num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 312 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 313 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 314 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 315 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 316 | struct page **pages = NULL; |
| 317 | unsigned long nr_pages; |
| 318 | unsigned long nr_pages_ret = 0; |
| 319 | unsigned long total_compressed = 0; |
| 320 | unsigned long total_in = 0; |
| 321 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 322 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 323 | int i; |
| 324 | int will_compress; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 325 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 326 | orig_start = start; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 327 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 328 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 329 | again: |
| 330 | will_compress = 0; |
| 331 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 332 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 333 | |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 334 | /* |
| 335 | * we don't want to send crud past the end of i_size through |
| 336 | * compression, that's just a waste of CPU time. So, if the |
| 337 | * end of the file is before the start of our current |
| 338 | * requested range of bytes, we bail out to the uncompressed |
| 339 | * cleanup code that can deal with all of this. |
| 340 | * |
| 341 | * It isn't really the fastest way to fix things, but this is a |
| 342 | * very uncommon corner. |
| 343 | */ |
| 344 | if (actual_end <= start) |
| 345 | goto cleanup_and_bail_uncompressed; |
| 346 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 347 | total_compressed = actual_end - start; |
| 348 | |
| 349 | /* we want to make sure that amount of ram required to uncompress |
| 350 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 351 | * of a compressed extent to 128k. This is a crucial number |
| 352 | * because it also controls how easily we can spread reads across |
| 353 | * cpus for decompression. |
| 354 | * |
| 355 | * We also want to make sure the amount of IO required to do |
| 356 | * a random read is reasonably small, so we limit the size of |
| 357 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 358 | */ |
| 359 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 360 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 361 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 362 | disk_num_bytes = num_bytes; |
| 363 | total_in = 0; |
| 364 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 365 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 366 | /* |
| 367 | * we do compression for mount -o compress and when the |
| 368 | * inode has not been flagged as nocompress. This flag can |
| 369 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 370 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 371 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 372 | btrfs_test_opt(root, COMPRESS)) { |
| 373 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 374 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 375 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 376 | ret = btrfs_zlib_compress_pages(inode->i_mapping, start, |
| 377 | total_compressed, pages, |
| 378 | nr_pages, &nr_pages_ret, |
| 379 | &total_in, |
| 380 | &total_compressed, |
| 381 | max_compressed); |
| 382 | |
| 383 | if (!ret) { |
| 384 | unsigned long offset = total_compressed & |
| 385 | (PAGE_CACHE_SIZE - 1); |
| 386 | struct page *page = pages[nr_pages_ret - 1]; |
| 387 | char *kaddr; |
| 388 | |
| 389 | /* zero the tail end of the last page, we might be |
| 390 | * sending it down to disk |
| 391 | */ |
| 392 | if (offset) { |
| 393 | kaddr = kmap_atomic(page, KM_USER0); |
| 394 | memset(kaddr + offset, 0, |
| 395 | PAGE_CACHE_SIZE - offset); |
| 396 | kunmap_atomic(kaddr, KM_USER0); |
| 397 | } |
| 398 | will_compress = 1; |
| 399 | } |
| 400 | } |
| 401 | if (start == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 402 | trans = btrfs_join_transaction(root, 1); |
| 403 | BUG_ON(!trans); |
| 404 | btrfs_set_trans_block_group(trans, inode); |
| 405 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 406 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 407 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 408 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 409 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 410 | */ |
| 411 | ret = cow_file_range_inline(trans, root, inode, |
| 412 | start, end, 0, NULL); |
| 413 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 414 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 415 | ret = cow_file_range_inline(trans, root, inode, |
| 416 | start, end, |
| 417 | total_compressed, pages); |
| 418 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 419 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 420 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 421 | /* |
| 422 | * inline extent creation worked, we don't need |
| 423 | * to create any more async work items. Unlock |
| 424 | * and free up our temp pages. |
| 425 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 426 | extent_clear_unlock_delalloc(inode, |
| 427 | &BTRFS_I(inode)->io_tree, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 428 | start, end, NULL, 1, 0, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 429 | 0, 1, 1, 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 430 | ret = 0; |
| 431 | goto free_pages_out; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | if (will_compress) { |
| 436 | /* |
| 437 | * we aren't doing an inline extent round the compressed size |
| 438 | * up to a block size boundary so the allocator does sane |
| 439 | * things |
| 440 | */ |
| 441 | total_compressed = (total_compressed + blocksize - 1) & |
| 442 | ~(blocksize - 1); |
| 443 | |
| 444 | /* |
| 445 | * one last check to make sure the compression is really a |
| 446 | * win, compare the page count read with the blocks on disk |
| 447 | */ |
| 448 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 449 | ~(PAGE_CACHE_SIZE - 1); |
| 450 | if (total_compressed >= total_in) { |
| 451 | will_compress = 0; |
| 452 | } else { |
| 453 | disk_num_bytes = total_compressed; |
| 454 | num_bytes = total_in; |
| 455 | } |
| 456 | } |
| 457 | if (!will_compress && pages) { |
| 458 | /* |
| 459 | * the compression code ran but failed to make things smaller, |
| 460 | * free any pages it allocated and our page pointer array |
| 461 | */ |
| 462 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 463 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 464 | page_cache_release(pages[i]); |
| 465 | } |
| 466 | kfree(pages); |
| 467 | pages = NULL; |
| 468 | total_compressed = 0; |
| 469 | nr_pages_ret = 0; |
| 470 | |
| 471 | /* flag the file so we don't compress in the future */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 472 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 473 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 474 | if (will_compress) { |
| 475 | *num_added += 1; |
| 476 | |
| 477 | /* the async work queues will take care of doing actual |
| 478 | * allocation on disk for these compressed pages, |
| 479 | * and will submit them to the elevator. |
| 480 | */ |
| 481 | add_async_extent(async_cow, start, num_bytes, |
| 482 | total_compressed, pages, nr_pages_ret); |
| 483 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 484 | if (start + num_bytes < end && start + num_bytes < actual_end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 485 | start += num_bytes; |
| 486 | pages = NULL; |
| 487 | cond_resched(); |
| 488 | goto again; |
| 489 | } |
| 490 | } else { |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 491 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 492 | /* |
| 493 | * No compression, but we still need to write the pages in |
| 494 | * the file we've been given so far. redirty the locked |
| 495 | * page if it corresponds to our extent and set things up |
| 496 | * for the async work queue to run cow_file_range to do |
| 497 | * the normal delalloc dance |
| 498 | */ |
| 499 | if (page_offset(locked_page) >= start && |
| 500 | page_offset(locked_page) <= end) { |
| 501 | __set_page_dirty_nobuffers(locked_page); |
| 502 | /* unlocked later on in the async handlers */ |
| 503 | } |
| 504 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0); |
| 505 | *num_added += 1; |
| 506 | } |
| 507 | |
| 508 | out: |
| 509 | return 0; |
| 510 | |
| 511 | free_pages_out: |
| 512 | for (i = 0; i < nr_pages_ret; i++) { |
| 513 | WARN_ON(pages[i]->mapping); |
| 514 | page_cache_release(pages[i]); |
| 515 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 516 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 517 | |
| 518 | goto out; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * phase two of compressed writeback. This is the ordered portion |
| 523 | * of the code, which only gets called in the order the work was |
| 524 | * queued. We walk all the async extents created by compress_file_range |
| 525 | * and send them down to the disk. |
| 526 | */ |
| 527 | static noinline int submit_compressed_extents(struct inode *inode, |
| 528 | struct async_cow *async_cow) |
| 529 | { |
| 530 | struct async_extent *async_extent; |
| 531 | u64 alloc_hint = 0; |
| 532 | struct btrfs_trans_handle *trans; |
| 533 | struct btrfs_key ins; |
| 534 | struct extent_map *em; |
| 535 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 536 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 537 | struct extent_io_tree *io_tree; |
| 538 | int ret; |
| 539 | |
| 540 | if (list_empty(&async_cow->extents)) |
| 541 | return 0; |
| 542 | |
| 543 | trans = btrfs_join_transaction(root, 1); |
| 544 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 545 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 546 | async_extent = list_entry(async_cow->extents.next, |
| 547 | struct async_extent, list); |
| 548 | list_del(&async_extent->list); |
| 549 | |
| 550 | io_tree = &BTRFS_I(inode)->io_tree; |
| 551 | |
| 552 | /* did the compression code fall back to uncompressed IO? */ |
| 553 | if (!async_extent->pages) { |
| 554 | int page_started = 0; |
| 555 | unsigned long nr_written = 0; |
| 556 | |
| 557 | lock_extent(io_tree, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 558 | async_extent->start + |
| 559 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 560 | |
| 561 | /* allocate blocks */ |
| 562 | cow_file_range(inode, async_cow->locked_page, |
| 563 | async_extent->start, |
| 564 | async_extent->start + |
| 565 | async_extent->ram_size - 1, |
| 566 | &page_started, &nr_written, 0); |
| 567 | |
| 568 | /* |
| 569 | * if page_started, cow_file_range inserted an |
| 570 | * inline extent and took care of all the unlocking |
| 571 | * and IO for us. Otherwise, we need to submit |
| 572 | * all those pages down to the drive. |
| 573 | */ |
| 574 | if (!page_started) |
| 575 | extent_write_locked_range(io_tree, |
| 576 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 577 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 578 | async_extent->ram_size - 1, |
| 579 | btrfs_get_extent, |
| 580 | WB_SYNC_ALL); |
| 581 | kfree(async_extent); |
| 582 | cond_resched(); |
| 583 | continue; |
| 584 | } |
| 585 | |
| 586 | lock_extent(io_tree, async_extent->start, |
| 587 | async_extent->start + async_extent->ram_size - 1, |
| 588 | GFP_NOFS); |
| 589 | /* |
| 590 | * here we're doing allocation and writeback of the |
| 591 | * compressed pages |
| 592 | */ |
| 593 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 594 | async_extent->start + |
| 595 | async_extent->ram_size - 1, 0); |
| 596 | |
| 597 | ret = btrfs_reserve_extent(trans, root, |
| 598 | async_extent->compressed_size, |
| 599 | async_extent->compressed_size, |
| 600 | 0, alloc_hint, |
| 601 | (u64)-1, &ins, 1); |
| 602 | BUG_ON(ret); |
| 603 | em = alloc_extent_map(GFP_NOFS); |
| 604 | em->start = async_extent->start; |
| 605 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 606 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 607 | |
| 608 | em->block_start = ins.objectid; |
| 609 | em->block_len = ins.offset; |
| 610 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 611 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 612 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 613 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 614 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 615 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 616 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 617 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 618 | if (ret != -EEXIST) { |
| 619 | free_extent_map(em); |
| 620 | break; |
| 621 | } |
| 622 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 623 | async_extent->start + |
| 624 | async_extent->ram_size - 1, 0); |
| 625 | } |
| 626 | |
| 627 | ret = btrfs_add_ordered_extent(inode, async_extent->start, |
| 628 | ins.objectid, |
| 629 | async_extent->ram_size, |
| 630 | ins.offset, |
| 631 | BTRFS_ORDERED_COMPRESSED); |
| 632 | BUG_ON(ret); |
| 633 | |
| 634 | btrfs_end_transaction(trans, root); |
| 635 | |
| 636 | /* |
| 637 | * clear dirty, set writeback and unlock the pages. |
| 638 | */ |
| 639 | extent_clear_unlock_delalloc(inode, |
| 640 | &BTRFS_I(inode)->io_tree, |
| 641 | async_extent->start, |
| 642 | async_extent->start + |
| 643 | async_extent->ram_size - 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 644 | NULL, 1, 1, 0, 1, 1, 0, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 645 | |
| 646 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 647 | async_extent->start, |
| 648 | async_extent->ram_size, |
| 649 | ins.objectid, |
| 650 | ins.offset, async_extent->pages, |
| 651 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 652 | |
| 653 | BUG_ON(ret); |
| 654 | trans = btrfs_join_transaction(root, 1); |
| 655 | alloc_hint = ins.objectid + ins.offset; |
| 656 | kfree(async_extent); |
| 657 | cond_resched(); |
| 658 | } |
| 659 | |
| 660 | btrfs_end_transaction(trans, root); |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * when extent_io.c finds a delayed allocation range in the file, |
| 666 | * the call backs end up in this code. The basic idea is to |
| 667 | * allocate extents on disk for the range, and create ordered data structs |
| 668 | * in ram to track those extents. |
| 669 | * |
| 670 | * locked_page is the page that writepage had locked already. We use |
| 671 | * it to make sure we don't do extra locks or unlocks. |
| 672 | * |
| 673 | * *page_started is set to one if we unlock locked_page and do everything |
| 674 | * required to start IO on it. It may be clean and already done with |
| 675 | * IO when we return. |
| 676 | */ |
| 677 | static noinline int cow_file_range(struct inode *inode, |
| 678 | struct page *locked_page, |
| 679 | u64 start, u64 end, int *page_started, |
| 680 | unsigned long *nr_written, |
| 681 | int unlock) |
| 682 | { |
| 683 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 684 | struct btrfs_trans_handle *trans; |
| 685 | u64 alloc_hint = 0; |
| 686 | u64 num_bytes; |
| 687 | unsigned long ram_size; |
| 688 | u64 disk_num_bytes; |
| 689 | u64 cur_alloc_size; |
| 690 | u64 blocksize = root->sectorsize; |
| 691 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 692 | u64 isize = i_size_read(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 693 | struct btrfs_key ins; |
| 694 | struct extent_map *em; |
| 695 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 696 | int ret = 0; |
| 697 | |
| 698 | trans = btrfs_join_transaction(root, 1); |
| 699 | BUG_ON(!trans); |
| 700 | btrfs_set_trans_block_group(trans, inode); |
| 701 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 702 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 703 | |
| 704 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 705 | num_bytes = max(blocksize, num_bytes); |
| 706 | disk_num_bytes = num_bytes; |
| 707 | ret = 0; |
| 708 | |
| 709 | if (start == 0) { |
| 710 | /* lets try to make an inline extent */ |
| 711 | ret = cow_file_range_inline(trans, root, inode, |
| 712 | start, end, 0, NULL); |
| 713 | if (ret == 0) { |
| 714 | extent_clear_unlock_delalloc(inode, |
| 715 | &BTRFS_I(inode)->io_tree, |
| 716 | start, end, NULL, 1, 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 717 | 1, 1, 1, 1, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 718 | *nr_written = *nr_written + |
| 719 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 720 | *page_started = 1; |
| 721 | ret = 0; |
| 722 | goto out; |
| 723 | } |
| 724 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 725 | |
| 726 | BUG_ON(disk_num_bytes > |
| 727 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 728 | |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 729 | |
| 730 | read_lock(&BTRFS_I(inode)->extent_tree.lock); |
| 731 | em = search_extent_mapping(&BTRFS_I(inode)->extent_tree, |
| 732 | start, num_bytes); |
| 733 | if (em) { |
| 734 | alloc_hint = em->block_start; |
| 735 | free_extent_map(em); |
| 736 | } |
| 737 | read_unlock(&BTRFS_I(inode)->extent_tree.lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 738 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 739 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 740 | while (disk_num_bytes > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 741 | cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 742 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 743 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 744 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 745 | BUG_ON(ret); |
| 746 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 747 | em = alloc_extent_map(GFP_NOFS); |
| 748 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 749 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 750 | ram_size = ins.offset; |
| 751 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 752 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 753 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 754 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 755 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 756 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 757 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 758 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 759 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 760 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 761 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 762 | if (ret != -EEXIST) { |
| 763 | free_extent_map(em); |
| 764 | break; |
| 765 | } |
| 766 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 767 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 768 | } |
| 769 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 770 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 771 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 772 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 773 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 774 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 775 | if (root->root_key.objectid == |
| 776 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 777 | ret = btrfs_reloc_clone_csums(inode, start, |
| 778 | cur_alloc_size); |
| 779 | BUG_ON(ret); |
| 780 | } |
| 781 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 782 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 783 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 784 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 785 | /* we're not doing compressed IO, don't unlock the first |
| 786 | * page (which the caller expects to stay locked), don't |
| 787 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 788 | * |
| 789 | * Do set the Private2 bit so we know this page was properly |
| 790 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 791 | */ |
| 792 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 793 | start, start + ram_size - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 794 | locked_page, unlock, 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 795 | 1, 0, 0, 0, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 796 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 797 | num_bytes -= cur_alloc_size; |
| 798 | alloc_hint = ins.objectid + ins.offset; |
| 799 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 800 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 801 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 802 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 803 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 804 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 805 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 806 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 807 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 808 | /* |
| 809 | * work queue call back to started compression on a file and pages |
| 810 | */ |
| 811 | static noinline void async_cow_start(struct btrfs_work *work) |
| 812 | { |
| 813 | struct async_cow *async_cow; |
| 814 | int num_added = 0; |
| 815 | async_cow = container_of(work, struct async_cow, work); |
| 816 | |
| 817 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 818 | async_cow->start, async_cow->end, async_cow, |
| 819 | &num_added); |
| 820 | if (num_added == 0) |
| 821 | async_cow->inode = NULL; |
| 822 | } |
| 823 | |
| 824 | /* |
| 825 | * work queue call back to submit previously compressed pages |
| 826 | */ |
| 827 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 828 | { |
| 829 | struct async_cow *async_cow; |
| 830 | struct btrfs_root *root; |
| 831 | unsigned long nr_pages; |
| 832 | |
| 833 | async_cow = container_of(work, struct async_cow, work); |
| 834 | |
| 835 | root = async_cow->root; |
| 836 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 837 | PAGE_CACHE_SHIFT; |
| 838 | |
| 839 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 840 | |
| 841 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 842 | 5 * 1042 * 1024 && |
| 843 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 844 | wake_up(&root->fs_info->async_submit_wait); |
| 845 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 846 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 847 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 848 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 849 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 850 | static noinline void async_cow_free(struct btrfs_work *work) |
| 851 | { |
| 852 | struct async_cow *async_cow; |
| 853 | async_cow = container_of(work, struct async_cow, work); |
| 854 | kfree(async_cow); |
| 855 | } |
| 856 | |
| 857 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 858 | u64 start, u64 end, int *page_started, |
| 859 | unsigned long *nr_written) |
| 860 | { |
| 861 | struct async_cow *async_cow; |
| 862 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 863 | unsigned long nr_pages; |
| 864 | u64 cur_end; |
| 865 | int limit = 10 * 1024 * 1042; |
| 866 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 867 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 868 | EXTENT_DELALLOC, 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 869 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 870 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
| 871 | async_cow->inode = inode; |
| 872 | async_cow->root = root; |
| 873 | async_cow->locked_page = locked_page; |
| 874 | async_cow->start = start; |
| 875 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 876 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 877 | cur_end = end; |
| 878 | else |
| 879 | cur_end = min(end, start + 512 * 1024 - 1); |
| 880 | |
| 881 | async_cow->end = cur_end; |
| 882 | INIT_LIST_HEAD(&async_cow->extents); |
| 883 | |
| 884 | async_cow->work.func = async_cow_start; |
| 885 | async_cow->work.ordered_func = async_cow_submit; |
| 886 | async_cow->work.ordered_free = async_cow_free; |
| 887 | async_cow->work.flags = 0; |
| 888 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 889 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 890 | PAGE_CACHE_SHIFT; |
| 891 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 892 | |
| 893 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 894 | &async_cow->work); |
| 895 | |
| 896 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 897 | wait_event(root->fs_info->async_submit_wait, |
| 898 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 899 | limit)); |
| 900 | } |
| 901 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 902 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 903 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 904 | wait_event(root->fs_info->async_submit_wait, |
| 905 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 906 | 0)); |
| 907 | } |
| 908 | |
| 909 | *nr_written += nr_pages; |
| 910 | start = cur_end + 1; |
| 911 | } |
| 912 | *page_started = 1; |
| 913 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 914 | } |
| 915 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 916 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 917 | u64 bytenr, u64 num_bytes) |
| 918 | { |
| 919 | int ret; |
| 920 | struct btrfs_ordered_sum *sums; |
| 921 | LIST_HEAD(list); |
| 922 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 923 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
| 924 | bytenr + num_bytes - 1, &list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 925 | if (ret == 0 && list_empty(&list)) |
| 926 | return 0; |
| 927 | |
| 928 | while (!list_empty(&list)) { |
| 929 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 930 | list_del(&sums->list); |
| 931 | kfree(sums); |
| 932 | } |
| 933 | return 1; |
| 934 | } |
| 935 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 936 | /* |
| 937 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 938 | * of the extents that exist in the file, and COWs the file as required. |
| 939 | * |
| 940 | * If no cow copies or snapshots exist, we write directly to the existing |
| 941 | * blocks on disk |
| 942 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 943 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 944 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 945 | u64 start, u64 end, int *page_started, int force, |
| 946 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 947 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 948 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 949 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 950 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 951 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 952 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 953 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 954 | u64 cow_start; |
| 955 | u64 cur_offset; |
| 956 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 957 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 958 | u64 disk_bytenr; |
| 959 | u64 num_bytes; |
| 960 | int extent_type; |
| 961 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 962 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 963 | int nocow; |
| 964 | int check_prev = 1; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 965 | |
| 966 | path = btrfs_alloc_path(); |
| 967 | BUG_ON(!path); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 968 | trans = btrfs_join_transaction(root, 1); |
| 969 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 970 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 971 | cow_start = (u64)-1; |
| 972 | cur_offset = start; |
| 973 | while (1) { |
| 974 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 975 | cur_offset, 0); |
| 976 | BUG_ON(ret < 0); |
| 977 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 978 | leaf = path->nodes[0]; |
| 979 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 980 | path->slots[0] - 1); |
| 981 | if (found_key.objectid == inode->i_ino && |
| 982 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 983 | path->slots[0]--; |
| 984 | } |
| 985 | check_prev = 0; |
| 986 | next_slot: |
| 987 | leaf = path->nodes[0]; |
| 988 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 989 | ret = btrfs_next_leaf(root, path); |
| 990 | if (ret < 0) |
| 991 | BUG_ON(1); |
| 992 | if (ret > 0) |
| 993 | break; |
| 994 | leaf = path->nodes[0]; |
| 995 | } |
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 | nocow = 0; |
| 998 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 999 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1000 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1001 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1002 | if (found_key.objectid > inode->i_ino || |
| 1003 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1004 | found_key.offset > end) |
| 1005 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1006 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1007 | if (found_key.offset > cur_offset) { |
| 1008 | extent_end = found_key.offset; |
| 1009 | goto out_check; |
| 1010 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1011 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1012 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1013 | struct btrfs_file_extent_item); |
| 1014 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1015 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1016 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1017 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1018 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1019 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1020 | extent_end = found_key.offset + |
| 1021 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1022 | if (extent_end <= start) { |
| 1023 | path->slots[0]++; |
| 1024 | goto next_slot; |
| 1025 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1026 | if (disk_bytenr == 0) |
| 1027 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1028 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1029 | btrfs_file_extent_encryption(leaf, fi) || |
| 1030 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1031 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1032 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1033 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1034 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1035 | goto out_check; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1036 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1037 | found_key.offset - |
| 1038 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1039 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1040 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1041 | disk_bytenr += cur_offset - found_key.offset; |
| 1042 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1043 | /* |
| 1044 | * force cow if csum exists in the range. |
| 1045 | * this ensure that csum for a given extent are |
| 1046 | * either valid or do not exist. |
| 1047 | */ |
| 1048 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1049 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1050 | nocow = 1; |
| 1051 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1052 | extent_end = found_key.offset + |
| 1053 | btrfs_file_extent_inline_len(leaf, fi); |
| 1054 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1055 | } else { |
| 1056 | BUG_ON(1); |
| 1057 | } |
| 1058 | out_check: |
| 1059 | if (extent_end <= start) { |
| 1060 | path->slots[0]++; |
| 1061 | goto next_slot; |
| 1062 | } |
| 1063 | if (!nocow) { |
| 1064 | if (cow_start == (u64)-1) |
| 1065 | cow_start = cur_offset; |
| 1066 | cur_offset = extent_end; |
| 1067 | if (cur_offset > end) |
| 1068 | break; |
| 1069 | path->slots[0]++; |
| 1070 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1071 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1072 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1073 | btrfs_release_path(root, path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1074 | if (cow_start != (u64)-1) { |
| 1075 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1076 | found_key.offset - 1, page_started, |
| 1077 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1078 | BUG_ON(ret); |
| 1079 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1080 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1081 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1082 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1083 | struct extent_map *em; |
| 1084 | struct extent_map_tree *em_tree; |
| 1085 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 1086 | em = alloc_extent_map(GFP_NOFS); |
| 1087 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1088 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1089 | em->len = num_bytes; |
| 1090 | em->block_len = num_bytes; |
| 1091 | em->block_start = disk_bytenr; |
| 1092 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1093 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1094 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1095 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1096 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1097 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1098 | if (ret != -EEXIST) { |
| 1099 | free_extent_map(em); |
| 1100 | break; |
| 1101 | } |
| 1102 | btrfs_drop_extent_cache(inode, em->start, |
| 1103 | em->start + em->len - 1, 0); |
| 1104 | } |
| 1105 | type = BTRFS_ORDERED_PREALLOC; |
| 1106 | } else { |
| 1107 | type = BTRFS_ORDERED_NOCOW; |
| 1108 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1109 | |
| 1110 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1111 | num_bytes, num_bytes, type); |
| 1112 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1113 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1114 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 1115 | cur_offset, cur_offset + num_bytes - 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1116 | locked_page, 1, 1, 1, 0, 0, 0, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1117 | cur_offset = extent_end; |
| 1118 | if (cur_offset > end) |
| 1119 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1120 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1121 | btrfs_release_path(root, path); |
| 1122 | |
| 1123 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1124 | cow_start = cur_offset; |
| 1125 | if (cow_start != (u64)-1) { |
| 1126 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1127 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1128 | BUG_ON(ret); |
| 1129 | } |
| 1130 | |
| 1131 | ret = btrfs_end_transaction(trans, root); |
| 1132 | BUG_ON(ret); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1133 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1134 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1135 | } |
| 1136 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1137 | /* |
| 1138 | * extent_io.c call back to do delayed allocation processing |
| 1139 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1140 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1141 | u64 start, u64 end, int *page_started, |
| 1142 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1143 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1144 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1145 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1146 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1147 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1148 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1149 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1150 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1151 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1152 | page_started, 0, nr_written); |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1153 | else if (!btrfs_test_opt(root, COMPRESS)) |
| 1154 | ret = cow_file_range(inode, locked_page, start, end, |
| 1155 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1156 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1157 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1158 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1159 | return ret; |
| 1160 | } |
| 1161 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1162 | /* |
| 1163 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1164 | * bytes in this file, and to maintain the list of inodes that |
| 1165 | * have pending delalloc work to be done. |
| 1166 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1167 | 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] | 1168 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1169 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1170 | /* |
| 1171 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1172 | * but in this case, we are only testeing for the DELALLOC |
| 1173 | * bit, which is only set or cleared with irqs on |
| 1174 | */ |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1175 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1176 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1177 | btrfs_delalloc_reserve_space(root, inode, end - start + 1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1178 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1179 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1180 | root->fs_info->delalloc_bytes += end - start + 1; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1181 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1182 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1183 | &root->fs_info->delalloc_inodes); |
| 1184 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1185 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1186 | } |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1190 | /* |
| 1191 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1192 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1193 | 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] | 1194 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1195 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1196 | /* |
| 1197 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1198 | * but in this case, we are only testeing for the DELALLOC |
| 1199 | * bit, which is only set or cleared with irqs on |
| 1200 | */ |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1201 | if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1202 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1203 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1204 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1205 | if (end - start + 1 > root->fs_info->delalloc_bytes) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1206 | printk(KERN_INFO "btrfs warning: delalloc account " |
| 1207 | "%llu %llu\n", |
| 1208 | (unsigned long long)end - start + 1, |
| 1209 | (unsigned long long) |
| 1210 | root->fs_info->delalloc_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1211 | btrfs_delalloc_free_space(root, inode, (u64)-1); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1212 | root->fs_info->delalloc_bytes = 0; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1213 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1214 | } else { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1215 | btrfs_delalloc_free_space(root, inode, |
| 1216 | end - start + 1); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1217 | root->fs_info->delalloc_bytes -= end - start + 1; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1218 | BTRFS_I(inode)->delalloc_bytes -= end - start + 1; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1219 | } |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1220 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
| 1221 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1222 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1223 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1224 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1225 | } |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1229 | /* |
| 1230 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1231 | * we don't create bios that span stripes or chunks |
| 1232 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1233 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1234 | size_t size, struct bio *bio, |
| 1235 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1236 | { |
| 1237 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1238 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1239 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1240 | u64 length = 0; |
| 1241 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1242 | int ret; |
| 1243 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1244 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1245 | return 0; |
| 1246 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1247 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1248 | map_tree = &root->fs_info->mapping_tree; |
| 1249 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1250 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1251 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1252 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1253 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1254 | return 1; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1255 | return 0; |
| 1256 | } |
| 1257 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1258 | /* |
| 1259 | * in order to insert checksums into the metadata in large chunks, |
| 1260 | * we wait until bio submission time. All the pages in the bio are |
| 1261 | * checksummed and sums are attached onto the ordered extent record. |
| 1262 | * |
| 1263 | * At IO completion time the cums attached on the ordered extent record |
| 1264 | * are inserted into the btree |
| 1265 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1266 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1267 | struct bio *bio, int mirror_num, |
| 1268 | unsigned long bio_flags) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1269 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1270 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1271 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1272 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1273 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1274 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1275 | return 0; |
| 1276 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1277 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1278 | /* |
| 1279 | * in order to insert checksums into the metadata in large chunks, |
| 1280 | * we wait until bio submission time. All the pages in the bio are |
| 1281 | * checksummed and sums are attached onto the ordered extent record. |
| 1282 | * |
| 1283 | * At IO completion time the cums attached on the ordered extent record |
| 1284 | * are inserted into the btree |
| 1285 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1286 | 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] | 1287 | int mirror_num, unsigned long bio_flags) |
| 1288 | { |
| 1289 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1290 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1291 | } |
| 1292 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1293 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1294 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1295 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1296 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1297 | 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] | 1298 | int mirror_num, unsigned long bio_flags) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1299 | { |
| 1300 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1301 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1302 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1303 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1304 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1305 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1306 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 1307 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1308 | |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1309 | if (!(rw & (1 << BIO_RW))) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1310 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1311 | return btrfs_submit_compressed_read(inode, bio, |
| 1312 | mirror_num, bio_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1313 | } else if (!skip_sum) |
| 1314 | btrfs_lookup_bio_sums(root, inode, bio, NULL); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1315 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1316 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1317 | /* csum items have already been cloned */ |
| 1318 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1319 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1320 | /* we're doing a write, do the async checksumming */ |
| 1321 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1322 | inode, rw, bio, mirror_num, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1323 | bio_flags, __btrfs_submit_bio_start, |
| 1324 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1325 | } |
| 1326 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1327 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1328 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1329 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1330 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1331 | /* |
| 1332 | * given a list of ordered sums record them in the inode. This happens |
| 1333 | * at IO completion time based on sums calculated at bio submission time. |
| 1334 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1335 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1336 | struct inode *inode, u64 file_offset, |
| 1337 | struct list_head *list) |
| 1338 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1339 | struct btrfs_ordered_sum *sum; |
| 1340 | |
| 1341 | btrfs_set_trans_block_group(trans, inode); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1342 | |
| 1343 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1344 | btrfs_csum_file_blocks(trans, |
| 1345 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1346 | } |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1350 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end) |
| 1351 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1352 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1353 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1354 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
| 1355 | GFP_NOFS); |
| 1356 | } |
| 1357 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1358 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1359 | struct btrfs_writepage_fixup { |
| 1360 | struct page *page; |
| 1361 | struct btrfs_work work; |
| 1362 | }; |
| 1363 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1364 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1365 | { |
| 1366 | struct btrfs_writepage_fixup *fixup; |
| 1367 | struct btrfs_ordered_extent *ordered; |
| 1368 | struct page *page; |
| 1369 | struct inode *inode; |
| 1370 | u64 page_start; |
| 1371 | u64 page_end; |
| 1372 | |
| 1373 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1374 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1375 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1376 | lock_page(page); |
| 1377 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1378 | ClearPageChecked(page); |
| 1379 | goto out_page; |
| 1380 | } |
| 1381 | |
| 1382 | inode = page->mapping->host; |
| 1383 | page_start = page_offset(page); |
| 1384 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1385 | |
| 1386 | 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] | 1387 | |
| 1388 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1389 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1390 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1391 | |
| 1392 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1393 | if (ordered) { |
| 1394 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, |
| 1395 | page_end, GFP_NOFS); |
| 1396 | unlock_page(page); |
| 1397 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1398 | goto again; |
| 1399 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1400 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1401 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1402 | ClearPageChecked(page); |
| 1403 | out: |
| 1404 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); |
| 1405 | out_page: |
| 1406 | unlock_page(page); |
| 1407 | page_cache_release(page); |
| 1408 | } |
| 1409 | |
| 1410 | /* |
| 1411 | * There are a few paths in the higher layers of the kernel that directly |
| 1412 | * set the page dirty bit without asking the filesystem if it is a |
| 1413 | * good idea. This causes problems because we want to make sure COW |
| 1414 | * properly happens and the data=ordered rules are followed. |
| 1415 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1416 | * 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] | 1417 | * hasn't been properly setup for IO. We kick off an async process |
| 1418 | * to fix it up. The async helper will wait for ordered extents, set |
| 1419 | * the delalloc bit and make it safe to write the page. |
| 1420 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1421 | 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] | 1422 | { |
| 1423 | struct inode *inode = page->mapping->host; |
| 1424 | struct btrfs_writepage_fixup *fixup; |
| 1425 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1426 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1427 | /* this page is properly in the ordered list */ |
| 1428 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1429 | return 0; |
| 1430 | |
| 1431 | if (PageChecked(page)) |
| 1432 | return -EAGAIN; |
| 1433 | |
| 1434 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1435 | if (!fixup) |
| 1436 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1437 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1438 | SetPageChecked(page); |
| 1439 | page_cache_get(page); |
| 1440 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1441 | fixup->page = page; |
| 1442 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1443 | return -EAGAIN; |
| 1444 | } |
| 1445 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1446 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1447 | struct inode *inode, u64 file_pos, |
| 1448 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1449 | u64 num_bytes, u64 ram_bytes, |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 1450 | u64 locked_end, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1451 | u8 compression, u8 encryption, |
| 1452 | u16 other_encoding, int extent_type) |
| 1453 | { |
| 1454 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1455 | struct btrfs_file_extent_item *fi; |
| 1456 | struct btrfs_path *path; |
| 1457 | struct extent_buffer *leaf; |
| 1458 | struct btrfs_key ins; |
| 1459 | u64 hint; |
| 1460 | int ret; |
| 1461 | |
| 1462 | path = btrfs_alloc_path(); |
| 1463 | BUG_ON(!path); |
| 1464 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1465 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1466 | |
| 1467 | /* |
| 1468 | * we may be replacing one extent in the tree with another. |
| 1469 | * The new extent is pinned in the extent map, and we don't want |
| 1470 | * to drop it from the cache until it is completely in the btree. |
| 1471 | * |
| 1472 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1473 | * the caller is expected to unpin it and allow it to be merged |
| 1474 | * with the others. |
| 1475 | */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1476 | ret = btrfs_drop_extents(trans, root, inode, file_pos, |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 1477 | file_pos + num_bytes, locked_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1478 | file_pos, &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1479 | BUG_ON(ret); |
| 1480 | |
| 1481 | ins.objectid = inode->i_ino; |
| 1482 | ins.offset = file_pos; |
| 1483 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1484 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1485 | BUG_ON(ret); |
| 1486 | leaf = path->nodes[0]; |
| 1487 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1488 | struct btrfs_file_extent_item); |
| 1489 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1490 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1491 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1492 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1493 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1494 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1495 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1496 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1497 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1498 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1499 | |
| 1500 | btrfs_unlock_up_safe(path, 1); |
| 1501 | btrfs_set_lock_blocking(leaf); |
| 1502 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1503 | btrfs_mark_buffer_dirty(leaf); |
| 1504 | |
| 1505 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1506 | |
| 1507 | ins.objectid = disk_bytenr; |
| 1508 | ins.offset = disk_num_bytes; |
| 1509 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1510 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1511 | root->root_key.objectid, |
| 1512 | inode->i_ino, file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1513 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1514 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1515 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1516 | return 0; |
| 1517 | } |
| 1518 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1519 | /* |
| 1520 | * helper function for btrfs_finish_ordered_io, this |
| 1521 | * just reads in some of the csum leaves to prime them into ram |
| 1522 | * before we start the transaction. It limits the amount of btree |
| 1523 | * reads required while inside the transaction. |
| 1524 | */ |
| 1525 | static noinline void reada_csum(struct btrfs_root *root, |
| 1526 | struct btrfs_path *path, |
| 1527 | struct btrfs_ordered_extent *ordered_extent) |
| 1528 | { |
| 1529 | struct btrfs_ordered_sum *sum; |
| 1530 | u64 bytenr; |
| 1531 | |
| 1532 | sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum, |
| 1533 | list); |
| 1534 | bytenr = sum->sums[0].bytenr; |
| 1535 | |
| 1536 | /* |
| 1537 | * we don't care about the results, the point of this search is |
| 1538 | * just to get the btree leaves into ram |
| 1539 | */ |
| 1540 | btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0); |
| 1541 | } |
| 1542 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1543 | /* as ordered data IO finishes, this gets called so we can finish |
| 1544 | * an ordered extent if the range of bytes in the file it covers are |
| 1545 | * fully written. |
| 1546 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1547 | 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] | 1548 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1549 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1550 | struct btrfs_trans_handle *trans; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1551 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1552 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1553 | struct btrfs_path *path; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1554 | int compressed = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1555 | int ret; |
| 1556 | |
| 1557 | ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1558 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1559 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1560 | |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1561 | /* |
| 1562 | * before we join the transaction, try to do some of our IO. |
| 1563 | * This will limit the amount of IO that we have to do with |
| 1564 | * the transaction running. We're unlikely to need to do any |
| 1565 | * IO if the file extents are new, the disk_i_size checks |
| 1566 | * covers the most common case. |
| 1567 | */ |
| 1568 | if (start < BTRFS_I(inode)->disk_i_size) { |
| 1569 | path = btrfs_alloc_path(); |
| 1570 | if (path) { |
| 1571 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 1572 | inode->i_ino, |
| 1573 | start, 0); |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1574 | ordered_extent = btrfs_lookup_ordered_extent(inode, |
| 1575 | start); |
| 1576 | if (!list_empty(&ordered_extent->list)) { |
| 1577 | btrfs_release_path(root, path); |
| 1578 | reada_csum(root, path, ordered_extent); |
| 1579 | } |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1580 | btrfs_free_path(path); |
| 1581 | } |
| 1582 | } |
| 1583 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 1584 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1585 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1586 | if (!ordered_extent) |
| 1587 | ordered_extent = btrfs_lookup_ordered_extent(inode, start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1588 | BUG_ON(!ordered_extent); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1589 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) |
| 1590 | goto nocow; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1591 | |
| 1592 | lock_extent(io_tree, ordered_extent->file_offset, |
| 1593 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1594 | GFP_NOFS); |
| 1595 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1596 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1597 | compressed = 1; |
| 1598 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 1599 | BUG_ON(compressed); |
| 1600 | ret = btrfs_mark_extent_written(trans, root, inode, |
| 1601 | ordered_extent->file_offset, |
| 1602 | ordered_extent->file_offset + |
| 1603 | ordered_extent->len); |
| 1604 | BUG_ON(ret); |
| 1605 | } else { |
| 1606 | ret = insert_reserved_file_extent(trans, inode, |
| 1607 | ordered_extent->file_offset, |
| 1608 | ordered_extent->start, |
| 1609 | ordered_extent->disk_len, |
| 1610 | ordered_extent->len, |
| 1611 | ordered_extent->len, |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 1612 | ordered_extent->file_offset + |
| 1613 | ordered_extent->len, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1614 | compressed, 0, 0, |
| 1615 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1616 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1617 | ordered_extent->file_offset, |
| 1618 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1619 | BUG_ON(ret); |
| 1620 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1621 | unlock_extent(io_tree, ordered_extent->file_offset, |
| 1622 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1623 | GFP_NOFS); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1624 | nocow: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1625 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1626 | &ordered_extent->list); |
| 1627 | |
Chris Mason | 3435302 | 2008-09-23 20:19:49 -0400 | [diff] [blame] | 1628 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 1629 | btrfs_ordered_update_i_size(inode, ordered_extent); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1630 | btrfs_update_inode(trans, root, inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1631 | btrfs_remove_ordered_extent(inode, ordered_extent); |
Chris Mason | 3435302 | 2008-09-23 20:19:49 -0400 | [diff] [blame] | 1632 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 1633 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1634 | /* once for us */ |
| 1635 | btrfs_put_ordered_extent(ordered_extent); |
| 1636 | /* once for the tree */ |
| 1637 | btrfs_put_ordered_extent(ordered_extent); |
| 1638 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1639 | btrfs_end_transaction(trans, root); |
| 1640 | return 0; |
| 1641 | } |
| 1642 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1643 | 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] | 1644 | struct extent_state *state, int uptodate) |
| 1645 | { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1646 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1647 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1648 | } |
| 1649 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1650 | /* |
| 1651 | * When IO fails, either with EIO or csum verification fails, we |
| 1652 | * try other mirrors that might have a good copy of the data. This |
| 1653 | * io_failure_record is used to record state as we go through all the |
| 1654 | * mirrors. If another mirror has good data, the page is set up to date |
| 1655 | * and things continue. If a good mirror can't be found, the original |
| 1656 | * bio end_io callback is called to indicate things have failed. |
| 1657 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1658 | struct io_failure_record { |
| 1659 | struct page *page; |
| 1660 | u64 start; |
| 1661 | u64 len; |
| 1662 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1663 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1664 | int last_mirror; |
| 1665 | }; |
| 1666 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1667 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1668 | struct page *page, u64 start, u64 end, |
| 1669 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1670 | { |
| 1671 | struct io_failure_record *failrec = NULL; |
| 1672 | u64 private; |
| 1673 | struct extent_map *em; |
| 1674 | struct inode *inode = page->mapping->host; |
| 1675 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1676 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1677 | struct bio *bio; |
| 1678 | int num_copies; |
| 1679 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1680 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1681 | u64 logical; |
| 1682 | |
| 1683 | ret = get_state_private(failure_tree, start, &private); |
| 1684 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1685 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1686 | if (!failrec) |
| 1687 | return -ENOMEM; |
| 1688 | failrec->start = start; |
| 1689 | failrec->len = end - start + 1; |
| 1690 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1691 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1692 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1693 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1694 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1695 | if (em->start > start || em->start + em->len < start) { |
| 1696 | free_extent_map(em); |
| 1697 | em = NULL; |
| 1698 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1699 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1700 | |
| 1701 | if (!em || IS_ERR(em)) { |
| 1702 | kfree(failrec); |
| 1703 | return -EIO; |
| 1704 | } |
| 1705 | logical = start - em->start; |
| 1706 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1707 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1708 | logical = em->block_start; |
| 1709 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 1710 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1711 | failrec->logical = logical; |
| 1712 | free_extent_map(em); |
| 1713 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1714 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1715 | set_state_private(failure_tree, start, |
| 1716 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1717 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1718 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1719 | } |
| 1720 | num_copies = btrfs_num_copies( |
| 1721 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1722 | failrec->logical, failrec->len); |
| 1723 | failrec->last_mirror++; |
| 1724 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1725 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1726 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1727 | failrec->start, |
| 1728 | EXTENT_LOCKED); |
| 1729 | if (state && state->start != failrec->start) |
| 1730 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1731 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1732 | } |
| 1733 | if (!state || failrec->last_mirror > num_copies) { |
| 1734 | set_state_private(failure_tree, failrec->start, 0); |
| 1735 | clear_extent_bits(failure_tree, failrec->start, |
| 1736 | failrec->start + failrec->len - 1, |
| 1737 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1738 | kfree(failrec); |
| 1739 | return -EIO; |
| 1740 | } |
| 1741 | bio = bio_alloc(GFP_NOFS, 1); |
| 1742 | bio->bi_private = state; |
| 1743 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1744 | bio->bi_sector = failrec->logical >> 9; |
| 1745 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1746 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1747 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1748 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1749 | if (failed_bio->bi_rw & (1 << BIO_RW)) |
| 1750 | rw = WRITE; |
| 1751 | else |
| 1752 | rw = READ; |
| 1753 | |
| 1754 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1755 | failrec->last_mirror, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1756 | failrec->bio_flags); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1757 | return 0; |
| 1758 | } |
| 1759 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1760 | /* |
| 1761 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1762 | * to see if we need to process or clean up an io_failure_record |
| 1763 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1764 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1765 | { |
| 1766 | u64 private; |
| 1767 | u64 private_failure; |
| 1768 | struct io_failure_record *failure; |
| 1769 | int ret; |
| 1770 | |
| 1771 | private = 0; |
| 1772 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 1773 | (u64)-1, 1, EXTENT_DIRTY)) { |
| 1774 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1775 | start, &private_failure); |
| 1776 | if (ret == 0) { |
| 1777 | failure = (struct io_failure_record *)(unsigned long) |
| 1778 | private_failure; |
| 1779 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1780 | failure->start, 0); |
| 1781 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1782 | failure->start, |
| 1783 | failure->start + failure->len - 1, |
| 1784 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1785 | GFP_NOFS); |
| 1786 | kfree(failure); |
| 1787 | } |
| 1788 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1789 | return 0; |
| 1790 | } |
| 1791 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1792 | /* |
| 1793 | * when reads are done, we need to check csums to verify the data is correct |
| 1794 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1795 | * the io_failure_record routines to find good copies |
| 1796 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1797 | 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] | 1798 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1799 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1800 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1801 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1802 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1803 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1804 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1805 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1806 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1807 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1808 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1809 | if (PageChecked(page)) { |
| 1810 | ClearPageChecked(page); |
| 1811 | goto good; |
| 1812 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1813 | |
| 1814 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1815 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1816 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1817 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1818 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1819 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1820 | GFP_NOFS); |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1824 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1825 | private = state->private; |
| 1826 | ret = 0; |
| 1827 | } else { |
| 1828 | ret = get_state_private(io_tree, start, &private); |
| 1829 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1830 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1831 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1832 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1833 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1834 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1835 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1836 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1837 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1838 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1839 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1840 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1841 | /* if the io failure tree for this inode is non-empty, |
| 1842 | * check to see if we've recovered from a failed IO |
| 1843 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1844 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1845 | return 0; |
| 1846 | |
| 1847 | zeroit: |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 1848 | if (printk_ratelimit()) { |
| 1849 | printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " |
| 1850 | "private %llu\n", page->mapping->host->i_ino, |
| 1851 | (unsigned long long)start, csum, |
| 1852 | (unsigned long long)private); |
| 1853 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1854 | memset(kaddr + offset, 1, end - start + 1); |
| 1855 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1856 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1857 | if (private == 0) |
| 1858 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1859 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1860 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1861 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1862 | /* |
| 1863 | * This creates an orphan entry for the given inode in case something goes |
| 1864 | * wrong in the middle of an unlink/truncate. |
| 1865 | */ |
| 1866 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 1867 | { |
| 1868 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1869 | int ret = 0; |
| 1870 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1871 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1872 | |
| 1873 | /* already on the orphan list, we're good */ |
| 1874 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1875 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1876 | return 0; |
| 1877 | } |
| 1878 | |
| 1879 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 1880 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1881 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1882 | |
| 1883 | /* |
| 1884 | * insert an orphan item to track this unlinked/truncated file |
| 1885 | */ |
| 1886 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 1887 | |
| 1888 | return ret; |
| 1889 | } |
| 1890 | |
| 1891 | /* |
| 1892 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 1893 | * item for this particular inode. |
| 1894 | */ |
| 1895 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 1896 | { |
| 1897 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1898 | int ret = 0; |
| 1899 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1900 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1901 | |
| 1902 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1903 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1904 | return 0; |
| 1905 | } |
| 1906 | |
| 1907 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 1908 | if (!trans) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1909 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1910 | return 0; |
| 1911 | } |
| 1912 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1913 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1914 | |
| 1915 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 1916 | |
| 1917 | return ret; |
| 1918 | } |
| 1919 | |
| 1920 | /* |
| 1921 | * this cleans up any orphans that may be left on the list from the last use |
| 1922 | * of this root. |
| 1923 | */ |
| 1924 | void btrfs_orphan_cleanup(struct btrfs_root *root) |
| 1925 | { |
| 1926 | struct btrfs_path *path; |
| 1927 | struct extent_buffer *leaf; |
| 1928 | struct btrfs_item *item; |
| 1929 | struct btrfs_key key, found_key; |
| 1930 | struct btrfs_trans_handle *trans; |
| 1931 | struct inode *inode; |
| 1932 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 1933 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1934 | path = btrfs_alloc_path(); |
| 1935 | if (!path) |
| 1936 | return; |
| 1937 | path->reada = -1; |
| 1938 | |
| 1939 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 1940 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 1941 | key.offset = (u64)-1; |
| 1942 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1943 | |
| 1944 | while (1) { |
| 1945 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1946 | if (ret < 0) { |
| 1947 | printk(KERN_ERR "Error searching slot for orphan: %d" |
| 1948 | "\n", ret); |
| 1949 | break; |
| 1950 | } |
| 1951 | |
| 1952 | /* |
| 1953 | * if ret == 0 means we found what we were searching for, which |
| 1954 | * is weird, but possible, so only screw with path if we didnt |
| 1955 | * find the key and see if we have stuff that matches |
| 1956 | */ |
| 1957 | if (ret > 0) { |
| 1958 | if (path->slots[0] == 0) |
| 1959 | break; |
| 1960 | path->slots[0]--; |
| 1961 | } |
| 1962 | |
| 1963 | /* pull out the item */ |
| 1964 | leaf = path->nodes[0]; |
| 1965 | item = btrfs_item_nr(leaf, path->slots[0]); |
| 1966 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1967 | |
| 1968 | /* make sure the item matches what we want */ |
| 1969 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 1970 | break; |
| 1971 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 1972 | break; |
| 1973 | |
| 1974 | /* release the path since we're done with it */ |
| 1975 | btrfs_release_path(root, path); |
| 1976 | |
| 1977 | /* |
| 1978 | * this is where we are basically btrfs_lookup, without the |
| 1979 | * crossing root thing. we store the inode number in the |
| 1980 | * offset of the orphan item. |
| 1981 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1982 | found_key.objectid = found_key.offset; |
| 1983 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 1984 | found_key.offset = 0; |
| 1985 | inode = btrfs_iget(root->fs_info->sb, &found_key, root); |
| 1986 | if (IS_ERR(inode)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1987 | break; |
| 1988 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1989 | /* |
| 1990 | * add this inode to the orphan list so btrfs_orphan_del does |
| 1991 | * the proper thing when we hit it |
| 1992 | */ |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1993 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1994 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1995 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1996 | |
| 1997 | /* |
| 1998 | * if this is a bad inode, means we actually succeeded in |
| 1999 | * removing the inode, but not the orphan record, which means |
| 2000 | * we need to manually delete the orphan since iput will just |
| 2001 | * do a destroy_inode |
| 2002 | */ |
| 2003 | if (is_bad_inode(inode)) { |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2004 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2005 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2006 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2007 | iput(inode); |
| 2008 | continue; |
| 2009 | } |
| 2010 | |
| 2011 | /* if we have links, this was a truncate, lets do that */ |
| 2012 | if (inode->i_nlink) { |
| 2013 | nr_truncate++; |
| 2014 | btrfs_truncate(inode); |
| 2015 | } else { |
| 2016 | nr_unlink++; |
| 2017 | } |
| 2018 | |
| 2019 | /* this will do delete_inode and everything for us */ |
| 2020 | iput(inode); |
| 2021 | } |
| 2022 | |
| 2023 | if (nr_unlink) |
| 2024 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2025 | if (nr_truncate) |
| 2026 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
| 2027 | |
| 2028 | btrfs_free_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2029 | } |
| 2030 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2031 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2032 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2033 | * don't find any xattrs, we know there can't be any acls. |
| 2034 | * |
| 2035 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2036 | */ |
| 2037 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2038 | int slot, u64 objectid) |
| 2039 | { |
| 2040 | u32 nritems = btrfs_header_nritems(leaf); |
| 2041 | struct btrfs_key found_key; |
| 2042 | int scanned = 0; |
| 2043 | |
| 2044 | slot++; |
| 2045 | while (slot < nritems) { |
| 2046 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2047 | |
| 2048 | /* we found a different objectid, there must not be acls */ |
| 2049 | if (found_key.objectid != objectid) |
| 2050 | return 0; |
| 2051 | |
| 2052 | /* we found an xattr, assume we've got an acl */ |
| 2053 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2054 | return 1; |
| 2055 | |
| 2056 | /* |
| 2057 | * we found a key greater than an xattr key, there can't |
| 2058 | * be any acls later on |
| 2059 | */ |
| 2060 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2061 | return 0; |
| 2062 | |
| 2063 | slot++; |
| 2064 | scanned++; |
| 2065 | |
| 2066 | /* |
| 2067 | * it goes inode, inode backrefs, xattrs, extents, |
| 2068 | * so if there are a ton of hard links to an inode there can |
| 2069 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2070 | * this is just an optimization |
| 2071 | */ |
| 2072 | if (scanned >= 8) |
| 2073 | break; |
| 2074 | } |
| 2075 | /* we hit the end of the leaf before we found an xattr or |
| 2076 | * something larger than an xattr. We have to assume the inode |
| 2077 | * has acls |
| 2078 | */ |
| 2079 | return 1; |
| 2080 | } |
| 2081 | |
| 2082 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2083 | * read an inode from the btree into the in-memory inode |
| 2084 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2085 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2086 | { |
| 2087 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2088 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2089 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2090 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2091 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2092 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2093 | int maybe_acls; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2094 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2095 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2096 | int ret; |
| 2097 | |
| 2098 | path = btrfs_alloc_path(); |
| 2099 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2100 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2101 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2102 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2103 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2104 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2105 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2106 | leaf = path->nodes[0]; |
| 2107 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2108 | struct btrfs_inode_item); |
| 2109 | |
| 2110 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2111 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2112 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2113 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2114 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2115 | |
| 2116 | tspec = btrfs_inode_atime(inode_item); |
| 2117 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2118 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2119 | |
| 2120 | tspec = btrfs_inode_mtime(inode_item); |
| 2121 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2122 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2123 | |
| 2124 | tspec = btrfs_inode_ctime(inode_item); |
| 2125 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2126 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2127 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2128 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2129 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2130 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2131 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2132 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2133 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2134 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2135 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2136 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2137 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2138 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2139 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2140 | /* |
| 2141 | * try to precache a NULL acl entry for files that don't have |
| 2142 | * any xattrs or acls |
| 2143 | */ |
| 2144 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 2145 | if (!maybe_acls) |
| 2146 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2147 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2148 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2149 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2150 | btrfs_free_path(path); |
| 2151 | inode_item = NULL; |
| 2152 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2153 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2154 | case S_IFREG: |
| 2155 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2156 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2157 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2158 | inode->i_fop = &btrfs_file_operations; |
| 2159 | inode->i_op = &btrfs_file_inode_operations; |
| 2160 | break; |
| 2161 | case S_IFDIR: |
| 2162 | inode->i_fop = &btrfs_dir_file_operations; |
| 2163 | if (root == root->fs_info->tree_root) |
| 2164 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2165 | else |
| 2166 | inode->i_op = &btrfs_dir_inode_operations; |
| 2167 | break; |
| 2168 | case S_IFLNK: |
| 2169 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2170 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2171 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2172 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2173 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2174 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2175 | init_special_inode(inode, inode->i_mode, rdev); |
| 2176 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2177 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2178 | |
| 2179 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2180 | return; |
| 2181 | |
| 2182 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2183 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2184 | make_bad_inode(inode); |
| 2185 | } |
| 2186 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2187 | /* |
| 2188 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2189 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2190 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2191 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2192 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2193 | struct inode *inode) |
| 2194 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2195 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2196 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2197 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2198 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2199 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2200 | |
| 2201 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2202 | inode->i_atime.tv_sec); |
| 2203 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2204 | inode->i_atime.tv_nsec); |
| 2205 | |
| 2206 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2207 | inode->i_mtime.tv_sec); |
| 2208 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2209 | inode->i_mtime.tv_nsec); |
| 2210 | |
| 2211 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2212 | inode->i_ctime.tv_sec); |
| 2213 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2214 | inode->i_ctime.tv_nsec); |
| 2215 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2216 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2217 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2218 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2219 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2220 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2221 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2222 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2223 | } |
| 2224 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2225 | /* |
| 2226 | * copy everything in the in-memory inode into the btree. |
| 2227 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2228 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2229 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2230 | { |
| 2231 | struct btrfs_inode_item *inode_item; |
| 2232 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2233 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2234 | int ret; |
| 2235 | |
| 2236 | path = btrfs_alloc_path(); |
| 2237 | BUG_ON(!path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2238 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2239 | ret = btrfs_lookup_inode(trans, root, path, |
| 2240 | &BTRFS_I(inode)->location, 1); |
| 2241 | if (ret) { |
| 2242 | if (ret > 0) |
| 2243 | ret = -ENOENT; |
| 2244 | goto failed; |
| 2245 | } |
| 2246 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2247 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2248 | leaf = path->nodes[0]; |
| 2249 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2250 | struct btrfs_inode_item); |
| 2251 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2252 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2253 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2254 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2255 | ret = 0; |
| 2256 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2257 | btrfs_free_path(path); |
| 2258 | return ret; |
| 2259 | } |
| 2260 | |
| 2261 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2262 | /* |
| 2263 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2264 | * recovery code. It remove a link in a directory with a given name, and |
| 2265 | * also drops the back refs in the inode to the directory |
| 2266 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2267 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2268 | struct btrfs_root *root, |
| 2269 | struct inode *dir, struct inode *inode, |
| 2270 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2271 | { |
| 2272 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2273 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2274 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2275 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2276 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2277 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2278 | |
| 2279 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2280 | if (!path) { |
| 2281 | ret = -ENOMEM; |
| 2282 | goto err; |
| 2283 | } |
| 2284 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2285 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2286 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2287 | name, name_len, -1); |
| 2288 | if (IS_ERR(di)) { |
| 2289 | ret = PTR_ERR(di); |
| 2290 | goto err; |
| 2291 | } |
| 2292 | if (!di) { |
| 2293 | ret = -ENOENT; |
| 2294 | goto err; |
| 2295 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2296 | leaf = path->nodes[0]; |
| 2297 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2298 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2299 | if (ret) |
| 2300 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2301 | btrfs_release_path(root, path); |
| 2302 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2303 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2304 | inode->i_ino, |
| 2305 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2306 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2307 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2308 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2309 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2310 | goto err; |
| 2311 | } |
| 2312 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2313 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2314 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2315 | if (IS_ERR(di)) { |
| 2316 | ret = PTR_ERR(di); |
| 2317 | goto err; |
| 2318 | } |
| 2319 | if (!di) { |
| 2320 | ret = -ENOENT; |
| 2321 | goto err; |
| 2322 | } |
| 2323 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2324 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2325 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2326 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 2327 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2328 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2329 | |
| 2330 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2331 | dir, index); |
| 2332 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2333 | err: |
| 2334 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2335 | if (ret) |
| 2336 | goto out; |
| 2337 | |
| 2338 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2339 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2340 | btrfs_update_inode(trans, root, dir); |
| 2341 | btrfs_drop_nlink(inode); |
| 2342 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2343 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2344 | return ret; |
| 2345 | } |
| 2346 | |
| 2347 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2348 | { |
| 2349 | struct btrfs_root *root; |
| 2350 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2351 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2352 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2353 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2354 | |
| 2355 | root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2356 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2357 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2358 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2359 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2360 | |
| 2361 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2362 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2363 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2364 | dentry->d_name.name, dentry->d_name.len); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2365 | |
| 2366 | if (inode->i_nlink == 0) |
| 2367 | ret = btrfs_orphan_add(trans, inode); |
| 2368 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2369 | nr = trans->blocks_used; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2370 | |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2371 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2372 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2373 | return ret; |
| 2374 | } |
| 2375 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 2376 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2377 | struct btrfs_root *root, |
| 2378 | struct inode *dir, u64 objectid, |
| 2379 | const char *name, int name_len) |
| 2380 | { |
| 2381 | struct btrfs_path *path; |
| 2382 | struct extent_buffer *leaf; |
| 2383 | struct btrfs_dir_item *di; |
| 2384 | struct btrfs_key key; |
| 2385 | u64 index; |
| 2386 | int ret; |
| 2387 | |
| 2388 | path = btrfs_alloc_path(); |
| 2389 | if (!path) |
| 2390 | return -ENOMEM; |
| 2391 | |
| 2392 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2393 | name, name_len, -1); |
| 2394 | BUG_ON(!di || IS_ERR(di)); |
| 2395 | |
| 2396 | leaf = path->nodes[0]; |
| 2397 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2398 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2399 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2400 | BUG_ON(ret); |
| 2401 | btrfs_release_path(root, path); |
| 2402 | |
| 2403 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2404 | objectid, root->root_key.objectid, |
| 2405 | dir->i_ino, &index, name, name_len); |
| 2406 | if (ret < 0) { |
| 2407 | BUG_ON(ret != -ENOENT); |
| 2408 | di = btrfs_search_dir_index_item(root, path, dir->i_ino, |
| 2409 | name, name_len); |
| 2410 | BUG_ON(!di || IS_ERR(di)); |
| 2411 | |
| 2412 | leaf = path->nodes[0]; |
| 2413 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2414 | btrfs_release_path(root, path); |
| 2415 | index = key.offset; |
| 2416 | } |
| 2417 | |
| 2418 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 2419 | index, name, name_len, -1); |
| 2420 | BUG_ON(!di || IS_ERR(di)); |
| 2421 | |
| 2422 | leaf = path->nodes[0]; |
| 2423 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2424 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2425 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2426 | BUG_ON(ret); |
| 2427 | btrfs_release_path(root, path); |
| 2428 | |
| 2429 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2430 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2431 | ret = btrfs_update_inode(trans, root, dir); |
| 2432 | BUG_ON(ret); |
| 2433 | dir->i_sb->s_dirt = 1; |
| 2434 | |
| 2435 | btrfs_free_path(path); |
| 2436 | return 0; |
| 2437 | } |
| 2438 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2439 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 2440 | { |
| 2441 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2442 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2443 | int ret; |
| 2444 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2445 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2446 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2447 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 2448 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 2449 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 2450 | return -ENOTEMPTY; |
| 2451 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2452 | trans = btrfs_start_transaction(root, 1); |
| 2453 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2454 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 2455 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 2456 | err = btrfs_unlink_subvol(trans, root, dir, |
| 2457 | BTRFS_I(inode)->location.objectid, |
| 2458 | dentry->d_name.name, |
| 2459 | dentry->d_name.len); |
| 2460 | goto out; |
| 2461 | } |
| 2462 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2463 | err = btrfs_orphan_add(trans, inode); |
| 2464 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 2465 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2466 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2467 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2468 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2469 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2470 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2471 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 2472 | out: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2473 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2474 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2475 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2476 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2477 | if (ret && !err) |
| 2478 | err = ret; |
| 2479 | return err; |
| 2480 | } |
| 2481 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2482 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2483 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2484 | * when truncating bytes in a file, it is possible to avoid reading |
| 2485 | * the leaves that contain only checksum items. This can be the |
| 2486 | * majority of the IO required to delete a large file, but it must |
| 2487 | * be done carefully. |
| 2488 | * |
| 2489 | * The keys in the level just above the leaves are checked to make sure |
| 2490 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 2491 | * after the new size. |
| 2492 | * |
| 2493 | * Then the key for the next leaf is checked to make sure it also has |
| 2494 | * a checksum item for the same file. If it does, we know our target leaf |
| 2495 | * contains only checksum items, and it can be safely freed without reading |
| 2496 | * it. |
| 2497 | * |
| 2498 | * This is just an optimization targeted at large files. It may do |
| 2499 | * nothing. It will return 0 unless things went badly. |
| 2500 | */ |
| 2501 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 2502 | struct btrfs_root *root, |
| 2503 | struct btrfs_path *path, |
| 2504 | struct inode *inode, u64 new_size) |
| 2505 | { |
| 2506 | struct btrfs_key key; |
| 2507 | int ret; |
| 2508 | int nritems; |
| 2509 | struct btrfs_key found_key; |
| 2510 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2511 | struct btrfs_leaf_ref *ref; |
| 2512 | u64 leaf_gen; |
| 2513 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2514 | |
| 2515 | path->lowest_level = 1; |
| 2516 | key.objectid = inode->i_ino; |
| 2517 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 2518 | key.offset = new_size; |
| 2519 | again: |
| 2520 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2521 | if (ret < 0) |
| 2522 | goto out; |
| 2523 | |
| 2524 | if (path->nodes[1] == NULL) { |
| 2525 | ret = 0; |
| 2526 | goto out; |
| 2527 | } |
| 2528 | ret = 0; |
| 2529 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 2530 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 2531 | |
| 2532 | if (!nritems) |
| 2533 | goto out; |
| 2534 | |
| 2535 | if (path->slots[1] >= nritems) |
| 2536 | goto next_node; |
| 2537 | |
| 2538 | /* did we find a key greater than anything we want to delete? */ |
| 2539 | if (found_key.objectid > inode->i_ino || |
| 2540 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 2541 | goto out; |
| 2542 | |
| 2543 | /* we check the next key in the node to make sure the leave contains |
| 2544 | * only checksum items. This comparison doesn't work if our |
| 2545 | * leaf is the last one in the node |
| 2546 | */ |
| 2547 | if (path->slots[1] + 1 >= nritems) { |
| 2548 | next_node: |
| 2549 | /* search forward from the last key in the node, this |
| 2550 | * will bring us into the next node in the tree |
| 2551 | */ |
| 2552 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 2553 | |
| 2554 | /* unlikely, but we inc below, so check to be safe */ |
| 2555 | if (found_key.offset == (u64)-1) |
| 2556 | goto out; |
| 2557 | |
| 2558 | /* search_forward needs a path with locks held, do the |
| 2559 | * search again for the original key. It is possible |
| 2560 | * this will race with a balance and return a path that |
| 2561 | * we could modify, but this drop is just an optimization |
| 2562 | * and is allowed to miss some leaves. |
| 2563 | */ |
| 2564 | btrfs_release_path(root, path); |
| 2565 | found_key.offset++; |
| 2566 | |
| 2567 | /* setup a max key for search_forward */ |
| 2568 | other_key.offset = (u64)-1; |
| 2569 | other_key.type = key.type; |
| 2570 | other_key.objectid = key.objectid; |
| 2571 | |
| 2572 | path->keep_locks = 1; |
| 2573 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 2574 | path, 0, 0); |
| 2575 | path->keep_locks = 0; |
| 2576 | if (ret || found_key.objectid != key.objectid || |
| 2577 | found_key.type != key.type) { |
| 2578 | ret = 0; |
| 2579 | goto out; |
| 2580 | } |
| 2581 | |
| 2582 | key.offset = found_key.offset; |
| 2583 | btrfs_release_path(root, path); |
| 2584 | cond_resched(); |
| 2585 | goto again; |
| 2586 | } |
| 2587 | |
| 2588 | /* we know there's one more slot after us in the tree, |
| 2589 | * read that key so we can verify it is also a checksum item |
| 2590 | */ |
| 2591 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 2592 | |
| 2593 | if (found_key.objectid < inode->i_ino) |
| 2594 | goto next_key; |
| 2595 | |
| 2596 | if (found_key.type != key.type || found_key.offset < new_size) |
| 2597 | goto next_key; |
| 2598 | |
| 2599 | /* |
| 2600 | * if the key for the next leaf isn't a csum key from this objectid, |
| 2601 | * we can't be sure there aren't good items inside this leaf. |
| 2602 | * Bail out |
| 2603 | */ |
| 2604 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 2605 | goto out; |
| 2606 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2607 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 2608 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2609 | /* |
| 2610 | * it is safe to delete this leaf, it contains only |
| 2611 | * csum items from this inode at an offset >= new_size |
| 2612 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2613 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2614 | BUG_ON(ret); |
| 2615 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2616 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 2617 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 2618 | if (ref) { |
| 2619 | ref->root_gen = root->root_key.offset; |
| 2620 | ref->bytenr = leaf_start; |
| 2621 | ref->owner = 0; |
| 2622 | ref->generation = leaf_gen; |
| 2623 | ref->nritems = 0; |
| 2624 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2625 | btrfs_sort_leaf_ref(ref); |
| 2626 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2627 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 2628 | WARN_ON(ret); |
| 2629 | btrfs_free_leaf_ref(root, ref); |
| 2630 | } else { |
| 2631 | WARN_ON(1); |
| 2632 | } |
| 2633 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2634 | next_key: |
| 2635 | btrfs_release_path(root, path); |
| 2636 | |
| 2637 | if (other_key.objectid == inode->i_ino && |
| 2638 | other_key.type == key.type && other_key.offset > key.offset) { |
| 2639 | key.offset = other_key.offset; |
| 2640 | cond_resched(); |
| 2641 | goto again; |
| 2642 | } |
| 2643 | ret = 0; |
| 2644 | out: |
| 2645 | /* fixup any changes we've made to the path */ |
| 2646 | path->lowest_level = 0; |
| 2647 | path->keep_locks = 0; |
| 2648 | btrfs_release_path(root, path); |
| 2649 | return ret; |
| 2650 | } |
| 2651 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2652 | #endif |
| 2653 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2654 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2655 | * this can truncate away extent items, csum items and directory items. |
| 2656 | * 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] | 2657 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2658 | * |
| 2659 | * csum items that cross the new i_size are truncated to the new size |
| 2660 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2661 | * |
| 2662 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 2663 | * 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] | 2664 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2665 | noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 2666 | struct btrfs_root *root, |
| 2667 | struct inode *inode, |
| 2668 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2669 | { |
| 2670 | int ret; |
| 2671 | struct btrfs_path *path; |
| 2672 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2673 | struct btrfs_key found_key; |
Chris Mason | 06d9a8d | 2009-02-04 09:30:58 -0500 | [diff] [blame] | 2674 | u32 found_type = (u8)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2675 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2676 | struct btrfs_file_extent_item *fi; |
| 2677 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2678 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2679 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2680 | u64 item_end = 0; |
| 2681 | int found_extent; |
| 2682 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2683 | int pending_del_nr = 0; |
| 2684 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2685 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2686 | int encoding; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2687 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2688 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2689 | if (root->ref_cows) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2690 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2691 | path = btrfs_alloc_path(); |
| 2692 | BUG_ON(!path); |
Julia Lawall | 33c17ad | 2009-07-22 16:49:01 -0400 | [diff] [blame] | 2693 | path->reada = -1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2694 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2695 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 2696 | key.objectid = inode->i_ino; |
| 2697 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2698 | key.type = (u8)-1; |
| 2699 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2700 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2701 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2702 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2703 | if (ret < 0) |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2704 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2705 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2706 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2707 | /* there are no items in the tree for us to truncate, we're |
| 2708 | * done |
| 2709 | */ |
| 2710 | if (path->slots[0] == 0) { |
| 2711 | ret = 0; |
| 2712 | goto error; |
| 2713 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2714 | path->slots[0]--; |
| 2715 | } |
| 2716 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2717 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2718 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2719 | leaf = path->nodes[0]; |
| 2720 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2721 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2722 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2723 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2724 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2725 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2726 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2727 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2728 | break; |
| 2729 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2730 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2731 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2732 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2733 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2734 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2735 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 2736 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 2737 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 2738 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2739 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2740 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2741 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2742 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2743 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2744 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2745 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 2746 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2747 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2748 | if (item_end < new_size) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2749 | if (found_type == BTRFS_DIR_ITEM_KEY) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2750 | found_type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2751 | else if (found_type == BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2752 | found_type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2753 | else if (found_type == BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2754 | found_type = BTRFS_XATTR_ITEM_KEY; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2755 | else if (found_type == BTRFS_XATTR_ITEM_KEY) |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2756 | found_type = BTRFS_INODE_REF_KEY; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2757 | else if (found_type) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2758 | found_type--; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2759 | else |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2760 | break; |
Yan | a61721d | 2007-09-17 11:08:38 -0400 | [diff] [blame] | 2761 | btrfs_set_key_type(&key, found_type); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2762 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2763 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2764 | if (found_key.offset >= new_size) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2765 | del_item = 1; |
| 2766 | else |
| 2767 | del_item = 0; |
| 2768 | found_extent = 0; |
| 2769 | |
| 2770 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2771 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 2772 | goto delete; |
| 2773 | |
| 2774 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2775 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2776 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2777 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2778 | u64 orig_num_bytes = |
| 2779 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2780 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2781 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 2782 | extent_num_bytes = extent_num_bytes & |
| 2783 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2784 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2785 | extent_num_bytes); |
| 2786 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2787 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2788 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2789 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2790 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2791 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2792 | extent_num_bytes = |
| 2793 | btrfs_file_extent_disk_num_bytes(leaf, |
| 2794 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2795 | extent_offset = found_key.offset - |
| 2796 | btrfs_file_extent_offset(leaf, fi); |
| 2797 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2798 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2799 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2800 | if (extent_start != 0) { |
| 2801 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2802 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2803 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2804 | } |
| 2805 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2806 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2807 | /* |
| 2808 | * we can't truncate inline items that have had |
| 2809 | * special encodings |
| 2810 | */ |
| 2811 | if (!del_item && |
| 2812 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 2813 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 2814 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2815 | u32 size = new_size - found_key.offset; |
| 2816 | |
| 2817 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2818 | inode_sub_bytes(inode, item_end + 1 - |
| 2819 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2820 | } |
| 2821 | size = |
| 2822 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2823 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2824 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2825 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2826 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2827 | inode_sub_bytes(inode, item_end + 1 - |
| 2828 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2829 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2830 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2831 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2832 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2833 | if (!pending_del_nr) { |
| 2834 | /* no pending yet, add ourselves */ |
| 2835 | pending_del_slot = path->slots[0]; |
| 2836 | pending_del_nr = 1; |
| 2837 | } else if (pending_del_nr && |
| 2838 | path->slots[0] + 1 == pending_del_slot) { |
| 2839 | /* hop on the pending chunk */ |
| 2840 | pending_del_nr++; |
| 2841 | pending_del_slot = path->slots[0]; |
| 2842 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2843 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2844 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2845 | } else { |
| 2846 | break; |
| 2847 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2848 | if (found_extent && root->ref_cows) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2849 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2850 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2851 | extent_num_bytes, 0, |
| 2852 | btrfs_header_owner(leaf), |
| 2853 | inode->i_ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2854 | BUG_ON(ret); |
| 2855 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2856 | next: |
| 2857 | if (path->slots[0] == 0) { |
| 2858 | if (pending_del_nr) |
| 2859 | goto del_pending; |
| 2860 | btrfs_release_path(root, path); |
Chris Mason | 06d9a8d | 2009-02-04 09:30:58 -0500 | [diff] [blame] | 2861 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 2862 | break; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2863 | goto search_again; |
| 2864 | } |
| 2865 | |
| 2866 | path->slots[0]--; |
| 2867 | if (pending_del_nr && |
| 2868 | path->slots[0] + 1 != pending_del_slot) { |
| 2869 | struct btrfs_key debug; |
| 2870 | del_pending: |
| 2871 | btrfs_item_key_to_cpu(path->nodes[0], &debug, |
| 2872 | pending_del_slot); |
| 2873 | ret = btrfs_del_items(trans, root, path, |
| 2874 | pending_del_slot, |
| 2875 | pending_del_nr); |
| 2876 | BUG_ON(ret); |
| 2877 | pending_del_nr = 0; |
| 2878 | btrfs_release_path(root, path); |
Chris Mason | 06d9a8d | 2009-02-04 09:30:58 -0500 | [diff] [blame] | 2879 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 2880 | break; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2881 | goto search_again; |
| 2882 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2883 | } |
| 2884 | ret = 0; |
| 2885 | error: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2886 | if (pending_del_nr) { |
| 2887 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 2888 | pending_del_nr); |
| 2889 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2890 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2891 | return ret; |
| 2892 | } |
| 2893 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2894 | /* |
| 2895 | * taken from block_truncate_page, but does cow as it zeros out |
| 2896 | * any bytes left in the last page in the file. |
| 2897 | */ |
| 2898 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 2899 | { |
| 2900 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2901 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2902 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 2903 | struct btrfs_ordered_extent *ordered; |
| 2904 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2905 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2906 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 2907 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 2908 | struct page *page; |
| 2909 | int ret = 0; |
| 2910 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2911 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2912 | |
| 2913 | if ((offset & (blocksize - 1)) == 0) |
| 2914 | goto out; |
| 2915 | |
| 2916 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2917 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2918 | page = grab_cache_page(mapping, index); |
| 2919 | if (!page) |
| 2920 | goto out; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2921 | |
| 2922 | page_start = page_offset(page); |
| 2923 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 2924 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2925 | if (!PageUptodate(page)) { |
| 2926 | ret = btrfs_readpage(NULL, page); |
| 2927 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2928 | if (page->mapping != mapping) { |
| 2929 | unlock_page(page); |
| 2930 | page_cache_release(page); |
| 2931 | goto again; |
| 2932 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2933 | if (!PageUptodate(page)) { |
| 2934 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 2935 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2936 | } |
| 2937 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2938 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2939 | |
| 2940 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 2941 | set_page_extent_mapped(page); |
| 2942 | |
| 2943 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 2944 | if (ordered) { |
| 2945 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 2946 | unlock_page(page); |
| 2947 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 2948 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2949 | btrfs_put_ordered_extent(ordered); |
| 2950 | goto again; |
| 2951 | } |
| 2952 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2953 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2954 | ret = 0; |
| 2955 | if (offset != PAGE_CACHE_SIZE) { |
| 2956 | kaddr = kmap(page); |
| 2957 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 2958 | flush_dcache_page(page); |
| 2959 | kunmap(page); |
| 2960 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2961 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2962 | set_page_dirty(page); |
| 2963 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2964 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 2965 | out_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2966 | unlock_page(page); |
| 2967 | page_cache_release(page); |
| 2968 | out: |
| 2969 | return ret; |
| 2970 | } |
| 2971 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 2972 | int btrfs_cont_expand(struct inode *inode, loff_t size) |
| 2973 | { |
| 2974 | struct btrfs_trans_handle *trans; |
| 2975 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2976 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 2977 | struct extent_map *em; |
| 2978 | u64 mask = root->sectorsize - 1; |
| 2979 | u64 hole_start = (inode->i_size + mask) & ~mask; |
| 2980 | u64 block_end = (size + mask) & ~mask; |
| 2981 | u64 last_byte; |
| 2982 | u64 cur_offset; |
| 2983 | u64 hole_size; |
| 2984 | int err; |
| 2985 | |
| 2986 | if (size <= hole_start) |
| 2987 | return 0; |
| 2988 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2989 | err = btrfs_check_metadata_free_space(root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 2990 | if (err) |
| 2991 | return err; |
| 2992 | |
| 2993 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 2994 | |
| 2995 | while (1) { |
| 2996 | struct btrfs_ordered_extent *ordered; |
| 2997 | btrfs_wait_ordered_range(inode, hole_start, |
| 2998 | block_end - hole_start); |
| 2999 | lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3000 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3001 | if (!ordered) |
| 3002 | break; |
| 3003 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3004 | btrfs_put_ordered_extent(ordered); |
| 3005 | } |
| 3006 | |
| 3007 | trans = btrfs_start_transaction(root, 1); |
| 3008 | btrfs_set_trans_block_group(trans, inode); |
| 3009 | |
| 3010 | cur_offset = hole_start; |
| 3011 | while (1) { |
| 3012 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3013 | block_end - cur_offset, 0); |
| 3014 | BUG_ON(IS_ERR(em) || !em); |
| 3015 | last_byte = min(extent_map_end(em), block_end); |
| 3016 | last_byte = (last_byte + mask) & ~mask; |
| 3017 | if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3018 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3019 | hole_size = last_byte - cur_offset; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3020 | err = btrfs_drop_extents(trans, root, inode, |
| 3021 | cur_offset, |
| 3022 | cur_offset + hole_size, |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 3023 | block_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 3024 | cur_offset, &hint_byte, 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3025 | if (err) |
| 3026 | break; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3027 | err = btrfs_insert_file_extent(trans, root, |
| 3028 | inode->i_ino, cur_offset, 0, |
| 3029 | 0, hole_size, 0, hole_size, |
| 3030 | 0, 0, 0); |
| 3031 | btrfs_drop_extent_cache(inode, hole_start, |
| 3032 | last_byte - 1, 0); |
| 3033 | } |
| 3034 | free_extent_map(em); |
| 3035 | cur_offset = last_byte; |
| 3036 | if (err || cur_offset >= block_end) |
| 3037 | break; |
| 3038 | } |
| 3039 | |
| 3040 | btrfs_end_transaction(trans, root); |
| 3041 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 3042 | return err; |
| 3043 | } |
| 3044 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3045 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3046 | { |
| 3047 | struct inode *inode = dentry->d_inode; |
| 3048 | int err; |
| 3049 | |
| 3050 | err = inode_change_ok(inode, attr); |
| 3051 | if (err) |
| 3052 | return err; |
| 3053 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3054 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
| 3055 | if (attr->ia_size > inode->i_size) { |
| 3056 | err = btrfs_cont_expand(inode, attr->ia_size); |
| 3057 | if (err) |
| 3058 | return err; |
| 3059 | } else if (inode->i_size > 0 && |
| 3060 | attr->ia_size == 0) { |
| 3061 | |
| 3062 | /* we're truncating a file that used to have good |
| 3063 | * data down to zero. Make sure it gets into |
| 3064 | * the ordered flush list so that any new writes |
| 3065 | * get down to disk quickly. |
| 3066 | */ |
| 3067 | BTRFS_I(inode)->ordered_data_close = 1; |
| 3068 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3069 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3070 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3071 | err = inode_setattr(inode, attr); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3072 | |
| 3073 | if (!err && ((attr->ia_valid & ATTR_MODE))) |
| 3074 | err = btrfs_acl_chmod(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3075 | return err; |
| 3076 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3077 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3078 | void btrfs_delete_inode(struct inode *inode) |
| 3079 | { |
| 3080 | struct btrfs_trans_handle *trans; |
| 3081 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3082 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3083 | int ret; |
| 3084 | |
| 3085 | truncate_inode_pages(&inode->i_data, 0); |
| 3086 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3087 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3088 | goto no_delete; |
| 3089 | } |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3090 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3091 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3092 | btrfs_i_size_write(inode, 0); |
Yan Zheng | 180591b | 2009-01-06 09:58:06 -0500 | [diff] [blame] | 3093 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3094 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3095 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3096 | ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3097 | if (ret) { |
| 3098 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3099 | goto no_delete_lock; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | btrfs_orphan_del(trans, inode); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3103 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3104 | nr = trans->blocks_used; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3105 | clear_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3106 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3107 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3108 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3109 | return; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3110 | |
| 3111 | no_delete_lock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3112 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3113 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3114 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3115 | no_delete: |
| 3116 | clear_inode(inode); |
| 3117 | } |
| 3118 | |
| 3119 | /* |
| 3120 | * this returns the key found in the dir entry in the location pointer. |
| 3121 | * If no dir entries were found, location->objectid is 0. |
| 3122 | */ |
| 3123 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3124 | struct btrfs_key *location) |
| 3125 | { |
| 3126 | const char *name = dentry->d_name.name; |
| 3127 | int namelen = dentry->d_name.len; |
| 3128 | struct btrfs_dir_item *di; |
| 3129 | struct btrfs_path *path; |
| 3130 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3131 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3132 | |
| 3133 | path = btrfs_alloc_path(); |
| 3134 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3135 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3136 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 3137 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3138 | if (IS_ERR(di)) |
| 3139 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3140 | |
| 3141 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3142 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3143 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3144 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3145 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3146 | btrfs_free_path(path); |
| 3147 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3148 | out_err: |
| 3149 | location->objectid = 0; |
| 3150 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | /* |
| 3154 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3155 | * needs to be changed to reflect the root directory of the tree root. This |
| 3156 | * is kind of like crossing a mount point. |
| 3157 | */ |
| 3158 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3159 | struct inode *dir, |
| 3160 | struct dentry *dentry, |
| 3161 | struct btrfs_key *location, |
| 3162 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3163 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3164 | struct btrfs_path *path; |
| 3165 | struct btrfs_root *new_root; |
| 3166 | struct btrfs_root_ref *ref; |
| 3167 | struct extent_buffer *leaf; |
| 3168 | int ret; |
| 3169 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3170 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3171 | path = btrfs_alloc_path(); |
| 3172 | if (!path) { |
| 3173 | err = -ENOMEM; |
| 3174 | goto out; |
| 3175 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3176 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3177 | err = -ENOENT; |
| 3178 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3179 | BTRFS_I(dir)->root->root_key.objectid, |
| 3180 | location->objectid); |
| 3181 | if (ret) { |
| 3182 | if (ret < 0) |
| 3183 | err = ret; |
| 3184 | goto out; |
| 3185 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3186 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3187 | leaf = path->nodes[0]; |
| 3188 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 3189 | if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || |
| 3190 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3191 | goto out; |
| 3192 | |
| 3193 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3194 | (unsigned long)(ref + 1), |
| 3195 | dentry->d_name.len); |
| 3196 | if (ret) |
| 3197 | goto out; |
| 3198 | |
| 3199 | btrfs_release_path(root->fs_info->tree_root, path); |
| 3200 | |
| 3201 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3202 | if (IS_ERR(new_root)) { |
| 3203 | err = PTR_ERR(new_root); |
| 3204 | goto out; |
| 3205 | } |
| 3206 | |
| 3207 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3208 | err = -ENOENT; |
| 3209 | goto out; |
| 3210 | } |
| 3211 | |
| 3212 | *sub_root = new_root; |
| 3213 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3214 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3215 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3216 | err = 0; |
| 3217 | out: |
| 3218 | btrfs_free_path(path); |
| 3219 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3220 | } |
| 3221 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3222 | static void inode_tree_add(struct inode *inode) |
| 3223 | { |
| 3224 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3225 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3226 | struct rb_node **p; |
| 3227 | struct rb_node *parent; |
| 3228 | |
| 3229 | again: |
| 3230 | p = &root->inode_tree.rb_node; |
| 3231 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3232 | |
| 3233 | spin_lock(&root->inode_lock); |
| 3234 | while (*p) { |
| 3235 | parent = *p; |
| 3236 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3237 | |
| 3238 | if (inode->i_ino < entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3239 | p = &parent->rb_left; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3240 | else if (inode->i_ino > entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3241 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3242 | else { |
| 3243 | WARN_ON(!(entry->vfs_inode.i_state & |
| 3244 | (I_WILL_FREE | I_FREEING | I_CLEAR))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3245 | rb_erase(parent, &root->inode_tree); |
| 3246 | RB_CLEAR_NODE(parent); |
| 3247 | spin_unlock(&root->inode_lock); |
| 3248 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3249 | } |
| 3250 | } |
| 3251 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3252 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3253 | spin_unlock(&root->inode_lock); |
| 3254 | } |
| 3255 | |
| 3256 | static void inode_tree_del(struct inode *inode) |
| 3257 | { |
| 3258 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3259 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3260 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3261 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3262 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3263 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
| 3264 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3265 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3266 | } |
| 3267 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3268 | static noinline void init_btrfs_i(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3269 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3270 | struct btrfs_inode *bi = BTRFS_I(inode); |
| 3271 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3272 | bi->generation = 0; |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 3273 | bi->sequence = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3274 | bi->last_trans = 0; |
| 3275 | bi->logged_trans = 0; |
| 3276 | bi->delalloc_bytes = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3277 | bi->reserved_bytes = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3278 | bi->disk_i_size = 0; |
| 3279 | bi->flags = 0; |
| 3280 | bi->index_cnt = (u64)-1; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3281 | bi->last_unlink_trans = 0; |
Chris Mason | 2757495 | 2009-05-14 13:10:02 -0400 | [diff] [blame] | 3282 | bi->ordered_data_close = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3283 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 3284 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3285 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3286 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
| 3287 | inode->i_mapping, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3288 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3289 | INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3290 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 3291 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 3292 | mutex_init(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3293 | mutex_init(&BTRFS_I(inode)->log_mutex); |
| 3294 | } |
| 3295 | |
| 3296 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 3297 | { |
| 3298 | struct btrfs_iget_args *args = p; |
| 3299 | inode->i_ino = args->ino; |
| 3300 | init_btrfs_i(inode); |
| 3301 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3302 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3303 | return 0; |
| 3304 | } |
| 3305 | |
| 3306 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 3307 | { |
| 3308 | struct btrfs_iget_args *args = opaque; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3309 | return args->ino == inode->i_ino && |
| 3310 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3311 | } |
| 3312 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3313 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 3314 | u64 objectid, |
| 3315 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3316 | { |
| 3317 | struct inode *inode; |
| 3318 | struct btrfs_iget_args args; |
| 3319 | args.ino = objectid; |
| 3320 | args.root = root; |
| 3321 | |
| 3322 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 3323 | btrfs_init_locked_inode, |
| 3324 | (void *)&args); |
| 3325 | return inode; |
| 3326 | } |
| 3327 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3328 | /* Get an inode object given its location and corresponding root. |
| 3329 | * Returns in *is_new if the inode was read from disk |
| 3330 | */ |
| 3331 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3332 | struct btrfs_root *root) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3333 | { |
| 3334 | struct inode *inode; |
| 3335 | |
| 3336 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 3337 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3338 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3339 | |
| 3340 | if (inode->i_state & I_NEW) { |
| 3341 | BTRFS_I(inode)->root = root; |
| 3342 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 3343 | btrfs_read_locked_inode(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3344 | |
| 3345 | inode_tree_add(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3346 | unlock_new_inode(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | return inode; |
| 3350 | } |
| 3351 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3352 | static struct inode *new_simple_dir(struct super_block *s, |
| 3353 | struct btrfs_key *key, |
| 3354 | struct btrfs_root *root) |
| 3355 | { |
| 3356 | struct inode *inode = new_inode(s); |
| 3357 | |
| 3358 | if (!inode) |
| 3359 | return ERR_PTR(-ENOMEM); |
| 3360 | |
| 3361 | init_btrfs_i(inode); |
| 3362 | |
| 3363 | BTRFS_I(inode)->root = root; |
| 3364 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 3365 | BTRFS_I(inode)->dummy_inode = 1; |
| 3366 | |
| 3367 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 3368 | inode->i_op = &simple_dir_inode_operations; |
| 3369 | inode->i_fop = &simple_dir_operations; |
| 3370 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 3371 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 3372 | |
| 3373 | return inode; |
| 3374 | } |
| 3375 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3376 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3377 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3378 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3379 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3380 | struct btrfs_root *sub_root = root; |
| 3381 | struct btrfs_key location; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3382 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3383 | |
| 3384 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 3385 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3386 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3387 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3388 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3389 | if (ret < 0) |
| 3390 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3391 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3392 | if (location.objectid == 0) |
| 3393 | return NULL; |
| 3394 | |
| 3395 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
| 3396 | inode = btrfs_iget(dir->i_sb, &location, root); |
| 3397 | return inode; |
| 3398 | } |
| 3399 | |
| 3400 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 3401 | |
| 3402 | ret = fixup_tree_root_location(root, dir, dentry, |
| 3403 | &location, &sub_root); |
| 3404 | if (ret < 0) { |
| 3405 | if (ret != -ENOENT) |
| 3406 | inode = ERR_PTR(ret); |
| 3407 | else |
| 3408 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 3409 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3410 | inode = btrfs_iget(dir->i_sb, &location, sub_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3411 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3412 | return inode; |
| 3413 | } |
| 3414 | |
| 3415 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 3416 | struct nameidata *nd) |
| 3417 | { |
| 3418 | struct inode *inode; |
| 3419 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3420 | inode = btrfs_lookup_dentry(dir, dentry); |
| 3421 | if (IS_ERR(inode)) |
| 3422 | return ERR_CAST(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3423 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3424 | return d_splice_alias(inode, dentry); |
| 3425 | } |
| 3426 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3427 | static unsigned char btrfs_filetype_table[] = { |
| 3428 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 3429 | }; |
| 3430 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 3431 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 3432 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3433 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 3434 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3435 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3436 | struct btrfs_item *item; |
| 3437 | struct btrfs_dir_item *di; |
| 3438 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3439 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3440 | struct btrfs_path *path; |
| 3441 | int ret; |
| 3442 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3443 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3444 | int slot; |
| 3445 | int advance; |
| 3446 | unsigned char d_type; |
| 3447 | int over = 0; |
| 3448 | u32 di_cur; |
| 3449 | u32 di_total; |
| 3450 | u32 di_len; |
| 3451 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3452 | char tmp_name[32]; |
| 3453 | char *name_ptr; |
| 3454 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3455 | |
| 3456 | /* FIXME, use a real flag for deciding about the key type */ |
| 3457 | if (root->fs_info->tree_root == root) |
| 3458 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3459 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3460 | /* special case for "." */ |
| 3461 | if (filp->f_pos == 0) { |
| 3462 | over = filldir(dirent, ".", 1, |
| 3463 | 1, inode->i_ino, |
| 3464 | DT_DIR); |
| 3465 | if (over) |
| 3466 | return 0; |
| 3467 | filp->f_pos = 1; |
| 3468 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3469 | /* special case for .., just use the back ref */ |
| 3470 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3471 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3472 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 3473 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3474 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3475 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3476 | filp->f_pos = 2; |
| 3477 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3478 | path = btrfs_alloc_path(); |
| 3479 | path->reada = 2; |
| 3480 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3481 | btrfs_set_key_type(&key, key_type); |
| 3482 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3483 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3484 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3485 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3486 | if (ret < 0) |
| 3487 | goto err; |
| 3488 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3489 | |
| 3490 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3491 | leaf = path->nodes[0]; |
| 3492 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3493 | slot = path->slots[0]; |
| 3494 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3495 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3496 | ret = btrfs_next_leaf(root, path); |
| 3497 | if (ret) |
| 3498 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3499 | leaf = path->nodes[0]; |
| 3500 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3501 | slot = path->slots[0]; |
| 3502 | } else { |
| 3503 | slot++; |
| 3504 | path->slots[0]++; |
| 3505 | } |
| 3506 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3507 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3508 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3509 | item = btrfs_item_nr(leaf, slot); |
| 3510 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3511 | |
| 3512 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3513 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3514 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3515 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3516 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3517 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3518 | |
| 3519 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3520 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3521 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 3522 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3523 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3524 | |
| 3525 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3526 | struct btrfs_key location; |
| 3527 | |
| 3528 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3529 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3530 | name_ptr = tmp_name; |
| 3531 | } else { |
| 3532 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3533 | if (!name_ptr) { |
| 3534 | ret = -ENOMEM; |
| 3535 | goto err; |
| 3536 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3537 | } |
| 3538 | read_extent_buffer(leaf, name_ptr, |
| 3539 | (unsigned long)(di + 1), name_len); |
| 3540 | |
| 3541 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 3542 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3543 | |
| 3544 | /* is this a reference to our own snapshot? If so |
| 3545 | * skip it |
| 3546 | */ |
| 3547 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 3548 | location.objectid == root->root_key.objectid) { |
| 3549 | over = 0; |
| 3550 | goto skip; |
| 3551 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3552 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3553 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3554 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3555 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3556 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3557 | if (name_ptr != tmp_name) |
| 3558 | kfree(name_ptr); |
| 3559 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3560 | if (over) |
| 3561 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 3562 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3563 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3564 | di_cur += di_len; |
| 3565 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 3566 | } |
| 3567 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 3568 | |
| 3569 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 3570 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Chris Mason | 89f135d | 2009-01-28 15:34:27 -0500 | [diff] [blame] | 3571 | filp->f_pos = INT_LIMIT(off_t); |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 3572 | else |
| 3573 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3574 | nopos: |
| 3575 | ret = 0; |
| 3576 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3577 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3578 | return ret; |
| 3579 | } |
| 3580 | |
| 3581 | int btrfs_write_inode(struct inode *inode, int wait) |
| 3582 | { |
| 3583 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3584 | struct btrfs_trans_handle *trans; |
| 3585 | int ret = 0; |
| 3586 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3587 | if (root->fs_info->btree_inode == inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 3588 | return 0; |
| 3589 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3590 | if (wait) { |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 3591 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3592 | btrfs_set_trans_block_group(trans, inode); |
| 3593 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3594 | } |
| 3595 | return ret; |
| 3596 | } |
| 3597 | |
| 3598 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3599 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3600 | * inode changes. But, it is most likely to find the inode in cache. |
| 3601 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 3602 | * to keep or drop this code. |
| 3603 | */ |
| 3604 | void btrfs_dirty_inode(struct inode *inode) |
| 3605 | { |
| 3606 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3607 | struct btrfs_trans_handle *trans; |
| 3608 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 3609 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3610 | btrfs_set_trans_block_group(trans, inode); |
| 3611 | btrfs_update_inode(trans, root, inode); |
| 3612 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3613 | } |
| 3614 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3615 | /* |
| 3616 | * find the highest existing sequence number in a directory |
| 3617 | * and then set the in-memory index_cnt variable to reflect |
| 3618 | * free sequence numbers |
| 3619 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3620 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 3621 | { |
| 3622 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3623 | struct btrfs_key key, found_key; |
| 3624 | struct btrfs_path *path; |
| 3625 | struct extent_buffer *leaf; |
| 3626 | int ret; |
| 3627 | |
| 3628 | key.objectid = inode->i_ino; |
| 3629 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 3630 | key.offset = (u64)-1; |
| 3631 | |
| 3632 | path = btrfs_alloc_path(); |
| 3633 | if (!path) |
| 3634 | return -ENOMEM; |
| 3635 | |
| 3636 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3637 | if (ret < 0) |
| 3638 | goto out; |
| 3639 | /* FIXME: we should be able to handle this */ |
| 3640 | if (ret == 0) |
| 3641 | goto out; |
| 3642 | ret = 0; |
| 3643 | |
| 3644 | /* |
| 3645 | * MAGIC NUMBER EXPLANATION: |
| 3646 | * since we search a directory based on f_pos we have to start at 2 |
| 3647 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 3648 | * else has to start at 2 |
| 3649 | */ |
| 3650 | if (path->slots[0] == 0) { |
| 3651 | BTRFS_I(inode)->index_cnt = 2; |
| 3652 | goto out; |
| 3653 | } |
| 3654 | |
| 3655 | path->slots[0]--; |
| 3656 | |
| 3657 | leaf = path->nodes[0]; |
| 3658 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3659 | |
| 3660 | if (found_key.objectid != inode->i_ino || |
| 3661 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 3662 | BTRFS_I(inode)->index_cnt = 2; |
| 3663 | goto out; |
| 3664 | } |
| 3665 | |
| 3666 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 3667 | out: |
| 3668 | btrfs_free_path(path); |
| 3669 | return ret; |
| 3670 | } |
| 3671 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3672 | /* |
| 3673 | * helper to find a free sequence number in a given directory. This current |
| 3674 | * code is very simple, later versions will do smarter things in the btree |
| 3675 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3676 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3677 | { |
| 3678 | int ret = 0; |
| 3679 | |
| 3680 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 3681 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3682 | if (ret) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3683 | return ret; |
| 3684 | } |
| 3685 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3686 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3687 | BTRFS_I(dir)->index_cnt++; |
| 3688 | |
| 3689 | return ret; |
| 3690 | } |
| 3691 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3692 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 3693 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3694 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3695 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3696 | u64 ref_objectid, u64 objectid, |
| 3697 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3698 | { |
| 3699 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3700 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3701 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3702 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3703 | struct btrfs_inode_ref *ref; |
| 3704 | struct btrfs_key key[2]; |
| 3705 | u32 sizes[2]; |
| 3706 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3707 | int ret; |
| 3708 | int owner; |
| 3709 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3710 | path = btrfs_alloc_path(); |
| 3711 | BUG_ON(!path); |
| 3712 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3713 | inode = new_inode(root->fs_info->sb); |
| 3714 | if (!inode) |
| 3715 | return ERR_PTR(-ENOMEM); |
| 3716 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3717 | if (dir) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3718 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 3719 | if (ret) { |
| 3720 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3721 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 3722 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3723 | } |
| 3724 | /* |
| 3725 | * index_cnt is ignored for everything but a dir, |
| 3726 | * btrfs_get_inode_index_count has an explanation for the magic |
| 3727 | * number |
| 3728 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3729 | init_btrfs_i(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3730 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3731 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3732 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3733 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3734 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3735 | if (mode & S_IFDIR) |
| 3736 | owner = 0; |
| 3737 | else |
| 3738 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3739 | BTRFS_I(inode)->block_group = |
| 3740 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3741 | |
| 3742 | key[0].objectid = objectid; |
| 3743 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 3744 | key[0].offset = 0; |
| 3745 | |
| 3746 | key[1].objectid = objectid; |
| 3747 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 3748 | key[1].offset = ref_objectid; |
| 3749 | |
| 3750 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 3751 | sizes[1] = name_len + sizeof(*ref); |
| 3752 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3753 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3754 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 3755 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3756 | goto fail; |
| 3757 | |
Chris Mason | 79683f2 | 2008-11-19 22:00:53 -0500 | [diff] [blame] | 3758 | inode->i_uid = current_fsuid(); |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 3759 | |
Chris Mason | 42f15d7 | 2009-02-06 11:35:57 -0500 | [diff] [blame] | 3760 | if (dir && (dir->i_mode & S_ISGID)) { |
Chris Ball | 8c087b5 | 2009-02-04 09:29:54 -0500 | [diff] [blame] | 3761 | inode->i_gid = dir->i_gid; |
| 3762 | if (S_ISDIR(mode)) |
| 3763 | mode |= S_ISGID; |
| 3764 | } else |
| 3765 | inode->i_gid = current_fsgid(); |
| 3766 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3767 | inode->i_mode = mode; |
| 3768 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3769 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3770 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3771 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3772 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3773 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3774 | |
| 3775 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 3776 | struct btrfs_inode_ref); |
| 3777 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3778 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3779 | ptr = (unsigned long)(ref + 1); |
| 3780 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 3781 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3782 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3783 | btrfs_free_path(path); |
| 3784 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3785 | location = &BTRFS_I(inode)->location; |
| 3786 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3787 | location->offset = 0; |
| 3788 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 3789 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3790 | btrfs_inherit_iflags(inode, dir); |
| 3791 | |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 3792 | if ((mode & S_IFREG)) { |
| 3793 | if (btrfs_test_opt(root, NODATASUM)) |
| 3794 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 3795 | if (btrfs_test_opt(root, NODATACOW)) |
| 3796 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 3797 | } |
| 3798 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3799 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3800 | inode_tree_add(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3801 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3802 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3803 | if (dir) |
| 3804 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3805 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 3806 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3807 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 3811 | { |
| 3812 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 3813 | } |
| 3814 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3815 | /* |
| 3816 | * utility function to add 'inode' into 'parent_inode' with |
| 3817 | * a give name and a given sequence number. |
| 3818 | * if 'add_backref' is true, also insert a backref from the |
| 3819 | * inode to the parent directory. |
| 3820 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3821 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 3822 | struct inode *parent_inode, struct inode *inode, |
| 3823 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3824 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3825 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3826 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3827 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3828 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3829 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 3830 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 3831 | } else { |
| 3832 | key.objectid = inode->i_ino; |
| 3833 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 3834 | key.offset = 0; |
| 3835 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3836 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3837 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 3838 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 3839 | key.objectid, root->root_key.objectid, |
| 3840 | parent_inode->i_ino, |
| 3841 | index, name, name_len); |
| 3842 | } else if (add_backref) { |
| 3843 | ret = btrfs_insert_inode_ref(trans, root, |
| 3844 | name, name_len, inode->i_ino, |
| 3845 | parent_inode->i_ino, index); |
| 3846 | } |
| 3847 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3848 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 3849 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 3850 | parent_inode->i_ino, &key, |
| 3851 | btrfs_inode_type(inode), index); |
| 3852 | BUG_ON(ret); |
| 3853 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3854 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3855 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 3856 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3857 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3858 | } |
| 3859 | return ret; |
| 3860 | } |
| 3861 | |
| 3862 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3863 | struct dentry *dentry, struct inode *inode, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3864 | int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3865 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3866 | int err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 3867 | inode, dentry->d_name.name, |
| 3868 | dentry->d_name.len, backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3869 | if (!err) { |
| 3870 | d_instantiate(dentry, inode); |
| 3871 | return 0; |
| 3872 | } |
| 3873 | if (err > 0) |
| 3874 | err = -EEXIST; |
| 3875 | return err; |
| 3876 | } |
| 3877 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3878 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 3879 | int mode, dev_t rdev) |
| 3880 | { |
| 3881 | struct btrfs_trans_handle *trans; |
| 3882 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3883 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3884 | int err; |
| 3885 | int drop_inode = 0; |
| 3886 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3887 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3888 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3889 | |
| 3890 | if (!new_valid_dev(rdev)) |
| 3891 | return -EINVAL; |
| 3892 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3893 | err = btrfs_check_metadata_free_space(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3894 | if (err) |
| 3895 | goto fail; |
| 3896 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3897 | trans = btrfs_start_transaction(root, 1); |
| 3898 | btrfs_set_trans_block_group(trans, dir); |
| 3899 | |
| 3900 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 3901 | if (err) { |
| 3902 | err = -ENOSPC; |
| 3903 | goto out_unlock; |
| 3904 | } |
| 3905 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3906 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3907 | dentry->d_name.len, |
| 3908 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3909 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3910 | err = PTR_ERR(inode); |
| 3911 | if (IS_ERR(inode)) |
| 3912 | goto out_unlock; |
| 3913 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3914 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3915 | if (err) { |
| 3916 | drop_inode = 1; |
| 3917 | goto out_unlock; |
| 3918 | } |
| 3919 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3920 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3921 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3922 | if (err) |
| 3923 | drop_inode = 1; |
| 3924 | else { |
| 3925 | inode->i_op = &btrfs_special_inode_operations; |
| 3926 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 3927 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3928 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3929 | btrfs_update_inode_block_group(trans, inode); |
| 3930 | btrfs_update_inode_block_group(trans, dir); |
| 3931 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3932 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3933 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3934 | fail: |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3935 | if (drop_inode) { |
| 3936 | inode_dec_link_count(inode); |
| 3937 | iput(inode); |
| 3938 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3939 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3940 | return err; |
| 3941 | } |
| 3942 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3943 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 3944 | int mode, struct nameidata *nd) |
| 3945 | { |
| 3946 | struct btrfs_trans_handle *trans; |
| 3947 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3948 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3949 | int err; |
| 3950 | int drop_inode = 0; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3951 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3952 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3953 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3954 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3955 | err = btrfs_check_metadata_free_space(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3956 | if (err) |
| 3957 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3958 | trans = btrfs_start_transaction(root, 1); |
| 3959 | btrfs_set_trans_block_group(trans, dir); |
| 3960 | |
| 3961 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 3962 | if (err) { |
| 3963 | err = -ENOSPC; |
| 3964 | goto out_unlock; |
| 3965 | } |
| 3966 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3967 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3968 | dentry->d_name.len, |
| 3969 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3970 | objectid, BTRFS_I(dir)->block_group, mode, |
| 3971 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3972 | err = PTR_ERR(inode); |
| 3973 | if (IS_ERR(inode)) |
| 3974 | goto out_unlock; |
| 3975 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3976 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3977 | if (err) { |
| 3978 | drop_inode = 1; |
| 3979 | goto out_unlock; |
| 3980 | } |
| 3981 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3982 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3983 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3984 | if (err) |
| 3985 | drop_inode = 1; |
| 3986 | else { |
| 3987 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3988 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3989 | inode->i_fop = &btrfs_file_operations; |
| 3990 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3991 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3992 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3993 | btrfs_update_inode_block_group(trans, inode); |
| 3994 | btrfs_update_inode_block_group(trans, dir); |
| 3995 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3996 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 3997 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3998 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3999 | if (drop_inode) { |
| 4000 | inode_dec_link_count(inode); |
| 4001 | iput(inode); |
| 4002 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4003 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4004 | return err; |
| 4005 | } |
| 4006 | |
| 4007 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4008 | struct dentry *dentry) |
| 4009 | { |
| 4010 | struct btrfs_trans_handle *trans; |
| 4011 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4012 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4013 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4014 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4015 | int err; |
| 4016 | int drop_inode = 0; |
| 4017 | |
| 4018 | if (inode->i_nlink == 0) |
| 4019 | return -ENOENT; |
| 4020 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4021 | btrfs_inc_nlink(inode); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4022 | err = btrfs_check_metadata_free_space(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4023 | if (err) |
| 4024 | goto fail; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4025 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4026 | if (err) |
| 4027 | goto fail; |
| 4028 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4029 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4030 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4031 | btrfs_set_trans_block_group(trans, dir); |
| 4032 | atomic_inc(&inode->i_count); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4033 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4034 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4035 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4036 | if (err) |
| 4037 | drop_inode = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4038 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4039 | btrfs_update_inode_block_group(trans, dir); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4040 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4041 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4042 | if (err) |
| 4043 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4044 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4045 | nr = trans->blocks_used; |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4046 | |
| 4047 | btrfs_log_new_name(trans, inode, NULL, dentry->d_parent); |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4048 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4049 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4050 | if (drop_inode) { |
| 4051 | inode_dec_link_count(inode); |
| 4052 | iput(inode); |
| 4053 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4054 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4055 | return err; |
| 4056 | } |
| 4057 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4058 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4059 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4060 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4061 | struct btrfs_trans_handle *trans; |
| 4062 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4063 | int err = 0; |
| 4064 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4065 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4066 | u64 index = 0; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4067 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4068 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4069 | err = btrfs_check_metadata_free_space(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4070 | if (err) |
| 4071 | goto out_unlock; |
| 4072 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4073 | trans = btrfs_start_transaction(root, 1); |
| 4074 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4075 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4076 | if (IS_ERR(trans)) { |
| 4077 | err = PTR_ERR(trans); |
| 4078 | goto out_unlock; |
| 4079 | } |
| 4080 | |
| 4081 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4082 | if (err) { |
| 4083 | err = -ENOSPC; |
| 4084 | goto out_unlock; |
| 4085 | } |
| 4086 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4087 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4088 | dentry->d_name.len, |
| 4089 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4090 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 4091 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4092 | if (IS_ERR(inode)) { |
| 4093 | err = PTR_ERR(inode); |
| 4094 | goto out_fail; |
| 4095 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4096 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4097 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4098 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 4099 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4100 | if (err) |
| 4101 | goto out_fail; |
| 4102 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4103 | inode->i_op = &btrfs_dir_inode_operations; |
| 4104 | inode->i_fop = &btrfs_dir_file_operations; |
| 4105 | btrfs_set_trans_block_group(trans, inode); |
| 4106 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4107 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4108 | err = btrfs_update_inode(trans, root, inode); |
| 4109 | if (err) |
| 4110 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4111 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4112 | err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 4113 | inode, dentry->d_name.name, |
| 4114 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4115 | if (err) |
| 4116 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4117 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4118 | d_instantiate(dentry, inode); |
| 4119 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4120 | btrfs_update_inode_block_group(trans, inode); |
| 4121 | btrfs_update_inode_block_group(trans, dir); |
| 4122 | |
| 4123 | out_fail: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4124 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4125 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4126 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4127 | out_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4128 | if (drop_on_err) |
| 4129 | iput(inode); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4130 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4131 | return err; |
| 4132 | } |
| 4133 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4134 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4135 | * and an extent that you want to insert, deal with overlap and insert |
| 4136 | * the new extent into the tree. |
| 4137 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4138 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4139 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4140 | struct extent_map *em, |
| 4141 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4142 | { |
| 4143 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4144 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4145 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4146 | start_diff = map_start - em->start; |
| 4147 | em->start = map_start; |
| 4148 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4149 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4150 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4151 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4152 | em->block_len -= start_diff; |
| 4153 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4154 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4155 | } |
| 4156 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4157 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4158 | struct inode *inode, struct page *page, |
| 4159 | size_t pg_offset, u64 extent_offset, |
| 4160 | struct btrfs_file_extent_item *item) |
| 4161 | { |
| 4162 | int ret; |
| 4163 | struct extent_buffer *leaf = path->nodes[0]; |
| 4164 | char *tmp; |
| 4165 | size_t max_size; |
| 4166 | unsigned long inline_size; |
| 4167 | unsigned long ptr; |
| 4168 | |
| 4169 | WARN_ON(pg_offset != 0); |
| 4170 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4171 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4172 | btrfs_item_nr(leaf, path->slots[0])); |
| 4173 | tmp = kmalloc(inline_size, GFP_NOFS); |
| 4174 | ptr = btrfs_file_extent_inline_start(item); |
| 4175 | |
| 4176 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4177 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 4178 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4179 | ret = btrfs_zlib_decompress(tmp, page, extent_offset, |
| 4180 | inline_size, max_size); |
| 4181 | if (ret) { |
| 4182 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 4183 | unsigned long copy_size = min_t(u64, |
| 4184 | PAGE_CACHE_SIZE - pg_offset, |
| 4185 | max_size - extent_offset); |
| 4186 | memset(kaddr + pg_offset, 0, copy_size); |
| 4187 | kunmap_atomic(kaddr, KM_USER0); |
| 4188 | } |
| 4189 | kfree(tmp); |
| 4190 | return 0; |
| 4191 | } |
| 4192 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4193 | /* |
| 4194 | * 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] | 4195 | * the ugly parts come from merging extents from the disk with the in-ram |
| 4196 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4197 | * where the in-ram extents might be locked pending data=ordered completion. |
| 4198 | * |
| 4199 | * This also copies inline extents directly into the page. |
| 4200 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4201 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4202 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4203 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4204 | int create) |
| 4205 | { |
| 4206 | int ret; |
| 4207 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4208 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4209 | u64 extent_start = 0; |
| 4210 | u64 extent_end = 0; |
| 4211 | u64 objectid = inode->i_ino; |
| 4212 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4213 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4214 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4215 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4216 | struct extent_buffer *leaf; |
| 4217 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4218 | struct extent_map *em = NULL; |
| 4219 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4220 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4221 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4222 | int compressed; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4223 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4224 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4225 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4226 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4227 | if (em) |
| 4228 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4229 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4230 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4231 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4232 | if (em->start > start || em->start + em->len <= start) |
| 4233 | free_extent_map(em); |
| 4234 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4235 | free_extent_map(em); |
| 4236 | else |
| 4237 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4238 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4239 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4240 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4241 | err = -ENOMEM; |
| 4242 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4243 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4244 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4245 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 4246 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4247 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4248 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4249 | |
| 4250 | if (!path) { |
| 4251 | path = btrfs_alloc_path(); |
| 4252 | BUG_ON(!path); |
| 4253 | } |
| 4254 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4255 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 4256 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4257 | if (ret < 0) { |
| 4258 | err = ret; |
| 4259 | goto out; |
| 4260 | } |
| 4261 | |
| 4262 | if (ret != 0) { |
| 4263 | if (path->slots[0] == 0) |
| 4264 | goto not_found; |
| 4265 | path->slots[0]--; |
| 4266 | } |
| 4267 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4268 | leaf = path->nodes[0]; |
| 4269 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4270 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4271 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4272 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4273 | found_type = btrfs_key_type(&found_key); |
| 4274 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4275 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 4276 | goto not_found; |
| 4277 | } |
| 4278 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4279 | found_type = btrfs_file_extent_type(leaf, item); |
| 4280 | extent_start = found_key.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4281 | compressed = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4282 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4283 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4284 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4285 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4286 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 4287 | size_t size; |
| 4288 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4289 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 4290 | ~((u64)root->sectorsize - 1); |
| 4291 | } |
| 4292 | |
| 4293 | if (start >= extent_end) { |
| 4294 | path->slots[0]++; |
| 4295 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 4296 | ret = btrfs_next_leaf(root, path); |
| 4297 | if (ret < 0) { |
| 4298 | err = ret; |
| 4299 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4300 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4301 | if (ret > 0) |
| 4302 | goto not_found; |
| 4303 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4304 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4305 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4306 | if (found_key.objectid != objectid || |
| 4307 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 4308 | goto not_found; |
| 4309 | if (start + len <= found_key.offset) |
| 4310 | goto not_found; |
| 4311 | em->start = start; |
| 4312 | em->len = found_key.offset - start; |
| 4313 | goto not_found_em; |
| 4314 | } |
| 4315 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4316 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 4317 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4318 | em->start = extent_start; |
| 4319 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4320 | em->orig_start = extent_start - |
| 4321 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4322 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 4323 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4324 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4325 | goto insert; |
| 4326 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4327 | if (compressed) { |
| 4328 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 4329 | em->block_start = bytenr; |
| 4330 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 4331 | item); |
| 4332 | } else { |
| 4333 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 4334 | em->block_start = bytenr; |
| 4335 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4336 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 4337 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4338 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4339 | goto insert; |
| 4340 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4341 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4342 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4343 | size_t size; |
| 4344 | size_t extent_offset; |
| 4345 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4346 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4347 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4348 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4349 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4350 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4351 | goto out; |
| 4352 | } |
| 4353 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4354 | size = btrfs_file_extent_inline_len(leaf, item); |
| 4355 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4356 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4357 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4358 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4359 | em->len = (copy_size + root->sectorsize - 1) & |
| 4360 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 4361 | em->orig_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4362 | if (compressed) |
| 4363 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 4364 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4365 | if (create == 0 && !PageUptodate(page)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4366 | if (btrfs_file_extent_compression(leaf, item) == |
| 4367 | BTRFS_COMPRESS_ZLIB) { |
| 4368 | ret = uncompress_inline(path, inode, page, |
| 4369 | pg_offset, |
| 4370 | extent_offset, item); |
| 4371 | BUG_ON(ret); |
| 4372 | } else { |
| 4373 | map = kmap(page); |
| 4374 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 4375 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 4376 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 4377 | memset(map + pg_offset + copy_size, 0, |
| 4378 | PAGE_CACHE_SIZE - pg_offset - |
| 4379 | copy_size); |
| 4380 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4381 | kunmap(page); |
| 4382 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4383 | flush_dcache_page(page); |
| 4384 | } else if (create && PageUptodate(page)) { |
| 4385 | if (!trans) { |
| 4386 | kunmap(page); |
| 4387 | free_extent_map(em); |
| 4388 | em = NULL; |
| 4389 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4390 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4391 | goto again; |
| 4392 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4393 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4394 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4395 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4396 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4397 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4398 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4399 | set_extent_uptodate(io_tree, em->start, |
| 4400 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4401 | goto insert; |
| 4402 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4403 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4404 | WARN_ON(1); |
| 4405 | } |
| 4406 | not_found: |
| 4407 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4408 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4409 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4410 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4411 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4412 | insert: |
| 4413 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4414 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4415 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 4416 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 4417 | (unsigned long long)em->len, |
| 4418 | (unsigned long long)start, |
| 4419 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4420 | err = -EIO; |
| 4421 | goto out; |
| 4422 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4423 | |
| 4424 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4425 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4426 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4427 | /* it is possible that someone inserted the extent into the tree |
| 4428 | * while we had the lock dropped. It is also possible that |
| 4429 | * an overlapping map exists in the tree |
| 4430 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4431 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4432 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4433 | |
| 4434 | ret = 0; |
| 4435 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4436 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4437 | if (existing && (existing->start > start || |
| 4438 | existing->start + existing->len <= start)) { |
| 4439 | free_extent_map(existing); |
| 4440 | existing = NULL; |
| 4441 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4442 | if (!existing) { |
| 4443 | existing = lookup_extent_mapping(em_tree, em->start, |
| 4444 | em->len); |
| 4445 | if (existing) { |
| 4446 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4447 | em, start, |
| 4448 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4449 | free_extent_map(existing); |
| 4450 | if (err) { |
| 4451 | free_extent_map(em); |
| 4452 | em = NULL; |
| 4453 | } |
| 4454 | } else { |
| 4455 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4456 | free_extent_map(em); |
| 4457 | em = NULL; |
| 4458 | } |
| 4459 | } else { |
| 4460 | free_extent_map(em); |
| 4461 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4462 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4463 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4464 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4465 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4466 | out: |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4467 | if (path) |
| 4468 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4469 | if (trans) { |
| 4470 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4471 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4472 | err = ret; |
| 4473 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4474 | if (err) { |
| 4475 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4476 | return ERR_PTR(err); |
| 4477 | } |
| 4478 | return em; |
| 4479 | } |
| 4480 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4481 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 4482 | const struct iovec *iov, loff_t offset, |
| 4483 | unsigned long nr_segs) |
| 4484 | { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4485 | return -EINVAL; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 4486 | } |
| 4487 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4488 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4489 | __u64 start, __u64 len) |
| 4490 | { |
| 4491 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent); |
| 4492 | } |
| 4493 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4494 | int btrfs_readpage(struct file *file, struct page *page) |
| 4495 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4496 | struct extent_io_tree *tree; |
| 4497 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4498 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4499 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4500 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4501 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 4502 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4503 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4504 | |
| 4505 | |
| 4506 | if (current->flags & PF_MEMALLOC) { |
| 4507 | redirty_page_for_writepage(wbc, page); |
| 4508 | unlock_page(page); |
| 4509 | return 0; |
| 4510 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4511 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4512 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 4513 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4514 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4515 | int btrfs_writepages(struct address_space *mapping, |
| 4516 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 4517 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4518 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4519 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4520 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 4521 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 4522 | } |
| 4523 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 4524 | static int |
| 4525 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 4526 | struct list_head *pages, unsigned nr_pages) |
| 4527 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4528 | struct extent_io_tree *tree; |
| 4529 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 4530 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 4531 | btrfs_get_extent); |
| 4532 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4533 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4534 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4535 | struct extent_io_tree *tree; |
| 4536 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4537 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4538 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4539 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 4540 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4541 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4542 | if (ret == 1) { |
| 4543 | ClearPagePrivate(page); |
| 4544 | set_page_private(page, 0); |
| 4545 | page_cache_release(page); |
| 4546 | } |
| 4547 | return ret; |
| 4548 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4549 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4550 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 4551 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 4552 | if (PageWriteback(page) || PageDirty(page)) |
| 4553 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 4554 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4555 | } |
| 4556 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4557 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 4558 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4559 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4560 | struct btrfs_ordered_extent *ordered; |
| 4561 | u64 page_start = page_offset(page); |
| 4562 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4563 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4564 | |
| 4565 | /* |
| 4566 | * we have the page locked, so new writeback can't start, |
| 4567 | * and the dirty bit won't be cleared while we are here. |
| 4568 | * |
| 4569 | * Wait for IO on this page so that we can safely clear |
| 4570 | * the PagePrivate2 bit and do ordered accounting |
| 4571 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4572 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4573 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4574 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4575 | if (offset) { |
| 4576 | btrfs_releasepage(page, GFP_NOFS); |
| 4577 | return; |
| 4578 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4579 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 4580 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 4581 | page_offset(page)); |
| 4582 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4583 | /* |
| 4584 | * IO on this page will never be started, so we need |
| 4585 | * to account for any ordered extents now |
| 4586 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4587 | clear_extent_bit(tree, page_start, page_end, |
| 4588 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 4589 | EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4590 | /* |
| 4591 | * whoever cleared the private bit is responsible |
| 4592 | * for the finish_ordered_io |
| 4593 | */ |
| 4594 | if (TestClearPagePrivate2(page)) { |
| 4595 | btrfs_finish_ordered_io(page->mapping->host, |
| 4596 | page_start, page_end); |
| 4597 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4598 | btrfs_put_ordered_extent(ordered); |
| 4599 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 4600 | } |
| 4601 | clear_extent_bit(tree, page_start, page_end, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4602 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 4603 | 1, 1, NULL, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4604 | __btrfs_releasepage(page, GFP_NOFS); |
| 4605 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 4606 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 4607 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 4608 | ClearPagePrivate(page); |
| 4609 | set_page_private(page, 0); |
| 4610 | page_cache_release(page); |
| 4611 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4612 | } |
| 4613 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4614 | /* |
| 4615 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 4616 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 4617 | * be careful to check for EOF conditions here. We set the page up correctly |
| 4618 | * for a written page which means we get ENOSPC checking when writing into |
| 4619 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 4620 | * support these features. |
| 4621 | * |
| 4622 | * We are not allowed to take the i_mutex here so we have to play games to |
| 4623 | * protect against truncate races as the page could now be beyond EOF. Because |
| 4624 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 4625 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 4626 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 4627 | * unlock the page. |
| 4628 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 4629 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4630 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 4631 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4632 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4633 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4634 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4635 | struct btrfs_ordered_extent *ordered; |
| 4636 | char *kaddr; |
| 4637 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4638 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4639 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4640 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4641 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4642 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4643 | ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 4644 | if (ret) { |
| 4645 | if (ret == -ENOMEM) |
| 4646 | ret = VM_FAULT_OOM; |
| 4647 | else /* -ENOSPC, -EIO, etc */ |
| 4648 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4649 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 4650 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4651 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 4652 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4653 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4654 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4655 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4656 | page_start = page_offset(page); |
| 4657 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4658 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4659 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4660 | (page_start >= size)) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4661 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4662 | /* page got truncated out from underneath us */ |
| 4663 | goto out_unlock; |
| 4664 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4665 | wait_on_page_writeback(page); |
| 4666 | |
| 4667 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 4668 | set_page_extent_mapped(page); |
| 4669 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4670 | /* |
| 4671 | * we can't set the delalloc bits if there are pending ordered |
| 4672 | * extents. Drop our locks and wait for them to finish |
| 4673 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4674 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4675 | if (ordered) { |
| 4676 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 4677 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4678 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4679 | btrfs_put_ordered_extent(ordered); |
| 4680 | goto again; |
| 4681 | } |
| 4682 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 4683 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4684 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4685 | |
| 4686 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4687 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4688 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4689 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4690 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4691 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4692 | if (zero_start != PAGE_CACHE_SIZE) { |
| 4693 | kaddr = kmap(page); |
| 4694 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 4695 | flush_dcache_page(page); |
| 4696 | kunmap(page); |
| 4697 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4698 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4699 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 4700 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4701 | |
| 4702 | BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4703 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4704 | |
| 4705 | out_unlock: |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 4706 | if (!ret) |
| 4707 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4708 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4709 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 4710 | return ret; |
| 4711 | } |
| 4712 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4713 | static void btrfs_truncate(struct inode *inode) |
| 4714 | { |
| 4715 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4716 | int ret; |
| 4717 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4718 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4719 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4720 | |
| 4721 | if (!S_ISREG(inode->i_mode)) |
| 4722 | return; |
| 4723 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 4724 | return; |
| 4725 | |
| 4726 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 4727 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4728 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4729 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4730 | |
| 4731 | /* |
| 4732 | * setattr is responsible for setting the ordered_data_close flag, |
| 4733 | * but that is only tested during the last file release. That |
| 4734 | * could happen well after the next commit, leaving a great big |
| 4735 | * window where new writes may get lost if someone chooses to write |
| 4736 | * to this file after truncating to zero |
| 4737 | * |
| 4738 | * The inode doesn't have any dirty data here, and so if we commit |
| 4739 | * this is a noop. If someone immediately starts writing to the inode |
| 4740 | * it is very likely we'll catch some of their writes in this |
| 4741 | * transaction, and the commit will find this file on the ordered |
| 4742 | * data list with good things to send down. |
| 4743 | * |
| 4744 | * This is a best effort solution, there is still a window where |
| 4745 | * using truncate to replace the contents of the file will |
| 4746 | * end up with a zero length file after a crash. |
| 4747 | */ |
| 4748 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 4749 | btrfs_add_ordered_operation(trans, root, inode); |
| 4750 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4751 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4752 | btrfs_i_size_write(inode, inode->i_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4753 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4754 | ret = btrfs_orphan_add(trans, inode); |
| 4755 | if (ret) |
| 4756 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4757 | /* 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] | 4758 | ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4759 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4760 | btrfs_update_inode(trans, root, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4761 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4762 | ret = btrfs_orphan_del(trans, inode); |
| 4763 | BUG_ON(ret); |
| 4764 | |
| 4765 | out: |
| 4766 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4767 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4768 | BUG_ON(ret); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4769 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4770 | } |
| 4771 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 4772 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4773 | * create a new subvolume directory/inode (helper for the ioctl). |
| 4774 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4775 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
| 4776 | struct btrfs_root *new_root, struct dentry *dentry, |
| 4777 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4778 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4779 | struct inode *inode; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 4780 | int error; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4781 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4782 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4783 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4784 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4785 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4786 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4787 | inode->i_op = &btrfs_dir_inode_operations; |
| 4788 | inode->i_fop = &btrfs_dir_file_operations; |
| 4789 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4790 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4791 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 4792 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 4793 | error = btrfs_update_inode(trans, new_root, inode); |
| 4794 | if (error) |
| 4795 | return error; |
| 4796 | |
| 4797 | d_instantiate(dentry, inode); |
| 4798 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4799 | } |
| 4800 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4801 | /* helper function for file defrag and space balancing. This |
| 4802 | * forces readahead on a given range of bytes in an inode |
| 4803 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4804 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4805 | struct file_ra_state *ra, struct file *file, |
| 4806 | pgoff_t offset, pgoff_t last_index) |
| 4807 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 4808 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4809 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4810 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 4811 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 4812 | } |
| 4813 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4814 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 4815 | { |
| 4816 | struct btrfs_inode *ei; |
| 4817 | |
| 4818 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 4819 | if (!ei) |
| 4820 | return NULL; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 4821 | ei->last_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4822 | ei->logged_trans = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4823 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4824 | INIT_LIST_HEAD(&ei->i_orphan); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4825 | INIT_LIST_HEAD(&ei->ordered_operations); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4826 | return &ei->vfs_inode; |
| 4827 | } |
| 4828 | |
| 4829 | void btrfs_destroy_inode(struct inode *inode) |
| 4830 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4831 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4832 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4833 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4834 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 4835 | WARN_ON(inode->i_data.nrpages); |
| 4836 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4837 | /* |
| 4838 | * Make sure we're properly removed from the ordered operation |
| 4839 | * lists. |
| 4840 | */ |
| 4841 | smp_mb(); |
| 4842 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 4843 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 4844 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 4845 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 4846 | } |
| 4847 | |
| 4848 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4849 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 4850 | printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan" |
| 4851 | " list\n", inode->i_ino); |
| 4852 | dump_stack(); |
| 4853 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4854 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4855 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4856 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4857 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 4858 | if (!ordered) |
| 4859 | break; |
| 4860 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4861 | printk(KERN_ERR "btrfs found ordered " |
| 4862 | "extent %llu %llu on inode cleanup\n", |
| 4863 | (unsigned long long)ordered->file_offset, |
| 4864 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4865 | btrfs_remove_ordered_extent(inode, ordered); |
| 4866 | btrfs_put_ordered_extent(ordered); |
| 4867 | btrfs_put_ordered_extent(ordered); |
| 4868 | } |
| 4869 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4870 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 4871 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4872 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 4873 | } |
| 4874 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 4875 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4876 | { |
| 4877 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 4878 | |
| 4879 | inode_init_once(&ei->vfs_inode); |
| 4880 | } |
| 4881 | |
| 4882 | void btrfs_destroy_cachep(void) |
| 4883 | { |
| 4884 | if (btrfs_inode_cachep) |
| 4885 | kmem_cache_destroy(btrfs_inode_cachep); |
| 4886 | if (btrfs_trans_handle_cachep) |
| 4887 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 4888 | if (btrfs_transaction_cachep) |
| 4889 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4890 | if (btrfs_path_cachep) |
| 4891 | kmem_cache_destroy(btrfs_path_cachep); |
| 4892 | } |
| 4893 | |
| 4894 | int btrfs_init_cachep(void) |
| 4895 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 4896 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 4897 | sizeof(struct btrfs_inode), 0, |
| 4898 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4899 | if (!btrfs_inode_cachep) |
| 4900 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 4901 | |
| 4902 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 4903 | sizeof(struct btrfs_trans_handle), 0, |
| 4904 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4905 | if (!btrfs_trans_handle_cachep) |
| 4906 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 4907 | |
| 4908 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 4909 | sizeof(struct btrfs_transaction), 0, |
| 4910 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4911 | if (!btrfs_transaction_cachep) |
| 4912 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 4913 | |
| 4914 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 4915 | sizeof(struct btrfs_path), 0, |
| 4916 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4917 | if (!btrfs_path_cachep) |
| 4918 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 4919 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4920 | return 0; |
| 4921 | fail: |
| 4922 | btrfs_destroy_cachep(); |
| 4923 | return -ENOMEM; |
| 4924 | } |
| 4925 | |
| 4926 | static int btrfs_getattr(struct vfsmount *mnt, |
| 4927 | struct dentry *dentry, struct kstat *stat) |
| 4928 | { |
| 4929 | struct inode *inode = dentry->d_inode; |
| 4930 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 4931 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 4932 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4933 | stat->blocks = (inode_get_bytes(inode) + |
| 4934 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4935 | return 0; |
| 4936 | } |
| 4937 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4938 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 4939 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4940 | { |
| 4941 | struct btrfs_trans_handle *trans; |
| 4942 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 4943 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4944 | struct inode *new_inode = new_dentry->d_inode; |
| 4945 | struct inode *old_inode = old_dentry->d_inode; |
| 4946 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4947 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 4948 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4949 | int ret; |
| 4950 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 4951 | /* we only allow rename subvolume link between subvolumes */ |
| 4952 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 4953 | return -EXDEV; |
| 4954 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 4955 | if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 4956 | (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) |
| 4957 | return -ENOTEMPTY; |
| 4958 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4959 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 4960 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4961 | return -ENOTEMPTY; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 4962 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4963 | ret = btrfs_check_metadata_free_space(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4964 | if (ret) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 4965 | return ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4966 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4967 | /* |
| 4968 | * we're using rename to replace one file with another. |
| 4969 | * and the replacement file is large. Start IO on it now so |
| 4970 | * we don't add too much work to the end of the transaction |
| 4971 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 4972 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size && |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4973 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 4974 | filemap_flush(old_inode->i_mapping); |
| 4975 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4976 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4977 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 4978 | if (dest != root) |
| 4979 | btrfs_record_root_in_trans(trans, dest); |
| 4980 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4981 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4982 | * make sure the inode gets flushed if it is replacing |
| 4983 | * something. |
| 4984 | */ |
| 4985 | if (new_inode && new_inode->i_size && |
| 4986 | old_inode && S_ISREG(old_inode->i_mode)) { |
| 4987 | btrfs_add_ordered_operation(trans, root, old_inode); |
| 4988 | } |
| 4989 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 4990 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 4991 | /* force full log commit if subvolume involved. */ |
| 4992 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 4993 | } else { |
| 4994 | /* |
| 4995 | * this is an ugly little race, but the rename is required |
| 4996 | * to make sure that if we crash, the inode is either at the |
| 4997 | * old name or the new one. pinning the log transaction lets |
| 4998 | * us make sure we don't allow a log commit to come in after |
| 4999 | * we unlink the name but before we add the new name back in. |
| 5000 | */ |
| 5001 | btrfs_pin_log_trans(root); |
| 5002 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5003 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5004 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5005 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5006 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 5007 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 5008 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5009 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5010 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 5011 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 5012 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 5013 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5014 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 5015 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 5016 | old_dentry->d_name.name, |
| 5017 | old_dentry->d_name.len); |
| 5018 | } else { |
| 5019 | btrfs_inc_nlink(old_dentry->d_inode); |
| 5020 | ret = btrfs_unlink_inode(trans, root, old_dir, |
| 5021 | old_dentry->d_inode, |
| 5022 | old_dentry->d_name.name, |
| 5023 | old_dentry->d_name.len); |
| 5024 | } |
| 5025 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5026 | |
| 5027 | if (new_inode) { |
| 5028 | new_inode->i_ctime = CURRENT_TIME; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 5029 | if (unlikely(new_inode->i_ino == |
| 5030 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 5031 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 5032 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 5033 | root_objectid, |
| 5034 | new_dentry->d_name.name, |
| 5035 | new_dentry->d_name.len); |
| 5036 | BUG_ON(new_inode->i_nlink == 0); |
| 5037 | } else { |
| 5038 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 5039 | new_dentry->d_inode, |
| 5040 | new_dentry->d_name.name, |
| 5041 | new_dentry->d_name.len); |
| 5042 | } |
| 5043 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5044 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5045 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 5046 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5047 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5048 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5049 | ret = btrfs_set_inode_index(new_dir, &index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 5050 | BUG_ON(ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5051 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 5052 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 5053 | new_dentry->d_name.name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5054 | new_dentry->d_name.len, 1, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 5055 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5056 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 5057 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 5058 | btrfs_log_new_name(trans, old_inode, old_dir, |
| 5059 | new_dentry->d_parent); |
| 5060 | btrfs_end_log_trans(root); |
| 5061 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5062 | |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 5063 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame^] | 5064 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5065 | return ret; |
| 5066 | } |
| 5067 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5068 | /* |
| 5069 | * some fairly slow code that needs optimization. This walks the list |
| 5070 | * of all the inodes with pending delalloc and forces them to disk. |
| 5071 | */ |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5072 | int btrfs_start_delalloc_inodes(struct btrfs_root *root) |
| 5073 | { |
| 5074 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 5075 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5076 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5077 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5078 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 5079 | return -EROFS; |
| 5080 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5081 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5082 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5083 | binode = list_entry(head->next, struct btrfs_inode, |
| 5084 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5085 | inode = igrab(&binode->vfs_inode); |
| 5086 | if (!inode) |
| 5087 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5088 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5089 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5090 | filemap_flush(inode->i_mapping); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 5091 | iput(inode); |
| 5092 | } |
| 5093 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5094 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5095 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 5096 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5097 | |
| 5098 | /* the filemap_flush will queue IO into the worker threads, but |
| 5099 | * we have to make sure the IO is actually started and that |
| 5100 | * ordered extents get created before we return |
| 5101 | */ |
| 5102 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5103 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5104 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5105 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5106 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 5107 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 5108 | } |
| 5109 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5110 | return 0; |
| 5111 | } |
| 5112 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5113 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 5114 | const char *symname) |
| 5115 | { |
| 5116 | struct btrfs_trans_handle *trans; |
| 5117 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5118 | struct btrfs_path *path; |
| 5119 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5120 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5121 | int err; |
| 5122 | int drop_inode = 0; |
| 5123 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5124 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5125 | int name_len; |
| 5126 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5127 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5128 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5129 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5130 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5131 | |
| 5132 | name_len = strlen(symname) + 1; |
| 5133 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 5134 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5135 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5136 | err = btrfs_check_metadata_free_space(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5137 | if (err) |
| 5138 | goto out_fail; |
| 5139 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5140 | trans = btrfs_start_transaction(root, 1); |
| 5141 | btrfs_set_trans_block_group(trans, dir); |
| 5142 | |
| 5143 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 5144 | if (err) { |
| 5145 | err = -ENOSPC; |
| 5146 | goto out_unlock; |
| 5147 | } |
| 5148 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5149 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5150 | dentry->d_name.len, |
| 5151 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5152 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 5153 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5154 | err = PTR_ERR(inode); |
| 5155 | if (IS_ERR(inode)) |
| 5156 | goto out_unlock; |
| 5157 | |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 5158 | err = btrfs_init_inode_security(inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5159 | if (err) { |
| 5160 | drop_inode = 1; |
| 5161 | goto out_unlock; |
| 5162 | } |
| 5163 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5164 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5165 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5166 | if (err) |
| 5167 | drop_inode = 1; |
| 5168 | else { |
| 5169 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 5170 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5171 | inode->i_fop = &btrfs_file_operations; |
| 5172 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5173 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5174 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5175 | btrfs_update_inode_block_group(trans, inode); |
| 5176 | btrfs_update_inode_block_group(trans, dir); |
| 5177 | if (drop_inode) |
| 5178 | goto out_unlock; |
| 5179 | |
| 5180 | path = btrfs_alloc_path(); |
| 5181 | BUG_ON(!path); |
| 5182 | key.objectid = inode->i_ino; |
| 5183 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5184 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 5185 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 5186 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 5187 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5188 | if (err) { |
| 5189 | drop_inode = 1; |
| 5190 | goto out_unlock; |
| 5191 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5192 | leaf = path->nodes[0]; |
| 5193 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 5194 | struct btrfs_file_extent_item); |
| 5195 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 5196 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5197 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5198 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 5199 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 5200 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 5201 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 5202 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5203 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5204 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 5205 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5206 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5207 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5208 | inode->i_op = &btrfs_symlink_inode_operations; |
| 5209 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 5210 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5211 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5212 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5213 | err = btrfs_update_inode(trans, root, inode); |
| 5214 | if (err) |
| 5215 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5216 | |
| 5217 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5218 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 5219 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5220 | out_fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5221 | if (drop_inode) { |
| 5222 | inode_dec_link_count(inode); |
| 5223 | iput(inode); |
| 5224 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5225 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5226 | return err; |
| 5227 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 5228 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5229 | static int prealloc_file_range(struct btrfs_trans_handle *trans, |
| 5230 | struct inode *inode, u64 start, u64 end, |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5231 | u64 locked_end, u64 alloc_hint, int mode) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5232 | { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5233 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5234 | struct btrfs_key ins; |
| 5235 | u64 alloc_size; |
| 5236 | u64 cur_offset = start; |
| 5237 | u64 num_bytes = end - start; |
| 5238 | int ret = 0; |
| 5239 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5240 | while (num_bytes > 0) { |
| 5241 | alloc_size = min(num_bytes, root->fs_info->max_extent); |
| 5242 | ret = btrfs_reserve_extent(trans, root, alloc_size, |
| 5243 | root->sectorsize, 0, alloc_hint, |
| 5244 | (u64)-1, &ins, 1); |
| 5245 | if (ret) { |
| 5246 | WARN_ON(1); |
| 5247 | goto out; |
| 5248 | } |
| 5249 | ret = insert_reserved_file_extent(trans, inode, |
| 5250 | cur_offset, ins.objectid, |
| 5251 | ins.offset, ins.offset, |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5252 | ins.offset, locked_end, |
| 5253 | 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5254 | BTRFS_FILE_EXTENT_PREALLOC); |
| 5255 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 5256 | btrfs_drop_extent_cache(inode, cur_offset, |
| 5257 | cur_offset + ins.offset -1, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5258 | num_bytes -= ins.offset; |
| 5259 | cur_offset += ins.offset; |
| 5260 | alloc_hint = ins.objectid + ins.offset; |
| 5261 | } |
| 5262 | out: |
| 5263 | if (cur_offset > start) { |
| 5264 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5265 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5266 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
| 5267 | cur_offset > i_size_read(inode)) |
| 5268 | btrfs_i_size_write(inode, cur_offset); |
| 5269 | ret = btrfs_update_inode(trans, root, inode); |
| 5270 | BUG_ON(ret); |
| 5271 | } |
| 5272 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5273 | return ret; |
| 5274 | } |
| 5275 | |
| 5276 | static long btrfs_fallocate(struct inode *inode, int mode, |
| 5277 | loff_t offset, loff_t len) |
| 5278 | { |
| 5279 | u64 cur_offset; |
| 5280 | u64 last_byte; |
| 5281 | u64 alloc_start; |
| 5282 | u64 alloc_end; |
| 5283 | u64 alloc_hint = 0; |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5284 | u64 locked_end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5285 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 5286 | struct extent_map *em; |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5287 | struct btrfs_trans_handle *trans; |
Josef Bacik | a970b0a | 2009-06-27 21:07:34 -0400 | [diff] [blame] | 5288 | struct btrfs_root *root; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5289 | int ret; |
| 5290 | |
| 5291 | alloc_start = offset & ~mask; |
| 5292 | alloc_end = (offset + len + mask) & ~mask; |
| 5293 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5294 | /* |
| 5295 | * wait for ordered IO before we have any locks. We'll loop again |
| 5296 | * below with the locks held. |
| 5297 | */ |
| 5298 | btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start); |
| 5299 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5300 | mutex_lock(&inode->i_mutex); |
| 5301 | if (alloc_start > inode->i_size) { |
| 5302 | ret = btrfs_cont_expand(inode, alloc_start); |
| 5303 | if (ret) |
| 5304 | goto out; |
| 5305 | } |
| 5306 | |
Josef Bacik | a970b0a | 2009-06-27 21:07:34 -0400 | [diff] [blame] | 5307 | root = BTRFS_I(inode)->root; |
| 5308 | |
| 5309 | ret = btrfs_check_data_free_space(root, inode, |
| 5310 | alloc_end - alloc_start); |
| 5311 | if (ret) |
| 5312 | goto out; |
| 5313 | |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5314 | locked_end = alloc_end - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5315 | while (1) { |
| 5316 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5317 | |
| 5318 | trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1); |
| 5319 | if (!trans) { |
| 5320 | ret = -EIO; |
Josef Bacik | a970b0a | 2009-06-27 21:07:34 -0400 | [diff] [blame] | 5321 | goto out_free; |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5322 | } |
| 5323 | |
| 5324 | /* the extent lock is ordered inside the running |
| 5325 | * transaction |
| 5326 | */ |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5327 | lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
| 5328 | GFP_NOFS); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5329 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 5330 | alloc_end - 1); |
| 5331 | if (ordered && |
| 5332 | ordered->file_offset + ordered->len > alloc_start && |
| 5333 | ordered->file_offset < alloc_end) { |
| 5334 | btrfs_put_ordered_extent(ordered); |
| 5335 | unlock_extent(&BTRFS_I(inode)->io_tree, |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5336 | alloc_start, locked_end, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5337 | btrfs_end_transaction(trans, BTRFS_I(inode)->root); |
| 5338 | |
| 5339 | /* |
| 5340 | * we can't wait on the range with the transaction |
| 5341 | * running or with the extent lock held |
| 5342 | */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5343 | btrfs_wait_ordered_range(inode, alloc_start, |
| 5344 | alloc_end - alloc_start); |
| 5345 | } else { |
| 5346 | if (ordered) |
| 5347 | btrfs_put_ordered_extent(ordered); |
| 5348 | break; |
| 5349 | } |
| 5350 | } |
| 5351 | |
| 5352 | cur_offset = alloc_start; |
| 5353 | while (1) { |
| 5354 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 5355 | alloc_end - cur_offset, 0); |
| 5356 | BUG_ON(IS_ERR(em) || !em); |
| 5357 | last_byte = min(extent_map_end(em), alloc_end); |
| 5358 | last_byte = (last_byte + mask) & ~mask; |
| 5359 | if (em->block_start == EXTENT_MAP_HOLE) { |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5360 | ret = prealloc_file_range(trans, inode, cur_offset, |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5361 | last_byte, locked_end + 1, |
| 5362 | alloc_hint, mode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5363 | if (ret < 0) { |
| 5364 | free_extent_map(em); |
| 5365 | break; |
| 5366 | } |
| 5367 | } |
| 5368 | if (em->block_start <= EXTENT_MAP_LAST_BYTE) |
| 5369 | alloc_hint = em->block_start; |
| 5370 | free_extent_map(em); |
| 5371 | |
| 5372 | cur_offset = last_byte; |
| 5373 | if (cur_offset >= alloc_end) { |
| 5374 | ret = 0; |
| 5375 | break; |
| 5376 | } |
| 5377 | } |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 5378 | unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5379 | GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 5380 | |
| 5381 | btrfs_end_transaction(trans, BTRFS_I(inode)->root); |
Josef Bacik | a970b0a | 2009-06-27 21:07:34 -0400 | [diff] [blame] | 5382 | out_free: |
| 5383 | btrfs_free_reserved_data_space(root, inode, alloc_end - alloc_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5384 | out: |
| 5385 | mutex_unlock(&inode->i_mutex); |
| 5386 | return ret; |
| 5387 | } |
| 5388 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5389 | static int btrfs_set_page_dirty(struct page *page) |
| 5390 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5391 | return __set_page_dirty_nobuffers(page); |
| 5392 | } |
| 5393 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5394 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5395 | { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5396 | if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5397 | return -EACCES; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5398 | return generic_permission(inode, mask, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5399 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5400 | |
| 5401 | static struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 5402 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5403 | .lookup = btrfs_lookup, |
| 5404 | .create = btrfs_create, |
| 5405 | .unlink = btrfs_unlink, |
| 5406 | .link = btrfs_link, |
| 5407 | .mkdir = btrfs_mkdir, |
| 5408 | .rmdir = btrfs_rmdir, |
| 5409 | .rename = btrfs_rename, |
| 5410 | .symlink = btrfs_symlink, |
| 5411 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5412 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5413 | .setxattr = btrfs_setxattr, |
| 5414 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5415 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5416 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5417 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5418 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5419 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
| 5420 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5421 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5422 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5423 | static struct file_operations btrfs_dir_file_operations = { |
| 5424 | .llseek = generic_file_llseek, |
| 5425 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 5426 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 5427 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5428 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 5429 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5430 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 5431 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5432 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5433 | }; |
| 5434 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5435 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5436 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 5437 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5438 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5439 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5440 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 5441 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5442 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 5443 | .set_bit_hook = btrfs_set_bit_hook, |
| 5444 | .clear_bit_hook = btrfs_clear_bit_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 5445 | }; |
| 5446 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 5447 | /* |
| 5448 | * btrfs doesn't support the bmap operation because swapfiles |
| 5449 | * use bmap to make a mapping of extents in the file. They assume |
| 5450 | * these extents won't change over the life of the file and they |
| 5451 | * use the bmap result to do IO directly to the drive. |
| 5452 | * |
| 5453 | * the btrfs bmap call would return logical addresses that aren't |
| 5454 | * suitable for IO and they also will change frequently as COW |
| 5455 | * operations happen. So, swapfile + btrfs == corruption. |
| 5456 | * |
| 5457 | * For now we're avoiding this by dropping bmap. |
| 5458 | */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5459 | static struct address_space_operations btrfs_aops = { |
| 5460 | .readpage = btrfs_readpage, |
| 5461 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 5462 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 5463 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5464 | .sync_page = block_sync_page, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 5465 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5466 | .invalidatepage = btrfs_invalidatepage, |
| 5467 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5468 | .set_page_dirty = btrfs_set_page_dirty, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5469 | }; |
| 5470 | |
| 5471 | static struct address_space_operations btrfs_symlink_aops = { |
| 5472 | .readpage = btrfs_readpage, |
| 5473 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 5474 | .invalidatepage = btrfs_invalidatepage, |
| 5475 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5476 | }; |
| 5477 | |
| 5478 | static struct inode_operations btrfs_file_inode_operations = { |
| 5479 | .truncate = btrfs_truncate, |
| 5480 | .getattr = btrfs_getattr, |
| 5481 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5482 | .setxattr = btrfs_setxattr, |
| 5483 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5484 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5485 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5486 | .permission = btrfs_permission, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5487 | .fallocate = btrfs_fallocate, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5488 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5489 | }; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5490 | static struct inode_operations btrfs_special_inode_operations = { |
| 5491 | .getattr = btrfs_getattr, |
| 5492 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5493 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5494 | .setxattr = btrfs_setxattr, |
| 5495 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5496 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 5497 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5498 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5499 | static struct inode_operations btrfs_symlink_inode_operations = { |
| 5500 | .readlink = generic_readlink, |
| 5501 | .follow_link = page_follow_link_light, |
| 5502 | .put_link = page_put_link, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 5503 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 5504 | .setxattr = btrfs_setxattr, |
| 5505 | .getxattr = btrfs_getxattr, |
| 5506 | .listxattr = btrfs_listxattr, |
| 5507 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5508 | }; |