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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 40 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 41 | #include "ctree.h" |
| 42 | #include "disk-io.h" |
| 43 | #include "transaction.h" |
| 44 | #include "btrfs_inode.h" |
| 45 | #include "ioctl.h" |
| 46 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 47 | #include "volumes.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 48 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 49 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 50 | #include "tree-log.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 51 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 52 | #include "locking.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 53 | |
| 54 | struct btrfs_iget_args { |
| 55 | u64 ino; |
| 56 | struct btrfs_root *root; |
| 57 | }; |
| 58 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 59 | static const struct inode_operations btrfs_dir_inode_operations; |
| 60 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 61 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 62 | static const struct inode_operations btrfs_special_inode_operations; |
| 63 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 64 | static const struct address_space_operations btrfs_aops; |
| 65 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 66 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 67 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 68 | |
| 69 | static struct kmem_cache *btrfs_inode_cachep; |
| 70 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 71 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 72 | struct kmem_cache *btrfs_path_cachep; |
| 73 | |
| 74 | #define S_SHIFT 12 |
| 75 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 76 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 77 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 78 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 79 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 80 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 81 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 82 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 83 | }; |
| 84 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 85 | static void btrfs_truncate(struct inode *inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 86 | 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] | 87 | static noinline int cow_file_range(struct inode *inode, |
| 88 | struct page *locked_page, |
| 89 | u64 start, u64 end, int *page_started, |
| 90 | unsigned long *nr_written, int unlock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 91 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 92 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
| 93 | struct inode *inode, struct inode *dir) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 94 | { |
| 95 | int err; |
| 96 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 97 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 98 | if (!err) |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 99 | err = btrfs_xattr_security_init(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 100 | return err; |
| 101 | } |
| 102 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 103 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 104 | * this does all the hard work for inserting an inline extent into |
| 105 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 106 | * no overlapping inline items exist in the btree |
| 107 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 108 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 109 | struct btrfs_root *root, struct inode *inode, |
| 110 | u64 start, size_t size, size_t compressed_size, |
| 111 | struct page **compressed_pages) |
| 112 | { |
| 113 | struct btrfs_key key; |
| 114 | struct btrfs_path *path; |
| 115 | struct extent_buffer *leaf; |
| 116 | struct page *page = NULL; |
| 117 | char *kaddr; |
| 118 | unsigned long ptr; |
| 119 | struct btrfs_file_extent_item *ei; |
| 120 | int err = 0; |
| 121 | int ret; |
| 122 | size_t cur_size = size; |
| 123 | size_t datasize; |
| 124 | unsigned long offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 125 | int compress_type = BTRFS_COMPRESS_NONE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 126 | |
| 127 | if (compressed_size && compressed_pages) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 128 | compress_type = root->fs_info->compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 129 | cur_size = compressed_size; |
| 130 | } |
| 131 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 132 | path = btrfs_alloc_path(); |
| 133 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 134 | return -ENOMEM; |
| 135 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 136 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 137 | btrfs_set_trans_block_group(trans, inode); |
| 138 | |
| 139 | key.objectid = inode->i_ino; |
| 140 | key.offset = start; |
| 141 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 142 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 143 | |
| 144 | inode_add_bytes(inode, size); |
| 145 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 146 | datasize); |
| 147 | BUG_ON(ret); |
| 148 | if (ret) { |
| 149 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 150 | goto fail; |
| 151 | } |
| 152 | leaf = path->nodes[0]; |
| 153 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 154 | struct btrfs_file_extent_item); |
| 155 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 156 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 157 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 158 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 159 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 160 | ptr = btrfs_file_extent_inline_start(ei); |
| 161 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 162 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 163 | struct page *cpage; |
| 164 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 165 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 167 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 168 | PAGE_CACHE_SIZE); |
| 169 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 170 | kaddr = kmap_atomic(cpage, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 172 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 173 | |
| 174 | i++; |
| 175 | ptr += cur_size; |
| 176 | compressed_size -= cur_size; |
| 177 | } |
| 178 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 179 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 180 | } else { |
| 181 | page = find_get_page(inode->i_mapping, |
| 182 | start >> PAGE_CACHE_SHIFT); |
| 183 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 184 | kaddr = kmap_atomic(page, KM_USER0); |
| 185 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 186 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
| 187 | kunmap_atomic(kaddr, KM_USER0); |
| 188 | page_cache_release(page); |
| 189 | } |
| 190 | btrfs_mark_buffer_dirty(leaf); |
| 191 | btrfs_free_path(path); |
| 192 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 193 | /* |
| 194 | * we're an inline extent, so nobody can |
| 195 | * extend the file past i_size without locking |
| 196 | * a page we already have locked. |
| 197 | * |
| 198 | * We must do any isize and inode updates |
| 199 | * before we unlock the pages. Otherwise we |
| 200 | * could end up racing with unlink. |
| 201 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 202 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
| 203 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 204 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 205 | return 0; |
| 206 | fail: |
| 207 | btrfs_free_path(path); |
| 208 | return err; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /* |
| 213 | * conditionally insert an inline extent into the file. This |
| 214 | * does the checks required to make sure the data is small enough |
| 215 | * to fit as an inline extent. |
| 216 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 217 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 218 | struct btrfs_root *root, |
| 219 | struct inode *inode, u64 start, u64 end, |
| 220 | size_t compressed_size, |
| 221 | struct page **compressed_pages) |
| 222 | { |
| 223 | u64 isize = i_size_read(inode); |
| 224 | u64 actual_end = min(end + 1, isize); |
| 225 | u64 inline_len = actual_end - start; |
| 226 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 227 | ~((u64)root->sectorsize - 1); |
| 228 | u64 hint_byte; |
| 229 | u64 data_len = inline_len; |
| 230 | int ret; |
| 231 | |
| 232 | if (compressed_size) |
| 233 | data_len = compressed_size; |
| 234 | |
| 235 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 236 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 238 | (!compressed_size && |
| 239 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 240 | end + 1 < isize || |
| 241 | data_len > root->fs_info->max_inline) { |
| 242 | return 1; |
| 243 | } |
| 244 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 245 | ret = btrfs_drop_extents(trans, inode, start, aligned_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 246 | &hint_byte, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 247 | BUG_ON(ret); |
| 248 | |
| 249 | if (isize > actual_end) |
| 250 | inline_len = min_t(u64, isize, actual_end); |
| 251 | ret = insert_inline_extent(trans, root, inode, start, |
| 252 | inline_len, compressed_size, |
| 253 | compressed_pages); |
| 254 | BUG_ON(ret); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 255 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 256 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 260 | struct async_extent { |
| 261 | u64 start; |
| 262 | u64 ram_size; |
| 263 | u64 compressed_size; |
| 264 | struct page **pages; |
| 265 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 266 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 267 | struct list_head list; |
| 268 | }; |
| 269 | |
| 270 | struct async_cow { |
| 271 | struct inode *inode; |
| 272 | struct btrfs_root *root; |
| 273 | struct page *locked_page; |
| 274 | u64 start; |
| 275 | u64 end; |
| 276 | struct list_head extents; |
| 277 | struct btrfs_work work; |
| 278 | }; |
| 279 | |
| 280 | static noinline int add_async_extent(struct async_cow *cow, |
| 281 | u64 start, u64 ram_size, |
| 282 | u64 compressed_size, |
| 283 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 284 | unsigned long nr_pages, |
| 285 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 286 | { |
| 287 | struct async_extent *async_extent; |
| 288 | |
| 289 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
| 290 | async_extent->start = start; |
| 291 | async_extent->ram_size = ram_size; |
| 292 | async_extent->compressed_size = compressed_size; |
| 293 | async_extent->pages = pages; |
| 294 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 295 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 296 | list_add_tail(&async_extent->list, &cow->extents); |
| 297 | return 0; |
| 298 | } |
| 299 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 300 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 301 | * we create compressed extents in two phases. The first |
| 302 | * phase compresses a range of pages that have already been |
| 303 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 304 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 305 | * This is done inside an ordered work queue, and the compression |
| 306 | * is spread across many cpus. The actual IO submission is step |
| 307 | * two, and the ordered work queue takes care of making sure that |
| 308 | * happens in the same order things were put onto the queue by |
| 309 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 310 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 311 | * If this code finds it can't get good compression, it puts an |
| 312 | * entry onto the work queue to write the uncompressed bytes. This |
| 313 | * makes sure that both compressed inodes and uncompressed inodes |
| 314 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 315 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 316 | static noinline int compress_file_range(struct inode *inode, |
| 317 | struct page *locked_page, |
| 318 | u64 start, u64 end, |
| 319 | struct async_cow *async_cow, |
| 320 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 321 | { |
| 322 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 323 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 324 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 325 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 326 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 327 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 328 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 329 | struct page **pages = NULL; |
| 330 | unsigned long nr_pages; |
| 331 | unsigned long nr_pages_ret = 0; |
| 332 | unsigned long total_compressed = 0; |
| 333 | unsigned long total_in = 0; |
| 334 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 335 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 336 | int i; |
| 337 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 338 | int compress_type = root->fs_info->compress_type; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 339 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 340 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 341 | again: |
| 342 | will_compress = 0; |
| 343 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 344 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 345 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 346 | /* |
| 347 | * we don't want to send crud past the end of i_size through |
| 348 | * compression, that's just a waste of CPU time. So, if the |
| 349 | * end of the file is before the start of our current |
| 350 | * requested range of bytes, we bail out to the uncompressed |
| 351 | * cleanup code that can deal with all of this. |
| 352 | * |
| 353 | * It isn't really the fastest way to fix things, but this is a |
| 354 | * very uncommon corner. |
| 355 | */ |
| 356 | if (actual_end <= start) |
| 357 | goto cleanup_and_bail_uncompressed; |
| 358 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 359 | total_compressed = actual_end - start; |
| 360 | |
| 361 | /* we want to make sure that amount of ram required to uncompress |
| 362 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 363 | * of a compressed extent to 128k. This is a crucial number |
| 364 | * because it also controls how easily we can spread reads across |
| 365 | * cpus for decompression. |
| 366 | * |
| 367 | * We also want to make sure the amount of IO required to do |
| 368 | * a random read is reasonably small, so we limit the size of |
| 369 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 370 | */ |
| 371 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 372 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 373 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 374 | total_in = 0; |
| 375 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 376 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 377 | /* |
| 378 | * we do compression for mount -o compress and when the |
| 379 | * inode has not been flagged as nocompress. This flag can |
| 380 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 381 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 382 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 383 | (btrfs_test_opt(root, COMPRESS) || |
| 384 | (BTRFS_I(inode)->force_compress))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 385 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 386 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 387 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 388 | if (BTRFS_I(inode)->force_compress) |
| 389 | compress_type = BTRFS_I(inode)->force_compress; |
| 390 | |
| 391 | ret = btrfs_compress_pages(compress_type, |
| 392 | inode->i_mapping, start, |
| 393 | total_compressed, pages, |
| 394 | nr_pages, &nr_pages_ret, |
| 395 | &total_in, |
| 396 | &total_compressed, |
| 397 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 398 | |
| 399 | if (!ret) { |
| 400 | unsigned long offset = total_compressed & |
| 401 | (PAGE_CACHE_SIZE - 1); |
| 402 | struct page *page = pages[nr_pages_ret - 1]; |
| 403 | char *kaddr; |
| 404 | |
| 405 | /* zero the tail end of the last page, we might be |
| 406 | * sending it down to disk |
| 407 | */ |
| 408 | if (offset) { |
| 409 | kaddr = kmap_atomic(page, KM_USER0); |
| 410 | memset(kaddr + offset, 0, |
| 411 | PAGE_CACHE_SIZE - offset); |
| 412 | kunmap_atomic(kaddr, KM_USER0); |
| 413 | } |
| 414 | will_compress = 1; |
| 415 | } |
| 416 | } |
| 417 | if (start == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 418 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 419 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 420 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 421 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 422 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 423 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 424 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 425 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 426 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 427 | */ |
| 428 | ret = cow_file_range_inline(trans, root, inode, |
| 429 | start, end, 0, NULL); |
| 430 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 431 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 432 | ret = cow_file_range_inline(trans, root, inode, |
| 433 | start, end, |
| 434 | total_compressed, pages); |
| 435 | } |
| 436 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 437 | /* |
| 438 | * inline extent creation worked, we don't need |
| 439 | * to create any more async work items. Unlock |
| 440 | * and free up our temp pages. |
| 441 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 442 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 443 | &BTRFS_I(inode)->io_tree, |
| 444 | start, end, NULL, |
| 445 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 446 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 447 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 448 | |
| 449 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 450 | goto free_pages_out; |
| 451 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 452 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | if (will_compress) { |
| 456 | /* |
| 457 | * we aren't doing an inline extent round the compressed size |
| 458 | * up to a block size boundary so the allocator does sane |
| 459 | * things |
| 460 | */ |
| 461 | total_compressed = (total_compressed + blocksize - 1) & |
| 462 | ~(blocksize - 1); |
| 463 | |
| 464 | /* |
| 465 | * one last check to make sure the compression is really a |
| 466 | * win, compare the page count read with the blocks on disk |
| 467 | */ |
| 468 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 469 | ~(PAGE_CACHE_SIZE - 1); |
| 470 | if (total_compressed >= total_in) { |
| 471 | will_compress = 0; |
| 472 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 473 | num_bytes = total_in; |
| 474 | } |
| 475 | } |
| 476 | if (!will_compress && pages) { |
| 477 | /* |
| 478 | * the compression code ran but failed to make things smaller, |
| 479 | * free any pages it allocated and our page pointer array |
| 480 | */ |
| 481 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 482 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 483 | page_cache_release(pages[i]); |
| 484 | } |
| 485 | kfree(pages); |
| 486 | pages = NULL; |
| 487 | total_compressed = 0; |
| 488 | nr_pages_ret = 0; |
| 489 | |
| 490 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 491 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 492 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 493 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 494 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 495 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 496 | if (will_compress) { |
| 497 | *num_added += 1; |
| 498 | |
| 499 | /* the async work queues will take care of doing actual |
| 500 | * allocation on disk for these compressed pages, |
| 501 | * and will submit them to the elevator. |
| 502 | */ |
| 503 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 504 | total_compressed, pages, nr_pages_ret, |
| 505 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 506 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 507 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 508 | start += num_bytes; |
| 509 | pages = NULL; |
| 510 | cond_resched(); |
| 511 | goto again; |
| 512 | } |
| 513 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 514 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 515 | /* |
| 516 | * No compression, but we still need to write the pages in |
| 517 | * the file we've been given so far. redirty the locked |
| 518 | * page if it corresponds to our extent and set things up |
| 519 | * for the async work queue to run cow_file_range to do |
| 520 | * the normal delalloc dance |
| 521 | */ |
| 522 | if (page_offset(locked_page) >= start && |
| 523 | page_offset(locked_page) <= end) { |
| 524 | __set_page_dirty_nobuffers(locked_page); |
| 525 | /* unlocked later on in the async handlers */ |
| 526 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 527 | add_async_extent(async_cow, start, end - start + 1, |
| 528 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 529 | *num_added += 1; |
| 530 | } |
| 531 | |
| 532 | out: |
| 533 | return 0; |
| 534 | |
| 535 | free_pages_out: |
| 536 | for (i = 0; i < nr_pages_ret; i++) { |
| 537 | WARN_ON(pages[i]->mapping); |
| 538 | page_cache_release(pages[i]); |
| 539 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 540 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 541 | |
| 542 | goto out; |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * phase two of compressed writeback. This is the ordered portion |
| 547 | * of the code, which only gets called in the order the work was |
| 548 | * queued. We walk all the async extents created by compress_file_range |
| 549 | * and send them down to the disk. |
| 550 | */ |
| 551 | static noinline int submit_compressed_extents(struct inode *inode, |
| 552 | struct async_cow *async_cow) |
| 553 | { |
| 554 | struct async_extent *async_extent; |
| 555 | u64 alloc_hint = 0; |
| 556 | struct btrfs_trans_handle *trans; |
| 557 | struct btrfs_key ins; |
| 558 | struct extent_map *em; |
| 559 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 560 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 561 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 562 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 563 | |
| 564 | if (list_empty(&async_cow->extents)) |
| 565 | return 0; |
| 566 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 567 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 568 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 569 | async_extent = list_entry(async_cow->extents.next, |
| 570 | struct async_extent, list); |
| 571 | list_del(&async_extent->list); |
| 572 | |
| 573 | io_tree = &BTRFS_I(inode)->io_tree; |
| 574 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 575 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 576 | /* did the compression code fall back to uncompressed IO? */ |
| 577 | if (!async_extent->pages) { |
| 578 | int page_started = 0; |
| 579 | unsigned long nr_written = 0; |
| 580 | |
| 581 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 582 | async_extent->start + |
| 583 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 584 | |
| 585 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 586 | ret = cow_file_range(inode, async_cow->locked_page, |
| 587 | async_extent->start, |
| 588 | async_extent->start + |
| 589 | async_extent->ram_size - 1, |
| 590 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 591 | |
| 592 | /* |
| 593 | * if page_started, cow_file_range inserted an |
| 594 | * inline extent and took care of all the unlocking |
| 595 | * and IO for us. Otherwise, we need to submit |
| 596 | * all those pages down to the drive. |
| 597 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 598 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 599 | extent_write_locked_range(io_tree, |
| 600 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 601 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 602 | async_extent->ram_size - 1, |
| 603 | btrfs_get_extent, |
| 604 | WB_SYNC_ALL); |
| 605 | kfree(async_extent); |
| 606 | cond_resched(); |
| 607 | continue; |
| 608 | } |
| 609 | |
| 610 | lock_extent(io_tree, async_extent->start, |
| 611 | async_extent->start + async_extent->ram_size - 1, |
| 612 | GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 613 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 614 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 615 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 616 | ret = btrfs_reserve_extent(trans, root, |
| 617 | async_extent->compressed_size, |
| 618 | async_extent->compressed_size, |
| 619 | 0, alloc_hint, |
| 620 | (u64)-1, &ins, 1); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 621 | btrfs_end_transaction(trans, root); |
| 622 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 623 | if (ret) { |
| 624 | int i; |
| 625 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 626 | WARN_ON(async_extent->pages[i]->mapping); |
| 627 | page_cache_release(async_extent->pages[i]); |
| 628 | } |
| 629 | kfree(async_extent->pages); |
| 630 | async_extent->nr_pages = 0; |
| 631 | async_extent->pages = NULL; |
| 632 | unlock_extent(io_tree, async_extent->start, |
| 633 | async_extent->start + |
| 634 | async_extent->ram_size - 1, GFP_NOFS); |
| 635 | goto retry; |
| 636 | } |
| 637 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 638 | /* |
| 639 | * here we're doing allocation and writeback of the |
| 640 | * compressed pages |
| 641 | */ |
| 642 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 643 | async_extent->start + |
| 644 | async_extent->ram_size - 1, 0); |
| 645 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 646 | em = alloc_extent_map(GFP_NOFS); |
| 647 | em->start = async_extent->start; |
| 648 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 649 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 650 | |
| 651 | em->block_start = ins.objectid; |
| 652 | em->block_len = ins.offset; |
| 653 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 654 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 655 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 656 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 657 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 658 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 659 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 660 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 661 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 662 | if (ret != -EEXIST) { |
| 663 | free_extent_map(em); |
| 664 | break; |
| 665 | } |
| 666 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 667 | async_extent->start + |
| 668 | async_extent->ram_size - 1, 0); |
| 669 | } |
| 670 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 671 | ret = btrfs_add_ordered_extent_compress(inode, |
| 672 | async_extent->start, |
| 673 | ins.objectid, |
| 674 | async_extent->ram_size, |
| 675 | ins.offset, |
| 676 | BTRFS_ORDERED_COMPRESSED, |
| 677 | async_extent->compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 678 | BUG_ON(ret); |
| 679 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 680 | /* |
| 681 | * clear dirty, set writeback and unlock the pages. |
| 682 | */ |
| 683 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 684 | &BTRFS_I(inode)->io_tree, |
| 685 | async_extent->start, |
| 686 | async_extent->start + |
| 687 | async_extent->ram_size - 1, |
| 688 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 689 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 690 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 691 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 692 | |
| 693 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 694 | async_extent->start, |
| 695 | async_extent->ram_size, |
| 696 | ins.objectid, |
| 697 | ins.offset, async_extent->pages, |
| 698 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 699 | |
| 700 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 701 | alloc_hint = ins.objectid + ins.offset; |
| 702 | kfree(async_extent); |
| 703 | cond_resched(); |
| 704 | } |
| 705 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 709 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 710 | u64 num_bytes) |
| 711 | { |
| 712 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 713 | struct extent_map *em; |
| 714 | u64 alloc_hint = 0; |
| 715 | |
| 716 | read_lock(&em_tree->lock); |
| 717 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 718 | if (em) { |
| 719 | /* |
| 720 | * if block start isn't an actual block number then find the |
| 721 | * first block in this inode and use that as a hint. If that |
| 722 | * block is also bogus then just don't worry about it. |
| 723 | */ |
| 724 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 725 | free_extent_map(em); |
| 726 | em = search_extent_mapping(em_tree, 0, 0); |
| 727 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 728 | alloc_hint = em->block_start; |
| 729 | if (em) |
| 730 | free_extent_map(em); |
| 731 | } else { |
| 732 | alloc_hint = em->block_start; |
| 733 | free_extent_map(em); |
| 734 | } |
| 735 | } |
| 736 | read_unlock(&em_tree->lock); |
| 737 | |
| 738 | return alloc_hint; |
| 739 | } |
| 740 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 741 | /* |
| 742 | * when extent_io.c finds a delayed allocation range in the file, |
| 743 | * the call backs end up in this code. The basic idea is to |
| 744 | * allocate extents on disk for the range, and create ordered data structs |
| 745 | * in ram to track those extents. |
| 746 | * |
| 747 | * locked_page is the page that writepage had locked already. We use |
| 748 | * it to make sure we don't do extra locks or unlocks. |
| 749 | * |
| 750 | * *page_started is set to one if we unlock locked_page and do everything |
| 751 | * required to start IO on it. It may be clean and already done with |
| 752 | * IO when we return. |
| 753 | */ |
| 754 | static noinline int cow_file_range(struct inode *inode, |
| 755 | struct page *locked_page, |
| 756 | u64 start, u64 end, int *page_started, |
| 757 | unsigned long *nr_written, |
| 758 | int unlock) |
| 759 | { |
| 760 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 761 | struct btrfs_trans_handle *trans; |
| 762 | u64 alloc_hint = 0; |
| 763 | u64 num_bytes; |
| 764 | unsigned long ram_size; |
| 765 | u64 disk_num_bytes; |
| 766 | u64 cur_alloc_size; |
| 767 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 768 | struct btrfs_key ins; |
| 769 | struct extent_map *em; |
| 770 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 771 | int ret = 0; |
| 772 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 773 | BUG_ON(root == root->fs_info->tree_root); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 774 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 775 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 776 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 777 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 778 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 779 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 780 | num_bytes = max(blocksize, num_bytes); |
| 781 | disk_num_bytes = num_bytes; |
| 782 | ret = 0; |
| 783 | |
| 784 | if (start == 0) { |
| 785 | /* lets try to make an inline extent */ |
| 786 | ret = cow_file_range_inline(trans, root, inode, |
| 787 | start, end, 0, NULL); |
| 788 | if (ret == 0) { |
| 789 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 790 | &BTRFS_I(inode)->io_tree, |
| 791 | start, end, NULL, |
| 792 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 793 | EXTENT_CLEAR_UNLOCK | |
| 794 | EXTENT_CLEAR_DELALLOC | |
| 795 | EXTENT_CLEAR_DIRTY | |
| 796 | EXTENT_SET_WRITEBACK | |
| 797 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 798 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 799 | *nr_written = *nr_written + |
| 800 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 801 | *page_started = 1; |
| 802 | ret = 0; |
| 803 | goto out; |
| 804 | } |
| 805 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 806 | |
| 807 | BUG_ON(disk_num_bytes > |
| 808 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 809 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 810 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 811 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 812 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 813 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 814 | unsigned long op; |
| 815 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 816 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 817 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 818 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 819 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 820 | BUG_ON(ret); |
| 821 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 822 | em = alloc_extent_map(GFP_NOFS); |
| 823 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 824 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 825 | ram_size = ins.offset; |
| 826 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 827 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 828 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 829 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 830 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 831 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 832 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 833 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 834 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 835 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 836 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 837 | if (ret != -EEXIST) { |
| 838 | free_extent_map(em); |
| 839 | break; |
| 840 | } |
| 841 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 842 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 843 | } |
| 844 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 845 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 846 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 847 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 848 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 849 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 850 | if (root->root_key.objectid == |
| 851 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 852 | ret = btrfs_reloc_clone_csums(inode, start, |
| 853 | cur_alloc_size); |
| 854 | BUG_ON(ret); |
| 855 | } |
| 856 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 857 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 858 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 859 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 860 | /* we're not doing compressed IO, don't unlock the first |
| 861 | * page (which the caller expects to stay locked), don't |
| 862 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 863 | * |
| 864 | * Do set the Private2 bit so we know this page was properly |
| 865 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 866 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 867 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 868 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 869 | EXTENT_SET_PRIVATE2; |
| 870 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 871 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 872 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 873 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 874 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 875 | num_bytes -= cur_alloc_size; |
| 876 | alloc_hint = ins.objectid + ins.offset; |
| 877 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 878 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 879 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 880 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 881 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 882 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 883 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 884 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 885 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 886 | /* |
| 887 | * work queue call back to started compression on a file and pages |
| 888 | */ |
| 889 | static noinline void async_cow_start(struct btrfs_work *work) |
| 890 | { |
| 891 | struct async_cow *async_cow; |
| 892 | int num_added = 0; |
| 893 | async_cow = container_of(work, struct async_cow, work); |
| 894 | |
| 895 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 896 | async_cow->start, async_cow->end, async_cow, |
| 897 | &num_added); |
| 898 | if (num_added == 0) |
| 899 | async_cow->inode = NULL; |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * work queue call back to submit previously compressed pages |
| 904 | */ |
| 905 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 906 | { |
| 907 | struct async_cow *async_cow; |
| 908 | struct btrfs_root *root; |
| 909 | unsigned long nr_pages; |
| 910 | |
| 911 | async_cow = container_of(work, struct async_cow, work); |
| 912 | |
| 913 | root = async_cow->root; |
| 914 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 915 | PAGE_CACHE_SHIFT; |
| 916 | |
| 917 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 918 | |
| 919 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 920 | 5 * 1042 * 1024 && |
| 921 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 922 | wake_up(&root->fs_info->async_submit_wait); |
| 923 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 924 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 925 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 926 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 927 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 928 | static noinline void async_cow_free(struct btrfs_work *work) |
| 929 | { |
| 930 | struct async_cow *async_cow; |
| 931 | async_cow = container_of(work, struct async_cow, work); |
| 932 | kfree(async_cow); |
| 933 | } |
| 934 | |
| 935 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 936 | u64 start, u64 end, int *page_started, |
| 937 | unsigned long *nr_written) |
| 938 | { |
| 939 | struct async_cow *async_cow; |
| 940 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 941 | unsigned long nr_pages; |
| 942 | u64 cur_end; |
| 943 | int limit = 10 * 1024 * 1042; |
| 944 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 945 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 946 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 947 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 948 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
| 949 | async_cow->inode = inode; |
| 950 | async_cow->root = root; |
| 951 | async_cow->locked_page = locked_page; |
| 952 | async_cow->start = start; |
| 953 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 954 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 955 | cur_end = end; |
| 956 | else |
| 957 | cur_end = min(end, start + 512 * 1024 - 1); |
| 958 | |
| 959 | async_cow->end = cur_end; |
| 960 | INIT_LIST_HEAD(&async_cow->extents); |
| 961 | |
| 962 | async_cow->work.func = async_cow_start; |
| 963 | async_cow->work.ordered_func = async_cow_submit; |
| 964 | async_cow->work.ordered_free = async_cow_free; |
| 965 | async_cow->work.flags = 0; |
| 966 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 967 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 968 | PAGE_CACHE_SHIFT; |
| 969 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 970 | |
| 971 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 972 | &async_cow->work); |
| 973 | |
| 974 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 975 | wait_event(root->fs_info->async_submit_wait, |
| 976 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 977 | limit)); |
| 978 | } |
| 979 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 980 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 981 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 982 | wait_event(root->fs_info->async_submit_wait, |
| 983 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 984 | 0)); |
| 985 | } |
| 986 | |
| 987 | *nr_written += nr_pages; |
| 988 | start = cur_end + 1; |
| 989 | } |
| 990 | *page_started = 1; |
| 991 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 992 | } |
| 993 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 994 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 995 | u64 bytenr, u64 num_bytes) |
| 996 | { |
| 997 | int ret; |
| 998 | struct btrfs_ordered_sum *sums; |
| 999 | LIST_HEAD(list); |
| 1000 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1001 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
| 1002 | bytenr + num_bytes - 1, &list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1003 | if (ret == 0 && list_empty(&list)) |
| 1004 | return 0; |
| 1005 | |
| 1006 | while (!list_empty(&list)) { |
| 1007 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1008 | list_del(&sums->list); |
| 1009 | kfree(sums); |
| 1010 | } |
| 1011 | return 1; |
| 1012 | } |
| 1013 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1014 | /* |
| 1015 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1016 | * of the extents that exist in the file, and COWs the file as required. |
| 1017 | * |
| 1018 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1019 | * blocks on disk |
| 1020 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1021 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1022 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1023 | u64 start, u64 end, int *page_started, int force, |
| 1024 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1025 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1026 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1027 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1028 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1029 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1030 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1031 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1032 | u64 cow_start; |
| 1033 | u64 cur_offset; |
| 1034 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1035 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1036 | u64 disk_bytenr; |
| 1037 | u64 num_bytes; |
| 1038 | int extent_type; |
| 1039 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1040 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1041 | int nocow; |
| 1042 | int check_prev = 1; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1043 | bool nolock = false; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1044 | |
| 1045 | path = btrfs_alloc_path(); |
| 1046 | BUG_ON(!path); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1047 | if (root == root->fs_info->tree_root) { |
| 1048 | nolock = true; |
| 1049 | trans = btrfs_join_transaction_nolock(root, 1); |
| 1050 | } else { |
| 1051 | trans = btrfs_join_transaction(root, 1); |
| 1052 | } |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1053 | BUG_ON(IS_ERR(trans)); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1054 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1055 | cow_start = (u64)-1; |
| 1056 | cur_offset = start; |
| 1057 | while (1) { |
| 1058 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 1059 | cur_offset, 0); |
| 1060 | BUG_ON(ret < 0); |
| 1061 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1062 | leaf = path->nodes[0]; |
| 1063 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1064 | path->slots[0] - 1); |
| 1065 | if (found_key.objectid == inode->i_ino && |
| 1066 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1067 | path->slots[0]--; |
| 1068 | } |
| 1069 | check_prev = 0; |
| 1070 | next_slot: |
| 1071 | leaf = path->nodes[0]; |
| 1072 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1073 | ret = btrfs_next_leaf(root, path); |
| 1074 | if (ret < 0) |
| 1075 | BUG_ON(1); |
| 1076 | if (ret > 0) |
| 1077 | break; |
| 1078 | leaf = path->nodes[0]; |
| 1079 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1080 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1081 | nocow = 0; |
| 1082 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1083 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1084 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1085 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1086 | if (found_key.objectid > inode->i_ino || |
| 1087 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1088 | found_key.offset > end) |
| 1089 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1090 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1091 | if (found_key.offset > cur_offset) { |
| 1092 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1093 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1094 | goto out_check; |
| 1095 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1096 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1097 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1098 | struct btrfs_file_extent_item); |
| 1099 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1100 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1101 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1102 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1103 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1104 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1105 | extent_end = found_key.offset + |
| 1106 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1107 | if (extent_end <= start) { |
| 1108 | path->slots[0]++; |
| 1109 | goto next_slot; |
| 1110 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1111 | if (disk_bytenr == 0) |
| 1112 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1113 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1114 | btrfs_file_extent_encryption(leaf, fi) || |
| 1115 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1116 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1117 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1118 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1119 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1120 | goto out_check; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1121 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1122 | found_key.offset - |
| 1123 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1124 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1125 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1126 | disk_bytenr += cur_offset - found_key.offset; |
| 1127 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1128 | /* |
| 1129 | * force cow if csum exists in the range. |
| 1130 | * this ensure that csum for a given extent are |
| 1131 | * either valid or do not exist. |
| 1132 | */ |
| 1133 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1134 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1135 | nocow = 1; |
| 1136 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1137 | extent_end = found_key.offset + |
| 1138 | btrfs_file_extent_inline_len(leaf, fi); |
| 1139 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1140 | } else { |
| 1141 | BUG_ON(1); |
| 1142 | } |
| 1143 | out_check: |
| 1144 | if (extent_end <= start) { |
| 1145 | path->slots[0]++; |
| 1146 | goto next_slot; |
| 1147 | } |
| 1148 | if (!nocow) { |
| 1149 | if (cow_start == (u64)-1) |
| 1150 | cow_start = cur_offset; |
| 1151 | cur_offset = extent_end; |
| 1152 | if (cur_offset > end) |
| 1153 | break; |
| 1154 | path->slots[0]++; |
| 1155 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1156 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1157 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1158 | btrfs_release_path(root, path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1159 | if (cow_start != (u64)-1) { |
| 1160 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1161 | found_key.offset - 1, page_started, |
| 1162 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1163 | BUG_ON(ret); |
| 1164 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1165 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1166 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1167 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1168 | struct extent_map *em; |
| 1169 | struct extent_map_tree *em_tree; |
| 1170 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 1171 | em = alloc_extent_map(GFP_NOFS); |
| 1172 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1173 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1174 | em->len = num_bytes; |
| 1175 | em->block_len = num_bytes; |
| 1176 | em->block_start = disk_bytenr; |
| 1177 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1178 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1179 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1180 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1181 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1182 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1183 | if (ret != -EEXIST) { |
| 1184 | free_extent_map(em); |
| 1185 | break; |
| 1186 | } |
| 1187 | btrfs_drop_extent_cache(inode, em->start, |
| 1188 | em->start + em->len - 1, 0); |
| 1189 | } |
| 1190 | type = BTRFS_ORDERED_PREALLOC; |
| 1191 | } else { |
| 1192 | type = BTRFS_ORDERED_NOCOW; |
| 1193 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1194 | |
| 1195 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1196 | num_bytes, num_bytes, type); |
| 1197 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1198 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1199 | if (root->root_key.objectid == |
| 1200 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1201 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1202 | num_bytes); |
| 1203 | BUG_ON(ret); |
| 1204 | } |
| 1205 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1206 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1207 | cur_offset, cur_offset + num_bytes - 1, |
| 1208 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1209 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1210 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1211 | cur_offset = extent_end; |
| 1212 | if (cur_offset > end) |
| 1213 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1214 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1215 | btrfs_release_path(root, path); |
| 1216 | |
| 1217 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1218 | cow_start = cur_offset; |
| 1219 | if (cow_start != (u64)-1) { |
| 1220 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1221 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1222 | BUG_ON(ret); |
| 1223 | } |
| 1224 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1225 | if (nolock) { |
| 1226 | ret = btrfs_end_transaction_nolock(trans, root); |
| 1227 | BUG_ON(ret); |
| 1228 | } else { |
| 1229 | ret = btrfs_end_transaction(trans, root); |
| 1230 | BUG_ON(ret); |
| 1231 | } |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1232 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1233 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1234 | } |
| 1235 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1236 | /* |
| 1237 | * extent_io.c call back to do delayed allocation processing |
| 1238 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1239 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1240 | u64 start, u64 end, int *page_started, |
| 1241 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1242 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1243 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1244 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1245 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1246 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1247 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1248 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1249 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1250 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1251 | page_started, 0, nr_written); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1252 | else if (!btrfs_test_opt(root, COMPRESS) && |
| 1253 | !(BTRFS_I(inode)->force_compress)) |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1254 | ret = cow_file_range(inode, locked_page, start, end, |
| 1255 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1256 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1257 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1258 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1259 | return ret; |
| 1260 | } |
| 1261 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1262 | static int btrfs_split_extent_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1263 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1264 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1265 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1266 | if (!(orig->state & EXTENT_DELALLOC)) |
| 1267 | return 0; |
| 1268 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1269 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1275 | * extents so we can keep track of new extents that are just merged onto old |
| 1276 | * extents, such as when we are doing sequential writes, so we can properly |
| 1277 | * account for the metadata space we'll need. |
| 1278 | */ |
| 1279 | static int btrfs_merge_extent_hook(struct inode *inode, |
| 1280 | struct extent_state *new, |
| 1281 | struct extent_state *other) |
| 1282 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1283 | /* not delalloc, ignore it */ |
| 1284 | if (!(other->state & EXTENT_DELALLOC)) |
| 1285 | return 0; |
| 1286 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1287 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1288 | return 0; |
| 1289 | } |
| 1290 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1291 | /* |
| 1292 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1293 | * bytes in this file, and to maintain the list of inodes that |
| 1294 | * have pending delalloc work to be done. |
| 1295 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1296 | static int btrfs_set_bit_hook(struct inode *inode, |
| 1297 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1298 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1299 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1300 | /* |
| 1301 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1302 | * but in this case, we are only testeing for the DELALLOC |
| 1303 | * bit, which is only set or cleared with irqs on |
| 1304 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1305 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1306 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1307 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1308 | int do_list = (root->root_key.objectid != |
| 1309 | BTRFS_ROOT_TREE_OBJECTID); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1310 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1311 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1312 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1313 | else |
| 1314 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1315 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1316 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1317 | BTRFS_I(inode)->delalloc_bytes += len; |
| 1318 | root->fs_info->delalloc_bytes += len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1319 | if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1320 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1321 | &root->fs_info->delalloc_inodes); |
| 1322 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1323 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1324 | } |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1328 | /* |
| 1329 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1330 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1331 | static int btrfs_clear_bit_hook(struct inode *inode, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1332 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1333 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1334 | /* |
| 1335 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1336 | * but in this case, we are only testeing for the DELALLOC |
| 1337 | * bit, which is only set or cleared with irqs on |
| 1338 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1339 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1340 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1341 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1342 | int do_list = (root->root_key.objectid != |
| 1343 | BTRFS_ROOT_TREE_OBJECTID); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1344 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1345 | if (*bits & EXTENT_FIRST_DELALLOC) |
| 1346 | *bits &= ~EXTENT_FIRST_DELALLOC; |
| 1347 | else if (!(*bits & EXTENT_DO_ACCOUNTING)) |
| 1348 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
| 1349 | |
| 1350 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1351 | btrfs_delalloc_release_metadata(inode, len); |
| 1352 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1353 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 1354 | && do_list) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1355 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1356 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1357 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1358 | root->fs_info->delalloc_bytes -= len; |
| 1359 | BTRFS_I(inode)->delalloc_bytes -= len; |
| 1360 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1361 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1362 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1363 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1364 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1365 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1366 | } |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1370 | /* |
| 1371 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1372 | * we don't create bios that span stripes or chunks |
| 1373 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1374 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1375 | size_t size, struct bio *bio, |
| 1376 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1377 | { |
| 1378 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1379 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1380 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1381 | u64 length = 0; |
| 1382 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1383 | int ret; |
| 1384 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1385 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1386 | return 0; |
| 1387 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1388 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1389 | map_tree = &root->fs_info->mapping_tree; |
| 1390 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1391 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1392 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1393 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1394 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1395 | return 1; |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 1396 | return ret; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1397 | } |
| 1398 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1399 | /* |
| 1400 | * in order to insert checksums into the metadata in large chunks, |
| 1401 | * we wait until bio submission time. All the pages in the bio are |
| 1402 | * checksummed and sums are attached onto the ordered extent record. |
| 1403 | * |
| 1404 | * At IO completion time the cums attached on the ordered extent record |
| 1405 | * are inserted into the btree |
| 1406 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1407 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1408 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1409 | unsigned long bio_flags, |
| 1410 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1411 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1412 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1413 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1414 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1415 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1416 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1417 | return 0; |
| 1418 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1419 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1420 | /* |
| 1421 | * in order to insert checksums into the metadata in large chunks, |
| 1422 | * we wait until bio submission time. All the pages in the bio are |
| 1423 | * checksummed and sums are attached onto the ordered extent record. |
| 1424 | * |
| 1425 | * At IO completion time the cums attached on the ordered extent record |
| 1426 | * are inserted into the btree |
| 1427 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1428 | static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1429 | int mirror_num, unsigned long bio_flags, |
| 1430 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1431 | { |
| 1432 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1433 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1434 | } |
| 1435 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1436 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1437 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1438 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1439 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1440 | static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1441 | int mirror_num, unsigned long bio_flags, |
| 1442 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1443 | { |
| 1444 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1445 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1446 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1447 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1448 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1449 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1450 | if (root == root->fs_info->tree_root) |
| 1451 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2); |
| 1452 | else |
| 1453 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1454 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1455 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1456 | if (!(rw & REQ_WRITE)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1457 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1458 | return btrfs_submit_compressed_read(inode, bio, |
| 1459 | mirror_num, bio_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1460 | } else if (!skip_sum) |
| 1461 | btrfs_lookup_bio_sums(root, inode, bio, NULL); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1462 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1463 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1464 | /* csum items have already been cloned */ |
| 1465 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1466 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1467 | /* we're doing a write, do the async checksumming */ |
| 1468 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1469 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1470 | bio_flags, bio_offset, |
| 1471 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1472 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1473 | } |
| 1474 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1475 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1476 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1477 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1478 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1479 | /* |
| 1480 | * given a list of ordered sums record them in the inode. This happens |
| 1481 | * at IO completion time based on sums calculated at bio submission time. |
| 1482 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1483 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1484 | struct inode *inode, u64 file_offset, |
| 1485 | struct list_head *list) |
| 1486 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1487 | struct btrfs_ordered_sum *sum; |
| 1488 | |
| 1489 | btrfs_set_trans_block_group(trans, inode); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1490 | |
| 1491 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1492 | btrfs_csum_file_blocks(trans, |
| 1493 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1494 | } |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1498 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1499 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1500 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1501 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1502 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1503 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1504 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1505 | } |
| 1506 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1507 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1508 | struct btrfs_writepage_fixup { |
| 1509 | struct page *page; |
| 1510 | struct btrfs_work work; |
| 1511 | }; |
| 1512 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1513 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1514 | { |
| 1515 | struct btrfs_writepage_fixup *fixup; |
| 1516 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1517 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1518 | struct page *page; |
| 1519 | struct inode *inode; |
| 1520 | u64 page_start; |
| 1521 | u64 page_end; |
| 1522 | |
| 1523 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1524 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1525 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1526 | lock_page(page); |
| 1527 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1528 | ClearPageChecked(page); |
| 1529 | goto out_page; |
| 1530 | } |
| 1531 | |
| 1532 | inode = page->mapping->host; |
| 1533 | page_start = page_offset(page); |
| 1534 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1535 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1536 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
| 1537 | &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1538 | |
| 1539 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1540 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1541 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1542 | |
| 1543 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1544 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1545 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1546 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1547 | unlock_page(page); |
| 1548 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1549 | goto again; |
| 1550 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1551 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1552 | BUG(); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1553 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1554 | ClearPageChecked(page); |
| 1555 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1556 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1557 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1558 | out_page: |
| 1559 | unlock_page(page); |
| 1560 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1561 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | * There are a few paths in the higher layers of the kernel that directly |
| 1566 | * set the page dirty bit without asking the filesystem if it is a |
| 1567 | * good idea. This causes problems because we want to make sure COW |
| 1568 | * properly happens and the data=ordered rules are followed. |
| 1569 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1570 | * 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] | 1571 | * hasn't been properly setup for IO. We kick off an async process |
| 1572 | * to fix it up. The async helper will wait for ordered extents, set |
| 1573 | * the delalloc bit and make it safe to write the page. |
| 1574 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1575 | 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] | 1576 | { |
| 1577 | struct inode *inode = page->mapping->host; |
| 1578 | struct btrfs_writepage_fixup *fixup; |
| 1579 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1580 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1581 | /* this page is properly in the ordered list */ |
| 1582 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1583 | return 0; |
| 1584 | |
| 1585 | if (PageChecked(page)) |
| 1586 | return -EAGAIN; |
| 1587 | |
| 1588 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1589 | if (!fixup) |
| 1590 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1591 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1592 | SetPageChecked(page); |
| 1593 | page_cache_get(page); |
| 1594 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1595 | fixup->page = page; |
| 1596 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1597 | return -EAGAIN; |
| 1598 | } |
| 1599 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1600 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1601 | struct inode *inode, u64 file_pos, |
| 1602 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1603 | u64 num_bytes, u64 ram_bytes, |
| 1604 | u8 compression, u8 encryption, |
| 1605 | u16 other_encoding, int extent_type) |
| 1606 | { |
| 1607 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1608 | struct btrfs_file_extent_item *fi; |
| 1609 | struct btrfs_path *path; |
| 1610 | struct extent_buffer *leaf; |
| 1611 | struct btrfs_key ins; |
| 1612 | u64 hint; |
| 1613 | int ret; |
| 1614 | |
| 1615 | path = btrfs_alloc_path(); |
| 1616 | BUG_ON(!path); |
| 1617 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1618 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1619 | |
| 1620 | /* |
| 1621 | * we may be replacing one extent in the tree with another. |
| 1622 | * The new extent is pinned in the extent map, and we don't want |
| 1623 | * to drop it from the cache until it is completely in the btree. |
| 1624 | * |
| 1625 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1626 | * the caller is expected to unpin it and allow it to be merged |
| 1627 | * with the others. |
| 1628 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1629 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1630 | &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1631 | BUG_ON(ret); |
| 1632 | |
| 1633 | ins.objectid = inode->i_ino; |
| 1634 | ins.offset = file_pos; |
| 1635 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1636 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1637 | BUG_ON(ret); |
| 1638 | leaf = path->nodes[0]; |
| 1639 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1640 | struct btrfs_file_extent_item); |
| 1641 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1642 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1643 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1644 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1645 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1646 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1647 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1648 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1649 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1650 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1651 | |
| 1652 | btrfs_unlock_up_safe(path, 1); |
| 1653 | btrfs_set_lock_blocking(leaf); |
| 1654 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1655 | btrfs_mark_buffer_dirty(leaf); |
| 1656 | |
| 1657 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1658 | |
| 1659 | ins.objectid = disk_bytenr; |
| 1660 | ins.offset = disk_num_bytes; |
| 1661 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1662 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1663 | root->root_key.objectid, |
| 1664 | inode->i_ino, file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1665 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1666 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1667 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1668 | return 0; |
| 1669 | } |
| 1670 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1671 | /* |
| 1672 | * helper function for btrfs_finish_ordered_io, this |
| 1673 | * just reads in some of the csum leaves to prime them into ram |
| 1674 | * before we start the transaction. It limits the amount of btree |
| 1675 | * reads required while inside the transaction. |
| 1676 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1677 | /* as ordered data IO finishes, this gets called so we can finish |
| 1678 | * an ordered extent if the range of bytes in the file it covers are |
| 1679 | * fully written. |
| 1680 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1681 | 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] | 1682 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1683 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1684 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1685 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1686 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1687 | struct extent_state *cached_state = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1688 | int compress_type = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1689 | int ret; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1690 | bool nolock = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1691 | |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 1692 | ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 1693 | end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1694 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1695 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1696 | BUG_ON(!ordered_extent); |
Josef Bacik | efd049f | 2010-02-02 20:50:10 +0000 | [diff] [blame] | 1697 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1698 | nolock = (root == root->fs_info->tree_root); |
| 1699 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1700 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
| 1701 | BUG_ON(!list_empty(&ordered_extent->list)); |
| 1702 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1703 | if (!ret) { |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1704 | if (nolock) |
| 1705 | trans = btrfs_join_transaction_nolock(root, 1); |
| 1706 | else |
| 1707 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1708 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1709 | btrfs_set_trans_block_group(trans, inode); |
| 1710 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1711 | ret = btrfs_update_inode(trans, root, inode); |
| 1712 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1713 | } |
| 1714 | goto out; |
| 1715 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1716 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1717 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 1718 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1719 | 0, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1720 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1721 | if (nolock) |
| 1722 | trans = btrfs_join_transaction_nolock(root, 1); |
| 1723 | else |
| 1724 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1725 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1726 | btrfs_set_trans_block_group(trans, inode); |
| 1727 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1728 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1729 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1730 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1731 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1732 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1733 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1734 | ordered_extent->file_offset, |
| 1735 | ordered_extent->file_offset + |
| 1736 | ordered_extent->len); |
| 1737 | BUG_ON(ret); |
| 1738 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 1739 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1740 | ret = insert_reserved_file_extent(trans, inode, |
| 1741 | ordered_extent->file_offset, |
| 1742 | ordered_extent->start, |
| 1743 | ordered_extent->disk_len, |
| 1744 | ordered_extent->len, |
| 1745 | ordered_extent->len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1746 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1747 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1748 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1749 | ordered_extent->file_offset, |
| 1750 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1751 | BUG_ON(ret); |
| 1752 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1753 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 1754 | ordered_extent->file_offset + |
| 1755 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
| 1756 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1757 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1758 | &ordered_extent->list); |
| 1759 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1760 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1761 | ret = btrfs_update_inode(trans, root, inode); |
| 1762 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1763 | out: |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1764 | if (nolock) { |
| 1765 | if (trans) |
| 1766 | btrfs_end_transaction_nolock(trans, root); |
| 1767 | } else { |
| 1768 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
| 1769 | if (trans) |
| 1770 | btrfs_end_transaction(trans, root); |
| 1771 | } |
| 1772 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1773 | /* once for us */ |
| 1774 | btrfs_put_ordered_extent(ordered_extent); |
| 1775 | /* once for the tree */ |
| 1776 | btrfs_put_ordered_extent(ordered_extent); |
| 1777 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1778 | return 0; |
| 1779 | } |
| 1780 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1781 | 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] | 1782 | struct extent_state *state, int uptodate) |
| 1783 | { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1784 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1785 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1786 | } |
| 1787 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1788 | /* |
| 1789 | * When IO fails, either with EIO or csum verification fails, we |
| 1790 | * try other mirrors that might have a good copy of the data. This |
| 1791 | * io_failure_record is used to record state as we go through all the |
| 1792 | * mirrors. If another mirror has good data, the page is set up to date |
| 1793 | * and things continue. If a good mirror can't be found, the original |
| 1794 | * bio end_io callback is called to indicate things have failed. |
| 1795 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1796 | struct io_failure_record { |
| 1797 | struct page *page; |
| 1798 | u64 start; |
| 1799 | u64 len; |
| 1800 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1801 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1802 | int last_mirror; |
| 1803 | }; |
| 1804 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1805 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1806 | struct page *page, u64 start, u64 end, |
| 1807 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1808 | { |
| 1809 | struct io_failure_record *failrec = NULL; |
| 1810 | u64 private; |
| 1811 | struct extent_map *em; |
| 1812 | struct inode *inode = page->mapping->host; |
| 1813 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1814 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1815 | struct bio *bio; |
| 1816 | int num_copies; |
| 1817 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1818 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1819 | u64 logical; |
| 1820 | |
| 1821 | ret = get_state_private(failure_tree, start, &private); |
| 1822 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1823 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1824 | if (!failrec) |
| 1825 | return -ENOMEM; |
| 1826 | failrec->start = start; |
| 1827 | failrec->len = end - start + 1; |
| 1828 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1829 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1830 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1831 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1832 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1833 | if (em->start > start || em->start + em->len < start) { |
| 1834 | free_extent_map(em); |
| 1835 | em = NULL; |
| 1836 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1837 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1838 | |
| 1839 | if (!em || IS_ERR(em)) { |
| 1840 | kfree(failrec); |
| 1841 | return -EIO; |
| 1842 | } |
| 1843 | logical = start - em->start; |
| 1844 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1845 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1846 | logical = em->block_start; |
| 1847 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1848 | extent_set_compress_type(&failrec->bio_flags, |
| 1849 | em->compress_type); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1850 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1851 | failrec->logical = logical; |
| 1852 | free_extent_map(em); |
| 1853 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1854 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1855 | set_state_private(failure_tree, start, |
| 1856 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1857 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1858 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1859 | } |
| 1860 | num_copies = btrfs_num_copies( |
| 1861 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1862 | failrec->logical, failrec->len); |
| 1863 | failrec->last_mirror++; |
| 1864 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1865 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1866 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1867 | failrec->start, |
| 1868 | EXTENT_LOCKED); |
| 1869 | if (state && state->start != failrec->start) |
| 1870 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1871 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1872 | } |
| 1873 | if (!state || failrec->last_mirror > num_copies) { |
| 1874 | set_state_private(failure_tree, failrec->start, 0); |
| 1875 | clear_extent_bits(failure_tree, failrec->start, |
| 1876 | failrec->start + failrec->len - 1, |
| 1877 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1878 | kfree(failrec); |
| 1879 | return -EIO; |
| 1880 | } |
| 1881 | bio = bio_alloc(GFP_NOFS, 1); |
| 1882 | bio->bi_private = state; |
| 1883 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1884 | bio->bi_sector = failrec->logical >> 9; |
| 1885 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1886 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1887 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1888 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1889 | if (failed_bio->bi_rw & REQ_WRITE) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1890 | rw = WRITE; |
| 1891 | else |
| 1892 | rw = READ; |
| 1893 | |
| 1894 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1895 | failrec->last_mirror, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1896 | failrec->bio_flags, 0); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1897 | return 0; |
| 1898 | } |
| 1899 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1900 | /* |
| 1901 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1902 | * to see if we need to process or clean up an io_failure_record |
| 1903 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1904 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1905 | { |
| 1906 | u64 private; |
| 1907 | u64 private_failure; |
| 1908 | struct io_failure_record *failure; |
| 1909 | int ret; |
| 1910 | |
| 1911 | private = 0; |
| 1912 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 1913 | (u64)-1, 1, EXTENT_DIRTY)) { |
| 1914 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1915 | start, &private_failure); |
| 1916 | if (ret == 0) { |
| 1917 | failure = (struct io_failure_record *)(unsigned long) |
| 1918 | private_failure; |
| 1919 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1920 | failure->start, 0); |
| 1921 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1922 | failure->start, |
| 1923 | failure->start + failure->len - 1, |
| 1924 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1925 | GFP_NOFS); |
| 1926 | kfree(failure); |
| 1927 | } |
| 1928 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1929 | return 0; |
| 1930 | } |
| 1931 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1932 | /* |
| 1933 | * when reads are done, we need to check csums to verify the data is correct |
| 1934 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1935 | * the io_failure_record routines to find good copies |
| 1936 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1937 | 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] | 1938 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1939 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1940 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1941 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1942 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1943 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1944 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1945 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1946 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1947 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1948 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1949 | if (PageChecked(page)) { |
| 1950 | ClearPageChecked(page); |
| 1951 | goto good; |
| 1952 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1953 | |
| 1954 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 1955 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1956 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1957 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1958 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1959 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1960 | GFP_NOFS); |
| 1961 | return 0; |
| 1962 | } |
| 1963 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1964 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1965 | private = state->private; |
| 1966 | ret = 0; |
| 1967 | } else { |
| 1968 | ret = get_state_private(io_tree, start, &private); |
| 1969 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1970 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1971 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1972 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1973 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1974 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1975 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1976 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1977 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1978 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1979 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1980 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1981 | /* if the io failure tree for this inode is non-empty, |
| 1982 | * check to see if we've recovered from a failed IO |
| 1983 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1984 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1985 | return 0; |
| 1986 | |
| 1987 | zeroit: |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 1988 | if (printk_ratelimit()) { |
| 1989 | printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " |
| 1990 | "private %llu\n", page->mapping->host->i_ino, |
| 1991 | (unsigned long long)start, csum, |
| 1992 | (unsigned long long)private); |
| 1993 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1994 | memset(kaddr + offset, 1, end - start + 1); |
| 1995 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1996 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1997 | if (private == 0) |
| 1998 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1999 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2000 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2001 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2002 | struct delayed_iput { |
| 2003 | struct list_head list; |
| 2004 | struct inode *inode; |
| 2005 | }; |
| 2006 | |
| 2007 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2008 | { |
| 2009 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2010 | struct delayed_iput *delayed; |
| 2011 | |
| 2012 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2013 | return; |
| 2014 | |
| 2015 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2016 | delayed->inode = inode; |
| 2017 | |
| 2018 | spin_lock(&fs_info->delayed_iput_lock); |
| 2019 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2020 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2021 | } |
| 2022 | |
| 2023 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2024 | { |
| 2025 | LIST_HEAD(list); |
| 2026 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2027 | struct delayed_iput *delayed; |
| 2028 | int empty; |
| 2029 | |
| 2030 | spin_lock(&fs_info->delayed_iput_lock); |
| 2031 | empty = list_empty(&fs_info->delayed_iputs); |
| 2032 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2033 | if (empty) |
| 2034 | return; |
| 2035 | |
| 2036 | down_read(&root->fs_info->cleanup_work_sem); |
| 2037 | spin_lock(&fs_info->delayed_iput_lock); |
| 2038 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2039 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2040 | |
| 2041 | while (!list_empty(&list)) { |
| 2042 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2043 | list_del(&delayed->list); |
| 2044 | iput(delayed->inode); |
| 2045 | kfree(delayed); |
| 2046 | } |
| 2047 | up_read(&root->fs_info->cleanup_work_sem); |
| 2048 | } |
| 2049 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2050 | /* |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2051 | * calculate extra metadata reservation when snapshotting a subvolume |
| 2052 | * contains orphan files. |
| 2053 | */ |
| 2054 | void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans, |
| 2055 | struct btrfs_pending_snapshot *pending, |
| 2056 | u64 *bytes_to_reserve) |
| 2057 | { |
| 2058 | struct btrfs_root *root; |
| 2059 | struct btrfs_block_rsv *block_rsv; |
| 2060 | u64 num_bytes; |
| 2061 | int index; |
| 2062 | |
| 2063 | root = pending->root; |
| 2064 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2065 | return; |
| 2066 | |
| 2067 | block_rsv = root->orphan_block_rsv; |
| 2068 | |
| 2069 | /* orphan block reservation for the snapshot */ |
| 2070 | num_bytes = block_rsv->size; |
| 2071 | |
| 2072 | /* |
| 2073 | * after the snapshot is created, COWing tree blocks may use more |
| 2074 | * space than it frees. So we should make sure there is enough |
| 2075 | * reserved space. |
| 2076 | */ |
| 2077 | index = trans->transid & 0x1; |
| 2078 | if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) { |
| 2079 | num_bytes += block_rsv->size - |
| 2080 | (block_rsv->reserved + block_rsv->freed[index]); |
| 2081 | } |
| 2082 | |
| 2083 | *bytes_to_reserve += num_bytes; |
| 2084 | } |
| 2085 | |
| 2086 | void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans, |
| 2087 | struct btrfs_pending_snapshot *pending) |
| 2088 | { |
| 2089 | struct btrfs_root *root = pending->root; |
| 2090 | struct btrfs_root *snap = pending->snap; |
| 2091 | struct btrfs_block_rsv *block_rsv; |
| 2092 | u64 num_bytes; |
| 2093 | int index; |
| 2094 | int ret; |
| 2095 | |
| 2096 | if (!root->orphan_block_rsv || list_empty(&root->orphan_list)) |
| 2097 | return; |
| 2098 | |
| 2099 | /* refill source subvolume's orphan block reservation */ |
| 2100 | block_rsv = root->orphan_block_rsv; |
| 2101 | index = trans->transid & 0x1; |
| 2102 | if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) { |
| 2103 | num_bytes = block_rsv->size - |
| 2104 | (block_rsv->reserved + block_rsv->freed[index]); |
| 2105 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2106 | root->orphan_block_rsv, |
| 2107 | num_bytes); |
| 2108 | BUG_ON(ret); |
| 2109 | } |
| 2110 | |
| 2111 | /* setup orphan block reservation for the snapshot */ |
| 2112 | block_rsv = btrfs_alloc_block_rsv(snap); |
| 2113 | BUG_ON(!block_rsv); |
| 2114 | |
| 2115 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2116 | snap->orphan_block_rsv = block_rsv; |
| 2117 | |
| 2118 | num_bytes = root->orphan_block_rsv->size; |
| 2119 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 2120 | block_rsv, num_bytes); |
| 2121 | BUG_ON(ret); |
| 2122 | |
| 2123 | #if 0 |
| 2124 | /* insert orphan item for the snapshot */ |
| 2125 | WARN_ON(!root->orphan_item_inserted); |
| 2126 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2127 | snap->root_key.objectid); |
| 2128 | BUG_ON(ret); |
| 2129 | snap->orphan_item_inserted = 1; |
| 2130 | #endif |
| 2131 | } |
| 2132 | |
| 2133 | enum btrfs_orphan_cleanup_state { |
| 2134 | ORPHAN_CLEANUP_STARTED = 1, |
| 2135 | ORPHAN_CLEANUP_DONE = 2, |
| 2136 | }; |
| 2137 | |
| 2138 | /* |
| 2139 | * This is called in transaction commmit time. If there are no orphan |
| 2140 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2141 | * structure. |
| 2142 | */ |
| 2143 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2144 | struct btrfs_root *root) |
| 2145 | { |
| 2146 | int ret; |
| 2147 | |
| 2148 | if (!list_empty(&root->orphan_list) || |
| 2149 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2150 | return; |
| 2151 | |
| 2152 | if (root->orphan_item_inserted && |
| 2153 | btrfs_root_refs(&root->root_item) > 0) { |
| 2154 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2155 | root->root_key.objectid); |
| 2156 | BUG_ON(ret); |
| 2157 | root->orphan_item_inserted = 0; |
| 2158 | } |
| 2159 | |
| 2160 | if (root->orphan_block_rsv) { |
| 2161 | WARN_ON(root->orphan_block_rsv->size > 0); |
| 2162 | btrfs_free_block_rsv(root, root->orphan_block_rsv); |
| 2163 | root->orphan_block_rsv = NULL; |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2168 | * This creates an orphan entry for the given inode in case something goes |
| 2169 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2170 | * |
| 2171 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2172 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2173 | */ |
| 2174 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2175 | { |
| 2176 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2177 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2178 | int reserve = 0; |
| 2179 | int insert = 0; |
| 2180 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2181 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2182 | if (!root->orphan_block_rsv) { |
| 2183 | block_rsv = btrfs_alloc_block_rsv(root); |
| 2184 | BUG_ON(!block_rsv); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2185 | } |
| 2186 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2187 | spin_lock(&root->orphan_lock); |
| 2188 | if (!root->orphan_block_rsv) { |
| 2189 | root->orphan_block_rsv = block_rsv; |
| 2190 | } else if (block_rsv) { |
| 2191 | btrfs_free_block_rsv(root, block_rsv); |
| 2192 | block_rsv = NULL; |
| 2193 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2194 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2195 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2196 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2197 | #if 0 |
| 2198 | /* |
| 2199 | * For proper ENOSPC handling, we should do orphan |
| 2200 | * cleanup when mounting. But this introduces backward |
| 2201 | * compatibility issue. |
| 2202 | */ |
| 2203 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2204 | insert = 2; |
| 2205 | else |
| 2206 | insert = 1; |
| 2207 | #endif |
| 2208 | insert = 1; |
| 2209 | } else { |
| 2210 | WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved); |
| 2211 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2212 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2213 | if (!BTRFS_I(inode)->orphan_meta_reserved) { |
| 2214 | BTRFS_I(inode)->orphan_meta_reserved = 1; |
| 2215 | reserve = 1; |
| 2216 | } |
| 2217 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2218 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2219 | if (block_rsv) |
| 2220 | btrfs_add_durable_block_rsv(root->fs_info, block_rsv); |
| 2221 | |
| 2222 | /* grab metadata reservation from transaction handle */ |
| 2223 | if (reserve) { |
| 2224 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
| 2225 | BUG_ON(ret); |
| 2226 | } |
| 2227 | |
| 2228 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2229 | if (insert >= 1) { |
| 2230 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 2231 | BUG_ON(ret); |
| 2232 | } |
| 2233 | |
| 2234 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2235 | if (insert >= 2) { |
| 2236 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2237 | root->root_key.objectid); |
| 2238 | BUG_ON(ret); |
| 2239 | } |
| 2240 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | /* |
| 2244 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2245 | * item for this particular inode. |
| 2246 | */ |
| 2247 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2248 | { |
| 2249 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2250 | int delete_item = 0; |
| 2251 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2252 | int ret = 0; |
| 2253 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2254 | spin_lock(&root->orphan_lock); |
| 2255 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2256 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2257 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2258 | } |
| 2259 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2260 | if (BTRFS_I(inode)->orphan_meta_reserved) { |
| 2261 | BTRFS_I(inode)->orphan_meta_reserved = 0; |
| 2262 | release_rsv = 1; |
| 2263 | } |
| 2264 | spin_unlock(&root->orphan_lock); |
| 2265 | |
| 2266 | if (trans && delete_item) { |
| 2267 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 2268 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2269 | } |
| 2270 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2271 | if (release_rsv) |
| 2272 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2273 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2274 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | /* |
| 2278 | * this cleans up any orphans that may be left on the list from the last use |
| 2279 | * of this root. |
| 2280 | */ |
| 2281 | void btrfs_orphan_cleanup(struct btrfs_root *root) |
| 2282 | { |
| 2283 | struct btrfs_path *path; |
| 2284 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2285 | struct btrfs_key key, found_key; |
| 2286 | struct btrfs_trans_handle *trans; |
| 2287 | struct inode *inode; |
| 2288 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2289 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2290 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2291 | return; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2292 | |
| 2293 | path = btrfs_alloc_path(); |
| 2294 | BUG_ON(!path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2295 | path->reada = -1; |
| 2296 | |
| 2297 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2298 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2299 | key.offset = (u64)-1; |
| 2300 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2301 | while (1) { |
| 2302 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2303 | if (ret < 0) { |
| 2304 | printk(KERN_ERR "Error searching slot for orphan: %d" |
| 2305 | "\n", ret); |
| 2306 | break; |
| 2307 | } |
| 2308 | |
| 2309 | /* |
| 2310 | * if ret == 0 means we found what we were searching for, which |
| 2311 | * is weird, but possible, so only screw with path if we didnt |
| 2312 | * find the key and see if we have stuff that matches |
| 2313 | */ |
| 2314 | if (ret > 0) { |
| 2315 | if (path->slots[0] == 0) |
| 2316 | break; |
| 2317 | path->slots[0]--; |
| 2318 | } |
| 2319 | |
| 2320 | /* pull out the item */ |
| 2321 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2322 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2323 | |
| 2324 | /* make sure the item matches what we want */ |
| 2325 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2326 | break; |
| 2327 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2328 | break; |
| 2329 | |
| 2330 | /* release the path since we're done with it */ |
| 2331 | btrfs_release_path(root, path); |
| 2332 | |
| 2333 | /* |
| 2334 | * this is where we are basically btrfs_lookup, without the |
| 2335 | * crossing root thing. we store the inode number in the |
| 2336 | * offset of the orphan item. |
| 2337 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2338 | found_key.objectid = found_key.offset; |
| 2339 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2340 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2341 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2342 | BUG_ON(IS_ERR(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2343 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2344 | /* |
| 2345 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2346 | * the proper thing when we hit it |
| 2347 | */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2348 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2349 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2350 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2351 | |
| 2352 | /* |
| 2353 | * if this is a bad inode, means we actually succeeded in |
| 2354 | * removing the inode, but not the orphan record, which means |
| 2355 | * we need to manually delete the orphan since iput will just |
| 2356 | * do a destroy_inode |
| 2357 | */ |
| 2358 | if (is_bad_inode(inode)) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2359 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2360 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2361 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2362 | iput(inode); |
| 2363 | continue; |
| 2364 | } |
| 2365 | |
| 2366 | /* if we have links, this was a truncate, lets do that */ |
| 2367 | if (inode->i_nlink) { |
| 2368 | nr_truncate++; |
| 2369 | btrfs_truncate(inode); |
| 2370 | } else { |
| 2371 | nr_unlink++; |
| 2372 | } |
| 2373 | |
| 2374 | /* this will do delete_inode and everything for us */ |
| 2375 | iput(inode); |
| 2376 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2377 | btrfs_free_path(path); |
| 2378 | |
| 2379 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 2380 | |
| 2381 | if (root->orphan_block_rsv) |
| 2382 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 2383 | (u64)-1); |
| 2384 | |
| 2385 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
| 2386 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 2387 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2388 | btrfs_end_transaction(trans, root); |
| 2389 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2390 | |
| 2391 | if (nr_unlink) |
| 2392 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2393 | if (nr_truncate) |
| 2394 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2395 | } |
| 2396 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2397 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2398 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2399 | * don't find any xattrs, we know there can't be any acls. |
| 2400 | * |
| 2401 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2402 | */ |
| 2403 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2404 | int slot, u64 objectid) |
| 2405 | { |
| 2406 | u32 nritems = btrfs_header_nritems(leaf); |
| 2407 | struct btrfs_key found_key; |
| 2408 | int scanned = 0; |
| 2409 | |
| 2410 | slot++; |
| 2411 | while (slot < nritems) { |
| 2412 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2413 | |
| 2414 | /* we found a different objectid, there must not be acls */ |
| 2415 | if (found_key.objectid != objectid) |
| 2416 | return 0; |
| 2417 | |
| 2418 | /* we found an xattr, assume we've got an acl */ |
| 2419 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2420 | return 1; |
| 2421 | |
| 2422 | /* |
| 2423 | * we found a key greater than an xattr key, there can't |
| 2424 | * be any acls later on |
| 2425 | */ |
| 2426 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2427 | return 0; |
| 2428 | |
| 2429 | slot++; |
| 2430 | scanned++; |
| 2431 | |
| 2432 | /* |
| 2433 | * it goes inode, inode backrefs, xattrs, extents, |
| 2434 | * so if there are a ton of hard links to an inode there can |
| 2435 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2436 | * this is just an optimization |
| 2437 | */ |
| 2438 | if (scanned >= 8) |
| 2439 | break; |
| 2440 | } |
| 2441 | /* we hit the end of the leaf before we found an xattr or |
| 2442 | * something larger than an xattr. We have to assume the inode |
| 2443 | * has acls |
| 2444 | */ |
| 2445 | return 1; |
| 2446 | } |
| 2447 | |
| 2448 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2449 | * read an inode from the btree into the in-memory inode |
| 2450 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2451 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2452 | { |
| 2453 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2454 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2455 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2456 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2457 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2458 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2459 | int maybe_acls; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2460 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2461 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2462 | int ret; |
| 2463 | |
| 2464 | path = btrfs_alloc_path(); |
| 2465 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2466 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2467 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2468 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2469 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2470 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2471 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2472 | leaf = path->nodes[0]; |
| 2473 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2474 | struct btrfs_inode_item); |
| 2475 | |
| 2476 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2477 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2478 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2479 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2480 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2481 | |
| 2482 | tspec = btrfs_inode_atime(inode_item); |
| 2483 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2484 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2485 | |
| 2486 | tspec = btrfs_inode_mtime(inode_item); |
| 2487 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2488 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2489 | |
| 2490 | tspec = btrfs_inode_ctime(inode_item); |
| 2491 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2492 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2493 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2494 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2495 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2496 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2497 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2498 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2499 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2500 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2501 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2502 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2503 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2504 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2505 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2506 | /* |
| 2507 | * try to precache a NULL acl entry for files that don't have |
| 2508 | * any xattrs or acls |
| 2509 | */ |
| 2510 | 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] | 2511 | if (!maybe_acls) |
| 2512 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2513 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2514 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, |
| 2515 | alloc_group_block, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2516 | btrfs_free_path(path); |
| 2517 | inode_item = NULL; |
| 2518 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2519 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2520 | case S_IFREG: |
| 2521 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2522 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2523 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2524 | inode->i_fop = &btrfs_file_operations; |
| 2525 | inode->i_op = &btrfs_file_inode_operations; |
| 2526 | break; |
| 2527 | case S_IFDIR: |
| 2528 | inode->i_fop = &btrfs_dir_file_operations; |
| 2529 | if (root == root->fs_info->tree_root) |
| 2530 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2531 | else |
| 2532 | inode->i_op = &btrfs_dir_inode_operations; |
| 2533 | break; |
| 2534 | case S_IFLNK: |
| 2535 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2536 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2537 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2538 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2539 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2540 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2541 | init_special_inode(inode, inode->i_mode, rdev); |
| 2542 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2543 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2544 | |
| 2545 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2546 | return; |
| 2547 | |
| 2548 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2549 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2550 | make_bad_inode(inode); |
| 2551 | } |
| 2552 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2553 | /* |
| 2554 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2555 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2556 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2557 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2558 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2559 | struct inode *inode) |
| 2560 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2561 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2562 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2563 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2564 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2565 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2566 | |
| 2567 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2568 | inode->i_atime.tv_sec); |
| 2569 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2570 | inode->i_atime.tv_nsec); |
| 2571 | |
| 2572 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2573 | inode->i_mtime.tv_sec); |
| 2574 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2575 | inode->i_mtime.tv_nsec); |
| 2576 | |
| 2577 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2578 | inode->i_ctime.tv_sec); |
| 2579 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2580 | inode->i_ctime.tv_nsec); |
| 2581 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2582 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2583 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2584 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2585 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2586 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2587 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2588 | btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2589 | } |
| 2590 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2591 | /* |
| 2592 | * copy everything in the in-memory inode into the btree. |
| 2593 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2594 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2595 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2596 | { |
| 2597 | struct btrfs_inode_item *inode_item; |
| 2598 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2599 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2600 | int ret; |
| 2601 | |
| 2602 | path = btrfs_alloc_path(); |
| 2603 | BUG_ON(!path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2604 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2605 | ret = btrfs_lookup_inode(trans, root, path, |
| 2606 | &BTRFS_I(inode)->location, 1); |
| 2607 | if (ret) { |
| 2608 | if (ret > 0) |
| 2609 | ret = -ENOENT; |
| 2610 | goto failed; |
| 2611 | } |
| 2612 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2613 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2614 | leaf = path->nodes[0]; |
| 2615 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2616 | struct btrfs_inode_item); |
| 2617 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2618 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2619 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2620 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2621 | ret = 0; |
| 2622 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2623 | btrfs_free_path(path); |
| 2624 | return ret; |
| 2625 | } |
| 2626 | |
| 2627 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2628 | /* |
| 2629 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2630 | * recovery code. It remove a link in a directory with a given name, and |
| 2631 | * also drops the back refs in the inode to the directory |
| 2632 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2633 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2634 | struct btrfs_root *root, |
| 2635 | struct inode *dir, struct inode *inode, |
| 2636 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2637 | { |
| 2638 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2639 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2640 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2641 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2642 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2643 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2644 | |
| 2645 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2646 | if (!path) { |
| 2647 | ret = -ENOMEM; |
| 2648 | goto err; |
| 2649 | } |
| 2650 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2651 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2652 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2653 | name, name_len, -1); |
| 2654 | if (IS_ERR(di)) { |
| 2655 | ret = PTR_ERR(di); |
| 2656 | goto err; |
| 2657 | } |
| 2658 | if (!di) { |
| 2659 | ret = -ENOENT; |
| 2660 | goto err; |
| 2661 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2662 | leaf = path->nodes[0]; |
| 2663 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2664 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2665 | if (ret) |
| 2666 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2667 | btrfs_release_path(root, path); |
| 2668 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2669 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2670 | inode->i_ino, |
| 2671 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2672 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2673 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2674 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2675 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2676 | goto err; |
| 2677 | } |
| 2678 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2679 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2680 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2681 | if (IS_ERR(di)) { |
| 2682 | ret = PTR_ERR(di); |
| 2683 | goto err; |
| 2684 | } |
| 2685 | if (!di) { |
| 2686 | ret = -ENOENT; |
| 2687 | goto err; |
| 2688 | } |
| 2689 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2690 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2691 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2692 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 2693 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2694 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2695 | |
| 2696 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2697 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 2698 | if (ret == -ENOENT) |
| 2699 | ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2700 | err: |
| 2701 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2702 | if (ret) |
| 2703 | goto out; |
| 2704 | |
| 2705 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2706 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2707 | btrfs_update_inode(trans, root, dir); |
| 2708 | btrfs_drop_nlink(inode); |
| 2709 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2710 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2711 | return ret; |
| 2712 | } |
| 2713 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2714 | /* helper to check if there is any shared block in the path */ |
| 2715 | static int check_path_shared(struct btrfs_root *root, |
| 2716 | struct btrfs_path *path) |
| 2717 | { |
| 2718 | struct extent_buffer *eb; |
| 2719 | int level; |
Dan Carpenter | 0e4dcbef | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 2720 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2721 | |
| 2722 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame^] | 2723 | int ret; |
| 2724 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2725 | if (!path->nodes[level]) |
| 2726 | break; |
| 2727 | eb = path->nodes[level]; |
| 2728 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2729 | continue; |
| 2730 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2731 | &refs, NULL); |
| 2732 | if (refs > 1) |
| 2733 | return 1; |
| 2734 | } |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame^] | 2735 | return 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | /* |
| 2739 | * helper to start transaction for unlink and rmdir. |
| 2740 | * |
| 2741 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2742 | * so in enospc case, we should make sure they will free space before |
| 2743 | * allowing them to use the global metadata reservation. |
| 2744 | */ |
| 2745 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2746 | struct dentry *dentry) |
| 2747 | { |
| 2748 | struct btrfs_trans_handle *trans; |
| 2749 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2750 | struct btrfs_path *path; |
| 2751 | struct btrfs_inode_ref *ref; |
| 2752 | struct btrfs_dir_item *di; |
| 2753 | struct inode *inode = dentry->d_inode; |
| 2754 | u64 index; |
| 2755 | int check_link = 1; |
| 2756 | int err = -ENOSPC; |
| 2757 | int ret; |
| 2758 | |
| 2759 | trans = btrfs_start_transaction(root, 10); |
| 2760 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 2761 | return trans; |
| 2762 | |
| 2763 | if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 2764 | return ERR_PTR(-ENOSPC); |
| 2765 | |
| 2766 | /* check if there is someone else holds reference */ |
| 2767 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 2768 | return ERR_PTR(-ENOSPC); |
| 2769 | |
| 2770 | if (atomic_read(&inode->i_count) > 2) |
| 2771 | return ERR_PTR(-ENOSPC); |
| 2772 | |
| 2773 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 2774 | return ERR_PTR(-ENOSPC); |
| 2775 | |
| 2776 | path = btrfs_alloc_path(); |
| 2777 | if (!path) { |
| 2778 | root->fs_info->enospc_unlink = 0; |
| 2779 | return ERR_PTR(-ENOMEM); |
| 2780 | } |
| 2781 | |
| 2782 | trans = btrfs_start_transaction(root, 0); |
| 2783 | if (IS_ERR(trans)) { |
| 2784 | btrfs_free_path(path); |
| 2785 | root->fs_info->enospc_unlink = 0; |
| 2786 | return trans; |
| 2787 | } |
| 2788 | |
| 2789 | path->skip_locking = 1; |
| 2790 | path->search_commit_root = 1; |
| 2791 | |
| 2792 | ret = btrfs_lookup_inode(trans, root, path, |
| 2793 | &BTRFS_I(dir)->location, 0); |
| 2794 | if (ret < 0) { |
| 2795 | err = ret; |
| 2796 | goto out; |
| 2797 | } |
| 2798 | if (ret == 0) { |
| 2799 | if (check_path_shared(root, path)) |
| 2800 | goto out; |
| 2801 | } else { |
| 2802 | check_link = 0; |
| 2803 | } |
| 2804 | btrfs_release_path(root, path); |
| 2805 | |
| 2806 | ret = btrfs_lookup_inode(trans, root, path, |
| 2807 | &BTRFS_I(inode)->location, 0); |
| 2808 | if (ret < 0) { |
| 2809 | err = ret; |
| 2810 | goto out; |
| 2811 | } |
| 2812 | if (ret == 0) { |
| 2813 | if (check_path_shared(root, path)) |
| 2814 | goto out; |
| 2815 | } else { |
| 2816 | check_link = 0; |
| 2817 | } |
| 2818 | btrfs_release_path(root, path); |
| 2819 | |
| 2820 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 2821 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 2822 | inode->i_ino, (u64)-1, 0); |
| 2823 | if (ret < 0) { |
| 2824 | err = ret; |
| 2825 | goto out; |
| 2826 | } |
| 2827 | BUG_ON(ret == 0); |
| 2828 | if (check_path_shared(root, path)) |
| 2829 | goto out; |
| 2830 | btrfs_release_path(root, path); |
| 2831 | } |
| 2832 | |
| 2833 | if (!check_link) { |
| 2834 | err = 0; |
| 2835 | goto out; |
| 2836 | } |
| 2837 | |
| 2838 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2839 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2840 | if (IS_ERR(di)) { |
| 2841 | err = PTR_ERR(di); |
| 2842 | goto out; |
| 2843 | } |
| 2844 | if (di) { |
| 2845 | if (check_path_shared(root, path)) |
| 2846 | goto out; |
| 2847 | } else { |
| 2848 | err = 0; |
| 2849 | goto out; |
| 2850 | } |
| 2851 | btrfs_release_path(root, path); |
| 2852 | |
| 2853 | ref = btrfs_lookup_inode_ref(trans, root, path, |
| 2854 | dentry->d_name.name, dentry->d_name.len, |
| 2855 | inode->i_ino, dir->i_ino, 0); |
| 2856 | if (IS_ERR(ref)) { |
| 2857 | err = PTR_ERR(ref); |
| 2858 | goto out; |
| 2859 | } |
| 2860 | BUG_ON(!ref); |
| 2861 | if (check_path_shared(root, path)) |
| 2862 | goto out; |
| 2863 | index = btrfs_inode_ref_index(path->nodes[0], ref); |
| 2864 | btrfs_release_path(root, path); |
| 2865 | |
| 2866 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index, |
| 2867 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2868 | if (IS_ERR(di)) { |
| 2869 | err = PTR_ERR(di); |
| 2870 | goto out; |
| 2871 | } |
| 2872 | BUG_ON(ret == -ENOENT); |
| 2873 | if (check_path_shared(root, path)) |
| 2874 | goto out; |
| 2875 | |
| 2876 | err = 0; |
| 2877 | out: |
| 2878 | btrfs_free_path(path); |
| 2879 | if (err) { |
| 2880 | btrfs_end_transaction(trans, root); |
| 2881 | root->fs_info->enospc_unlink = 0; |
| 2882 | return ERR_PTR(err); |
| 2883 | } |
| 2884 | |
| 2885 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 2886 | return trans; |
| 2887 | } |
| 2888 | |
| 2889 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 2890 | struct btrfs_root *root) |
| 2891 | { |
| 2892 | if (trans->block_rsv == &root->fs_info->global_block_rsv) { |
| 2893 | BUG_ON(!root->fs_info->enospc_unlink); |
| 2894 | root->fs_info->enospc_unlink = 0; |
| 2895 | } |
| 2896 | btrfs_end_transaction_throttle(trans, root); |
| 2897 | } |
| 2898 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2899 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2900 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2901 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2902 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2903 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2904 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2905 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2906 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2907 | trans = __unlink_start_trans(dir, dentry); |
| 2908 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2909 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2910 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2911 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2912 | |
| 2913 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2914 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2915 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2916 | dentry->d_name.name, dentry->d_name.len); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2917 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2918 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2919 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2920 | ret = btrfs_orphan_add(trans, inode); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2921 | BUG_ON(ret); |
| 2922 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2923 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2924 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2925 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2926 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2927 | return ret; |
| 2928 | } |
| 2929 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2930 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2931 | struct btrfs_root *root, |
| 2932 | struct inode *dir, u64 objectid, |
| 2933 | const char *name, int name_len) |
| 2934 | { |
| 2935 | struct btrfs_path *path; |
| 2936 | struct extent_buffer *leaf; |
| 2937 | struct btrfs_dir_item *di; |
| 2938 | struct btrfs_key key; |
| 2939 | u64 index; |
| 2940 | int ret; |
| 2941 | |
| 2942 | path = btrfs_alloc_path(); |
| 2943 | if (!path) |
| 2944 | return -ENOMEM; |
| 2945 | |
| 2946 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 2947 | name, name_len, -1); |
| 2948 | BUG_ON(!di || IS_ERR(di)); |
| 2949 | |
| 2950 | leaf = path->nodes[0]; |
| 2951 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2952 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2953 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2954 | BUG_ON(ret); |
| 2955 | btrfs_release_path(root, path); |
| 2956 | |
| 2957 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2958 | objectid, root->root_key.objectid, |
| 2959 | dir->i_ino, &index, name, name_len); |
| 2960 | if (ret < 0) { |
| 2961 | BUG_ON(ret != -ENOENT); |
| 2962 | di = btrfs_search_dir_index_item(root, path, dir->i_ino, |
| 2963 | name, name_len); |
| 2964 | BUG_ON(!di || IS_ERR(di)); |
| 2965 | |
| 2966 | leaf = path->nodes[0]; |
| 2967 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2968 | btrfs_release_path(root, path); |
| 2969 | index = key.offset; |
| 2970 | } |
| 2971 | |
| 2972 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 2973 | index, name, name_len, -1); |
| 2974 | BUG_ON(!di || IS_ERR(di)); |
| 2975 | |
| 2976 | leaf = path->nodes[0]; |
| 2977 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2978 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2979 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2980 | BUG_ON(ret); |
| 2981 | btrfs_release_path(root, path); |
| 2982 | |
| 2983 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2984 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2985 | ret = btrfs_update_inode(trans, root, dir); |
| 2986 | BUG_ON(ret); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2987 | |
| 2988 | btrfs_free_path(path); |
| 2989 | return 0; |
| 2990 | } |
| 2991 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2992 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 2993 | { |
| 2994 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2995 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2996 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2997 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2998 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2999 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 3000 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3001 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3002 | return -ENOTEMPTY; |
| 3003 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3004 | trans = __unlink_start_trans(dir, dentry); |
| 3005 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3006 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3007 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3008 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3009 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3010 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 3011 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3012 | BTRFS_I(inode)->location.objectid, |
| 3013 | dentry->d_name.name, |
| 3014 | dentry->d_name.len); |
| 3015 | goto out; |
| 3016 | } |
| 3017 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3018 | err = btrfs_orphan_add(trans, inode); |
| 3019 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3020 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3021 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3022 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3023 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3024 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3025 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3026 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3027 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3028 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3029 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3030 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3031 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3032 | return err; |
| 3033 | } |
| 3034 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3035 | #if 0 |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3036 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3037 | * when truncating bytes in a file, it is possible to avoid reading |
| 3038 | * the leaves that contain only checksum items. This can be the |
| 3039 | * majority of the IO required to delete a large file, but it must |
| 3040 | * be done carefully. |
| 3041 | * |
| 3042 | * The keys in the level just above the leaves are checked to make sure |
| 3043 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 3044 | * after the new size. |
| 3045 | * |
| 3046 | * Then the key for the next leaf is checked to make sure it also has |
| 3047 | * a checksum item for the same file. If it does, we know our target leaf |
| 3048 | * contains only checksum items, and it can be safely freed without reading |
| 3049 | * it. |
| 3050 | * |
| 3051 | * This is just an optimization targeted at large files. It may do |
| 3052 | * nothing. It will return 0 unless things went badly. |
| 3053 | */ |
| 3054 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 3055 | struct btrfs_root *root, |
| 3056 | struct btrfs_path *path, |
| 3057 | struct inode *inode, u64 new_size) |
| 3058 | { |
| 3059 | struct btrfs_key key; |
| 3060 | int ret; |
| 3061 | int nritems; |
| 3062 | struct btrfs_key found_key; |
| 3063 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3064 | struct btrfs_leaf_ref *ref; |
| 3065 | u64 leaf_gen; |
| 3066 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3067 | |
| 3068 | path->lowest_level = 1; |
| 3069 | key.objectid = inode->i_ino; |
| 3070 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 3071 | key.offset = new_size; |
| 3072 | again: |
| 3073 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3074 | if (ret < 0) |
| 3075 | goto out; |
| 3076 | |
| 3077 | if (path->nodes[1] == NULL) { |
| 3078 | ret = 0; |
| 3079 | goto out; |
| 3080 | } |
| 3081 | ret = 0; |
| 3082 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 3083 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 3084 | |
| 3085 | if (!nritems) |
| 3086 | goto out; |
| 3087 | |
| 3088 | if (path->slots[1] >= nritems) |
| 3089 | goto next_node; |
| 3090 | |
| 3091 | /* did we find a key greater than anything we want to delete? */ |
| 3092 | if (found_key.objectid > inode->i_ino || |
| 3093 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 3094 | goto out; |
| 3095 | |
| 3096 | /* we check the next key in the node to make sure the leave contains |
| 3097 | * only checksum items. This comparison doesn't work if our |
| 3098 | * leaf is the last one in the node |
| 3099 | */ |
| 3100 | if (path->slots[1] + 1 >= nritems) { |
| 3101 | next_node: |
| 3102 | /* search forward from the last key in the node, this |
| 3103 | * will bring us into the next node in the tree |
| 3104 | */ |
| 3105 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 3106 | |
| 3107 | /* unlikely, but we inc below, so check to be safe */ |
| 3108 | if (found_key.offset == (u64)-1) |
| 3109 | goto out; |
| 3110 | |
| 3111 | /* search_forward needs a path with locks held, do the |
| 3112 | * search again for the original key. It is possible |
| 3113 | * this will race with a balance and return a path that |
| 3114 | * we could modify, but this drop is just an optimization |
| 3115 | * and is allowed to miss some leaves. |
| 3116 | */ |
| 3117 | btrfs_release_path(root, path); |
| 3118 | found_key.offset++; |
| 3119 | |
| 3120 | /* setup a max key for search_forward */ |
| 3121 | other_key.offset = (u64)-1; |
| 3122 | other_key.type = key.type; |
| 3123 | other_key.objectid = key.objectid; |
| 3124 | |
| 3125 | path->keep_locks = 1; |
| 3126 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 3127 | path, 0, 0); |
| 3128 | path->keep_locks = 0; |
| 3129 | if (ret || found_key.objectid != key.objectid || |
| 3130 | found_key.type != key.type) { |
| 3131 | ret = 0; |
| 3132 | goto out; |
| 3133 | } |
| 3134 | |
| 3135 | key.offset = found_key.offset; |
| 3136 | btrfs_release_path(root, path); |
| 3137 | cond_resched(); |
| 3138 | goto again; |
| 3139 | } |
| 3140 | |
| 3141 | /* we know there's one more slot after us in the tree, |
| 3142 | * read that key so we can verify it is also a checksum item |
| 3143 | */ |
| 3144 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 3145 | |
| 3146 | if (found_key.objectid < inode->i_ino) |
| 3147 | goto next_key; |
| 3148 | |
| 3149 | if (found_key.type != key.type || found_key.offset < new_size) |
| 3150 | goto next_key; |
| 3151 | |
| 3152 | /* |
| 3153 | * if the key for the next leaf isn't a csum key from this objectid, |
| 3154 | * we can't be sure there aren't good items inside this leaf. |
| 3155 | * Bail out |
| 3156 | */ |
| 3157 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 3158 | goto out; |
| 3159 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3160 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 3161 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3162 | /* |
| 3163 | * it is safe to delete this leaf, it contains only |
| 3164 | * csum items from this inode at an offset >= new_size |
| 3165 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3166 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3167 | BUG_ON(ret); |
| 3168 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3169 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 3170 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 3171 | if (ref) { |
| 3172 | ref->root_gen = root->root_key.offset; |
| 3173 | ref->bytenr = leaf_start; |
| 3174 | ref->owner = 0; |
| 3175 | ref->generation = leaf_gen; |
| 3176 | ref->nritems = 0; |
| 3177 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3178 | btrfs_sort_leaf_ref(ref); |
| 3179 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 3180 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 3181 | WARN_ON(ret); |
| 3182 | btrfs_free_leaf_ref(root, ref); |
| 3183 | } else { |
| 3184 | WARN_ON(1); |
| 3185 | } |
| 3186 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3187 | next_key: |
| 3188 | btrfs_release_path(root, path); |
| 3189 | |
| 3190 | if (other_key.objectid == inode->i_ino && |
| 3191 | other_key.type == key.type && other_key.offset > key.offset) { |
| 3192 | key.offset = other_key.offset; |
| 3193 | cond_resched(); |
| 3194 | goto again; |
| 3195 | } |
| 3196 | ret = 0; |
| 3197 | out: |
| 3198 | /* fixup any changes we've made to the path */ |
| 3199 | path->lowest_level = 0; |
| 3200 | path->keep_locks = 0; |
| 3201 | btrfs_release_path(root, path); |
| 3202 | return ret; |
| 3203 | } |
| 3204 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3205 | #endif |
| 3206 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3207 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3208 | * this can truncate away extent items, csum items and directory items. |
| 3209 | * 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] | 3210 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3211 | * |
| 3212 | * csum items that cross the new i_size are truncated to the new size |
| 3213 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3214 | * |
| 3215 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3216 | * 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] | 3217 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3218 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3219 | struct btrfs_root *root, |
| 3220 | struct inode *inode, |
| 3221 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3222 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3223 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3224 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3225 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3226 | struct btrfs_key key; |
| 3227 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3228 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3229 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3230 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3231 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3232 | u64 mask = root->sectorsize - 1; |
| 3233 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3234 | int found_extent; |
| 3235 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3236 | int pending_del_nr = 0; |
| 3237 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3238 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3239 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3240 | int ret; |
| 3241 | int err = 0; |
| 3242 | |
| 3243 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3244 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3245 | if (root->ref_cows || root == root->fs_info->tree_root) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3246 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3247 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3248 | path = btrfs_alloc_path(); |
| 3249 | BUG_ON(!path); |
Julia Lawall | 33c17ad | 2009-07-22 16:49:01 -0400 | [diff] [blame] | 3250 | path->reada = -1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3251 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3252 | key.objectid = inode->i_ino; |
| 3253 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3254 | key.type = (u8)-1; |
| 3255 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3256 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3257 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3258 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3259 | if (ret < 0) { |
| 3260 | err = ret; |
| 3261 | goto out; |
| 3262 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3263 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3264 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3265 | /* there are no items in the tree for us to truncate, we're |
| 3266 | * done |
| 3267 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3268 | if (path->slots[0] == 0) |
| 3269 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3270 | path->slots[0]--; |
| 3271 | } |
| 3272 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3273 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3274 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3275 | leaf = path->nodes[0]; |
| 3276 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3277 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3278 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3279 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3280 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3281 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3282 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3283 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3284 | break; |
| 3285 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3286 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3287 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3288 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3289 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3290 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3291 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 3292 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 3293 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 3294 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3295 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3296 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3297 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3298 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3299 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3300 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3301 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3302 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3303 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3304 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3305 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3306 | } else { |
| 3307 | if (item_end < new_size) |
| 3308 | break; |
| 3309 | if (found_key.offset >= new_size) |
| 3310 | del_item = 1; |
| 3311 | else |
| 3312 | del_item = 0; |
| 3313 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3314 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3315 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3316 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3317 | goto delete; |
| 3318 | |
| 3319 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3320 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3321 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3322 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3323 | u64 orig_num_bytes = |
| 3324 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3325 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3326 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3327 | extent_num_bytes = extent_num_bytes & |
| 3328 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3329 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3330 | extent_num_bytes); |
| 3331 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3332 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3333 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3334 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3335 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3336 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3337 | extent_num_bytes = |
| 3338 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3339 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3340 | extent_offset = found_key.offset - |
| 3341 | btrfs_file_extent_offset(leaf, fi); |
| 3342 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3343 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3344 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3345 | if (extent_start != 0) { |
| 3346 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3347 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3348 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3349 | } |
| 3350 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3351 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3352 | /* |
| 3353 | * we can't truncate inline items that have had |
| 3354 | * special encodings |
| 3355 | */ |
| 3356 | if (!del_item && |
| 3357 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3358 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3359 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3360 | u32 size = new_size - found_key.offset; |
| 3361 | |
| 3362 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3363 | inode_sub_bytes(inode, item_end + 1 - |
| 3364 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3365 | } |
| 3366 | size = |
| 3367 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3368 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3369 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3370 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3371 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3372 | inode_sub_bytes(inode, item_end + 1 - |
| 3373 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3374 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3375 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3376 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3377 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3378 | if (!pending_del_nr) { |
| 3379 | /* no pending yet, add ourselves */ |
| 3380 | pending_del_slot = path->slots[0]; |
| 3381 | pending_del_nr = 1; |
| 3382 | } else if (pending_del_nr && |
| 3383 | path->slots[0] + 1 == pending_del_slot) { |
| 3384 | /* hop on the pending chunk */ |
| 3385 | pending_del_nr++; |
| 3386 | pending_del_slot = path->slots[0]; |
| 3387 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3388 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3389 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3390 | } else { |
| 3391 | break; |
| 3392 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3393 | if (found_extent && (root->ref_cows || |
| 3394 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3395 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3396 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3397 | extent_num_bytes, 0, |
| 3398 | btrfs_header_owner(leaf), |
| 3399 | inode->i_ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3400 | BUG_ON(ret); |
| 3401 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3402 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3403 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3404 | break; |
| 3405 | |
| 3406 | if (path->slots[0] == 0 || |
| 3407 | path->slots[0] != pending_del_slot) { |
| 3408 | if (root->ref_cows) { |
| 3409 | err = -EAGAIN; |
| 3410 | goto out; |
| 3411 | } |
| 3412 | if (pending_del_nr) { |
| 3413 | ret = btrfs_del_items(trans, root, path, |
| 3414 | pending_del_slot, |
| 3415 | pending_del_nr); |
| 3416 | BUG_ON(ret); |
| 3417 | pending_del_nr = 0; |
| 3418 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3419 | btrfs_release_path(root, path); |
| 3420 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3421 | } else { |
| 3422 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3423 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3424 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3425 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3426 | if (pending_del_nr) { |
| 3427 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3428 | pending_del_nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3429 | BUG_ON(ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3430 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3431 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3432 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3433 | } |
| 3434 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3435 | /* |
| 3436 | * taken from block_truncate_page, but does cow as it zeros out |
| 3437 | * any bytes left in the last page in the file. |
| 3438 | */ |
| 3439 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3440 | { |
| 3441 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3442 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3443 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3444 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3445 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3446 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3447 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3448 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3449 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3450 | struct page *page; |
| 3451 | int ret = 0; |
| 3452 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3453 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3454 | |
| 3455 | if ((offset & (blocksize - 1)) == 0) |
| 3456 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3457 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3458 | if (ret) |
| 3459 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3460 | |
| 3461 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3462 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3463 | page = grab_cache_page(mapping, index); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3464 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3465 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3466 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3467 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3468 | |
| 3469 | page_start = page_offset(page); |
| 3470 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3471 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3472 | if (!PageUptodate(page)) { |
| 3473 | ret = btrfs_readpage(NULL, page); |
| 3474 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3475 | if (page->mapping != mapping) { |
| 3476 | unlock_page(page); |
| 3477 | page_cache_release(page); |
| 3478 | goto again; |
| 3479 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3480 | if (!PageUptodate(page)) { |
| 3481 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3482 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3483 | } |
| 3484 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3485 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3486 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3487 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 3488 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3489 | set_page_extent_mapped(page); |
| 3490 | |
| 3491 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3492 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3493 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3494 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3495 | unlock_page(page); |
| 3496 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3497 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3498 | btrfs_put_ordered_extent(ordered); |
| 3499 | goto again; |
| 3500 | } |
| 3501 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3502 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3503 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3504 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3505 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3506 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3507 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3508 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3509 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3510 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3511 | goto out_unlock; |
| 3512 | } |
| 3513 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3514 | ret = 0; |
| 3515 | if (offset != PAGE_CACHE_SIZE) { |
| 3516 | kaddr = kmap(page); |
| 3517 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3518 | flush_dcache_page(page); |
| 3519 | kunmap(page); |
| 3520 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3521 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3522 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3523 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3524 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3525 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3526 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3527 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3528 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3529 | unlock_page(page); |
| 3530 | page_cache_release(page); |
| 3531 | out: |
| 3532 | return ret; |
| 3533 | } |
| 3534 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3535 | int btrfs_cont_expand(struct inode *inode, loff_t size) |
| 3536 | { |
| 3537 | struct btrfs_trans_handle *trans; |
| 3538 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3539 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3540 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3541 | struct extent_state *cached_state = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3542 | u64 mask = root->sectorsize - 1; |
| 3543 | u64 hole_start = (inode->i_size + mask) & ~mask; |
| 3544 | u64 block_end = (size + mask) & ~mask; |
| 3545 | u64 last_byte; |
| 3546 | u64 cur_offset; |
| 3547 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3548 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3549 | |
| 3550 | if (size <= hole_start) |
| 3551 | return 0; |
| 3552 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3553 | while (1) { |
| 3554 | struct btrfs_ordered_extent *ordered; |
| 3555 | btrfs_wait_ordered_range(inode, hole_start, |
| 3556 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3557 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
| 3558 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3559 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3560 | if (!ordered) |
| 3561 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3562 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3563 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3564 | btrfs_put_ordered_extent(ordered); |
| 3565 | } |
| 3566 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3567 | cur_offset = hole_start; |
| 3568 | while (1) { |
| 3569 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3570 | block_end - cur_offset, 0); |
| 3571 | BUG_ON(IS_ERR(em) || !em); |
| 3572 | last_byte = min(extent_map_end(em), block_end); |
| 3573 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3574 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3575 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3576 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3577 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3578 | trans = btrfs_start_transaction(root, 2); |
| 3579 | if (IS_ERR(trans)) { |
| 3580 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3581 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3582 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3583 | btrfs_set_trans_block_group(trans, inode); |
| 3584 | |
| 3585 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3586 | cur_offset + hole_size, |
| 3587 | &hint_byte, 1); |
| 3588 | BUG_ON(err); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3589 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3590 | err = btrfs_insert_file_extent(trans, root, |
| 3591 | inode->i_ino, cur_offset, 0, |
| 3592 | 0, hole_size, 0, hole_size, |
| 3593 | 0, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3594 | BUG_ON(err); |
| 3595 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3596 | btrfs_drop_extent_cache(inode, hole_start, |
| 3597 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3598 | |
| 3599 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3600 | } |
| 3601 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3602 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3603 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3604 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3605 | break; |
| 3606 | } |
| 3607 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3608 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3609 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3610 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3611 | return err; |
| 3612 | } |
| 3613 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3614 | static int btrfs_setattr_size(struct inode *inode, struct iattr *attr) |
| 3615 | { |
| 3616 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3617 | struct btrfs_trans_handle *trans; |
| 3618 | unsigned long nr; |
| 3619 | int ret; |
| 3620 | |
| 3621 | if (attr->ia_size == inode->i_size) |
| 3622 | return 0; |
| 3623 | |
| 3624 | if (attr->ia_size > inode->i_size) { |
| 3625 | unsigned long limit; |
| 3626 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
| 3627 | if (attr->ia_size > inode->i_sb->s_maxbytes) |
| 3628 | return -EFBIG; |
| 3629 | if (limit != RLIM_INFINITY && attr->ia_size > limit) { |
| 3630 | send_sig(SIGXFSZ, current, 0); |
| 3631 | return -EFBIG; |
| 3632 | } |
| 3633 | } |
| 3634 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3635 | trans = btrfs_start_transaction(root, 5); |
| 3636 | if (IS_ERR(trans)) |
| 3637 | return PTR_ERR(trans); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3638 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3639 | btrfs_set_trans_block_group(trans, inode); |
| 3640 | |
| 3641 | ret = btrfs_orphan_add(trans, inode); |
| 3642 | BUG_ON(ret); |
| 3643 | |
| 3644 | nr = trans->blocks_used; |
| 3645 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3646 | btrfs_btree_balance_dirty(root, nr); |
| 3647 | |
| 3648 | if (attr->ia_size > inode->i_size) { |
| 3649 | ret = btrfs_cont_expand(inode, attr->ia_size); |
| 3650 | if (ret) { |
| 3651 | btrfs_truncate(inode); |
| 3652 | return ret; |
| 3653 | } |
| 3654 | |
| 3655 | i_size_write(inode, attr->ia_size); |
| 3656 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
| 3657 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3658 | trans = btrfs_start_transaction(root, 0); |
| 3659 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3660 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3661 | trans->block_rsv = root->orphan_block_rsv; |
| 3662 | BUG_ON(!trans->block_rsv); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3663 | |
| 3664 | ret = btrfs_update_inode(trans, root, inode); |
| 3665 | BUG_ON(ret); |
| 3666 | if (inode->i_nlink > 0) { |
| 3667 | ret = btrfs_orphan_del(trans, inode); |
| 3668 | BUG_ON(ret); |
| 3669 | } |
| 3670 | nr = trans->blocks_used; |
| 3671 | btrfs_end_transaction(trans, root); |
| 3672 | btrfs_btree_balance_dirty(root, nr); |
| 3673 | return 0; |
| 3674 | } |
| 3675 | |
| 3676 | /* |
| 3677 | * We're truncating a file that used to have good data down to |
| 3678 | * zero. Make sure it gets into the ordered flush list so that |
| 3679 | * any new writes get down to disk quickly. |
| 3680 | */ |
| 3681 | if (attr->ia_size == 0) |
| 3682 | BTRFS_I(inode)->ordered_data_close = 1; |
| 3683 | |
| 3684 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3685 | ret = vmtruncate(inode, attr->ia_size); |
| 3686 | BUG_ON(ret); |
| 3687 | |
| 3688 | return 0; |
| 3689 | } |
| 3690 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3691 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3692 | { |
| 3693 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3694 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3695 | int err; |
| 3696 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3697 | if (btrfs_root_readonly(root)) |
| 3698 | return -EROFS; |
| 3699 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3700 | err = inode_change_ok(inode, attr); |
| 3701 | if (err) |
| 3702 | return err; |
| 3703 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3704 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3705 | err = btrfs_setattr_size(inode, attr); |
| 3706 | if (err) |
| 3707 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3708 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3709 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3710 | if (attr->ia_valid) { |
| 3711 | setattr_copy(inode, attr); |
| 3712 | mark_inode_dirty(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3713 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3714 | if (attr->ia_valid & ATTR_MODE) |
| 3715 | err = btrfs_acl_chmod(inode); |
| 3716 | } |
| 3717 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3718 | return err; |
| 3719 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3720 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3721 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3722 | { |
| 3723 | struct btrfs_trans_handle *trans; |
| 3724 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3725 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3726 | int ret; |
| 3727 | |
| 3728 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3729 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
| 3730 | root == root->fs_info->tree_root)) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3731 | goto no_delete; |
| 3732 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3733 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3734 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3735 | goto no_delete; |
| 3736 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3737 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3738 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3739 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3740 | if (root->fs_info->log_root_recovering) { |
| 3741 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3742 | goto no_delete; |
| 3743 | } |
| 3744 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3745 | if (inode->i_nlink > 0) { |
| 3746 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3747 | goto no_delete; |
| 3748 | } |
| 3749 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3750 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3751 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3752 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3753 | trans = btrfs_start_transaction(root, 0); |
| 3754 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3755 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3756 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3757 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3758 | ret = btrfs_block_rsv_check(trans, root, |
| 3759 | root->orphan_block_rsv, 0, 5); |
| 3760 | if (ret) { |
| 3761 | BUG_ON(ret != -EAGAIN); |
| 3762 | ret = btrfs_commit_transaction(trans, root); |
| 3763 | BUG_ON(ret); |
| 3764 | continue; |
| 3765 | } |
| 3766 | |
| 3767 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3768 | if (ret != -EAGAIN) |
| 3769 | break; |
| 3770 | |
| 3771 | nr = trans->blocks_used; |
| 3772 | btrfs_end_transaction(trans, root); |
| 3773 | trans = NULL; |
| 3774 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3775 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3776 | } |
| 3777 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3778 | if (ret == 0) { |
| 3779 | ret = btrfs_orphan_del(trans, inode); |
| 3780 | BUG_ON(ret); |
| 3781 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3782 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3783 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3784 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3785 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3786 | no_delete: |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3787 | end_writeback(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3788 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | /* |
| 3792 | * this returns the key found in the dir entry in the location pointer. |
| 3793 | * If no dir entries were found, location->objectid is 0. |
| 3794 | */ |
| 3795 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3796 | struct btrfs_key *location) |
| 3797 | { |
| 3798 | const char *name = dentry->d_name.name; |
| 3799 | int namelen = dentry->d_name.len; |
| 3800 | struct btrfs_dir_item *di; |
| 3801 | struct btrfs_path *path; |
| 3802 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3803 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3804 | |
| 3805 | path = btrfs_alloc_path(); |
| 3806 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3807 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3808 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 3809 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3810 | if (IS_ERR(di)) |
| 3811 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3812 | |
| 3813 | if (!di || IS_ERR(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3814 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3815 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3816 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3817 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3818 | btrfs_free_path(path); |
| 3819 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3820 | out_err: |
| 3821 | location->objectid = 0; |
| 3822 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | /* |
| 3826 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3827 | * needs to be changed to reflect the root directory of the tree root. This |
| 3828 | * is kind of like crossing a mount point. |
| 3829 | */ |
| 3830 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3831 | struct inode *dir, |
| 3832 | struct dentry *dentry, |
| 3833 | struct btrfs_key *location, |
| 3834 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3835 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3836 | struct btrfs_path *path; |
| 3837 | struct btrfs_root *new_root; |
| 3838 | struct btrfs_root_ref *ref; |
| 3839 | struct extent_buffer *leaf; |
| 3840 | int ret; |
| 3841 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3842 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3843 | path = btrfs_alloc_path(); |
| 3844 | if (!path) { |
| 3845 | err = -ENOMEM; |
| 3846 | goto out; |
| 3847 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3848 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3849 | err = -ENOENT; |
| 3850 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3851 | BTRFS_I(dir)->root->root_key.objectid, |
| 3852 | location->objectid); |
| 3853 | if (ret) { |
| 3854 | if (ret < 0) |
| 3855 | err = ret; |
| 3856 | goto out; |
| 3857 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3858 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3859 | leaf = path->nodes[0]; |
| 3860 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 3861 | if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || |
| 3862 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3863 | goto out; |
| 3864 | |
| 3865 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3866 | (unsigned long)(ref + 1), |
| 3867 | dentry->d_name.len); |
| 3868 | if (ret) |
| 3869 | goto out; |
| 3870 | |
| 3871 | btrfs_release_path(root->fs_info->tree_root, path); |
| 3872 | |
| 3873 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3874 | if (IS_ERR(new_root)) { |
| 3875 | err = PTR_ERR(new_root); |
| 3876 | goto out; |
| 3877 | } |
| 3878 | |
| 3879 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3880 | err = -ENOENT; |
| 3881 | goto out; |
| 3882 | } |
| 3883 | |
| 3884 | *sub_root = new_root; |
| 3885 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3886 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3887 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3888 | err = 0; |
| 3889 | out: |
| 3890 | btrfs_free_path(path); |
| 3891 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3892 | } |
| 3893 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3894 | static void inode_tree_add(struct inode *inode) |
| 3895 | { |
| 3896 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3897 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3898 | struct rb_node **p; |
| 3899 | struct rb_node *parent; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3900 | again: |
| 3901 | p = &root->inode_tree.rb_node; |
| 3902 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3903 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3904 | if (hlist_unhashed(&inode->i_hash)) |
| 3905 | return; |
| 3906 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3907 | spin_lock(&root->inode_lock); |
| 3908 | while (*p) { |
| 3909 | parent = *p; |
| 3910 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3911 | |
| 3912 | if (inode->i_ino < entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3913 | p = &parent->rb_left; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3914 | else if (inode->i_ino > entry->vfs_inode.i_ino) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3915 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3916 | else { |
| 3917 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 3918 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3919 | rb_erase(parent, &root->inode_tree); |
| 3920 | RB_CLEAR_NODE(parent); |
| 3921 | spin_unlock(&root->inode_lock); |
| 3922 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3926 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3927 | spin_unlock(&root->inode_lock); |
| 3928 | } |
| 3929 | |
| 3930 | static void inode_tree_del(struct inode *inode) |
| 3931 | { |
| 3932 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3933 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3934 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3935 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3936 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3937 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3938 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3939 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3940 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3941 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3942 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3943 | /* |
| 3944 | * Free space cache has inodes in the tree root, but the tree root has a |
| 3945 | * root_refs of 0, so this could end up dropping the tree root as a |
| 3946 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 3947 | * make sure we don't drop it. |
| 3948 | */ |
| 3949 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 3950 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3951 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3952 | spin_lock(&root->inode_lock); |
| 3953 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3954 | spin_unlock(&root->inode_lock); |
| 3955 | if (empty) |
| 3956 | btrfs_add_dead_root(root); |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 3961 | { |
| 3962 | struct rb_node *node; |
| 3963 | struct rb_node *prev; |
| 3964 | struct btrfs_inode *entry; |
| 3965 | struct inode *inode; |
| 3966 | u64 objectid = 0; |
| 3967 | |
| 3968 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3969 | |
| 3970 | spin_lock(&root->inode_lock); |
| 3971 | again: |
| 3972 | node = root->inode_tree.rb_node; |
| 3973 | prev = NULL; |
| 3974 | while (node) { |
| 3975 | prev = node; |
| 3976 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3977 | |
| 3978 | if (objectid < entry->vfs_inode.i_ino) |
| 3979 | node = node->rb_left; |
| 3980 | else if (objectid > entry->vfs_inode.i_ino) |
| 3981 | node = node->rb_right; |
| 3982 | else |
| 3983 | break; |
| 3984 | } |
| 3985 | if (!node) { |
| 3986 | while (prev) { |
| 3987 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
| 3988 | if (objectid <= entry->vfs_inode.i_ino) { |
| 3989 | node = prev; |
| 3990 | break; |
| 3991 | } |
| 3992 | prev = rb_next(prev); |
| 3993 | } |
| 3994 | } |
| 3995 | while (node) { |
| 3996 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3997 | objectid = entry->vfs_inode.i_ino + 1; |
| 3998 | inode = igrab(&entry->vfs_inode); |
| 3999 | if (inode) { |
| 4000 | spin_unlock(&root->inode_lock); |
| 4001 | if (atomic_read(&inode->i_count) > 1) |
| 4002 | d_prune_aliases(inode); |
| 4003 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4004 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4005 | * the inode cache when its usage count |
| 4006 | * hits zero. |
| 4007 | */ |
| 4008 | iput(inode); |
| 4009 | cond_resched(); |
| 4010 | spin_lock(&root->inode_lock); |
| 4011 | goto again; |
| 4012 | } |
| 4013 | |
| 4014 | if (cond_resched_lock(&root->inode_lock)) |
| 4015 | goto again; |
| 4016 | |
| 4017 | node = rb_next(node); |
| 4018 | } |
| 4019 | spin_unlock(&root->inode_lock); |
| 4020 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4021 | } |
| 4022 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4023 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4024 | { |
| 4025 | struct btrfs_iget_args *args = p; |
| 4026 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4027 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4028 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4029 | return 0; |
| 4030 | } |
| 4031 | |
| 4032 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4033 | { |
| 4034 | struct btrfs_iget_args *args = opaque; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4035 | return args->ino == inode->i_ino && |
| 4036 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4037 | } |
| 4038 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4039 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4040 | u64 objectid, |
| 4041 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4042 | { |
| 4043 | struct inode *inode; |
| 4044 | struct btrfs_iget_args args; |
| 4045 | args.ino = objectid; |
| 4046 | args.root = root; |
| 4047 | |
| 4048 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 4049 | btrfs_init_locked_inode, |
| 4050 | (void *)&args); |
| 4051 | return inode; |
| 4052 | } |
| 4053 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4054 | /* Get an inode object given its location and corresponding root. |
| 4055 | * Returns in *is_new if the inode was read from disk |
| 4056 | */ |
| 4057 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4058 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4059 | { |
| 4060 | struct inode *inode; |
| 4061 | |
| 4062 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4063 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4064 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4065 | |
| 4066 | if (inode->i_state & I_NEW) { |
| 4067 | BTRFS_I(inode)->root = root; |
| 4068 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4069 | btrfs_read_locked_inode(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4070 | |
| 4071 | inode_tree_add(inode); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4072 | unlock_new_inode(inode); |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4073 | if (new) |
| 4074 | *new = 1; |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4075 | } |
| 4076 | |
| 4077 | return inode; |
| 4078 | } |
| 4079 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4080 | static struct inode *new_simple_dir(struct super_block *s, |
| 4081 | struct btrfs_key *key, |
| 4082 | struct btrfs_root *root) |
| 4083 | { |
| 4084 | struct inode *inode = new_inode(s); |
| 4085 | |
| 4086 | if (!inode) |
| 4087 | return ERR_PTR(-ENOMEM); |
| 4088 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4089 | BTRFS_I(inode)->root = root; |
| 4090 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 4091 | BTRFS_I(inode)->dummy_inode = 1; |
| 4092 | |
| 4093 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 4094 | inode->i_op = &simple_dir_inode_operations; |
| 4095 | inode->i_fop = &simple_dir_operations; |
| 4096 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 4097 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 4098 | |
| 4099 | return inode; |
| 4100 | } |
| 4101 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4102 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4103 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4104 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4105 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4106 | struct btrfs_root *sub_root = root; |
| 4107 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4108 | int index; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4109 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4110 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4111 | dentry->d_op = &btrfs_dentry_operations; |
| 4112 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4113 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 4114 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4115 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4116 | ret = btrfs_inode_by_name(dir, dentry, &location); |
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 | if (ret < 0) |
| 4119 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4120 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4121 | if (location.objectid == 0) |
| 4122 | return NULL; |
| 4123 | |
| 4124 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4125 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4126 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4127 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4128 | |
| 4129 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4130 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4131 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4132 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4133 | &location, &sub_root); |
| 4134 | if (ret < 0) { |
| 4135 | if (ret != -ENOENT) |
| 4136 | inode = ERR_PTR(ret); |
| 4137 | else |
| 4138 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4139 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4140 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4141 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4142 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4143 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 4144 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4145 | down_read(&root->fs_info->cleanup_work_sem); |
| 4146 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
| 4147 | btrfs_orphan_cleanup(sub_root); |
| 4148 | up_read(&root->fs_info->cleanup_work_sem); |
| 4149 | } |
| 4150 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4151 | return inode; |
| 4152 | } |
| 4153 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4154 | static int btrfs_dentry_delete(struct dentry *dentry) |
| 4155 | { |
| 4156 | struct btrfs_root *root; |
| 4157 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4158 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 4159 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4160 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4161 | if (dentry->d_inode) { |
| 4162 | root = BTRFS_I(dentry->d_inode)->root; |
| 4163 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4164 | return 1; |
| 4165 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4166 | return 0; |
| 4167 | } |
| 4168 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4169 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 4170 | struct nameidata *nd) |
| 4171 | { |
| 4172 | struct inode *inode; |
| 4173 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4174 | inode = btrfs_lookup_dentry(dir, dentry); |
| 4175 | if (IS_ERR(inode)) |
| 4176 | return ERR_CAST(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4177 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4178 | return d_splice_alias(inode, dentry); |
| 4179 | } |
| 4180 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4181 | static unsigned char btrfs_filetype_table[] = { |
| 4182 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 4183 | }; |
| 4184 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 4185 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 4186 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4187 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4188 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4189 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4190 | struct btrfs_item *item; |
| 4191 | struct btrfs_dir_item *di; |
| 4192 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4193 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4194 | struct btrfs_path *path; |
| 4195 | int ret; |
| 4196 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4197 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4198 | int slot; |
| 4199 | int advance; |
| 4200 | unsigned char d_type; |
| 4201 | int over = 0; |
| 4202 | u32 di_cur; |
| 4203 | u32 di_total; |
| 4204 | u32 di_len; |
| 4205 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4206 | char tmp_name[32]; |
| 4207 | char *name_ptr; |
| 4208 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4209 | |
| 4210 | /* FIXME, use a real flag for deciding about the key type */ |
| 4211 | if (root->fs_info->tree_root == root) |
| 4212 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4213 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4214 | /* special case for "." */ |
| 4215 | if (filp->f_pos == 0) { |
| 4216 | over = filldir(dirent, ".", 1, |
| 4217 | 1, inode->i_ino, |
| 4218 | DT_DIR); |
| 4219 | if (over) |
| 4220 | return 0; |
| 4221 | filp->f_pos = 1; |
| 4222 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4223 | /* special case for .., just use the back ref */ |
| 4224 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4225 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4226 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4227 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4228 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4229 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4230 | filp->f_pos = 2; |
| 4231 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4232 | path = btrfs_alloc_path(); |
| 4233 | path->reada = 2; |
| 4234 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4235 | btrfs_set_key_type(&key, key_type); |
| 4236 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4237 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4238 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4239 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4240 | if (ret < 0) |
| 4241 | goto err; |
| 4242 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4243 | |
| 4244 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4245 | leaf = path->nodes[0]; |
| 4246 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4247 | slot = path->slots[0]; |
| 4248 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4249 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4250 | ret = btrfs_next_leaf(root, path); |
| 4251 | if (ret) |
| 4252 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4253 | leaf = path->nodes[0]; |
| 4254 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4255 | slot = path->slots[0]; |
| 4256 | } else { |
| 4257 | slot++; |
| 4258 | path->slots[0]++; |
| 4259 | } |
| 4260 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4261 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4262 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4263 | item = btrfs_item_nr(leaf, slot); |
| 4264 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4265 | |
| 4266 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4267 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4268 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4269 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4270 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4271 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4272 | |
| 4273 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4274 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4275 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 4276 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4277 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4278 | |
| 4279 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4280 | struct btrfs_key location; |
| 4281 | |
| 4282 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4283 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4284 | name_ptr = tmp_name; |
| 4285 | } else { |
| 4286 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4287 | if (!name_ptr) { |
| 4288 | ret = -ENOMEM; |
| 4289 | goto err; |
| 4290 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4291 | } |
| 4292 | read_extent_buffer(leaf, name_ptr, |
| 4293 | (unsigned long)(di + 1), name_len); |
| 4294 | |
| 4295 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4296 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4297 | |
| 4298 | /* is this a reference to our own snapshot? If so |
| 4299 | * skip it |
| 4300 | */ |
| 4301 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4302 | location.objectid == root->root_key.objectid) { |
| 4303 | over = 0; |
| 4304 | goto skip; |
| 4305 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4306 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4307 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4308 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4309 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4310 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4311 | if (name_ptr != tmp_name) |
| 4312 | kfree(name_ptr); |
| 4313 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4314 | if (over) |
| 4315 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4316 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4317 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4318 | di_cur += di_len; |
| 4319 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4320 | } |
| 4321 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4322 | |
| 4323 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4324 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4325 | /* |
| 4326 | * 32-bit glibc will use getdents64, but then strtol - |
| 4327 | * so the last number we can serve is this. |
| 4328 | */ |
| 4329 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4330 | else |
| 4331 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4332 | nopos: |
| 4333 | ret = 0; |
| 4334 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4335 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4336 | return ret; |
| 4337 | } |
| 4338 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4339 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4340 | { |
| 4341 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4342 | struct btrfs_trans_handle *trans; |
| 4343 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4344 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4345 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4346 | if (BTRFS_I(inode)->dummy_inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4347 | return 0; |
| 4348 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4349 | smp_mb(); |
| 4350 | nolock = (root->fs_info->closing && root == root->fs_info->tree_root); |
| 4351 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4352 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4353 | if (nolock) |
| 4354 | trans = btrfs_join_transaction_nolock(root, 1); |
| 4355 | else |
| 4356 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4357 | if (IS_ERR(trans)) |
| 4358 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4359 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4360 | if (nolock) |
| 4361 | ret = btrfs_end_transaction_nolock(trans, root); |
| 4362 | else |
| 4363 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4364 | } |
| 4365 | return ret; |
| 4366 | } |
| 4367 | |
| 4368 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4369 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4370 | * inode changes. But, it is most likely to find the inode in cache. |
| 4371 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4372 | * to keep or drop this code. |
| 4373 | */ |
| 4374 | void btrfs_dirty_inode(struct inode *inode) |
| 4375 | { |
| 4376 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4377 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4378 | int ret; |
| 4379 | |
| 4380 | if (BTRFS_I(inode)->dummy_inode) |
| 4381 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4382 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 4383 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4384 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4385 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4386 | |
| 4387 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4388 | if (ret && ret == -ENOSPC) { |
| 4389 | /* whoops, lets try again with the full transaction */ |
| 4390 | btrfs_end_transaction(trans, root); |
| 4391 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4392 | if (IS_ERR(trans)) { |
| 4393 | if (printk_ratelimit()) { |
| 4394 | printk(KERN_ERR "btrfs: fail to " |
| 4395 | "dirty inode %lu error %ld\n", |
| 4396 | inode->i_ino, PTR_ERR(trans)); |
| 4397 | } |
| 4398 | return; |
| 4399 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4400 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4401 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4402 | ret = btrfs_update_inode(trans, root, inode); |
| 4403 | if (ret) { |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4404 | if (printk_ratelimit()) { |
| 4405 | printk(KERN_ERR "btrfs: fail to " |
| 4406 | "dirty inode %lu error %d\n", |
| 4407 | inode->i_ino, ret); |
| 4408 | } |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4409 | } |
| 4410 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4411 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4412 | } |
| 4413 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4414 | /* |
| 4415 | * find the highest existing sequence number in a directory |
| 4416 | * and then set the in-memory index_cnt variable to reflect |
| 4417 | * free sequence numbers |
| 4418 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4419 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4420 | { |
| 4421 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4422 | struct btrfs_key key, found_key; |
| 4423 | struct btrfs_path *path; |
| 4424 | struct extent_buffer *leaf; |
| 4425 | int ret; |
| 4426 | |
| 4427 | key.objectid = inode->i_ino; |
| 4428 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4429 | key.offset = (u64)-1; |
| 4430 | |
| 4431 | path = btrfs_alloc_path(); |
| 4432 | if (!path) |
| 4433 | return -ENOMEM; |
| 4434 | |
| 4435 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4436 | if (ret < 0) |
| 4437 | goto out; |
| 4438 | /* FIXME: we should be able to handle this */ |
| 4439 | if (ret == 0) |
| 4440 | goto out; |
| 4441 | ret = 0; |
| 4442 | |
| 4443 | /* |
| 4444 | * MAGIC NUMBER EXPLANATION: |
| 4445 | * since we search a directory based on f_pos we have to start at 2 |
| 4446 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4447 | * else has to start at 2 |
| 4448 | */ |
| 4449 | if (path->slots[0] == 0) { |
| 4450 | BTRFS_I(inode)->index_cnt = 2; |
| 4451 | goto out; |
| 4452 | } |
| 4453 | |
| 4454 | path->slots[0]--; |
| 4455 | |
| 4456 | leaf = path->nodes[0]; |
| 4457 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4458 | |
| 4459 | if (found_key.objectid != inode->i_ino || |
| 4460 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4461 | BTRFS_I(inode)->index_cnt = 2; |
| 4462 | goto out; |
| 4463 | } |
| 4464 | |
| 4465 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4466 | out: |
| 4467 | btrfs_free_path(path); |
| 4468 | return ret; |
| 4469 | } |
| 4470 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4471 | /* |
| 4472 | * helper to find a free sequence number in a given directory. This current |
| 4473 | * code is very simple, later versions will do smarter things in the btree |
| 4474 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4475 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4476 | { |
| 4477 | int ret = 0; |
| 4478 | |
| 4479 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 4480 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4481 | if (ret) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4482 | return ret; |
| 4483 | } |
| 4484 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4485 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4486 | BTRFS_I(dir)->index_cnt++; |
| 4487 | |
| 4488 | return ret; |
| 4489 | } |
| 4490 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4491 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4492 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4493 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4494 | const char *name, int name_len, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4495 | u64 ref_objectid, u64 objectid, |
| 4496 | u64 alloc_hint, int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4497 | { |
| 4498 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4499 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4500 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4501 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4502 | struct btrfs_inode_ref *ref; |
| 4503 | struct btrfs_key key[2]; |
| 4504 | u32 sizes[2]; |
| 4505 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4506 | int ret; |
| 4507 | int owner; |
| 4508 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4509 | path = btrfs_alloc_path(); |
| 4510 | BUG_ON(!path); |
| 4511 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4512 | inode = new_inode(root->fs_info->sb); |
| 4513 | if (!inode) |
| 4514 | return ERR_PTR(-ENOMEM); |
| 4515 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4516 | if (dir) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4517 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4518 | if (ret) { |
| 4519 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4520 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4521 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4522 | } |
| 4523 | /* |
| 4524 | * index_cnt is ignored for everything but a dir, |
| 4525 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4526 | * number |
| 4527 | */ |
| 4528 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4529 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4530 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 4531 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4532 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4533 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4534 | if (mode & S_IFDIR) |
| 4535 | owner = 0; |
| 4536 | else |
| 4537 | owner = 1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4538 | BTRFS_I(inode)->block_group = |
| 4539 | btrfs_find_block_group(root, 0, alloc_hint, owner); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4540 | |
| 4541 | key[0].objectid = objectid; |
| 4542 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4543 | key[0].offset = 0; |
| 4544 | |
| 4545 | key[1].objectid = objectid; |
| 4546 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4547 | key[1].offset = ref_objectid; |
| 4548 | |
| 4549 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4550 | sizes[1] = name_len + sizeof(*ref); |
| 4551 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4552 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4553 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4554 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4555 | goto fail; |
| 4556 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 4557 | inode_init_owner(inode, dir, mode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4558 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4559 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4560 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4561 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4562 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4563 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4564 | |
| 4565 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4566 | struct btrfs_inode_ref); |
| 4567 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4568 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4569 | ptr = (unsigned long)(ref + 1); |
| 4570 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4571 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4572 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4573 | btrfs_free_path(path); |
| 4574 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4575 | location = &BTRFS_I(inode)->location; |
| 4576 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4577 | location->offset = 0; |
| 4578 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4579 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4580 | btrfs_inherit_iflags(inode, dir); |
| 4581 | |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4582 | if ((mode & S_IFREG)) { |
| 4583 | if (btrfs_test_opt(root, NODATASUM)) |
| 4584 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 4585 | if (btrfs_test_opt(root, NODATACOW)) |
| 4586 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4587 | } |
| 4588 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4589 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4590 | inode_tree_add(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4591 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4592 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4593 | if (dir) |
| 4594 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4595 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4596 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4597 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4601 | { |
| 4602 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4603 | } |
| 4604 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4605 | /* |
| 4606 | * utility function to add 'inode' into 'parent_inode' with |
| 4607 | * a give name and a given sequence number. |
| 4608 | * if 'add_backref' is true, also insert a backref from the |
| 4609 | * inode to the parent directory. |
| 4610 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4611 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4612 | struct inode *parent_inode, struct inode *inode, |
| 4613 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4614 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4615 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4616 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4617 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4618 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4619 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4620 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4621 | } else { |
| 4622 | key.objectid = inode->i_ino; |
| 4623 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4624 | key.offset = 0; |
| 4625 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4626 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4627 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4628 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4629 | key.objectid, root->root_key.objectid, |
| 4630 | parent_inode->i_ino, |
| 4631 | index, name, name_len); |
| 4632 | } else if (add_backref) { |
| 4633 | ret = btrfs_insert_inode_ref(trans, root, |
| 4634 | name, name_len, inode->i_ino, |
| 4635 | parent_inode->i_ino, index); |
| 4636 | } |
| 4637 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4638 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4639 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4640 | parent_inode->i_ino, &key, |
| 4641 | btrfs_inode_type(inode), index); |
| 4642 | BUG_ON(ret); |
| 4643 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4644 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4645 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4646 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4647 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4648 | } |
| 4649 | return ret; |
| 4650 | } |
| 4651 | |
| 4652 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4653 | struct inode *dir, struct dentry *dentry, |
| 4654 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4655 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4656 | int err = btrfs_add_link(trans, dir, inode, |
| 4657 | dentry->d_name.name, dentry->d_name.len, |
| 4658 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4659 | if (!err) { |
| 4660 | d_instantiate(dentry, inode); |
| 4661 | return 0; |
| 4662 | } |
| 4663 | if (err > 0) |
| 4664 | err = -EEXIST; |
| 4665 | return err; |
| 4666 | } |
| 4667 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4668 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4669 | int mode, dev_t rdev) |
| 4670 | { |
| 4671 | struct btrfs_trans_handle *trans; |
| 4672 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4673 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4674 | int err; |
| 4675 | int drop_inode = 0; |
| 4676 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4677 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4678 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4679 | |
| 4680 | if (!new_valid_dev(rdev)) |
| 4681 | return -EINVAL; |
| 4682 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4683 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4684 | if (err) |
| 4685 | return err; |
| 4686 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4687 | /* |
| 4688 | * 2 for inode item and ref |
| 4689 | * 2 for dir items |
| 4690 | * 1 for xattr if selinux is on |
| 4691 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4692 | trans = btrfs_start_transaction(root, 5); |
| 4693 | if (IS_ERR(trans)) |
| 4694 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4695 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4696 | btrfs_set_trans_block_group(trans, dir); |
| 4697 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4698 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4699 | dentry->d_name.len, dir->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4700 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4701 | err = PTR_ERR(inode); |
| 4702 | if (IS_ERR(inode)) |
| 4703 | goto out_unlock; |
| 4704 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4705 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4706 | if (err) { |
| 4707 | drop_inode = 1; |
| 4708 | goto out_unlock; |
| 4709 | } |
| 4710 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4711 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4712 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4713 | if (err) |
| 4714 | drop_inode = 1; |
| 4715 | else { |
| 4716 | inode->i_op = &btrfs_special_inode_operations; |
| 4717 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4718 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4719 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4720 | btrfs_update_inode_block_group(trans, inode); |
| 4721 | btrfs_update_inode_block_group(trans, dir); |
| 4722 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4723 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4724 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4725 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4726 | if (drop_inode) { |
| 4727 | inode_dec_link_count(inode); |
| 4728 | iput(inode); |
| 4729 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4730 | return err; |
| 4731 | } |
| 4732 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4733 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4734 | int mode, struct nameidata *nd) |
| 4735 | { |
| 4736 | struct btrfs_trans_handle *trans; |
| 4737 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4738 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4739 | int drop_inode = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4740 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4741 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4742 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4743 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4744 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4745 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4746 | if (err) |
| 4747 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4748 | /* |
| 4749 | * 2 for inode item and ref |
| 4750 | * 2 for dir items |
| 4751 | * 1 for xattr if selinux is on |
| 4752 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4753 | trans = btrfs_start_transaction(root, 5); |
| 4754 | if (IS_ERR(trans)) |
| 4755 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4756 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4757 | btrfs_set_trans_block_group(trans, dir); |
| 4758 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4759 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4760 | dentry->d_name.len, dir->i_ino, objectid, |
| 4761 | BTRFS_I(dir)->block_group, mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4762 | err = PTR_ERR(inode); |
| 4763 | if (IS_ERR(inode)) |
| 4764 | goto out_unlock; |
| 4765 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4766 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4767 | if (err) { |
| 4768 | drop_inode = 1; |
| 4769 | goto out_unlock; |
| 4770 | } |
| 4771 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4772 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4773 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4774 | if (err) |
| 4775 | drop_inode = 1; |
| 4776 | else { |
| 4777 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4778 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4779 | inode->i_fop = &btrfs_file_operations; |
| 4780 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4781 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4782 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4783 | btrfs_update_inode_block_group(trans, inode); |
| 4784 | btrfs_update_inode_block_group(trans, dir); |
| 4785 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4786 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4787 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4788 | if (drop_inode) { |
| 4789 | inode_dec_link_count(inode); |
| 4790 | iput(inode); |
| 4791 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4792 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4793 | return err; |
| 4794 | } |
| 4795 | |
| 4796 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4797 | struct dentry *dentry) |
| 4798 | { |
| 4799 | struct btrfs_trans_handle *trans; |
| 4800 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4801 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4802 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4803 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4804 | int err; |
| 4805 | int drop_inode = 0; |
| 4806 | |
| 4807 | if (inode->i_nlink == 0) |
| 4808 | return -ENOENT; |
| 4809 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4810 | /* do not allow sys_link's with other subvols of the same device */ |
| 4811 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
| 4812 | return -EPERM; |
| 4813 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4814 | btrfs_inc_nlink(inode); |
Josef Bacik | bc1cbf1 | 2010-11-23 19:50:59 +0000 | [diff] [blame] | 4815 | inode->i_ctime = CURRENT_TIME; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4816 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4817 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4818 | if (err) |
| 4819 | goto fail; |
| 4820 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4821 | /* |
| 4822 | * 1 item for inode ref |
| 4823 | * 2 items for dir items |
| 4824 | */ |
| 4825 | trans = btrfs_start_transaction(root, 3); |
| 4826 | if (IS_ERR(trans)) { |
| 4827 | err = PTR_ERR(trans); |
| 4828 | goto fail; |
| 4829 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4830 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4831 | btrfs_set_trans_block_group(trans, dir); |
| 4832 | atomic_inc(&inode->i_count); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4833 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4834 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4835 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4836 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4837 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4838 | } else { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4839 | struct dentry *parent = dget_parent(dentry); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4840 | btrfs_update_inode_block_group(trans, dir); |
| 4841 | err = btrfs_update_inode(trans, root, inode); |
| 4842 | BUG_ON(err); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4843 | btrfs_log_new_name(trans, inode, NULL, parent); |
| 4844 | dput(parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4845 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4846 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4847 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4848 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4849 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4850 | if (drop_inode) { |
| 4851 | inode_dec_link_count(inode); |
| 4852 | iput(inode); |
| 4853 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4854 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4855 | return err; |
| 4856 | } |
| 4857 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4858 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4859 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4860 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4861 | struct btrfs_trans_handle *trans; |
| 4862 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4863 | int err = 0; |
| 4864 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4865 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4866 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4867 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4868 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4869 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 4870 | if (err) |
| 4871 | return err; |
| 4872 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4873 | /* |
| 4874 | * 2 items for inode and ref |
| 4875 | * 2 items for dir items |
| 4876 | * 1 for xattr if selinux is on |
| 4877 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4878 | trans = btrfs_start_transaction(root, 5); |
| 4879 | if (IS_ERR(trans)) |
| 4880 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4881 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4882 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4883 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4884 | dentry->d_name.len, dir->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4885 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 4886 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4887 | if (IS_ERR(inode)) { |
| 4888 | err = PTR_ERR(inode); |
| 4889 | goto out_fail; |
| 4890 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4891 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4892 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4893 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 4894 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4895 | if (err) |
| 4896 | goto out_fail; |
| 4897 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4898 | inode->i_op = &btrfs_dir_inode_operations; |
| 4899 | inode->i_fop = &btrfs_dir_file_operations; |
| 4900 | btrfs_set_trans_block_group(trans, inode); |
| 4901 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4902 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4903 | err = btrfs_update_inode(trans, root, inode); |
| 4904 | if (err) |
| 4905 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4906 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4907 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 4908 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4909 | if (err) |
| 4910 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4911 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4912 | d_instantiate(dentry, inode); |
| 4913 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4914 | btrfs_update_inode_block_group(trans, inode); |
| 4915 | btrfs_update_inode_block_group(trans, dir); |
| 4916 | |
| 4917 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4918 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4919 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4920 | if (drop_on_err) |
| 4921 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4922 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4923 | return err; |
| 4924 | } |
| 4925 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4926 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4927 | * and an extent that you want to insert, deal with overlap and insert |
| 4928 | * the new extent into the tree. |
| 4929 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4930 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4931 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4932 | struct extent_map *em, |
| 4933 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4934 | { |
| 4935 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4936 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4937 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4938 | start_diff = map_start - em->start; |
| 4939 | em->start = map_start; |
| 4940 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4941 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4942 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4943 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4944 | em->block_len -= start_diff; |
| 4945 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4946 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4947 | } |
| 4948 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4949 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4950 | struct inode *inode, struct page *page, |
| 4951 | size_t pg_offset, u64 extent_offset, |
| 4952 | struct btrfs_file_extent_item *item) |
| 4953 | { |
| 4954 | int ret; |
| 4955 | struct extent_buffer *leaf = path->nodes[0]; |
| 4956 | char *tmp; |
| 4957 | size_t max_size; |
| 4958 | unsigned long inline_size; |
| 4959 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4960 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4961 | |
| 4962 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4963 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4964 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4965 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4966 | btrfs_item_nr(leaf, path->slots[0])); |
| 4967 | tmp = kmalloc(inline_size, GFP_NOFS); |
| 4968 | ptr = btrfs_file_extent_inline_start(item); |
| 4969 | |
| 4970 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4971 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 4972 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4973 | ret = btrfs_decompress(compress_type, tmp, page, |
| 4974 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4975 | if (ret) { |
| 4976 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 4977 | unsigned long copy_size = min_t(u64, |
| 4978 | PAGE_CACHE_SIZE - pg_offset, |
| 4979 | max_size - extent_offset); |
| 4980 | memset(kaddr + pg_offset, 0, copy_size); |
| 4981 | kunmap_atomic(kaddr, KM_USER0); |
| 4982 | } |
| 4983 | kfree(tmp); |
| 4984 | return 0; |
| 4985 | } |
| 4986 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4987 | /* |
| 4988 | * 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] | 4989 | * the ugly parts come from merging extents from the disk with the in-ram |
| 4990 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4991 | * where the in-ram extents might be locked pending data=ordered completion. |
| 4992 | * |
| 4993 | * This also copies inline extents directly into the page. |
| 4994 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4995 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4996 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4997 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4998 | int create) |
| 4999 | { |
| 5000 | int ret; |
| 5001 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5002 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5003 | u64 extent_start = 0; |
| 5004 | u64 extent_end = 0; |
| 5005 | u64 objectid = inode->i_ino; |
| 5006 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5007 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5008 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5009 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5010 | struct extent_buffer *leaf; |
| 5011 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5012 | struct extent_map *em = NULL; |
| 5013 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5014 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5015 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5016 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5017 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5018 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5019 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5020 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5021 | if (em) |
| 5022 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5023 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5024 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5025 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5026 | if (em->start > start || em->start + em->len <= start) |
| 5027 | free_extent_map(em); |
| 5028 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5029 | free_extent_map(em); |
| 5030 | else |
| 5031 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5032 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5033 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5034 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5035 | err = -ENOMEM; |
| 5036 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5037 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5038 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5039 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 5040 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5041 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5042 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5043 | |
| 5044 | if (!path) { |
| 5045 | path = btrfs_alloc_path(); |
| 5046 | BUG_ON(!path); |
| 5047 | } |
| 5048 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5049 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 5050 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5051 | if (ret < 0) { |
| 5052 | err = ret; |
| 5053 | goto out; |
| 5054 | } |
| 5055 | |
| 5056 | if (ret != 0) { |
| 5057 | if (path->slots[0] == 0) |
| 5058 | goto not_found; |
| 5059 | path->slots[0]--; |
| 5060 | } |
| 5061 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5062 | leaf = path->nodes[0]; |
| 5063 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5064 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5065 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5066 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5067 | found_type = btrfs_key_type(&found_key); |
| 5068 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5069 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 5070 | goto not_found; |
| 5071 | } |
| 5072 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5073 | found_type = btrfs_file_extent_type(leaf, item); |
| 5074 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5075 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5076 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5077 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5078 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5079 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5080 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5081 | size_t size; |
| 5082 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5083 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 5084 | ~((u64)root->sectorsize - 1); |
| 5085 | } |
| 5086 | |
| 5087 | if (start >= extent_end) { |
| 5088 | path->slots[0]++; |
| 5089 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 5090 | ret = btrfs_next_leaf(root, path); |
| 5091 | if (ret < 0) { |
| 5092 | err = ret; |
| 5093 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5094 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5095 | if (ret > 0) |
| 5096 | goto not_found; |
| 5097 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5098 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5099 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5100 | if (found_key.objectid != objectid || |
| 5101 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 5102 | goto not_found; |
| 5103 | if (start + len <= found_key.offset) |
| 5104 | goto not_found; |
| 5105 | em->start = start; |
| 5106 | em->len = found_key.offset - start; |
| 5107 | goto not_found_em; |
| 5108 | } |
| 5109 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5110 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5111 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5112 | em->start = extent_start; |
| 5113 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5114 | em->orig_start = extent_start - |
| 5115 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5116 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 5117 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5118 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5119 | goto insert; |
| 5120 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5121 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5122 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5123 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5124 | em->block_start = bytenr; |
| 5125 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 5126 | item); |
| 5127 | } else { |
| 5128 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 5129 | em->block_start = bytenr; |
| 5130 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5131 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 5132 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5133 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5134 | goto insert; |
| 5135 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5136 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5137 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5138 | size_t size; |
| 5139 | size_t extent_offset; |
| 5140 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5141 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5142 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5143 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5144 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5145 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5146 | goto out; |
| 5147 | } |
| 5148 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5149 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5150 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5151 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5152 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5153 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5154 | em->len = (copy_size + root->sectorsize - 1) & |
| 5155 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5156 | em->orig_start = EXTENT_MAP_INLINE; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5157 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5158 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5159 | em->compress_type = compress_type; |
| 5160 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5161 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5162 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5163 | if (btrfs_file_extent_compression(leaf, item) != |
| 5164 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5165 | ret = uncompress_inline(path, inode, page, |
| 5166 | pg_offset, |
| 5167 | extent_offset, item); |
| 5168 | BUG_ON(ret); |
| 5169 | } else { |
| 5170 | map = kmap(page); |
| 5171 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 5172 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 5173 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 5174 | memset(map + pg_offset + copy_size, 0, |
| 5175 | PAGE_CACHE_SIZE - pg_offset - |
| 5176 | copy_size); |
| 5177 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5178 | kunmap(page); |
| 5179 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5180 | flush_dcache_page(page); |
| 5181 | } else if (create && PageUptodate(page)) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5182 | WARN_ON(1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5183 | if (!trans) { |
| 5184 | kunmap(page); |
| 5185 | free_extent_map(em); |
| 5186 | em = NULL; |
| 5187 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 5188 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5189 | if (IS_ERR(trans)) |
| 5190 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5191 | goto again; |
| 5192 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5193 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5194 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5195 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5196 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5197 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5198 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5199 | set_extent_uptodate(io_tree, em->start, |
| 5200 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5201 | goto insert; |
| 5202 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5203 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5204 | WARN_ON(1); |
| 5205 | } |
| 5206 | not_found: |
| 5207 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5208 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5209 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5210 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5211 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5212 | insert: |
| 5213 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5214 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5215 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 5216 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 5217 | (unsigned long long)em->len, |
| 5218 | (unsigned long long)start, |
| 5219 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5220 | err = -EIO; |
| 5221 | goto out; |
| 5222 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5223 | |
| 5224 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5225 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5226 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5227 | /* it is possible that someone inserted the extent into the tree |
| 5228 | * while we had the lock dropped. It is also possible that |
| 5229 | * an overlapping map exists in the tree |
| 5230 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5231 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5232 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5233 | |
| 5234 | ret = 0; |
| 5235 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5236 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5237 | if (existing && (existing->start > start || |
| 5238 | existing->start + existing->len <= start)) { |
| 5239 | free_extent_map(existing); |
| 5240 | existing = NULL; |
| 5241 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5242 | if (!existing) { |
| 5243 | existing = lookup_extent_mapping(em_tree, em->start, |
| 5244 | em->len); |
| 5245 | if (existing) { |
| 5246 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5247 | em, start, |
| 5248 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5249 | free_extent_map(existing); |
| 5250 | if (err) { |
| 5251 | free_extent_map(em); |
| 5252 | em = NULL; |
| 5253 | } |
| 5254 | } else { |
| 5255 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5256 | free_extent_map(em); |
| 5257 | em = NULL; |
| 5258 | } |
| 5259 | } else { |
| 5260 | free_extent_map(em); |
| 5261 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5262 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5263 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5264 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5265 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5266 | out: |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5267 | if (path) |
| 5268 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5269 | if (trans) { |
| 5270 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5271 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5272 | err = ret; |
| 5273 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5274 | if (err) { |
| 5275 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5276 | return ERR_PTR(err); |
| 5277 | } |
| 5278 | return em; |
| 5279 | } |
| 5280 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5281 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 5282 | u64 start, u64 len) |
| 5283 | { |
| 5284 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5285 | struct btrfs_trans_handle *trans; |
| 5286 | struct extent_map *em; |
| 5287 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 5288 | struct btrfs_key ins; |
| 5289 | u64 alloc_hint; |
| 5290 | int ret; |
| 5291 | |
| 5292 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
| 5293 | |
| 5294 | trans = btrfs_join_transaction(root, 0); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5295 | if (IS_ERR(trans)) |
| 5296 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5297 | |
| 5298 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5299 | |
| 5300 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 5301 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
| 5302 | alloc_hint, (u64)-1, &ins, 1); |
| 5303 | if (ret) { |
| 5304 | em = ERR_PTR(ret); |
| 5305 | goto out; |
| 5306 | } |
| 5307 | |
| 5308 | em = alloc_extent_map(GFP_NOFS); |
| 5309 | if (!em) { |
| 5310 | em = ERR_PTR(-ENOMEM); |
| 5311 | goto out; |
| 5312 | } |
| 5313 | |
| 5314 | em->start = start; |
| 5315 | em->orig_start = em->start; |
| 5316 | em->len = ins.offset; |
| 5317 | |
| 5318 | em->block_start = ins.objectid; |
| 5319 | em->block_len = ins.offset; |
| 5320 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 5321 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5322 | |
| 5323 | while (1) { |
| 5324 | write_lock(&em_tree->lock); |
| 5325 | ret = add_extent_mapping(em_tree, em); |
| 5326 | write_unlock(&em_tree->lock); |
| 5327 | if (ret != -EEXIST) |
| 5328 | break; |
| 5329 | btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0); |
| 5330 | } |
| 5331 | |
| 5332 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 5333 | ins.offset, ins.offset, 0); |
| 5334 | if (ret) { |
| 5335 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 5336 | em = ERR_PTR(ret); |
| 5337 | } |
| 5338 | out: |
| 5339 | btrfs_end_transaction(trans, root); |
| 5340 | return em; |
| 5341 | } |
| 5342 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5343 | /* |
| 5344 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 5345 | * block must be cow'd |
| 5346 | */ |
| 5347 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 5348 | struct inode *inode, u64 offset, u64 len) |
| 5349 | { |
| 5350 | struct btrfs_path *path; |
| 5351 | int ret; |
| 5352 | struct extent_buffer *leaf; |
| 5353 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5354 | struct btrfs_file_extent_item *fi; |
| 5355 | struct btrfs_key key; |
| 5356 | u64 disk_bytenr; |
| 5357 | u64 backref_offset; |
| 5358 | u64 extent_end; |
| 5359 | u64 num_bytes; |
| 5360 | int slot; |
| 5361 | int found_type; |
| 5362 | |
| 5363 | path = btrfs_alloc_path(); |
| 5364 | if (!path) |
| 5365 | return -ENOMEM; |
| 5366 | |
| 5367 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 5368 | offset, 0); |
| 5369 | if (ret < 0) |
| 5370 | goto out; |
| 5371 | |
| 5372 | slot = path->slots[0]; |
| 5373 | if (ret == 1) { |
| 5374 | if (slot == 0) { |
| 5375 | /* can't find the item, must cow */ |
| 5376 | ret = 0; |
| 5377 | goto out; |
| 5378 | } |
| 5379 | slot--; |
| 5380 | } |
| 5381 | ret = 0; |
| 5382 | leaf = path->nodes[0]; |
| 5383 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5384 | if (key.objectid != inode->i_ino || |
| 5385 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5386 | /* not our file or wrong item type, must cow */ |
| 5387 | goto out; |
| 5388 | } |
| 5389 | |
| 5390 | if (key.offset > offset) { |
| 5391 | /* Wrong offset, must cow */ |
| 5392 | goto out; |
| 5393 | } |
| 5394 | |
| 5395 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5396 | found_type = btrfs_file_extent_type(leaf, fi); |
| 5397 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 5398 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 5399 | /* not a regular extent, must cow */ |
| 5400 | goto out; |
| 5401 | } |
| 5402 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5403 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 5404 | |
| 5405 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 5406 | if (extent_end < offset + len) { |
| 5407 | /* extent doesn't include our full range, must cow */ |
| 5408 | goto out; |
| 5409 | } |
| 5410 | |
| 5411 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 5412 | goto out; |
| 5413 | |
| 5414 | /* |
| 5415 | * look for other files referencing this extent, if we |
| 5416 | * find any we must cow |
| 5417 | */ |
| 5418 | if (btrfs_cross_ref_exist(trans, root, inode->i_ino, |
| 5419 | key.offset - backref_offset, disk_bytenr)) |
| 5420 | goto out; |
| 5421 | |
| 5422 | /* |
| 5423 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 5424 | * in this extent we are about to write. If there |
| 5425 | * are any csums in that range we have to cow in order |
| 5426 | * to keep the csums correct |
| 5427 | */ |
| 5428 | disk_bytenr += backref_offset; |
| 5429 | disk_bytenr += offset - key.offset; |
| 5430 | num_bytes = min(offset + len, extent_end) - offset; |
| 5431 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 5432 | goto out; |
| 5433 | /* |
| 5434 | * all of the above have passed, it is safe to overwrite this extent |
| 5435 | * without cow |
| 5436 | */ |
| 5437 | ret = 1; |
| 5438 | out: |
| 5439 | btrfs_free_path(path); |
| 5440 | return ret; |
| 5441 | } |
| 5442 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5443 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 5444 | struct buffer_head *bh_result, int create) |
| 5445 | { |
| 5446 | struct extent_map *em; |
| 5447 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5448 | u64 start = iblock << inode->i_blkbits; |
| 5449 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5450 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5451 | |
| 5452 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
| 5453 | if (IS_ERR(em)) |
| 5454 | return PTR_ERR(em); |
| 5455 | |
| 5456 | /* |
| 5457 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 5458 | * io. INLINE is special, and we could probably kludge it in here, but |
| 5459 | * it's still buffered so for safety lets just fall back to the generic |
| 5460 | * buffered path. |
| 5461 | * |
| 5462 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 5463 | * decompress it, so there will be buffering required no matter what we |
| 5464 | * do, so go ahead and fallback to buffered. |
| 5465 | * |
| 5466 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 5467 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 5468 | * the generic code. |
| 5469 | */ |
| 5470 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 5471 | em->block_start == EXTENT_MAP_INLINE) { |
| 5472 | free_extent_map(em); |
| 5473 | return -ENOTBLK; |
| 5474 | } |
| 5475 | |
| 5476 | /* Just a good old fashioned hole, return */ |
| 5477 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 5478 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5479 | free_extent_map(em); |
| 5480 | /* DIO will do one hole at a time, so just unlock a sector */ |
| 5481 | unlock_extent(&BTRFS_I(inode)->io_tree, start, |
| 5482 | start + root->sectorsize - 1, GFP_NOFS); |
| 5483 | return 0; |
| 5484 | } |
| 5485 | |
| 5486 | /* |
| 5487 | * We don't allocate a new extent in the following cases |
| 5488 | * |
| 5489 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 5490 | * existing extent. |
| 5491 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 5492 | * just use the extent. |
| 5493 | * |
| 5494 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5495 | if (!create) { |
| 5496 | len = em->len - (start - em->start); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5497 | goto map; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5498 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5499 | |
| 5500 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 5501 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 5502 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5503 | int type; |
| 5504 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5505 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5506 | |
| 5507 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5508 | type = BTRFS_ORDERED_PREALLOC; |
| 5509 | else |
| 5510 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5511 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5512 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5513 | |
| 5514 | /* |
| 5515 | * we're not going to log anything, but we do need |
| 5516 | * to make sure the current transaction stays open |
| 5517 | * while we look for nocow cross refs |
| 5518 | */ |
| 5519 | trans = btrfs_join_transaction(root, 0); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5520 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5521 | goto must_cow; |
| 5522 | |
| 5523 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
| 5524 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 5525 | block_start, len, len, type); |
| 5526 | btrfs_end_transaction(trans, root); |
| 5527 | if (ret) { |
| 5528 | free_extent_map(em); |
| 5529 | return ret; |
| 5530 | } |
| 5531 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5532 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5533 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5534 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5535 | must_cow: |
| 5536 | /* |
| 5537 | * this will cow the extent, reset the len in case we changed |
| 5538 | * it above |
| 5539 | */ |
| 5540 | len = bh_result->b_size; |
| 5541 | free_extent_map(em); |
| 5542 | em = btrfs_new_extent_direct(inode, start, len); |
| 5543 | if (IS_ERR(em)) |
| 5544 | return PTR_ERR(em); |
| 5545 | len = min(len, em->len - (start - em->start)); |
| 5546 | unlock: |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5547 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
| 5548 | EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1, |
| 5549 | 0, NULL, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5550 | map: |
| 5551 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 5552 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5553 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5554 | bh_result->b_bdev = em->bdev; |
| 5555 | set_buffer_mapped(bh_result); |
| 5556 | if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5557 | set_buffer_new(bh_result); |
| 5558 | |
| 5559 | free_extent_map(em); |
| 5560 | |
| 5561 | return 0; |
| 5562 | } |
| 5563 | |
| 5564 | struct btrfs_dio_private { |
| 5565 | struct inode *inode; |
| 5566 | u64 logical_offset; |
| 5567 | u64 disk_bytenr; |
| 5568 | u64 bytes; |
| 5569 | u32 *csums; |
| 5570 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5571 | |
| 5572 | /* number of bios pending for this dio */ |
| 5573 | atomic_t pending_bios; |
| 5574 | |
| 5575 | /* IO errors */ |
| 5576 | int errors; |
| 5577 | |
| 5578 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5579 | }; |
| 5580 | |
| 5581 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 5582 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5583 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5584 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 5585 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5586 | struct inode *inode = dip->inode; |
| 5587 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5588 | u64 start; |
| 5589 | u32 *private = dip->csums; |
| 5590 | |
| 5591 | start = dip->logical_offset; |
| 5592 | do { |
| 5593 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 5594 | struct page *page = bvec->bv_page; |
| 5595 | char *kaddr; |
| 5596 | u32 csum = ~(u32)0; |
| 5597 | unsigned long flags; |
| 5598 | |
| 5599 | local_irq_save(flags); |
| 5600 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 5601 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 5602 | csum, bvec->bv_len); |
| 5603 | btrfs_csum_final(csum, (char *)&csum); |
| 5604 | kunmap_atomic(kaddr, KM_IRQ0); |
| 5605 | local_irq_restore(flags); |
| 5606 | |
| 5607 | flush_dcache_page(bvec->bv_page); |
| 5608 | if (csum != *private) { |
| 5609 | printk(KERN_ERR "btrfs csum failed ino %lu off" |
| 5610 | " %llu csum %u private %u\n", |
| 5611 | inode->i_ino, (unsigned long long)start, |
| 5612 | csum, *private); |
| 5613 | err = -EIO; |
| 5614 | } |
| 5615 | } |
| 5616 | |
| 5617 | start += bvec->bv_len; |
| 5618 | private++; |
| 5619 | bvec++; |
| 5620 | } while (bvec <= bvec_end); |
| 5621 | |
| 5622 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
| 5623 | dip->logical_offset + dip->bytes - 1, GFP_NOFS); |
| 5624 | bio->bi_private = dip->private; |
| 5625 | |
| 5626 | kfree(dip->csums); |
| 5627 | kfree(dip); |
| 5628 | dio_end_io(bio, err); |
| 5629 | } |
| 5630 | |
| 5631 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 5632 | { |
| 5633 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5634 | struct inode *inode = dip->inode; |
| 5635 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5636 | struct btrfs_trans_handle *trans; |
| 5637 | struct btrfs_ordered_extent *ordered = NULL; |
| 5638 | struct extent_state *cached_state = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5639 | u64 ordered_offset = dip->logical_offset; |
| 5640 | u64 ordered_bytes = dip->bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5641 | int ret; |
| 5642 | |
| 5643 | if (err) |
| 5644 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5645 | again: |
| 5646 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 5647 | &ordered_offset, |
| 5648 | ordered_bytes); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5649 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5650 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5651 | |
| 5652 | BUG_ON(!ordered); |
| 5653 | |
| 5654 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5655 | if (IS_ERR(trans)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5656 | err = -ENOMEM; |
| 5657 | goto out; |
| 5658 | } |
| 5659 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5660 | |
| 5661 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) { |
| 5662 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5663 | if (!ret) |
| 5664 | ret = btrfs_update_inode(trans, root, inode); |
| 5665 | err = ret; |
| 5666 | goto out; |
| 5667 | } |
| 5668 | |
| 5669 | lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5670 | ordered->file_offset + ordered->len - 1, 0, |
| 5671 | &cached_state, GFP_NOFS); |
| 5672 | |
| 5673 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) { |
| 5674 | ret = btrfs_mark_extent_written(trans, inode, |
| 5675 | ordered->file_offset, |
| 5676 | ordered->file_offset + |
| 5677 | ordered->len); |
| 5678 | if (ret) { |
| 5679 | err = ret; |
| 5680 | goto out_unlock; |
| 5681 | } |
| 5682 | } else { |
| 5683 | ret = insert_reserved_file_extent(trans, inode, |
| 5684 | ordered->file_offset, |
| 5685 | ordered->start, |
| 5686 | ordered->disk_len, |
| 5687 | ordered->len, |
| 5688 | ordered->len, |
| 5689 | 0, 0, 0, |
| 5690 | BTRFS_FILE_EXTENT_REG); |
| 5691 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 5692 | ordered->file_offset, ordered->len); |
| 5693 | if (ret) { |
| 5694 | err = ret; |
| 5695 | WARN_ON(1); |
| 5696 | goto out_unlock; |
| 5697 | } |
| 5698 | } |
| 5699 | |
| 5700 | add_pending_csums(trans, inode, ordered->file_offset, &ordered->list); |
| 5701 | btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5702 | btrfs_update_inode(trans, root, inode); |
| 5703 | out_unlock: |
| 5704 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5705 | ordered->file_offset + ordered->len - 1, |
| 5706 | &cached_state, GFP_NOFS); |
| 5707 | out: |
| 5708 | btrfs_delalloc_release_metadata(inode, ordered->len); |
| 5709 | btrfs_end_transaction(trans, root); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5710 | ordered_offset = ordered->file_offset + ordered->len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5711 | btrfs_put_ordered_extent(ordered); |
| 5712 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5713 | |
| 5714 | out_test: |
| 5715 | /* |
| 5716 | * our bio might span multiple ordered extents. If we haven't |
| 5717 | * completed the accounting for the whole dio, go back and try again |
| 5718 | */ |
| 5719 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 5720 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 5721 | ordered_offset; |
| 5722 | goto again; |
| 5723 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5724 | out_done: |
| 5725 | bio->bi_private = dip->private; |
| 5726 | |
| 5727 | kfree(dip->csums); |
| 5728 | kfree(dip); |
| 5729 | dio_end_io(bio, err); |
| 5730 | } |
| 5731 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5732 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 5733 | struct bio *bio, int mirror_num, |
| 5734 | unsigned long bio_flags, u64 offset) |
| 5735 | { |
| 5736 | int ret; |
| 5737 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5738 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
| 5739 | BUG_ON(ret); |
| 5740 | return 0; |
| 5741 | } |
| 5742 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5743 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 5744 | { |
| 5745 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5746 | |
| 5747 | if (err) { |
| 5748 | printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 5749 | "sector %#Lx len %u err no %d\n", |
| 5750 | dip->inode->i_ino, bio->bi_rw, |
| 5751 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5752 | dip->errors = 1; |
| 5753 | |
| 5754 | /* |
| 5755 | * before atomic variable goto zero, we must make sure |
| 5756 | * dip->errors is perceived to be set. |
| 5757 | */ |
| 5758 | smp_mb__before_atomic_dec(); |
| 5759 | } |
| 5760 | |
| 5761 | /* if there are more bios still pending for this dio, just exit */ |
| 5762 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 5763 | goto out; |
| 5764 | |
| 5765 | if (dip->errors) |
| 5766 | bio_io_error(dip->orig_bio); |
| 5767 | else { |
| 5768 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 5769 | bio_endio(dip->orig_bio, 0); |
| 5770 | } |
| 5771 | out: |
| 5772 | bio_put(bio); |
| 5773 | } |
| 5774 | |
| 5775 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 5776 | u64 first_sector, gfp_t gfp_flags) |
| 5777 | { |
| 5778 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 5779 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 5780 | } |
| 5781 | |
| 5782 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 5783 | int rw, u64 file_offset, int skip_sum, |
| 5784 | u32 *csums) |
| 5785 | { |
| 5786 | int write = rw & REQ_WRITE; |
| 5787 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5788 | int ret; |
| 5789 | |
| 5790 | bio_get(bio); |
| 5791 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 5792 | if (ret) |
| 5793 | goto err; |
| 5794 | |
| 5795 | if (write && !skip_sum) { |
| 5796 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 5797 | inode, rw, bio, 0, 0, |
| 5798 | file_offset, |
| 5799 | __btrfs_submit_bio_start_direct_io, |
| 5800 | __btrfs_submit_bio_done); |
| 5801 | goto err; |
| 5802 | } else if (!skip_sum) |
| 5803 | btrfs_lookup_bio_sums_dio(root, inode, bio, |
| 5804 | file_offset, csums); |
| 5805 | |
| 5806 | ret = btrfs_map_bio(root, rw, bio, 0, 1); |
| 5807 | err: |
| 5808 | bio_put(bio); |
| 5809 | return ret; |
| 5810 | } |
| 5811 | |
| 5812 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 5813 | int skip_sum) |
| 5814 | { |
| 5815 | struct inode *inode = dip->inode; |
| 5816 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5817 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5818 | struct bio *bio; |
| 5819 | struct bio *orig_bio = dip->orig_bio; |
| 5820 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 5821 | u64 start_sector = orig_bio->bi_sector; |
| 5822 | u64 file_offset = dip->logical_offset; |
| 5823 | u64 submit_len = 0; |
| 5824 | u64 map_length; |
| 5825 | int nr_pages = 0; |
| 5826 | u32 *csums = dip->csums; |
| 5827 | int ret = 0; |
| 5828 | |
| 5829 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 5830 | if (!bio) |
| 5831 | return -ENOMEM; |
| 5832 | bio->bi_private = dip; |
| 5833 | bio->bi_end_io = btrfs_end_dio_bio; |
| 5834 | atomic_inc(&dip->pending_bios); |
| 5835 | |
| 5836 | map_length = orig_bio->bi_size; |
| 5837 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 5838 | &map_length, NULL, 0); |
| 5839 | if (ret) { |
| 5840 | bio_put(bio); |
| 5841 | return -EIO; |
| 5842 | } |
| 5843 | |
| 5844 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 5845 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 5846 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5847 | bvec->bv_offset) < bvec->bv_len)) { |
| 5848 | /* |
| 5849 | * inc the count before we submit the bio so |
| 5850 | * we know the end IO handler won't happen before |
| 5851 | * we inc the count. Otherwise, the dip might get freed |
| 5852 | * before we're done setting it up |
| 5853 | */ |
| 5854 | atomic_inc(&dip->pending_bios); |
| 5855 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 5856 | file_offset, skip_sum, |
| 5857 | csums); |
| 5858 | if (ret) { |
| 5859 | bio_put(bio); |
| 5860 | atomic_dec(&dip->pending_bios); |
| 5861 | goto out_err; |
| 5862 | } |
| 5863 | |
| 5864 | if (!skip_sum) |
| 5865 | csums = csums + nr_pages; |
| 5866 | start_sector += submit_len >> 9; |
| 5867 | file_offset += submit_len; |
| 5868 | |
| 5869 | submit_len = 0; |
| 5870 | nr_pages = 0; |
| 5871 | |
| 5872 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 5873 | start_sector, GFP_NOFS); |
| 5874 | if (!bio) |
| 5875 | goto out_err; |
| 5876 | bio->bi_private = dip; |
| 5877 | bio->bi_end_io = btrfs_end_dio_bio; |
| 5878 | |
| 5879 | map_length = orig_bio->bi_size; |
| 5880 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 5881 | &map_length, NULL, 0); |
| 5882 | if (ret) { |
| 5883 | bio_put(bio); |
| 5884 | goto out_err; |
| 5885 | } |
| 5886 | } else { |
| 5887 | submit_len += bvec->bv_len; |
| 5888 | nr_pages ++; |
| 5889 | bvec++; |
| 5890 | } |
| 5891 | } |
| 5892 | |
| 5893 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
| 5894 | csums); |
| 5895 | if (!ret) |
| 5896 | return 0; |
| 5897 | |
| 5898 | bio_put(bio); |
| 5899 | out_err: |
| 5900 | dip->errors = 1; |
| 5901 | /* |
| 5902 | * before atomic variable goto zero, we must |
| 5903 | * make sure dip->errors is perceived to be set. |
| 5904 | */ |
| 5905 | smp_mb__before_atomic_dec(); |
| 5906 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 5907 | bio_io_error(dip->orig_bio); |
| 5908 | |
| 5909 | /* bio_end_io() will handle error, so we needn't return it */ |
| 5910 | return 0; |
| 5911 | } |
| 5912 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5913 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 5914 | loff_t file_offset) |
| 5915 | { |
| 5916 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5917 | struct btrfs_dio_private *dip; |
| 5918 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5919 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5920 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5921 | int ret = 0; |
| 5922 | |
| 5923 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 5924 | |
| 5925 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 5926 | if (!dip) { |
| 5927 | ret = -ENOMEM; |
| 5928 | goto free_ordered; |
| 5929 | } |
| 5930 | dip->csums = NULL; |
| 5931 | |
| 5932 | if (!skip_sum) { |
| 5933 | dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); |
| 5934 | if (!dip->csums) { |
| 5935 | ret = -ENOMEM; |
| 5936 | goto free_ordered; |
| 5937 | } |
| 5938 | } |
| 5939 | |
| 5940 | dip->private = bio->bi_private; |
| 5941 | dip->inode = inode; |
| 5942 | dip->logical_offset = file_offset; |
| 5943 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5944 | dip->bytes = 0; |
| 5945 | do { |
| 5946 | dip->bytes += bvec->bv_len; |
| 5947 | bvec++; |
| 5948 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 5949 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5950 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5951 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5952 | dip->errors = 0; |
| 5953 | dip->orig_bio = bio; |
| 5954 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5955 | |
| 5956 | if (write) |
| 5957 | bio->bi_end_io = btrfs_endio_direct_write; |
| 5958 | else |
| 5959 | bio->bi_end_io = btrfs_endio_direct_read; |
| 5960 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5961 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 5962 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5963 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5964 | free_ordered: |
| 5965 | /* |
| 5966 | * If this is a write, we need to clean up the reserved space and kill |
| 5967 | * the ordered extent. |
| 5968 | */ |
| 5969 | if (write) { |
| 5970 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 5971 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5972 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 5973 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 5974 | btrfs_free_reserved_extent(root, ordered->start, |
| 5975 | ordered->disk_len); |
| 5976 | btrfs_put_ordered_extent(ordered); |
| 5977 | btrfs_put_ordered_extent(ordered); |
| 5978 | } |
| 5979 | bio_endio(bio, ret); |
| 5980 | } |
| 5981 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 5982 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 5983 | const struct iovec *iov, loff_t offset, |
| 5984 | unsigned long nr_segs) |
| 5985 | { |
| 5986 | int seg; |
| 5987 | size_t size; |
| 5988 | unsigned long addr; |
| 5989 | unsigned blocksize_mask = root->sectorsize - 1; |
| 5990 | ssize_t retval = -EINVAL; |
| 5991 | loff_t end = offset; |
| 5992 | |
| 5993 | if (offset & blocksize_mask) |
| 5994 | goto out; |
| 5995 | |
| 5996 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 5997 | for (seg = 0; seg < nr_segs; seg++) { |
| 5998 | addr = (unsigned long)iov[seg].iov_base; |
| 5999 | size = iov[seg].iov_len; |
| 6000 | end += size; |
| 6001 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
| 6002 | goto out; |
| 6003 | } |
| 6004 | retval = 0; |
| 6005 | out: |
| 6006 | return retval; |
| 6007 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6008 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 6009 | const struct iovec *iov, loff_t offset, |
| 6010 | unsigned long nr_segs) |
| 6011 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6012 | struct file *file = iocb->ki_filp; |
| 6013 | struct inode *inode = file->f_mapping->host; |
| 6014 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6015 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6016 | u64 lockstart, lockend; |
| 6017 | ssize_t ret; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6018 | int writing = rw & WRITE; |
| 6019 | int write_bits = 0; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6020 | size_t count = iov_length(iov, nr_segs); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6021 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6022 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
| 6023 | offset, nr_segs)) { |
| 6024 | return 0; |
| 6025 | } |
| 6026 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6027 | lockstart = offset; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6028 | lockend = offset + count - 1; |
| 6029 | |
| 6030 | if (writing) { |
| 6031 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 6032 | if (ret) |
| 6033 | goto out; |
| 6034 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6035 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6036 | while (1) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6037 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6038 | 0, &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6039 | /* |
| 6040 | * We're concerned with the entire range that we're going to be |
| 6041 | * doing DIO to, so we need to make sure theres no ordered |
| 6042 | * extents in this range. |
| 6043 | */ |
| 6044 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6045 | lockend - lockstart + 1); |
| 6046 | if (!ordered) |
| 6047 | break; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6048 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6049 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6050 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6051 | btrfs_put_ordered_extent(ordered); |
| 6052 | cond_resched(); |
| 6053 | } |
| 6054 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6055 | /* |
| 6056 | * we don't use btrfs_set_extent_delalloc because we don't want |
| 6057 | * the dirty or uptodate bits |
| 6058 | */ |
| 6059 | if (writing) { |
| 6060 | write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING; |
| 6061 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6062 | EXTENT_DELALLOC, 0, NULL, &cached_state, |
| 6063 | GFP_NOFS); |
| 6064 | if (ret) { |
| 6065 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6066 | lockend, EXTENT_LOCKED | write_bits, |
| 6067 | 1, 0, &cached_state, GFP_NOFS); |
| 6068 | goto out; |
| 6069 | } |
| 6070 | } |
| 6071 | |
| 6072 | free_extent_state(cached_state); |
| 6073 | cached_state = NULL; |
| 6074 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6075 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 6076 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 6077 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
| 6078 | btrfs_submit_direct, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6079 | |
| 6080 | if (ret < 0 && ret != -EIOCBQUEUED) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6081 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset, |
| 6082 | offset + iov_length(iov, nr_segs) - 1, |
| 6083 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6084 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6085 | } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) { |
| 6086 | /* |
| 6087 | * We're falling back to buffered, unlock the section we didn't |
| 6088 | * do IO on. |
| 6089 | */ |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6090 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret, |
| 6091 | offset + iov_length(iov, nr_segs) - 1, |
| 6092 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6093 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6094 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6095 | out: |
| 6096 | free_extent_state(cached_state); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6097 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6098 | } |
| 6099 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6100 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 6101 | __u64 start, __u64 len) |
| 6102 | { |
| 6103 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent); |
| 6104 | } |
| 6105 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6106 | int btrfs_readpage(struct file *file, struct page *page) |
| 6107 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6108 | struct extent_io_tree *tree; |
| 6109 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6110 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6111 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6112 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6113 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 6114 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6115 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6116 | |
| 6117 | |
| 6118 | if (current->flags & PF_MEMALLOC) { |
| 6119 | redirty_page_for_writepage(wbc, page); |
| 6120 | unlock_page(page); |
| 6121 | return 0; |
| 6122 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6123 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6124 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 6125 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6126 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6127 | int btrfs_writepages(struct address_space *mapping, |
| 6128 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6129 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6130 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6131 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6132 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6133 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 6134 | } |
| 6135 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6136 | static int |
| 6137 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 6138 | struct list_head *pages, unsigned nr_pages) |
| 6139 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6140 | struct extent_io_tree *tree; |
| 6141 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6142 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 6143 | btrfs_get_extent); |
| 6144 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6145 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6146 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6147 | struct extent_io_tree *tree; |
| 6148 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6149 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6150 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6151 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 6152 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6153 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6154 | if (ret == 1) { |
| 6155 | ClearPagePrivate(page); |
| 6156 | set_page_private(page, 0); |
| 6157 | page_cache_release(page); |
| 6158 | } |
| 6159 | return ret; |
| 6160 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6161 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6162 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 6163 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 6164 | if (PageWriteback(page) || PageDirty(page)) |
| 6165 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 6166 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6167 | } |
| 6168 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6169 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 6170 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6171 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6172 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6173 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6174 | u64 page_start = page_offset(page); |
| 6175 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6176 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6177 | |
| 6178 | /* |
| 6179 | * we have the page locked, so new writeback can't start, |
| 6180 | * and the dirty bit won't be cleared while we are here. |
| 6181 | * |
| 6182 | * Wait for IO on this page so that we can safely clear |
| 6183 | * the PagePrivate2 bit and do ordered accounting |
| 6184 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6185 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6186 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6187 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6188 | if (offset) { |
| 6189 | btrfs_releasepage(page, GFP_NOFS); |
| 6190 | return; |
| 6191 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6192 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6193 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6194 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 6195 | page_offset(page)); |
| 6196 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6197 | /* |
| 6198 | * IO on this page will never be started, so we need |
| 6199 | * to account for any ordered extents now |
| 6200 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6201 | clear_extent_bit(tree, page_start, page_end, |
| 6202 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6203 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6204 | &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6205 | /* |
| 6206 | * whoever cleared the private bit is responsible |
| 6207 | * for the finish_ordered_io |
| 6208 | */ |
| 6209 | if (TestClearPagePrivate2(page)) { |
| 6210 | btrfs_finish_ordered_io(page->mapping->host, |
| 6211 | page_start, page_end); |
| 6212 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6213 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6214 | cached_state = NULL; |
| 6215 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6216 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6217 | } |
| 6218 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6219 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6220 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6221 | __btrfs_releasepage(page, GFP_NOFS); |
| 6222 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6223 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6224 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6225 | ClearPagePrivate(page); |
| 6226 | set_page_private(page, 0); |
| 6227 | page_cache_release(page); |
| 6228 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6229 | } |
| 6230 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6231 | /* |
| 6232 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 6233 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 6234 | * be careful to check for EOF conditions here. We set the page up correctly |
| 6235 | * for a written page which means we get ENOSPC checking when writing into |
| 6236 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 6237 | * support these features. |
| 6238 | * |
| 6239 | * We are not allowed to take the i_mutex here so we have to play games to |
| 6240 | * protect against truncate races as the page could now be beyond EOF. Because |
| 6241 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 6242 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 6243 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 6244 | * unlock the page. |
| 6245 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6246 | 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] | 6247 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6248 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 6249 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6250 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6251 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 6252 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6253 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6254 | char *kaddr; |
| 6255 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6256 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6257 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6258 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6259 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6260 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6261 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6262 | if (ret) { |
| 6263 | if (ret == -ENOMEM) |
| 6264 | ret = VM_FAULT_OOM; |
| 6265 | else /* -ENOSPC, -EIO, etc */ |
| 6266 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6267 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6268 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6269 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6270 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6271 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6272 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6273 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6274 | page_start = page_offset(page); |
| 6275 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6276 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6277 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6278 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6279 | /* page got truncated out from underneath us */ |
| 6280 | goto out_unlock; |
| 6281 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6282 | wait_on_page_writeback(page); |
| 6283 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6284 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 6285 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6286 | set_page_extent_mapped(page); |
| 6287 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6288 | /* |
| 6289 | * we can't set the delalloc bits if there are pending ordered |
| 6290 | * extents. Drop our locks and wait for them to finish |
| 6291 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6292 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6293 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6294 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6295 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6296 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6297 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6298 | btrfs_put_ordered_extent(ordered); |
| 6299 | goto again; |
| 6300 | } |
| 6301 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6302 | /* |
| 6303 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 6304 | * if it was already dirty, so for space accounting reasons we need to |
| 6305 | * clear any delalloc bits for the range we are fixing to save. There |
| 6306 | * is probably a better way to do this, but for now keep consistent with |
| 6307 | * prepare_pages in the normal write path. |
| 6308 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6309 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6310 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6311 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6312 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6313 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 6314 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6315 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6316 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6317 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6318 | ret = VM_FAULT_SIGBUS; |
| 6319 | goto out_unlock; |
| 6320 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6321 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6322 | |
| 6323 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6324 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6325 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6326 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6327 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6328 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6329 | if (zero_start != PAGE_CACHE_SIZE) { |
| 6330 | kaddr = kmap(page); |
| 6331 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 6332 | flush_dcache_page(page); |
| 6333 | kunmap(page); |
| 6334 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6335 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6336 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6337 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6338 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6339 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 6340 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 6341 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6342 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6343 | |
| 6344 | out_unlock: |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6345 | if (!ret) |
| 6346 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6347 | unlock_page(page); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6348 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6349 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6350 | return ret; |
| 6351 | } |
| 6352 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6353 | static void btrfs_truncate(struct inode *inode) |
| 6354 | { |
| 6355 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6356 | int ret; |
| 6357 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6358 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6359 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6360 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6361 | if (!S_ISREG(inode->i_mode)) { |
| 6362 | WARN_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6363 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6364 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6365 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 6366 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 6367 | if (ret) |
| 6368 | return; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6369 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6370 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6371 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6372 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6373 | trans = btrfs_start_transaction(root, 0); |
| 6374 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6375 | btrfs_set_trans_block_group(trans, inode); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6376 | trans->block_rsv = root->orphan_block_rsv; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6377 | |
| 6378 | /* |
| 6379 | * setattr is responsible for setting the ordered_data_close flag, |
| 6380 | * but that is only tested during the last file release. That |
| 6381 | * could happen well after the next commit, leaving a great big |
| 6382 | * window where new writes may get lost if someone chooses to write |
| 6383 | * to this file after truncating to zero |
| 6384 | * |
| 6385 | * The inode doesn't have any dirty data here, and so if we commit |
| 6386 | * this is a noop. If someone immediately starts writing to the inode |
| 6387 | * it is very likely we'll catch some of their writes in this |
| 6388 | * transaction, and the commit will find this file on the ordered |
| 6389 | * data list with good things to send down. |
| 6390 | * |
| 6391 | * This is a best effort solution, there is still a window where |
| 6392 | * using truncate to replace the contents of the file will |
| 6393 | * end up with a zero length file after a crash. |
| 6394 | */ |
| 6395 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 6396 | btrfs_add_ordered_operation(trans, root, inode); |
| 6397 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6398 | while (1) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6399 | if (!trans) { |
| 6400 | trans = btrfs_start_transaction(root, 0); |
| 6401 | BUG_ON(IS_ERR(trans)); |
| 6402 | btrfs_set_trans_block_group(trans, inode); |
| 6403 | trans->block_rsv = root->orphan_block_rsv; |
| 6404 | } |
| 6405 | |
| 6406 | ret = btrfs_block_rsv_check(trans, root, |
| 6407 | root->orphan_block_rsv, 0, 5); |
| 6408 | if (ret) { |
| 6409 | BUG_ON(ret != -EAGAIN); |
| 6410 | ret = btrfs_commit_transaction(trans, root); |
| 6411 | BUG_ON(ret); |
| 6412 | trans = NULL; |
| 6413 | continue; |
| 6414 | } |
| 6415 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6416 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 6417 | inode->i_size, |
| 6418 | BTRFS_EXTENT_DATA_KEY); |
| 6419 | if (ret != -EAGAIN) |
| 6420 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6421 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6422 | ret = btrfs_update_inode(trans, root, inode); |
| 6423 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6424 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6425 | nr = trans->blocks_used; |
| 6426 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6427 | trans = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6428 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6429 | } |
| 6430 | |
| 6431 | if (ret == 0 && inode->i_nlink > 0) { |
| 6432 | ret = btrfs_orphan_del(trans, inode); |
| 6433 | BUG_ON(ret); |
| 6434 | } |
| 6435 | |
| 6436 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6437 | BUG_ON(ret); |
| 6438 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6439 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6440 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6441 | BUG_ON(ret); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6442 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6443 | } |
| 6444 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6445 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6446 | * create a new subvolume directory/inode (helper for the ioctl). |
| 6447 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6448 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6449 | struct btrfs_root *new_root, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6450 | u64 new_dirid, u64 alloc_hint) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6451 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6452 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6453 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6454 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6455 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6456 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6457 | new_dirid, alloc_hint, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6458 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 6459 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6460 | inode->i_op = &btrfs_dir_inode_operations; |
| 6461 | inode->i_fop = &btrfs_dir_file_operations; |
| 6462 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6463 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6464 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6465 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6466 | err = btrfs_update_inode(trans, new_root, inode); |
| 6467 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6468 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6469 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6470 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6471 | } |
| 6472 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6473 | /* helper function for file defrag and space balancing. This |
| 6474 | * forces readahead on a given range of bytes in an inode |
| 6475 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6476 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6477 | struct file_ra_state *ra, struct file *file, |
| 6478 | pgoff_t offset, pgoff_t last_index) |
| 6479 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6480 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6481 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6482 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 6483 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 6484 | } |
| 6485 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6486 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 6487 | { |
| 6488 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6489 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6490 | |
| 6491 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 6492 | if (!ei) |
| 6493 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6494 | |
| 6495 | ei->root = NULL; |
| 6496 | ei->space_info = NULL; |
| 6497 | ei->generation = 0; |
| 6498 | ei->sequence = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 6499 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6500 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6501 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6502 | ei->delalloc_bytes = 0; |
| 6503 | ei->reserved_bytes = 0; |
| 6504 | ei->disk_i_size = 0; |
| 6505 | ei->flags = 0; |
| 6506 | ei->index_cnt = (u64)-1; |
| 6507 | ei->last_unlink_trans = 0; |
| 6508 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6509 | spin_lock_init(&ei->accounting_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6510 | atomic_set(&ei->outstanding_extents, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6511 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6512 | |
| 6513 | ei->ordered_data_close = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6514 | ei->orphan_meta_reserved = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6515 | ei->dummy_inode = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6516 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6517 | |
| 6518 | inode = &ei->vfs_inode; |
| 6519 | extent_map_tree_init(&ei->extent_tree, GFP_NOFS); |
| 6520 | extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS); |
| 6521 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS); |
| 6522 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6523 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6524 | INIT_LIST_HEAD(&ei->i_orphan); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6525 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6526 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6527 | RB_CLEAR_NODE(&ei->rb_node); |
| 6528 | |
| 6529 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6530 | } |
| 6531 | |
| 6532 | void btrfs_destroy_inode(struct inode *inode) |
| 6533 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6534 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6535 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6536 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6537 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 6538 | WARN_ON(inode->i_data.nrpages); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6539 | WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents)); |
| 6540 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6541 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6542 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6543 | * This can happen where we create an inode, but somebody else also |
| 6544 | * created the same inode and we need to destroy the one we already |
| 6545 | * created. |
| 6546 | */ |
| 6547 | if (!root) |
| 6548 | goto free; |
| 6549 | |
| 6550 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6551 | * Make sure we're properly removed from the ordered operation |
| 6552 | * lists. |
| 6553 | */ |
| 6554 | smp_mb(); |
| 6555 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 6556 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 6557 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 6558 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 6559 | } |
| 6560 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6561 | if (root == root->fs_info->tree_root) { |
| 6562 | struct btrfs_block_group_cache *block_group; |
| 6563 | |
| 6564 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 6565 | BTRFS_I(inode)->block_group); |
| 6566 | if (block_group && block_group->inode == inode) { |
| 6567 | spin_lock(&block_group->lock); |
| 6568 | block_group->inode = NULL; |
| 6569 | spin_unlock(&block_group->lock); |
| 6570 | btrfs_put_block_group(block_group); |
| 6571 | } else if (block_group) { |
| 6572 | btrfs_put_block_group(block_group); |
| 6573 | } |
| 6574 | } |
| 6575 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6576 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6577 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6578 | printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", |
| 6579 | inode->i_ino); |
| 6580 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6581 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6582 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6583 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6584 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6585 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 6586 | if (!ordered) |
| 6587 | break; |
| 6588 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6589 | printk(KERN_ERR "btrfs found ordered " |
| 6590 | "extent %llu %llu on inode cleanup\n", |
| 6591 | (unsigned long long)ordered->file_offset, |
| 6592 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6593 | btrfs_remove_ordered_extent(inode, ordered); |
| 6594 | btrfs_put_ordered_extent(ordered); |
| 6595 | btrfs_put_ordered_extent(ordered); |
| 6596 | } |
| 6597 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6598 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6599 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6600 | free: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6601 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 6602 | } |
| 6603 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6604 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6605 | { |
| 6606 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6607 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6608 | if (btrfs_root_refs(&root->root_item) == 0 && |
| 6609 | root != root->fs_info->tree_root) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6610 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6611 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6612 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6613 | } |
| 6614 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 6615 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6616 | { |
| 6617 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 6618 | |
| 6619 | inode_init_once(&ei->vfs_inode); |
| 6620 | } |
| 6621 | |
| 6622 | void btrfs_destroy_cachep(void) |
| 6623 | { |
| 6624 | if (btrfs_inode_cachep) |
| 6625 | kmem_cache_destroy(btrfs_inode_cachep); |
| 6626 | if (btrfs_trans_handle_cachep) |
| 6627 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 6628 | if (btrfs_transaction_cachep) |
| 6629 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6630 | if (btrfs_path_cachep) |
| 6631 | kmem_cache_destroy(btrfs_path_cachep); |
| 6632 | } |
| 6633 | |
| 6634 | int btrfs_init_cachep(void) |
| 6635 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6636 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 6637 | sizeof(struct btrfs_inode), 0, |
| 6638 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6639 | if (!btrfs_inode_cachep) |
| 6640 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6641 | |
| 6642 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 6643 | sizeof(struct btrfs_trans_handle), 0, |
| 6644 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6645 | if (!btrfs_trans_handle_cachep) |
| 6646 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6647 | |
| 6648 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 6649 | sizeof(struct btrfs_transaction), 0, |
| 6650 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6651 | if (!btrfs_transaction_cachep) |
| 6652 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6653 | |
| 6654 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 6655 | sizeof(struct btrfs_path), 0, |
| 6656 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6657 | if (!btrfs_path_cachep) |
| 6658 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6659 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6660 | return 0; |
| 6661 | fail: |
| 6662 | btrfs_destroy_cachep(); |
| 6663 | return -ENOMEM; |
| 6664 | } |
| 6665 | |
| 6666 | static int btrfs_getattr(struct vfsmount *mnt, |
| 6667 | struct dentry *dentry, struct kstat *stat) |
| 6668 | { |
| 6669 | struct inode *inode = dentry->d_inode; |
| 6670 | generic_fillattr(inode, stat); |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6671 | stat->dev = BTRFS_I(inode)->root->anon_super.s_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 6672 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6673 | stat->blocks = (inode_get_bytes(inode) + |
| 6674 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6675 | return 0; |
| 6676 | } |
| 6677 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6678 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 6679 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6680 | { |
| 6681 | struct btrfs_trans_handle *trans; |
| 6682 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6683 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6684 | struct inode *new_inode = new_dentry->d_inode; |
| 6685 | struct inode *old_inode = old_dentry->d_inode; |
| 6686 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6687 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6688 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6689 | int ret; |
| 6690 | |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6691 | if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 6692 | return -EPERM; |
| 6693 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6694 | /* we only allow rename subvolume link between subvolumes */ |
| 6695 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6696 | return -EXDEV; |
| 6697 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6698 | if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 6699 | (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) |
| 6700 | return -ENOTEMPTY; |
| 6701 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6702 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6703 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6704 | return -ENOTEMPTY; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6705 | /* |
| 6706 | * we're using rename to replace one file with another. |
| 6707 | * and the replacement file is large. Start IO on it now so |
| 6708 | * we don't add too much work to the end of the transaction |
| 6709 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 6710 | 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] | 6711 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 6712 | filemap_flush(old_inode->i_mapping); |
| 6713 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6714 | /* close the racy window with snapshot create/destroy ioctl */ |
| 6715 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 6716 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6717 | /* |
| 6718 | * We want to reserve the absolute worst case amount of items. So if |
| 6719 | * both inodes are subvols and we need to unlink them then that would |
| 6720 | * require 4 item modifications, but if they are both normal inodes it |
| 6721 | * would require 5 item modifications, so we'll assume their normal |
| 6722 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 6723 | * should cover the worst case number of items we'll modify. |
| 6724 | */ |
| 6725 | trans = btrfs_start_transaction(root, 20); |
| 6726 | if (IS_ERR(trans)) |
| 6727 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6728 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6729 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6730 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6731 | if (dest != root) |
| 6732 | btrfs_record_root_in_trans(trans, dest); |
| 6733 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6734 | ret = btrfs_set_inode_index(new_dir, &index); |
| 6735 | if (ret) |
| 6736 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6737 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6738 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6739 | /* force full log commit if subvolume involved. */ |
| 6740 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 6741 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6742 | ret = btrfs_insert_inode_ref(trans, dest, |
| 6743 | new_dentry->d_name.name, |
| 6744 | new_dentry->d_name.len, |
| 6745 | old_inode->i_ino, |
| 6746 | new_dir->i_ino, index); |
| 6747 | if (ret) |
| 6748 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6749 | /* |
| 6750 | * this is an ugly little race, but the rename is required |
| 6751 | * to make sure that if we crash, the inode is either at the |
| 6752 | * old name or the new one. pinning the log transaction lets |
| 6753 | * us make sure we don't allow a log commit to come in after |
| 6754 | * we unlink the name but before we add the new name back in. |
| 6755 | */ |
| 6756 | btrfs_pin_log_trans(root); |
| 6757 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6758 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6759 | * make sure the inode gets flushed if it is replacing |
| 6760 | * something. |
| 6761 | */ |
| 6762 | if (new_inode && new_inode->i_size && |
| 6763 | old_inode && S_ISREG(old_inode->i_mode)) { |
| 6764 | btrfs_add_ordered_operation(trans, root, old_inode); |
| 6765 | } |
| 6766 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6767 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 6768 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 6769 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6770 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 6771 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 6772 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 6773 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6774 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6775 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 6776 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 6777 | old_dentry->d_name.name, |
| 6778 | old_dentry->d_name.len); |
| 6779 | } else { |
| 6780 | btrfs_inc_nlink(old_dentry->d_inode); |
| 6781 | ret = btrfs_unlink_inode(trans, root, old_dir, |
| 6782 | old_dentry->d_inode, |
| 6783 | old_dentry->d_name.name, |
| 6784 | old_dentry->d_name.len); |
| 6785 | } |
| 6786 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6787 | |
| 6788 | if (new_inode) { |
| 6789 | new_inode->i_ctime = CURRENT_TIME; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6790 | if (unlikely(new_inode->i_ino == |
| 6791 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 6792 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 6793 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 6794 | root_objectid, |
| 6795 | new_dentry->d_name.name, |
| 6796 | new_dentry->d_name.len); |
| 6797 | BUG_ON(new_inode->i_nlink == 0); |
| 6798 | } else { |
| 6799 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 6800 | new_dentry->d_inode, |
| 6801 | new_dentry->d_name.name, |
| 6802 | new_dentry->d_name.len); |
| 6803 | } |
| 6804 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6805 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6806 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6807 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6808 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6809 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6810 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6811 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 6812 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6813 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6814 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6815 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6816 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6817 | struct dentry *parent = dget_parent(new_dentry); |
| 6818 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
| 6819 | dput(parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6820 | btrfs_end_log_trans(root); |
| 6821 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6822 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 6823 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6824 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6825 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 6826 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6827 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6828 | return ret; |
| 6829 | } |
| 6830 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6831 | /* |
| 6832 | * some fairly slow code that needs optimization. This walks the list |
| 6833 | * of all the inodes with pending delalloc and forces them to disk. |
| 6834 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 6835 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6836 | { |
| 6837 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 6838 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6839 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6840 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 6841 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 6842 | return -EROFS; |
| 6843 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6844 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6845 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6846 | binode = list_entry(head->next, struct btrfs_inode, |
| 6847 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6848 | inode = igrab(&binode->vfs_inode); |
| 6849 | if (!inode) |
| 6850 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6851 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6852 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6853 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 6854 | if (delay_iput) |
| 6855 | btrfs_add_delayed_iput(inode); |
| 6856 | else |
| 6857 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6858 | } |
| 6859 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6860 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6861 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 6862 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6863 | |
| 6864 | /* the filemap_flush will queue IO into the worker threads, but |
| 6865 | * we have to make sure the IO is actually started and that |
| 6866 | * ordered extents get created before we return |
| 6867 | */ |
| 6868 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6869 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6870 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6871 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6872 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 6873 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 6874 | } |
| 6875 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6876 | return 0; |
| 6877 | } |
| 6878 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 6879 | int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput, |
| 6880 | int sync) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6881 | { |
| 6882 | struct btrfs_inode *binode; |
| 6883 | struct inode *inode = NULL; |
| 6884 | |
| 6885 | spin_lock(&root->fs_info->delalloc_lock); |
| 6886 | while (!list_empty(&root->fs_info->delalloc_inodes)) { |
| 6887 | binode = list_entry(root->fs_info->delalloc_inodes.next, |
| 6888 | struct btrfs_inode, delalloc_inodes); |
| 6889 | inode = igrab(&binode->vfs_inode); |
| 6890 | if (inode) { |
| 6891 | list_move_tail(&binode->delalloc_inodes, |
| 6892 | &root->fs_info->delalloc_inodes); |
| 6893 | break; |
| 6894 | } |
| 6895 | |
| 6896 | list_del_init(&binode->delalloc_inodes); |
| 6897 | cond_resched_lock(&root->fs_info->delalloc_lock); |
| 6898 | } |
| 6899 | spin_unlock(&root->fs_info->delalloc_lock); |
| 6900 | |
| 6901 | if (inode) { |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 6902 | if (sync) { |
| 6903 | filemap_write_and_wait(inode->i_mapping); |
| 6904 | /* |
| 6905 | * We have to do this because compression doesn't |
| 6906 | * actually set PG_writeback until it submits the pages |
| 6907 | * for IO, which happens in an async thread, so we could |
| 6908 | * race and not actually wait for any writeback pages |
| 6909 | * because they've not been submitted yet. Technically |
| 6910 | * this could still be the case for the ordered stuff |
| 6911 | * since the async thread may not have started to do its |
| 6912 | * work yet. If this becomes the case then we need to |
| 6913 | * figure out a way to make sure that in writepage we |
| 6914 | * wait for any async pages to be submitted before |
| 6915 | * returning so that fdatawait does what its supposed to |
| 6916 | * do. |
| 6917 | */ |
| 6918 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 6919 | } else { |
| 6920 | filemap_flush(inode->i_mapping); |
| 6921 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6922 | if (delay_iput) |
| 6923 | btrfs_add_delayed_iput(inode); |
| 6924 | else |
| 6925 | iput(inode); |
| 6926 | return 1; |
| 6927 | } |
| 6928 | return 0; |
| 6929 | } |
| 6930 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6931 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 6932 | const char *symname) |
| 6933 | { |
| 6934 | struct btrfs_trans_handle *trans; |
| 6935 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6936 | struct btrfs_path *path; |
| 6937 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6938 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6939 | int err; |
| 6940 | int drop_inode = 0; |
| 6941 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6942 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6943 | int name_len; |
| 6944 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6945 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6946 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6947 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6948 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6949 | |
| 6950 | name_len = strlen(symname) + 1; |
| 6951 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 6952 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6953 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6954 | err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid); |
| 6955 | if (err) |
| 6956 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6957 | /* |
| 6958 | * 2 items for inode item and ref |
| 6959 | * 2 items for dir items |
| 6960 | * 1 item for xattr if selinux is on |
| 6961 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6962 | trans = btrfs_start_transaction(root, 5); |
| 6963 | if (IS_ERR(trans)) |
| 6964 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6965 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6966 | btrfs_set_trans_block_group(trans, dir); |
| 6967 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6968 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6969 | dentry->d_name.len, dir->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6970 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 6971 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6972 | err = PTR_ERR(inode); |
| 6973 | if (IS_ERR(inode)) |
| 6974 | goto out_unlock; |
| 6975 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 6976 | err = btrfs_init_inode_security(trans, inode, dir); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6977 | if (err) { |
| 6978 | drop_inode = 1; |
| 6979 | goto out_unlock; |
| 6980 | } |
| 6981 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6982 | btrfs_set_trans_block_group(trans, inode); |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6983 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6984 | if (err) |
| 6985 | drop_inode = 1; |
| 6986 | else { |
| 6987 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 6988 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6989 | inode->i_fop = &btrfs_file_operations; |
| 6990 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6991 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6992 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6993 | btrfs_update_inode_block_group(trans, inode); |
| 6994 | btrfs_update_inode_block_group(trans, dir); |
| 6995 | if (drop_inode) |
| 6996 | goto out_unlock; |
| 6997 | |
| 6998 | path = btrfs_alloc_path(); |
| 6999 | BUG_ON(!path); |
| 7000 | key.objectid = inode->i_ino; |
| 7001 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7002 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 7003 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 7004 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 7005 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7006 | if (err) { |
| 7007 | drop_inode = 1; |
| 7008 | goto out_unlock; |
| 7009 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7010 | leaf = path->nodes[0]; |
| 7011 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 7012 | struct btrfs_file_extent_item); |
| 7013 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 7014 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7015 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7016 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 7017 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 7018 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 7019 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 7020 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7021 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7022 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 7023 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7024 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7025 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7026 | inode->i_op = &btrfs_symlink_inode_operations; |
| 7027 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7028 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7029 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7030 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7031 | err = btrfs_update_inode(trans, root, inode); |
| 7032 | if (err) |
| 7033 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7034 | |
| 7035 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7036 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 7037 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7038 | if (drop_inode) { |
| 7039 | inode_dec_link_count(inode); |
| 7040 | iput(inode); |
| 7041 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7042 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7043 | return err; |
| 7044 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7045 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7046 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7047 | u64 start, u64 num_bytes, u64 min_size, |
| 7048 | loff_t actual_len, u64 *alloc_hint, |
| 7049 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7050 | { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7051 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7052 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7053 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7054 | u64 i_size; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7055 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7056 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7057 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7058 | if (trans) |
| 7059 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7060 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7061 | if (own_trans) { |
| 7062 | trans = btrfs_start_transaction(root, 3); |
| 7063 | if (IS_ERR(trans)) { |
| 7064 | ret = PTR_ERR(trans); |
| 7065 | break; |
| 7066 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7067 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7068 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7069 | ret = btrfs_reserve_extent(trans, root, num_bytes, min_size, |
| 7070 | 0, *alloc_hint, (u64)-1, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7071 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7072 | if (own_trans) |
| 7073 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7074 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7075 | } |
| 7076 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7077 | ret = insert_reserved_file_extent(trans, inode, |
| 7078 | cur_offset, ins.objectid, |
| 7079 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 7080 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7081 | BTRFS_FILE_EXTENT_PREALLOC); |
| 7082 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 7083 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7084 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7085 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7086 | num_bytes -= ins.offset; |
| 7087 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7088 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7089 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7090 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7091 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7092 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7093 | (actual_len > inode->i_size) && |
| 7094 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7095 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7096 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7097 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7098 | i_size = cur_offset; |
| 7099 | i_size_write(inode, i_size); |
| 7100 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7101 | } |
| 7102 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7103 | ret = btrfs_update_inode(trans, root, inode); |
| 7104 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7105 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7106 | if (own_trans) |
| 7107 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7108 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7109 | return ret; |
| 7110 | } |
| 7111 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7112 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7113 | u64 start, u64 num_bytes, u64 min_size, |
| 7114 | loff_t actual_len, u64 *alloc_hint) |
| 7115 | { |
| 7116 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7117 | min_size, actual_len, alloc_hint, |
| 7118 | NULL); |
| 7119 | } |
| 7120 | |
| 7121 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 7122 | struct btrfs_trans_handle *trans, int mode, |
| 7123 | u64 start, u64 num_bytes, u64 min_size, |
| 7124 | loff_t actual_len, u64 *alloc_hint) |
| 7125 | { |
| 7126 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7127 | min_size, actual_len, alloc_hint, trans); |
| 7128 | } |
| 7129 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7130 | static long btrfs_fallocate(struct inode *inode, int mode, |
| 7131 | loff_t offset, loff_t len) |
| 7132 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7133 | struct extent_state *cached_state = NULL; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7134 | u64 cur_offset; |
| 7135 | u64 last_byte; |
| 7136 | u64 alloc_start; |
| 7137 | u64 alloc_end; |
| 7138 | u64 alloc_hint = 0; |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 7139 | u64 locked_end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7140 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 7141 | struct extent_map *em; |
| 7142 | int ret; |
| 7143 | |
| 7144 | alloc_start = offset & ~mask; |
| 7145 | alloc_end = (offset + len + mask) & ~mask; |
| 7146 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 7147 | /* |
| 7148 | * wait for ordered IO before we have any locks. We'll loop again |
| 7149 | * below with the locks held. |
| 7150 | */ |
| 7151 | btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start); |
| 7152 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7153 | mutex_lock(&inode->i_mutex); |
Josef Bacik | 0ed42a6 | 2010-11-22 18:55:39 +0000 | [diff] [blame] | 7154 | ret = inode_newsize_ok(inode, alloc_end); |
| 7155 | if (ret) |
| 7156 | goto out; |
| 7157 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7158 | if (alloc_start > inode->i_size) { |
| 7159 | ret = btrfs_cont_expand(inode, alloc_start); |
| 7160 | if (ret) |
| 7161 | goto out; |
| 7162 | } |
| 7163 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7164 | ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start); |
Josef Bacik | a970b0a | 2009-06-27 21:07:34 -0400 | [diff] [blame] | 7165 | if (ret) |
| 7166 | goto out; |
| 7167 | |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 7168 | locked_end = alloc_end - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7169 | while (1) { |
| 7170 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 7171 | |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 7172 | /* the extent lock is ordered inside the running |
| 7173 | * transaction |
| 7174 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7175 | lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start, |
| 7176 | locked_end, 0, &cached_state, GFP_NOFS); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7177 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 7178 | alloc_end - 1); |
| 7179 | if (ordered && |
| 7180 | ordered->file_offset + ordered->len > alloc_start && |
| 7181 | ordered->file_offset < alloc_end) { |
| 7182 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7183 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 7184 | alloc_start, locked_end, |
| 7185 | &cached_state, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 7186 | /* |
| 7187 | * we can't wait on the range with the transaction |
| 7188 | * running or with the extent lock held |
| 7189 | */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7190 | btrfs_wait_ordered_range(inode, alloc_start, |
| 7191 | alloc_end - alloc_start); |
| 7192 | } else { |
| 7193 | if (ordered) |
| 7194 | btrfs_put_ordered_extent(ordered); |
| 7195 | break; |
| 7196 | } |
| 7197 | } |
| 7198 | |
| 7199 | cur_offset = alloc_start; |
| 7200 | while (1) { |
| 7201 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 7202 | alloc_end - cur_offset, 0); |
| 7203 | BUG_ON(IS_ERR(em) || !em); |
| 7204 | last_byte = min(extent_map_end(em), alloc_end); |
| 7205 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7206 | if (em->block_start == EXTENT_MAP_HOLE || |
| 7207 | (cur_offset >= inode->i_size && |
| 7208 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
Josef Bacik | 8360977 | 2010-06-07 18:26:37 +0000 | [diff] [blame] | 7209 | ret = btrfs_prealloc_file_range(inode, mode, cur_offset, |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7210 | last_byte - cur_offset, |
| 7211 | 1 << inode->i_blkbits, |
| 7212 | offset + len, |
| 7213 | &alloc_hint); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7214 | if (ret < 0) { |
| 7215 | free_extent_map(em); |
| 7216 | break; |
| 7217 | } |
| 7218 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7219 | free_extent_map(em); |
| 7220 | |
| 7221 | cur_offset = last_byte; |
| 7222 | if (cur_offset >= alloc_end) { |
| 7223 | ret = 0; |
| 7224 | break; |
| 7225 | } |
| 7226 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7227 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
| 7228 | &cached_state, GFP_NOFS); |
Chris Mason | 546888d | 2009-04-21 11:53:38 -0400 | [diff] [blame] | 7229 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7230 | btrfs_free_reserved_data_space(inode, alloc_end - alloc_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7231 | out: |
| 7232 | mutex_unlock(&inode->i_mutex); |
| 7233 | return ret; |
| 7234 | } |
| 7235 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7236 | static int btrfs_set_page_dirty(struct page *page) |
| 7237 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7238 | return __set_page_dirty_nobuffers(page); |
| 7239 | } |
| 7240 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 7241 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7242 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7243 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7244 | |
| 7245 | if (btrfs_root_readonly(root) && (mask & MAY_WRITE)) |
| 7246 | return -EROFS; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7247 | if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7248 | return -EACCES; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7249 | return generic_permission(inode, mask, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7250 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7251 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7252 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7253 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7254 | .lookup = btrfs_lookup, |
| 7255 | .create = btrfs_create, |
| 7256 | .unlink = btrfs_unlink, |
| 7257 | .link = btrfs_link, |
| 7258 | .mkdir = btrfs_mkdir, |
| 7259 | .rmdir = btrfs_rmdir, |
| 7260 | .rename = btrfs_rename, |
| 7261 | .symlink = btrfs_symlink, |
| 7262 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7263 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7264 | .setxattr = btrfs_setxattr, |
| 7265 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7266 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7267 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7268 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7269 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7270 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7271 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7272 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7273 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7274 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 7275 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7276 | .llseek = generic_file_llseek, |
| 7277 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 7278 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7279 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7280 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7281 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7282 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 7283 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7284 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7285 | }; |
| 7286 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7287 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7288 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 7289 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7290 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7291 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7292 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7293 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 7294 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 7295 | .set_bit_hook = btrfs_set_bit_hook, |
| 7296 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7297 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 7298 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7299 | }; |
| 7300 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 7301 | /* |
| 7302 | * btrfs doesn't support the bmap operation because swapfiles |
| 7303 | * use bmap to make a mapping of extents in the file. They assume |
| 7304 | * these extents won't change over the life of the file and they |
| 7305 | * use the bmap result to do IO directly to the drive. |
| 7306 | * |
| 7307 | * the btrfs bmap call would return logical addresses that aren't |
| 7308 | * suitable for IO and they also will change frequently as COW |
| 7309 | * operations happen. So, swapfile + btrfs == corruption. |
| 7310 | * |
| 7311 | * For now we're avoiding this by dropping bmap. |
| 7312 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7313 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7314 | .readpage = btrfs_readpage, |
| 7315 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7316 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7317 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7318 | .sync_page = block_sync_page, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7319 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7320 | .invalidatepage = btrfs_invalidatepage, |
| 7321 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7322 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 7323 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7324 | }; |
| 7325 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7326 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7327 | .readpage = btrfs_readpage, |
| 7328 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 7329 | .invalidatepage = btrfs_invalidatepage, |
| 7330 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7331 | }; |
| 7332 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7333 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7334 | .truncate = btrfs_truncate, |
| 7335 | .getattr = btrfs_getattr, |
| 7336 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7337 | .setxattr = btrfs_setxattr, |
| 7338 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7339 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7340 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7341 | .permission = btrfs_permission, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7342 | .fallocate = btrfs_fallocate, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7343 | .fiemap = btrfs_fiemap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7344 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7345 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7346 | .getattr = btrfs_getattr, |
| 7347 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7348 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7349 | .setxattr = btrfs_setxattr, |
| 7350 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7351 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7352 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7353 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7354 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7355 | .readlink = generic_readlink, |
| 7356 | .follow_link = page_follow_link_light, |
| 7357 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 7358 | .getattr = btrfs_getattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7359 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 7360 | .setxattr = btrfs_setxattr, |
| 7361 | .getxattr = btrfs_getxattr, |
| 7362 | .listxattr = btrfs_listxattr, |
| 7363 | .removexattr = btrfs_removexattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7364 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7365 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 7366 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7367 | .d_delete = btrfs_dentry_delete, |
| 7368 | }; |