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> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 40 | #include <linux/ratelimit.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 41 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 42 | #include "ctree.h" |
| 43 | #include "disk-io.h" |
| 44 | #include "transaction.h" |
| 45 | #include "btrfs_inode.h" |
| 46 | #include "ioctl.h" |
| 47 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 48 | #include "volumes.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 49 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 50 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 51 | #include "tree-log.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 52 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 53 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 54 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 55 | #include "inode-map.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 56 | |
| 57 | struct btrfs_iget_args { |
| 58 | u64 ino; |
| 59 | struct btrfs_root *root; |
| 60 | }; |
| 61 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 62 | static const struct inode_operations btrfs_dir_inode_operations; |
| 63 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 64 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 65 | static const struct inode_operations btrfs_special_inode_operations; |
| 66 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 67 | static const struct address_space_operations btrfs_aops; |
| 68 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 69 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 70 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 71 | |
| 72 | static struct kmem_cache *btrfs_inode_cachep; |
| 73 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 74 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 75 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 76 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 77 | |
| 78 | #define S_SHIFT 12 |
| 79 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 80 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 81 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 82 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 83 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 84 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 85 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 86 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 87 | }; |
| 88 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 89 | static int btrfs_setsize(struct inode *inode, loff_t newsize); |
| 90 | static int btrfs_truncate(struct inode *inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 91 | 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] | 92 | static noinline int cow_file_range(struct inode *inode, |
| 93 | struct page *locked_page, |
| 94 | u64 start, u64 end, int *page_started, |
| 95 | unsigned long *nr_written, int unlock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 96 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 97 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 98 | struct inode *inode, struct inode *dir, |
| 99 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 100 | { |
| 101 | int err; |
| 102 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 103 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 104 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 105 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 106 | return err; |
| 107 | } |
| 108 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 109 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 110 | * this does all the hard work for inserting an inline extent into |
| 111 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 112 | * no overlapping inline items exist in the btree |
| 113 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 114 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 115 | struct btrfs_root *root, struct inode *inode, |
| 116 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 117 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 118 | struct page **compressed_pages) |
| 119 | { |
| 120 | struct btrfs_key key; |
| 121 | struct btrfs_path *path; |
| 122 | struct extent_buffer *leaf; |
| 123 | struct page *page = NULL; |
| 124 | char *kaddr; |
| 125 | unsigned long ptr; |
| 126 | struct btrfs_file_extent_item *ei; |
| 127 | int err = 0; |
| 128 | int ret; |
| 129 | size_t cur_size = size; |
| 130 | size_t datasize; |
| 131 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 132 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 133 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 134 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 135 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 136 | path = btrfs_alloc_path(); |
| 137 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 138 | return -ENOMEM; |
| 139 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 140 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 141 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 142 | key.objectid = btrfs_ino(inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 143 | key.offset = start; |
| 144 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 145 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 146 | |
| 147 | inode_add_bytes(inode, size); |
| 148 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 149 | datasize); |
| 150 | BUG_ON(ret); |
| 151 | if (ret) { |
| 152 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 153 | goto fail; |
| 154 | } |
| 155 | leaf = path->nodes[0]; |
| 156 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 157 | struct btrfs_file_extent_item); |
| 158 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 159 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 160 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 161 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 162 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 163 | ptr = btrfs_file_extent_inline_start(ei); |
| 164 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 165 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | struct page *cpage; |
| 167 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 168 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 169 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 170 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | PAGE_CACHE_SIZE); |
| 172 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 173 | kaddr = kmap_atomic(cpage, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 174 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 175 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 176 | |
| 177 | i++; |
| 178 | ptr += cur_size; |
| 179 | compressed_size -= cur_size; |
| 180 | } |
| 181 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 182 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 183 | } else { |
| 184 | page = find_get_page(inode->i_mapping, |
| 185 | start >> PAGE_CACHE_SHIFT); |
| 186 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 187 | kaddr = kmap_atomic(page, KM_USER0); |
| 188 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 189 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
| 190 | kunmap_atomic(kaddr, KM_USER0); |
| 191 | page_cache_release(page); |
| 192 | } |
| 193 | btrfs_mark_buffer_dirty(leaf); |
| 194 | btrfs_free_path(path); |
| 195 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 196 | /* |
| 197 | * we're an inline extent, so nobody can |
| 198 | * extend the file past i_size without locking |
| 199 | * a page we already have locked. |
| 200 | * |
| 201 | * We must do any isize and inode updates |
| 202 | * before we unlock the pages. Otherwise we |
| 203 | * could end up racing with unlink. |
| 204 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 205 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
| 206 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 207 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 208 | return 0; |
| 209 | fail: |
| 210 | btrfs_free_path(path); |
| 211 | return err; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /* |
| 216 | * conditionally insert an inline extent into the file. This |
| 217 | * does the checks required to make sure the data is small enough |
| 218 | * to fit as an inline extent. |
| 219 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 220 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 221 | struct btrfs_root *root, |
| 222 | struct inode *inode, u64 start, u64 end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 223 | size_t compressed_size, int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 224 | struct page **compressed_pages) |
| 225 | { |
| 226 | u64 isize = i_size_read(inode); |
| 227 | u64 actual_end = min(end + 1, isize); |
| 228 | u64 inline_len = actual_end - start; |
| 229 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 230 | ~((u64)root->sectorsize - 1); |
| 231 | u64 hint_byte; |
| 232 | u64 data_len = inline_len; |
| 233 | int ret; |
| 234 | |
| 235 | if (compressed_size) |
| 236 | data_len = compressed_size; |
| 237 | |
| 238 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 239 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 240 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 241 | (!compressed_size && |
| 242 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 243 | end + 1 < isize || |
| 244 | data_len > root->fs_info->max_inline) { |
| 245 | return 1; |
| 246 | } |
| 247 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 248 | ret = btrfs_drop_extents(trans, inode, start, aligned_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 249 | &hint_byte, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 250 | BUG_ON(ret); |
| 251 | |
| 252 | if (isize > actual_end) |
| 253 | inline_len = min_t(u64, isize, actual_end); |
| 254 | ret = insert_inline_extent(trans, root, inode, start, |
| 255 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 256 | compress_type, compressed_pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 257 | BUG_ON(ret); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 258 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 259 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 263 | struct async_extent { |
| 264 | u64 start; |
| 265 | u64 ram_size; |
| 266 | u64 compressed_size; |
| 267 | struct page **pages; |
| 268 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 269 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 270 | struct list_head list; |
| 271 | }; |
| 272 | |
| 273 | struct async_cow { |
| 274 | struct inode *inode; |
| 275 | struct btrfs_root *root; |
| 276 | struct page *locked_page; |
| 277 | u64 start; |
| 278 | u64 end; |
| 279 | struct list_head extents; |
| 280 | struct btrfs_work work; |
| 281 | }; |
| 282 | |
| 283 | static noinline int add_async_extent(struct async_cow *cow, |
| 284 | u64 start, u64 ram_size, |
| 285 | u64 compressed_size, |
| 286 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 287 | unsigned long nr_pages, |
| 288 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 289 | { |
| 290 | struct async_extent *async_extent; |
| 291 | |
| 292 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Yoshinori Sano | dac97e5 | 2011-02-15 12:01:42 +0000 | [diff] [blame] | 293 | BUG_ON(!async_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 294 | async_extent->start = start; |
| 295 | async_extent->ram_size = ram_size; |
| 296 | async_extent->compressed_size = compressed_size; |
| 297 | async_extent->pages = pages; |
| 298 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 299 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 300 | list_add_tail(&async_extent->list, &cow->extents); |
| 301 | return 0; |
| 302 | } |
| 303 | |
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 | * we create compressed extents in two phases. The first |
| 306 | * phase compresses a range of pages that have already been |
| 307 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 308 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 309 | * This is done inside an ordered work queue, and the compression |
| 310 | * is spread across many cpus. The actual IO submission is step |
| 311 | * two, and the ordered work queue takes care of making sure that |
| 312 | * happens in the same order things were put onto the queue by |
| 313 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 314 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 315 | * If this code finds it can't get good compression, it puts an |
| 316 | * entry onto the work queue to write the uncompressed bytes. This |
| 317 | * makes sure that both compressed inodes and uncompressed inodes |
| 318 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 319 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 320 | static noinline int compress_file_range(struct inode *inode, |
| 321 | struct page *locked_page, |
| 322 | u64 start, u64 end, |
| 323 | struct async_cow *async_cow, |
| 324 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 325 | { |
| 326 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 327 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 328 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 329 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 330 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 331 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 332 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 333 | struct page **pages = NULL; |
| 334 | unsigned long nr_pages; |
| 335 | unsigned long nr_pages_ret = 0; |
| 336 | unsigned long total_compressed = 0; |
| 337 | unsigned long total_in = 0; |
| 338 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 339 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 340 | int i; |
| 341 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 342 | int compress_type = root->fs_info->compress_type; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 343 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 344 | /* if this is a small write inside eof, kick off a defragbot */ |
| 345 | if (end <= BTRFS_I(inode)->disk_i_size && (end - start + 1) < 16 * 1024) |
| 346 | btrfs_add_inode_defrag(NULL, inode); |
| 347 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 348 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 349 | again: |
| 350 | will_compress = 0; |
| 351 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 352 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 353 | |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 354 | /* |
| 355 | * we don't want to send crud past the end of i_size through |
| 356 | * compression, that's just a waste of CPU time. So, if the |
| 357 | * end of the file is before the start of our current |
| 358 | * requested range of bytes, we bail out to the uncompressed |
| 359 | * cleanup code that can deal with all of this. |
| 360 | * |
| 361 | * It isn't really the fastest way to fix things, but this is a |
| 362 | * very uncommon corner. |
| 363 | */ |
| 364 | if (actual_end <= start) |
| 365 | goto cleanup_and_bail_uncompressed; |
| 366 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 367 | total_compressed = actual_end - start; |
| 368 | |
| 369 | /* we want to make sure that amount of ram required to uncompress |
| 370 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 371 | * of a compressed extent to 128k. This is a crucial number |
| 372 | * because it also controls how easily we can spread reads across |
| 373 | * cpus for decompression. |
| 374 | * |
| 375 | * We also want to make sure the amount of IO required to do |
| 376 | * a random read is reasonably small, so we limit the size of |
| 377 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 378 | */ |
| 379 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 380 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 381 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 382 | total_in = 0; |
| 383 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 384 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 385 | /* |
| 386 | * we do compression for mount -o compress and when the |
| 387 | * inode has not been flagged as nocompress. This flag can |
| 388 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 389 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 390 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 391 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 392 | (BTRFS_I(inode)->force_compress) || |
| 393 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 394 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 395 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 396 | if (!pages) { |
| 397 | /* just bail out to the uncompressed code */ |
| 398 | goto cont; |
| 399 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 400 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 401 | if (BTRFS_I(inode)->force_compress) |
| 402 | compress_type = BTRFS_I(inode)->force_compress; |
| 403 | |
| 404 | ret = btrfs_compress_pages(compress_type, |
| 405 | inode->i_mapping, start, |
| 406 | total_compressed, pages, |
| 407 | nr_pages, &nr_pages_ret, |
| 408 | &total_in, |
| 409 | &total_compressed, |
| 410 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 411 | |
| 412 | if (!ret) { |
| 413 | unsigned long offset = total_compressed & |
| 414 | (PAGE_CACHE_SIZE - 1); |
| 415 | struct page *page = pages[nr_pages_ret - 1]; |
| 416 | char *kaddr; |
| 417 | |
| 418 | /* zero the tail end of the last page, we might be |
| 419 | * sending it down to disk |
| 420 | */ |
| 421 | if (offset) { |
| 422 | kaddr = kmap_atomic(page, KM_USER0); |
| 423 | memset(kaddr + offset, 0, |
| 424 | PAGE_CACHE_SIZE - offset); |
| 425 | kunmap_atomic(kaddr, KM_USER0); |
| 426 | } |
| 427 | will_compress = 1; |
| 428 | } |
| 429 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 430 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 431 | if (start == 0) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 432 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 433 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 434 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 435 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 436 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 437 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 438 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 439 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 440 | */ |
| 441 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 442 | start, end, 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 443 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 444 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 445 | ret = cow_file_range_inline(trans, root, inode, |
| 446 | start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 447 | total_compressed, |
| 448 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 449 | } |
| 450 | if (ret == 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 451 | /* |
| 452 | * inline extent creation worked, we don't need |
| 453 | * to create any more async work items. Unlock |
| 454 | * and free up our temp pages. |
| 455 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 456 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 457 | &BTRFS_I(inode)->io_tree, |
| 458 | start, end, NULL, |
| 459 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 460 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 461 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 462 | |
| 463 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 464 | goto free_pages_out; |
| 465 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 466 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | if (will_compress) { |
| 470 | /* |
| 471 | * we aren't doing an inline extent round the compressed size |
| 472 | * up to a block size boundary so the allocator does sane |
| 473 | * things |
| 474 | */ |
| 475 | total_compressed = (total_compressed + blocksize - 1) & |
| 476 | ~(blocksize - 1); |
| 477 | |
| 478 | /* |
| 479 | * one last check to make sure the compression is really a |
| 480 | * win, compare the page count read with the blocks on disk |
| 481 | */ |
| 482 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 483 | ~(PAGE_CACHE_SIZE - 1); |
| 484 | if (total_compressed >= total_in) { |
| 485 | will_compress = 0; |
| 486 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 487 | num_bytes = total_in; |
| 488 | } |
| 489 | } |
| 490 | if (!will_compress && pages) { |
| 491 | /* |
| 492 | * the compression code ran but failed to make things smaller, |
| 493 | * free any pages it allocated and our page pointer array |
| 494 | */ |
| 495 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 496 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 497 | page_cache_release(pages[i]); |
| 498 | } |
| 499 | kfree(pages); |
| 500 | pages = NULL; |
| 501 | total_compressed = 0; |
| 502 | nr_pages_ret = 0; |
| 503 | |
| 504 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 505 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 506 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 507 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 508 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 509 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 510 | if (will_compress) { |
| 511 | *num_added += 1; |
| 512 | |
| 513 | /* the async work queues will take care of doing actual |
| 514 | * allocation on disk for these compressed pages, |
| 515 | * and will submit them to the elevator. |
| 516 | */ |
| 517 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 518 | total_compressed, pages, nr_pages_ret, |
| 519 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 520 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 521 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 522 | start += num_bytes; |
| 523 | pages = NULL; |
| 524 | cond_resched(); |
| 525 | goto again; |
| 526 | } |
| 527 | } else { |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 528 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 529 | /* |
| 530 | * No compression, but we still need to write the pages in |
| 531 | * the file we've been given so far. redirty the locked |
| 532 | * page if it corresponds to our extent and set things up |
| 533 | * for the async work queue to run cow_file_range to do |
| 534 | * the normal delalloc dance |
| 535 | */ |
| 536 | if (page_offset(locked_page) >= start && |
| 537 | page_offset(locked_page) <= end) { |
| 538 | __set_page_dirty_nobuffers(locked_page); |
| 539 | /* unlocked later on in the async handlers */ |
| 540 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 541 | add_async_extent(async_cow, start, end - start + 1, |
| 542 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 543 | *num_added += 1; |
| 544 | } |
| 545 | |
| 546 | out: |
| 547 | return 0; |
| 548 | |
| 549 | free_pages_out: |
| 550 | for (i = 0; i < nr_pages_ret; i++) { |
| 551 | WARN_ON(pages[i]->mapping); |
| 552 | page_cache_release(pages[i]); |
| 553 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 554 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 555 | |
| 556 | goto out; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * phase two of compressed writeback. This is the ordered portion |
| 561 | * of the code, which only gets called in the order the work was |
| 562 | * queued. We walk all the async extents created by compress_file_range |
| 563 | * and send them down to the disk. |
| 564 | */ |
| 565 | static noinline int submit_compressed_extents(struct inode *inode, |
| 566 | struct async_cow *async_cow) |
| 567 | { |
| 568 | struct async_extent *async_extent; |
| 569 | u64 alloc_hint = 0; |
| 570 | struct btrfs_trans_handle *trans; |
| 571 | struct btrfs_key ins; |
| 572 | struct extent_map *em; |
| 573 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 574 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 575 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 576 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 577 | |
| 578 | if (list_empty(&async_cow->extents)) |
| 579 | return 0; |
| 580 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 581 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 582 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 583 | async_extent = list_entry(async_cow->extents.next, |
| 584 | struct async_extent, list); |
| 585 | list_del(&async_extent->list); |
| 586 | |
| 587 | io_tree = &BTRFS_I(inode)->io_tree; |
| 588 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 589 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 590 | /* did the compression code fall back to uncompressed IO? */ |
| 591 | if (!async_extent->pages) { |
| 592 | int page_started = 0; |
| 593 | unsigned long nr_written = 0; |
| 594 | |
| 595 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 596 | async_extent->start + |
| 597 | async_extent->ram_size - 1, GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 598 | |
| 599 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 600 | ret = cow_file_range(inode, async_cow->locked_page, |
| 601 | async_extent->start, |
| 602 | async_extent->start + |
| 603 | async_extent->ram_size - 1, |
| 604 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * if page_started, cow_file_range inserted an |
| 608 | * inline extent and took care of all the unlocking |
| 609 | * and IO for us. Otherwise, we need to submit |
| 610 | * all those pages down to the drive. |
| 611 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 612 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 613 | extent_write_locked_range(io_tree, |
| 614 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 615 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 616 | async_extent->ram_size - 1, |
| 617 | btrfs_get_extent, |
| 618 | WB_SYNC_ALL); |
| 619 | kfree(async_extent); |
| 620 | cond_resched(); |
| 621 | continue; |
| 622 | } |
| 623 | |
| 624 | lock_extent(io_tree, async_extent->start, |
| 625 | async_extent->start + async_extent->ram_size - 1, |
| 626 | GFP_NOFS); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 627 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 628 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 629 | BUG_ON(IS_ERR(trans)); |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 630 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 631 | ret = btrfs_reserve_extent(trans, root, |
| 632 | async_extent->compressed_size, |
| 633 | async_extent->compressed_size, |
| 634 | 0, alloc_hint, |
| 635 | (u64)-1, &ins, 1); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 636 | btrfs_end_transaction(trans, root); |
| 637 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 638 | if (ret) { |
| 639 | int i; |
| 640 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 641 | WARN_ON(async_extent->pages[i]->mapping); |
| 642 | page_cache_release(async_extent->pages[i]); |
| 643 | } |
| 644 | kfree(async_extent->pages); |
| 645 | async_extent->nr_pages = 0; |
| 646 | async_extent->pages = NULL; |
| 647 | unlock_extent(io_tree, async_extent->start, |
| 648 | async_extent->start + |
| 649 | async_extent->ram_size - 1, GFP_NOFS); |
| 650 | goto retry; |
| 651 | } |
| 652 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 653 | /* |
| 654 | * here we're doing allocation and writeback of the |
| 655 | * compressed pages |
| 656 | */ |
| 657 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 658 | async_extent->start + |
| 659 | async_extent->ram_size - 1, 0); |
| 660 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 661 | em = alloc_extent_map(); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 662 | BUG_ON(!em); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 663 | em->start = async_extent->start; |
| 664 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 665 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 666 | |
| 667 | em->block_start = ins.objectid; |
| 668 | em->block_len = ins.offset; |
| 669 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 670 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 671 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 672 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 673 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 674 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 675 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 676 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 677 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 678 | if (ret != -EEXIST) { |
| 679 | free_extent_map(em); |
| 680 | break; |
| 681 | } |
| 682 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 683 | async_extent->start + |
| 684 | async_extent->ram_size - 1, 0); |
| 685 | } |
| 686 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 687 | ret = btrfs_add_ordered_extent_compress(inode, |
| 688 | async_extent->start, |
| 689 | ins.objectid, |
| 690 | async_extent->ram_size, |
| 691 | ins.offset, |
| 692 | BTRFS_ORDERED_COMPRESSED, |
| 693 | async_extent->compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 694 | BUG_ON(ret); |
| 695 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 696 | /* |
| 697 | * clear dirty, set writeback and unlock the pages. |
| 698 | */ |
| 699 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 700 | &BTRFS_I(inode)->io_tree, |
| 701 | async_extent->start, |
| 702 | async_extent->start + |
| 703 | async_extent->ram_size - 1, |
| 704 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 705 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 706 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 707 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 708 | |
| 709 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 710 | async_extent->start, |
| 711 | async_extent->ram_size, |
| 712 | ins.objectid, |
| 713 | ins.offset, async_extent->pages, |
| 714 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 715 | |
| 716 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 717 | alloc_hint = ins.objectid + ins.offset; |
| 718 | kfree(async_extent); |
| 719 | cond_resched(); |
| 720 | } |
| 721 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 722 | return 0; |
| 723 | } |
| 724 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 725 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 726 | u64 num_bytes) |
| 727 | { |
| 728 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 729 | struct extent_map *em; |
| 730 | u64 alloc_hint = 0; |
| 731 | |
| 732 | read_lock(&em_tree->lock); |
| 733 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 734 | if (em) { |
| 735 | /* |
| 736 | * if block start isn't an actual block number then find the |
| 737 | * first block in this inode and use that as a hint. If that |
| 738 | * block is also bogus then just don't worry about it. |
| 739 | */ |
| 740 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 741 | free_extent_map(em); |
| 742 | em = search_extent_mapping(em_tree, 0, 0); |
| 743 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 744 | alloc_hint = em->block_start; |
| 745 | if (em) |
| 746 | free_extent_map(em); |
| 747 | } else { |
| 748 | alloc_hint = em->block_start; |
| 749 | free_extent_map(em); |
| 750 | } |
| 751 | } |
| 752 | read_unlock(&em_tree->lock); |
| 753 | |
| 754 | return alloc_hint; |
| 755 | } |
| 756 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 757 | /* |
| 758 | * when extent_io.c finds a delayed allocation range in the file, |
| 759 | * the call backs end up in this code. The basic idea is to |
| 760 | * allocate extents on disk for the range, and create ordered data structs |
| 761 | * in ram to track those extents. |
| 762 | * |
| 763 | * locked_page is the page that writepage had locked already. We use |
| 764 | * it to make sure we don't do extra locks or unlocks. |
| 765 | * |
| 766 | * *page_started is set to one if we unlock locked_page and do everything |
| 767 | * required to start IO on it. It may be clean and already done with |
| 768 | * IO when we return. |
| 769 | */ |
| 770 | static noinline int cow_file_range(struct inode *inode, |
| 771 | struct page *locked_page, |
| 772 | u64 start, u64 end, int *page_started, |
| 773 | unsigned long *nr_written, |
| 774 | int unlock) |
| 775 | { |
| 776 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 777 | struct btrfs_trans_handle *trans; |
| 778 | u64 alloc_hint = 0; |
| 779 | u64 num_bytes; |
| 780 | unsigned long ram_size; |
| 781 | u64 disk_num_bytes; |
| 782 | u64 cur_alloc_size; |
| 783 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 784 | struct btrfs_key ins; |
| 785 | struct extent_map *em; |
| 786 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 787 | int ret = 0; |
| 788 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 789 | BUG_ON(btrfs_is_free_space_inode(root, inode)); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 790 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 791 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 792 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 793 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 794 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 795 | num_bytes = max(blocksize, num_bytes); |
| 796 | disk_num_bytes = num_bytes; |
| 797 | ret = 0; |
| 798 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 799 | /* if this is a small write inside eof, kick off defrag */ |
| 800 | if (end <= BTRFS_I(inode)->disk_i_size && num_bytes < 64 * 1024) |
| 801 | btrfs_add_inode_defrag(trans, inode); |
| 802 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 803 | if (start == 0) { |
| 804 | /* lets try to make an inline extent */ |
| 805 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 806 | start, end, 0, 0, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 807 | if (ret == 0) { |
| 808 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 809 | &BTRFS_I(inode)->io_tree, |
| 810 | start, end, NULL, |
| 811 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 812 | EXTENT_CLEAR_UNLOCK | |
| 813 | EXTENT_CLEAR_DELALLOC | |
| 814 | EXTENT_CLEAR_DIRTY | |
| 815 | EXTENT_SET_WRITEBACK | |
| 816 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 817 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 818 | *nr_written = *nr_written + |
| 819 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 820 | *page_started = 1; |
| 821 | ret = 0; |
| 822 | goto out; |
| 823 | } |
| 824 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 825 | |
| 826 | BUG_ON(disk_num_bytes > |
| 827 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
| 828 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 829 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 830 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 831 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 832 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 833 | unsigned long op; |
| 834 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 835 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 836 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 837 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 838 | (u64)-1, &ins, 1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 839 | BUG_ON(ret); |
| 840 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 841 | em = alloc_extent_map(); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 842 | BUG_ON(!em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 843 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 844 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 845 | ram_size = ins.offset; |
| 846 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 847 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 848 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 849 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 850 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 851 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 852 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 853 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 854 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 855 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 856 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 857 | if (ret != -EEXIST) { |
| 858 | free_extent_map(em); |
| 859 | break; |
| 860 | } |
| 861 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 862 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 863 | } |
| 864 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 865 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 866 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 867 | ram_size, cur_alloc_size, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 868 | BUG_ON(ret); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 869 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 870 | if (root->root_key.objectid == |
| 871 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 872 | ret = btrfs_reloc_clone_csums(inode, start, |
| 873 | cur_alloc_size); |
| 874 | BUG_ON(ret); |
| 875 | } |
| 876 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 877 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 878 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 879 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 880 | /* we're not doing compressed IO, don't unlock the first |
| 881 | * page (which the caller expects to stay locked), don't |
| 882 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 883 | * |
| 884 | * Do set the Private2 bit so we know this page was properly |
| 885 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 886 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 887 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 888 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 889 | EXTENT_SET_PRIVATE2; |
| 890 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 891 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 892 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 893 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 894 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 895 | num_bytes -= cur_alloc_size; |
| 896 | alloc_hint = ins.objectid + ins.offset; |
| 897 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 898 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 899 | out: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 900 | ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 901 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 902 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 903 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 904 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 905 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 906 | /* |
| 907 | * work queue call back to started compression on a file and pages |
| 908 | */ |
| 909 | static noinline void async_cow_start(struct btrfs_work *work) |
| 910 | { |
| 911 | struct async_cow *async_cow; |
| 912 | int num_added = 0; |
| 913 | async_cow = container_of(work, struct async_cow, work); |
| 914 | |
| 915 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 916 | async_cow->start, async_cow->end, async_cow, |
| 917 | &num_added); |
| 918 | if (num_added == 0) |
| 919 | async_cow->inode = NULL; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * work queue call back to submit previously compressed pages |
| 924 | */ |
| 925 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 926 | { |
| 927 | struct async_cow *async_cow; |
| 928 | struct btrfs_root *root; |
| 929 | unsigned long nr_pages; |
| 930 | |
| 931 | async_cow = container_of(work, struct async_cow, work); |
| 932 | |
| 933 | root = async_cow->root; |
| 934 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 935 | PAGE_CACHE_SHIFT; |
| 936 | |
| 937 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 938 | |
| 939 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 940 | 5 * 1042 * 1024 && |
| 941 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 942 | wake_up(&root->fs_info->async_submit_wait); |
| 943 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 944 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 945 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 946 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 947 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 948 | static noinline void async_cow_free(struct btrfs_work *work) |
| 949 | { |
| 950 | struct async_cow *async_cow; |
| 951 | async_cow = container_of(work, struct async_cow, work); |
| 952 | kfree(async_cow); |
| 953 | } |
| 954 | |
| 955 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 956 | u64 start, u64 end, int *page_started, |
| 957 | unsigned long *nr_written) |
| 958 | { |
| 959 | struct async_cow *async_cow; |
| 960 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 961 | unsigned long nr_pages; |
| 962 | u64 cur_end; |
| 963 | int limit = 10 * 1024 * 1042; |
| 964 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 965 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 966 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 967 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 968 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 969 | BUG_ON(!async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 970 | async_cow->inode = inode; |
| 971 | async_cow->root = root; |
| 972 | async_cow->locked_page = locked_page; |
| 973 | async_cow->start = start; |
| 974 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 975 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 976 | cur_end = end; |
| 977 | else |
| 978 | cur_end = min(end, start + 512 * 1024 - 1); |
| 979 | |
| 980 | async_cow->end = cur_end; |
| 981 | INIT_LIST_HEAD(&async_cow->extents); |
| 982 | |
| 983 | async_cow->work.func = async_cow_start; |
| 984 | async_cow->work.ordered_func = async_cow_submit; |
| 985 | async_cow->work.ordered_free = async_cow_free; |
| 986 | async_cow->work.flags = 0; |
| 987 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 988 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 989 | PAGE_CACHE_SHIFT; |
| 990 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 991 | |
| 992 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 993 | &async_cow->work); |
| 994 | |
| 995 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 996 | wait_event(root->fs_info->async_submit_wait, |
| 997 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 998 | limit)); |
| 999 | } |
| 1000 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1001 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1002 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1003 | wait_event(root->fs_info->async_submit_wait, |
| 1004 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1005 | 0)); |
| 1006 | } |
| 1007 | |
| 1008 | *nr_written += nr_pages; |
| 1009 | start = cur_end + 1; |
| 1010 | } |
| 1011 | *page_started = 1; |
| 1012 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1013 | } |
| 1014 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1015 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1016 | u64 bytenr, u64 num_bytes) |
| 1017 | { |
| 1018 | int ret; |
| 1019 | struct btrfs_ordered_sum *sums; |
| 1020 | LIST_HEAD(list); |
| 1021 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1022 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1023 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1024 | if (ret == 0 && list_empty(&list)) |
| 1025 | return 0; |
| 1026 | |
| 1027 | while (!list_empty(&list)) { |
| 1028 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1029 | list_del(&sums->list); |
| 1030 | kfree(sums); |
| 1031 | } |
| 1032 | return 1; |
| 1033 | } |
| 1034 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1035 | /* |
| 1036 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1037 | * of the extents that exist in the file, and COWs the file as required. |
| 1038 | * |
| 1039 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1040 | * blocks on disk |
| 1041 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1042 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1043 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1044 | u64 start, u64 end, int *page_started, int force, |
| 1045 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1046 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1047 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1048 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1049 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1050 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1051 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1052 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1053 | u64 cow_start; |
| 1054 | u64 cur_offset; |
| 1055 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1056 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1057 | u64 disk_bytenr; |
| 1058 | u64 num_bytes; |
| 1059 | int extent_type; |
| 1060 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1061 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1062 | int nocow; |
| 1063 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1064 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1065 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1066 | |
| 1067 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1068 | if (!path) |
| 1069 | return -ENOMEM; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1070 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1071 | nolock = btrfs_is_free_space_inode(root, inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1072 | |
| 1073 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1074 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1075 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1076 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1077 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1078 | BUG_ON(IS_ERR(trans)); |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1079 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
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 | cow_start = (u64)-1; |
| 1082 | cur_offset = start; |
| 1083 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1084 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1085 | cur_offset, 0); |
| 1086 | BUG_ON(ret < 0); |
| 1087 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1088 | leaf = path->nodes[0]; |
| 1089 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1090 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1091 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1092 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1093 | path->slots[0]--; |
| 1094 | } |
| 1095 | check_prev = 0; |
| 1096 | next_slot: |
| 1097 | leaf = path->nodes[0]; |
| 1098 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1099 | ret = btrfs_next_leaf(root, path); |
| 1100 | if (ret < 0) |
| 1101 | BUG_ON(1); |
| 1102 | if (ret > 0) |
| 1103 | break; |
| 1104 | leaf = path->nodes[0]; |
| 1105 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1106 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1107 | nocow = 0; |
| 1108 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1109 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1110 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1111 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1112 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1113 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1114 | found_key.offset > end) |
| 1115 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1116 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1117 | if (found_key.offset > cur_offset) { |
| 1118 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1119 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1120 | goto out_check; |
| 1121 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1122 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1123 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1124 | struct btrfs_file_extent_item); |
| 1125 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1126 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1127 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1128 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1129 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1130 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1131 | extent_end = found_key.offset + |
| 1132 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1133 | if (extent_end <= start) { |
| 1134 | path->slots[0]++; |
| 1135 | goto next_slot; |
| 1136 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1137 | if (disk_bytenr == 0) |
| 1138 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1139 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1140 | btrfs_file_extent_encryption(leaf, fi) || |
| 1141 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1142 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1143 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1144 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1145 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1146 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1147 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1148 | found_key.offset - |
| 1149 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1150 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1151 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1152 | disk_bytenr += cur_offset - found_key.offset; |
| 1153 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1154 | /* |
| 1155 | * force cow if csum exists in the range. |
| 1156 | * this ensure that csum for a given extent are |
| 1157 | * either valid or do not exist. |
| 1158 | */ |
| 1159 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1160 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1161 | nocow = 1; |
| 1162 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1163 | extent_end = found_key.offset + |
| 1164 | btrfs_file_extent_inline_len(leaf, fi); |
| 1165 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1166 | } else { |
| 1167 | BUG_ON(1); |
| 1168 | } |
| 1169 | out_check: |
| 1170 | if (extent_end <= start) { |
| 1171 | path->slots[0]++; |
| 1172 | goto next_slot; |
| 1173 | } |
| 1174 | if (!nocow) { |
| 1175 | if (cow_start == (u64)-1) |
| 1176 | cow_start = cur_offset; |
| 1177 | cur_offset = extent_end; |
| 1178 | if (cur_offset > end) |
| 1179 | break; |
| 1180 | path->slots[0]++; |
| 1181 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1182 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1183 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1184 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1185 | if (cow_start != (u64)-1) { |
| 1186 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1187 | found_key.offset - 1, page_started, |
| 1188 | nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1189 | BUG_ON(ret); |
| 1190 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1191 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1192 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1193 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1194 | struct extent_map *em; |
| 1195 | struct extent_map_tree *em_tree; |
| 1196 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1197 | em = alloc_extent_map(); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 1198 | BUG_ON(!em); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1199 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1200 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1201 | em->len = num_bytes; |
| 1202 | em->block_len = num_bytes; |
| 1203 | em->block_start = disk_bytenr; |
| 1204 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1205 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1206 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1207 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1208 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1209 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1210 | if (ret != -EEXIST) { |
| 1211 | free_extent_map(em); |
| 1212 | break; |
| 1213 | } |
| 1214 | btrfs_drop_extent_cache(inode, em->start, |
| 1215 | em->start + em->len - 1, 0); |
| 1216 | } |
| 1217 | type = BTRFS_ORDERED_PREALLOC; |
| 1218 | } else { |
| 1219 | type = BTRFS_ORDERED_NOCOW; |
| 1220 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1221 | |
| 1222 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1223 | num_bytes, num_bytes, type); |
| 1224 | BUG_ON(ret); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1225 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1226 | if (root->root_key.objectid == |
| 1227 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1228 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1229 | num_bytes); |
| 1230 | BUG_ON(ret); |
| 1231 | } |
| 1232 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1233 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1234 | cur_offset, cur_offset + num_bytes - 1, |
| 1235 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1236 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1237 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1238 | cur_offset = extent_end; |
| 1239 | if (cur_offset > end) |
| 1240 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1241 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1242 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1243 | |
| 1244 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1245 | cow_start = cur_offset; |
| 1246 | if (cow_start != (u64)-1) { |
| 1247 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1248 | page_started, nr_written, 1); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1249 | BUG_ON(ret); |
| 1250 | } |
| 1251 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1252 | if (nolock) { |
| 1253 | ret = btrfs_end_transaction_nolock(trans, root); |
| 1254 | BUG_ON(ret); |
| 1255 | } else { |
| 1256 | ret = btrfs_end_transaction(trans, root); |
| 1257 | BUG_ON(ret); |
| 1258 | } |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1259 | btrfs_free_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1260 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1261 | } |
| 1262 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1263 | /* |
| 1264 | * extent_io.c call back to do delayed allocation processing |
| 1265 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1266 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1267 | u64 start, u64 end, int *page_started, |
| 1268 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1269 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1270 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1271 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1272 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1273 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1274 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1275 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1276 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1277 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1278 | page_started, 0, nr_written); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1279 | else if (!btrfs_test_opt(root, COMPRESS) && |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 1280 | !(BTRFS_I(inode)->force_compress) && |
| 1281 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1282 | ret = cow_file_range(inode, locked_page, start, end, |
| 1283 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1284 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1285 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1286 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1287 | return ret; |
| 1288 | } |
| 1289 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1290 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1291 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1292 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1293 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1294 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1295 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1296 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1297 | spin_lock(&BTRFS_I(inode)->lock); |
| 1298 | BTRFS_I(inode)->outstanding_extents++; |
| 1299 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | /* |
| 1303 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1304 | * extents so we can keep track of new extents that are just merged onto old |
| 1305 | * extents, such as when we are doing sequential writes, so we can properly |
| 1306 | * account for the metadata space we'll need. |
| 1307 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1308 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1309 | struct extent_state *new, |
| 1310 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1311 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1312 | /* not delalloc, ignore it */ |
| 1313 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1314 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1315 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1316 | spin_lock(&BTRFS_I(inode)->lock); |
| 1317 | BTRFS_I(inode)->outstanding_extents--; |
| 1318 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1319 | } |
| 1320 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1321 | /* |
| 1322 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1323 | * bytes in this file, and to maintain the list of inodes that |
| 1324 | * have pending delalloc work to be done. |
| 1325 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1326 | static void btrfs_set_bit_hook(struct inode *inode, |
| 1327 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1328 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1329 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1330 | /* |
| 1331 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1332 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1333 | * bit, which is only set or cleared with irqs on |
| 1334 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1335 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1336 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1337 | u64 len = state->end + 1 - state->start; |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1338 | bool do_list = !btrfs_is_free_space_inode(root, inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1339 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1340 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1341 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1342 | } else { |
| 1343 | spin_lock(&BTRFS_I(inode)->lock); |
| 1344 | BTRFS_I(inode)->outstanding_extents++; |
| 1345 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1346 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1347 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1348 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1349 | BTRFS_I(inode)->delalloc_bytes += len; |
| 1350 | root->fs_info->delalloc_bytes += len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1351 | if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1352 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1353 | &root->fs_info->delalloc_inodes); |
| 1354 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1355 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1356 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1357 | } |
| 1358 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1359 | /* |
| 1360 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1361 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1362 | static void btrfs_clear_bit_hook(struct inode *inode, |
| 1363 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1364 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1365 | /* |
| 1366 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1367 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1368 | * bit, which is only set or cleared with irqs on |
| 1369 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1370 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1371 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1372 | u64 len = state->end + 1 - state->start; |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1373 | bool do_list = !btrfs_is_free_space_inode(root, inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1374 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1375 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1376 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1377 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1378 | spin_lock(&BTRFS_I(inode)->lock); |
| 1379 | BTRFS_I(inode)->outstanding_extents--; |
| 1380 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1381 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1382 | |
| 1383 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1384 | btrfs_delalloc_release_metadata(inode, len); |
| 1385 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1386 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 1387 | && do_list) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1388 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1389 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1390 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1391 | root->fs_info->delalloc_bytes -= len; |
| 1392 | BTRFS_I(inode)->delalloc_bytes -= len; |
| 1393 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1394 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1395 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1396 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1397 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1398 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1399 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1400 | } |
| 1401 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1402 | /* |
| 1403 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1404 | * we don't create bios that span stripes or chunks |
| 1405 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1406 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1407 | size_t size, struct bio *bio, |
| 1408 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1409 | { |
| 1410 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1411 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1412 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1413 | u64 length = 0; |
| 1414 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1415 | int ret; |
| 1416 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1417 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1418 | return 0; |
| 1419 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1420 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1421 | map_tree = &root->fs_info->mapping_tree; |
| 1422 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1423 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1424 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1425 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1426 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1427 | return 1; |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 1428 | return ret; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1429 | } |
| 1430 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1431 | /* |
| 1432 | * in order to insert checksums into the metadata in large chunks, |
| 1433 | * we wait until bio submission time. All the pages in the bio are |
| 1434 | * checksummed and sums are attached onto the ordered extent record. |
| 1435 | * |
| 1436 | * At IO completion time the cums attached on the ordered extent record |
| 1437 | * are inserted into the btree |
| 1438 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1439 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1440 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1441 | unsigned long bio_flags, |
| 1442 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1443 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1444 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1445 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1446 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1447 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1448 | BUG_ON(ret); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1449 | return 0; |
| 1450 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1451 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1452 | /* |
| 1453 | * in order to insert checksums into the metadata in large chunks, |
| 1454 | * we wait until bio submission time. All the pages in the bio are |
| 1455 | * checksummed and sums are attached onto the ordered extent record. |
| 1456 | * |
| 1457 | * At IO completion time the cums attached on the ordered extent record |
| 1458 | * are inserted into the btree |
| 1459 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1460 | 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] | 1461 | int mirror_num, unsigned long bio_flags, |
| 1462 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1463 | { |
| 1464 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1465 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1466 | } |
| 1467 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1468 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1469 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1470 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1471 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1472 | 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] | 1473 | int mirror_num, unsigned long bio_flags, |
| 1474 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1475 | { |
| 1476 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1477 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1478 | int skip_sum; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1479 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1480 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1481 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1482 | if (btrfs_is_free_space_inode(root, inode)) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1483 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2); |
| 1484 | else |
| 1485 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1486 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1487 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1488 | if (!(rw & REQ_WRITE)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1489 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1490 | return btrfs_submit_compressed_read(inode, bio, |
| 1491 | mirror_num, bio_flags); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1492 | } else if (!skip_sum) { |
| 1493 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1494 | if (ret) |
| 1495 | return ret; |
| 1496 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1497 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1498 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1499 | /* csum items have already been cloned */ |
| 1500 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1501 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1502 | /* we're doing a write, do the async checksumming */ |
| 1503 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1504 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1505 | bio_flags, bio_offset, |
| 1506 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1507 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1508 | } |
| 1509 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1510 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1511 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1512 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1513 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1514 | /* |
| 1515 | * given a list of ordered sums record them in the inode. This happens |
| 1516 | * at IO completion time based on sums calculated at bio submission time. |
| 1517 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1518 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1519 | struct inode *inode, u64 file_offset, |
| 1520 | struct list_head *list) |
| 1521 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1522 | struct btrfs_ordered_sum *sum; |
| 1523 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1524 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1525 | btrfs_csum_file_blocks(trans, |
| 1526 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1527 | } |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1531 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1532 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1533 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1534 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1535 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1536 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1537 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1538 | } |
| 1539 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1540 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1541 | struct btrfs_writepage_fixup { |
| 1542 | struct page *page; |
| 1543 | struct btrfs_work work; |
| 1544 | }; |
| 1545 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1546 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1547 | { |
| 1548 | struct btrfs_writepage_fixup *fixup; |
| 1549 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1550 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1551 | struct page *page; |
| 1552 | struct inode *inode; |
| 1553 | u64 page_start; |
| 1554 | u64 page_end; |
| 1555 | |
| 1556 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1557 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1558 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1559 | lock_page(page); |
| 1560 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1561 | ClearPageChecked(page); |
| 1562 | goto out_page; |
| 1563 | } |
| 1564 | |
| 1565 | inode = page->mapping->host; |
| 1566 | page_start = page_offset(page); |
| 1567 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1568 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1569 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
| 1570 | &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1571 | |
| 1572 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1573 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1574 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1575 | |
| 1576 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1577 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1578 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1579 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1580 | unlock_page(page); |
| 1581 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1582 | goto again; |
| 1583 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1584 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1585 | BUG(); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1586 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1587 | ClearPageChecked(page); |
| 1588 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1589 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1590 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1591 | out_page: |
| 1592 | unlock_page(page); |
| 1593 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1594 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | /* |
| 1598 | * There are a few paths in the higher layers of the kernel that directly |
| 1599 | * set the page dirty bit without asking the filesystem if it is a |
| 1600 | * good idea. This causes problems because we want to make sure COW |
| 1601 | * properly happens and the data=ordered rules are followed. |
| 1602 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1603 | * 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] | 1604 | * hasn't been properly setup for IO. We kick off an async process |
| 1605 | * to fix it up. The async helper will wait for ordered extents, set |
| 1606 | * the delalloc bit and make it safe to write the page. |
| 1607 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1608 | 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] | 1609 | { |
| 1610 | struct inode *inode = page->mapping->host; |
| 1611 | struct btrfs_writepage_fixup *fixup; |
| 1612 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1613 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1614 | /* this page is properly in the ordered list */ |
| 1615 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1616 | return 0; |
| 1617 | |
| 1618 | if (PageChecked(page)) |
| 1619 | return -EAGAIN; |
| 1620 | |
| 1621 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1622 | if (!fixup) |
| 1623 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1624 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1625 | SetPageChecked(page); |
| 1626 | page_cache_get(page); |
| 1627 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1628 | fixup->page = page; |
| 1629 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 1630 | return -EAGAIN; |
| 1631 | } |
| 1632 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1633 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1634 | struct inode *inode, u64 file_pos, |
| 1635 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1636 | u64 num_bytes, u64 ram_bytes, |
| 1637 | u8 compression, u8 encryption, |
| 1638 | u16 other_encoding, int extent_type) |
| 1639 | { |
| 1640 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1641 | struct btrfs_file_extent_item *fi; |
| 1642 | struct btrfs_path *path; |
| 1643 | struct extent_buffer *leaf; |
| 1644 | struct btrfs_key ins; |
| 1645 | u64 hint; |
| 1646 | int ret; |
| 1647 | |
| 1648 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1649 | if (!path) |
| 1650 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1651 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1652 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1653 | |
| 1654 | /* |
| 1655 | * we may be replacing one extent in the tree with another. |
| 1656 | * The new extent is pinned in the extent map, and we don't want |
| 1657 | * to drop it from the cache until it is completely in the btree. |
| 1658 | * |
| 1659 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1660 | * the caller is expected to unpin it and allow it to be merged |
| 1661 | * with the others. |
| 1662 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1663 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1664 | &hint, 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1665 | BUG_ON(ret); |
| 1666 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1667 | ins.objectid = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1668 | ins.offset = file_pos; |
| 1669 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1670 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
| 1671 | BUG_ON(ret); |
| 1672 | leaf = path->nodes[0]; |
| 1673 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1674 | struct btrfs_file_extent_item); |
| 1675 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1676 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1677 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1678 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1679 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1680 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1681 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1682 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1683 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1684 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1685 | |
| 1686 | btrfs_unlock_up_safe(path, 1); |
| 1687 | btrfs_set_lock_blocking(leaf); |
| 1688 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1689 | btrfs_mark_buffer_dirty(leaf); |
| 1690 | |
| 1691 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1692 | |
| 1693 | ins.objectid = disk_bytenr; |
| 1694 | ins.offset = disk_num_bytes; |
| 1695 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1696 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1697 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1698 | btrfs_ino(inode), file_pos, &ins); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1699 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1700 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1701 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1702 | return 0; |
| 1703 | } |
| 1704 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1705 | /* |
| 1706 | * helper function for btrfs_finish_ordered_io, this |
| 1707 | * just reads in some of the csum leaves to prime them into ram |
| 1708 | * before we start the transaction. It limits the amount of btree |
| 1709 | * reads required while inside the transaction. |
| 1710 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1711 | /* as ordered data IO finishes, this gets called so we can finish |
| 1712 | * an ordered extent if the range of bytes in the file it covers are |
| 1713 | * fully written. |
| 1714 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1715 | 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] | 1716 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1717 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1718 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1719 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1720 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1721 | struct extent_state *cached_state = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1722 | int compress_type = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1723 | int ret; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1724 | bool nolock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1725 | |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 1726 | ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 1727 | end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1728 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1729 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1730 | BUG_ON(!ordered_extent); |
Josef Bacik | efd049f | 2010-02-02 20:50:10 +0000 | [diff] [blame] | 1731 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1732 | nolock = btrfs_is_free_space_inode(root, inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1733 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1734 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
| 1735 | BUG_ON(!list_empty(&ordered_extent->list)); |
| 1736 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1737 | if (!ret) { |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1738 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1739 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1740 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1741 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1742 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1743 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1744 | ret = btrfs_update_inode(trans, root, inode); |
| 1745 | BUG_ON(ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1746 | } |
| 1747 | goto out; |
| 1748 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1749 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1750 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 1751 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 1752 | 0, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1753 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1754 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1755 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1756 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1757 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1758 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1759 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1760 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1761 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1762 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1763 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1764 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1765 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1766 | ordered_extent->file_offset, |
| 1767 | ordered_extent->file_offset + |
| 1768 | ordered_extent->len); |
| 1769 | BUG_ON(ret); |
| 1770 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 1771 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1772 | ret = insert_reserved_file_extent(trans, inode, |
| 1773 | ordered_extent->file_offset, |
| 1774 | ordered_extent->start, |
| 1775 | ordered_extent->disk_len, |
| 1776 | ordered_extent->len, |
| 1777 | ordered_extent->len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1778 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1779 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1780 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1781 | ordered_extent->file_offset, |
| 1782 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1783 | BUG_ON(ret); |
| 1784 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1785 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 1786 | ordered_extent->file_offset + |
| 1787 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
| 1788 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1789 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1790 | &ordered_extent->list); |
| 1791 | |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 1792 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
Miao Xie | a39f752 | 2011-09-11 10:52:25 -0400 | [diff] [blame] | 1793 | if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 1794 | ret = btrfs_update_inode(trans, root, inode); |
| 1795 | BUG_ON(ret); |
| 1796 | } |
| 1797 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1798 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 1799 | if (root != root->fs_info->tree_root) |
| 1800 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
| 1801 | if (trans) { |
| 1802 | if (nolock) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1803 | btrfs_end_transaction_nolock(trans, root); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 1804 | else |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1805 | btrfs_end_transaction(trans, root); |
| 1806 | } |
| 1807 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1808 | /* once for us */ |
| 1809 | btrfs_put_ordered_extent(ordered_extent); |
| 1810 | /* once for the tree */ |
| 1811 | btrfs_put_ordered_extent(ordered_extent); |
| 1812 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1813 | return 0; |
| 1814 | } |
| 1815 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1816 | 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] | 1817 | struct extent_state *state, int uptodate) |
| 1818 | { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1819 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 1820 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1821 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1822 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1823 | } |
| 1824 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1825 | /* |
| 1826 | * When IO fails, either with EIO or csum verification fails, we |
| 1827 | * try other mirrors that might have a good copy of the data. This |
| 1828 | * io_failure_record is used to record state as we go through all the |
| 1829 | * mirrors. If another mirror has good data, the page is set up to date |
| 1830 | * and things continue. If a good mirror can't be found, the original |
| 1831 | * bio end_io callback is called to indicate things have failed. |
| 1832 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1833 | struct io_failure_record { |
| 1834 | struct page *page; |
| 1835 | u64 start; |
| 1836 | u64 len; |
| 1837 | u64 logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1838 | unsigned long bio_flags; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1839 | int last_mirror; |
| 1840 | }; |
| 1841 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1842 | static int btrfs_io_failed_hook(struct bio *failed_bio, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1843 | struct page *page, u64 start, u64 end, |
| 1844 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1845 | { |
| 1846 | struct io_failure_record *failrec = NULL; |
| 1847 | u64 private; |
| 1848 | struct extent_map *em; |
| 1849 | struct inode *inode = page->mapping->host; |
| 1850 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1851 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1852 | struct bio *bio; |
| 1853 | int num_copies; |
| 1854 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1855 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1856 | u64 logical; |
| 1857 | |
| 1858 | ret = get_state_private(failure_tree, start, &private); |
| 1859 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1860 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 1861 | if (!failrec) |
| 1862 | return -ENOMEM; |
| 1863 | failrec->start = start; |
| 1864 | failrec->len = end - start + 1; |
| 1865 | failrec->last_mirror = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1866 | failrec->bio_flags = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1867 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1868 | read_lock(&em_tree->lock); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1869 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 1870 | if (em->start > start || em->start + em->len < start) { |
| 1871 | free_extent_map(em); |
| 1872 | em = NULL; |
| 1873 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1874 | read_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1875 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 1876 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1877 | kfree(failrec); |
| 1878 | return -EIO; |
| 1879 | } |
| 1880 | logical = start - em->start; |
| 1881 | logical = em->block_start + logical; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1882 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 1883 | logical = em->block_start; |
| 1884 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1885 | extent_set_compress_type(&failrec->bio_flags, |
| 1886 | em->compress_type); |
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 | failrec->logical = logical; |
| 1889 | free_extent_map(em); |
| 1890 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 1891 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1892 | set_state_private(failure_tree, start, |
| 1893 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1894 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 1895 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1896 | } |
| 1897 | num_copies = btrfs_num_copies( |
| 1898 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 1899 | failrec->logical, failrec->len); |
| 1900 | failrec->last_mirror++; |
| 1901 | if (!state) { |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1902 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1903 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 1904 | failrec->start, |
| 1905 | EXTENT_LOCKED); |
| 1906 | if (state && state->start != failrec->start) |
| 1907 | state = NULL; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1908 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1909 | } |
| 1910 | if (!state || failrec->last_mirror > num_copies) { |
| 1911 | set_state_private(failure_tree, failrec->start, 0); |
| 1912 | clear_extent_bits(failure_tree, failrec->start, |
| 1913 | failrec->start + failrec->len - 1, |
| 1914 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1915 | kfree(failrec); |
| 1916 | return -EIO; |
| 1917 | } |
| 1918 | bio = bio_alloc(GFP_NOFS, 1); |
| 1919 | bio->bi_private = state; |
| 1920 | bio->bi_end_io = failed_bio->bi_end_io; |
| 1921 | bio->bi_sector = failrec->logical >> 9; |
| 1922 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1923 | bio->bi_size = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1924 | |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1925 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1926 | if (failed_bio->bi_rw & REQ_WRITE) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1927 | rw = WRITE; |
| 1928 | else |
| 1929 | rw = READ; |
| 1930 | |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1931 | ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1932 | failrec->last_mirror, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1933 | failrec->bio_flags, 0); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1934 | return ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1935 | } |
| 1936 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1937 | /* |
| 1938 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 1939 | * to see if we need to process or clean up an io_failure_record |
| 1940 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1941 | static int btrfs_clean_io_failures(struct inode *inode, u64 start) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1942 | { |
| 1943 | u64 private; |
| 1944 | u64 private_failure; |
| 1945 | struct io_failure_record *failure; |
| 1946 | int ret; |
| 1947 | |
| 1948 | private = 0; |
| 1949 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1950 | (u64)-1, 1, EXTENT_DIRTY, 0)) { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1951 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1952 | start, &private_failure); |
| 1953 | if (ret == 0) { |
| 1954 | failure = (struct io_failure_record *)(unsigned long) |
| 1955 | private_failure; |
| 1956 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 1957 | failure->start, 0); |
| 1958 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 1959 | failure->start, |
| 1960 | failure->start + failure->len - 1, |
| 1961 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 1962 | GFP_NOFS); |
| 1963 | kfree(failure); |
| 1964 | } |
| 1965 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1966 | return 0; |
| 1967 | } |
| 1968 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1969 | /* |
| 1970 | * when reads are done, we need to check csums to verify the data is correct |
| 1971 | * if there's a match, we allow the bio to finish. If not, we go through |
| 1972 | * the io_failure_record routines to find good copies |
| 1973 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1974 | 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] | 1975 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1976 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1977 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1978 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1979 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1980 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1981 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1982 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1983 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1984 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1985 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1986 | if (PageChecked(page)) { |
| 1987 | ClearPageChecked(page); |
| 1988 | goto good; |
| 1989 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1990 | |
| 1991 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 1992 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1993 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1994 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1995 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1996 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1997 | GFP_NOFS); |
| 1998 | return 0; |
| 1999 | } |
| 2000 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 2001 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2002 | private = state->private; |
| 2003 | ret = 0; |
| 2004 | } else { |
| 2005 | ret = get_state_private(io_tree, start, &private); |
| 2006 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2007 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2008 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2009 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2010 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2011 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 2012 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2013 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2014 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2015 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2016 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2017 | good: |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2018 | /* if the io failure tree for this inode is non-empty, |
| 2019 | * check to see if we've recovered from a failed IO |
| 2020 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2021 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2022 | return 0; |
| 2023 | |
| 2024 | zeroit: |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 2025 | printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2026 | "private %llu\n", |
| 2027 | (unsigned long long)btrfs_ino(page->mapping->host), |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 2028 | (unsigned long long)start, csum, |
| 2029 | (unsigned long long)private); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2030 | memset(kaddr + offset, 1, end - start + 1); |
| 2031 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2032 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2033 | if (private == 0) |
| 2034 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2035 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2036 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2037 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2038 | struct delayed_iput { |
| 2039 | struct list_head list; |
| 2040 | struct inode *inode; |
| 2041 | }; |
| 2042 | |
| 2043 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2044 | { |
| 2045 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2046 | struct delayed_iput *delayed; |
| 2047 | |
| 2048 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2049 | return; |
| 2050 | |
| 2051 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2052 | delayed->inode = inode; |
| 2053 | |
| 2054 | spin_lock(&fs_info->delayed_iput_lock); |
| 2055 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2056 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2057 | } |
| 2058 | |
| 2059 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2060 | { |
| 2061 | LIST_HEAD(list); |
| 2062 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2063 | struct delayed_iput *delayed; |
| 2064 | int empty; |
| 2065 | |
| 2066 | spin_lock(&fs_info->delayed_iput_lock); |
| 2067 | empty = list_empty(&fs_info->delayed_iputs); |
| 2068 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2069 | if (empty) |
| 2070 | return; |
| 2071 | |
| 2072 | down_read(&root->fs_info->cleanup_work_sem); |
| 2073 | spin_lock(&fs_info->delayed_iput_lock); |
| 2074 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2075 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2076 | |
| 2077 | while (!list_empty(&list)) { |
| 2078 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2079 | list_del(&delayed->list); |
| 2080 | iput(delayed->inode); |
| 2081 | kfree(delayed); |
| 2082 | } |
| 2083 | up_read(&root->fs_info->cleanup_work_sem); |
| 2084 | } |
| 2085 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2086 | enum btrfs_orphan_cleanup_state { |
| 2087 | ORPHAN_CLEANUP_STARTED = 1, |
| 2088 | ORPHAN_CLEANUP_DONE = 2, |
| 2089 | }; |
| 2090 | |
| 2091 | /* |
| 2092 | * This is called in transaction commmit time. If there are no orphan |
| 2093 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2094 | * structure. |
| 2095 | */ |
| 2096 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2097 | struct btrfs_root *root) |
| 2098 | { |
| 2099 | int ret; |
| 2100 | |
| 2101 | if (!list_empty(&root->orphan_list) || |
| 2102 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2103 | return; |
| 2104 | |
| 2105 | if (root->orphan_item_inserted && |
| 2106 | btrfs_root_refs(&root->root_item) > 0) { |
| 2107 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2108 | root->root_key.objectid); |
| 2109 | BUG_ON(ret); |
| 2110 | root->orphan_item_inserted = 0; |
| 2111 | } |
| 2112 | |
| 2113 | if (root->orphan_block_rsv) { |
| 2114 | WARN_ON(root->orphan_block_rsv->size > 0); |
| 2115 | btrfs_free_block_rsv(root, root->orphan_block_rsv); |
| 2116 | root->orphan_block_rsv = NULL; |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2121 | * This creates an orphan entry for the given inode in case something goes |
| 2122 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2123 | * |
| 2124 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2125 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2126 | */ |
| 2127 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2128 | { |
| 2129 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2130 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2131 | int reserve = 0; |
| 2132 | int insert = 0; |
| 2133 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2134 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2135 | if (!root->orphan_block_rsv) { |
| 2136 | block_rsv = btrfs_alloc_block_rsv(root); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2137 | if (!block_rsv) |
| 2138 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2139 | } |
| 2140 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2141 | spin_lock(&root->orphan_lock); |
| 2142 | if (!root->orphan_block_rsv) { |
| 2143 | root->orphan_block_rsv = block_rsv; |
| 2144 | } else if (block_rsv) { |
| 2145 | btrfs_free_block_rsv(root, block_rsv); |
| 2146 | block_rsv = NULL; |
| 2147 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2148 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2149 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2150 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2151 | #if 0 |
| 2152 | /* |
| 2153 | * For proper ENOSPC handling, we should do orphan |
| 2154 | * cleanup when mounting. But this introduces backward |
| 2155 | * compatibility issue. |
| 2156 | */ |
| 2157 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2158 | insert = 2; |
| 2159 | else |
| 2160 | insert = 1; |
| 2161 | #endif |
| 2162 | insert = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2163 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2164 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2165 | if (!BTRFS_I(inode)->orphan_meta_reserved) { |
| 2166 | BTRFS_I(inode)->orphan_meta_reserved = 1; |
| 2167 | reserve = 1; |
| 2168 | } |
| 2169 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2170 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2171 | /* grab metadata reservation from transaction handle */ |
| 2172 | if (reserve) { |
| 2173 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
| 2174 | BUG_ON(ret); |
| 2175 | } |
| 2176 | |
| 2177 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2178 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2179 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2180 | BUG_ON(ret); |
| 2181 | } |
| 2182 | |
| 2183 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2184 | if (insert >= 2) { |
| 2185 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2186 | root->root_key.objectid); |
| 2187 | BUG_ON(ret); |
| 2188 | } |
| 2189 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | /* |
| 2193 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2194 | * item for this particular inode. |
| 2195 | */ |
| 2196 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2197 | { |
| 2198 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2199 | int delete_item = 0; |
| 2200 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2201 | int ret = 0; |
| 2202 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2203 | spin_lock(&root->orphan_lock); |
| 2204 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2205 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2206 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2207 | } |
| 2208 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2209 | if (BTRFS_I(inode)->orphan_meta_reserved) { |
| 2210 | BTRFS_I(inode)->orphan_meta_reserved = 0; |
| 2211 | release_rsv = 1; |
| 2212 | } |
| 2213 | spin_unlock(&root->orphan_lock); |
| 2214 | |
| 2215 | if (trans && delete_item) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2216 | ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode)); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2217 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2218 | } |
| 2219 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2220 | if (release_rsv) |
| 2221 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2222 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2223 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | /* |
| 2227 | * this cleans up any orphans that may be left on the list from the last use |
| 2228 | * of this root. |
| 2229 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2230 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2231 | { |
| 2232 | struct btrfs_path *path; |
| 2233 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2234 | struct btrfs_key key, found_key; |
| 2235 | struct btrfs_trans_handle *trans; |
| 2236 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 2237 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2238 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2239 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2240 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2241 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2242 | |
| 2243 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2244 | if (!path) { |
| 2245 | ret = -ENOMEM; |
| 2246 | goto out; |
| 2247 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2248 | path->reada = -1; |
| 2249 | |
| 2250 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2251 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2252 | key.offset = (u64)-1; |
| 2253 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2254 | while (1) { |
| 2255 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2256 | if (ret < 0) |
| 2257 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2258 | |
| 2259 | /* |
| 2260 | * if ret == 0 means we found what we were searching for, which |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2261 | * is weird, but possible, so only screw with path if we didn't |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2262 | * find the key and see if we have stuff that matches |
| 2263 | */ |
| 2264 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2265 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2266 | if (path->slots[0] == 0) |
| 2267 | break; |
| 2268 | path->slots[0]--; |
| 2269 | } |
| 2270 | |
| 2271 | /* pull out the item */ |
| 2272 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2273 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2274 | |
| 2275 | /* make sure the item matches what we want */ |
| 2276 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2277 | break; |
| 2278 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2279 | break; |
| 2280 | |
| 2281 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2282 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2283 | |
| 2284 | /* |
| 2285 | * this is where we are basically btrfs_lookup, without the |
| 2286 | * crossing root thing. we store the inode number in the |
| 2287 | * offset of the orphan item. |
| 2288 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 2289 | |
| 2290 | if (found_key.offset == last_objectid) { |
| 2291 | printk(KERN_ERR "btrfs: Error removing orphan entry, " |
| 2292 | "stopping orphan cleanup\n"); |
| 2293 | ret = -EINVAL; |
| 2294 | goto out; |
| 2295 | } |
| 2296 | |
| 2297 | last_objectid = found_key.offset; |
| 2298 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2299 | found_key.objectid = found_key.offset; |
| 2300 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2301 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2302 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2303 | ret = PTR_RET(inode); |
| 2304 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2305 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2306 | |
| 2307 | /* |
| 2308 | * Inode is already gone but the orphan item is still there, |
| 2309 | * kill the orphan item. |
| 2310 | */ |
| 2311 | if (ret == -ESTALE) { |
| 2312 | trans = btrfs_start_transaction(root, 1); |
| 2313 | if (IS_ERR(trans)) { |
| 2314 | ret = PTR_ERR(trans); |
| 2315 | goto out; |
| 2316 | } |
| 2317 | ret = btrfs_del_orphan_item(trans, root, |
| 2318 | found_key.objectid); |
| 2319 | BUG_ON(ret); |
| 2320 | btrfs_end_transaction(trans, root); |
| 2321 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2322 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2323 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2324 | /* |
| 2325 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2326 | * the proper thing when we hit it |
| 2327 | */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2328 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2329 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2330 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2331 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2332 | /* if we have links, this was a truncate, lets do that */ |
| 2333 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 2334 | if (!S_ISREG(inode->i_mode)) { |
| 2335 | WARN_ON(1); |
| 2336 | iput(inode); |
| 2337 | continue; |
| 2338 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2339 | nr_truncate++; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2340 | ret = btrfs_truncate(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2341 | } else { |
| 2342 | nr_unlink++; |
| 2343 | } |
| 2344 | |
| 2345 | /* this will do delete_inode and everything for us */ |
| 2346 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2347 | if (ret) |
| 2348 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2349 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2350 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 2351 | |
| 2352 | if (root->orphan_block_rsv) |
| 2353 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 2354 | (u64)-1); |
| 2355 | |
| 2356 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2357 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2358 | if (!IS_ERR(trans)) |
| 2359 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2360 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2361 | |
| 2362 | if (nr_unlink) |
| 2363 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2364 | if (nr_truncate) |
| 2365 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2366 | |
| 2367 | out: |
| 2368 | if (ret) |
| 2369 | printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret); |
| 2370 | btrfs_free_path(path); |
| 2371 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2372 | } |
| 2373 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2374 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2375 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2376 | * don't find any xattrs, we know there can't be any acls. |
| 2377 | * |
| 2378 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2379 | */ |
| 2380 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2381 | int slot, u64 objectid) |
| 2382 | { |
| 2383 | u32 nritems = btrfs_header_nritems(leaf); |
| 2384 | struct btrfs_key found_key; |
| 2385 | int scanned = 0; |
| 2386 | |
| 2387 | slot++; |
| 2388 | while (slot < nritems) { |
| 2389 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2390 | |
| 2391 | /* we found a different objectid, there must not be acls */ |
| 2392 | if (found_key.objectid != objectid) |
| 2393 | return 0; |
| 2394 | |
| 2395 | /* we found an xattr, assume we've got an acl */ |
| 2396 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2397 | return 1; |
| 2398 | |
| 2399 | /* |
| 2400 | * we found a key greater than an xattr key, there can't |
| 2401 | * be any acls later on |
| 2402 | */ |
| 2403 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2404 | return 0; |
| 2405 | |
| 2406 | slot++; |
| 2407 | scanned++; |
| 2408 | |
| 2409 | /* |
| 2410 | * it goes inode, inode backrefs, xattrs, extents, |
| 2411 | * so if there are a ton of hard links to an inode there can |
| 2412 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2413 | * this is just an optimization |
| 2414 | */ |
| 2415 | if (scanned >= 8) |
| 2416 | break; |
| 2417 | } |
| 2418 | /* we hit the end of the leaf before we found an xattr or |
| 2419 | * something larger than an xattr. We have to assume the inode |
| 2420 | * has acls |
| 2421 | */ |
| 2422 | return 1; |
| 2423 | } |
| 2424 | |
| 2425 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2426 | * read an inode from the btree into the in-memory inode |
| 2427 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2428 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2429 | { |
| 2430 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2431 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2432 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2433 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2434 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2435 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2436 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2437 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2438 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2439 | bool filled = false; |
| 2440 | |
| 2441 | ret = btrfs_fill_inode(inode, &rdev); |
| 2442 | if (!ret) |
| 2443 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2444 | |
| 2445 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 2446 | if (!path) |
| 2447 | goto make_bad; |
| 2448 | |
Josef Bacik | d90c732 | 2011-05-17 09:50:54 -0400 | [diff] [blame] | 2449 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2450 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2451 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2452 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2453 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2454 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2455 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2456 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2457 | |
| 2458 | if (filled) |
| 2459 | goto cache_acl; |
| 2460 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2461 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2462 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2463 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 2464 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 2465 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2466 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2467 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2468 | |
| 2469 | tspec = btrfs_inode_atime(inode_item); |
| 2470 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2471 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2472 | |
| 2473 | tspec = btrfs_inode_mtime(inode_item); |
| 2474 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2475 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2476 | |
| 2477 | tspec = btrfs_inode_ctime(inode_item); |
| 2478 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2479 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2480 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2481 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2482 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2483 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2484 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2485 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2486 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2487 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2488 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2489 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2490 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2491 | /* |
| 2492 | * try to precache a NULL acl entry for files that don't have |
| 2493 | * any xattrs or acls |
| 2494 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2495 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
| 2496 | btrfs_ino(inode)); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 2497 | if (!maybe_acls) |
| 2498 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2499 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2500 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2501 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2502 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2503 | case S_IFREG: |
| 2504 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2505 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2506 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2507 | inode->i_fop = &btrfs_file_operations; |
| 2508 | inode->i_op = &btrfs_file_inode_operations; |
| 2509 | break; |
| 2510 | case S_IFDIR: |
| 2511 | inode->i_fop = &btrfs_dir_file_operations; |
| 2512 | if (root == root->fs_info->tree_root) |
| 2513 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2514 | else |
| 2515 | inode->i_op = &btrfs_dir_inode_operations; |
| 2516 | break; |
| 2517 | case S_IFLNK: |
| 2518 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2519 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2520 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2521 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2522 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2523 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2524 | init_special_inode(inode, inode->i_mode, rdev); |
| 2525 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2526 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2527 | |
| 2528 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2529 | return; |
| 2530 | |
| 2531 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2532 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2533 | make_bad_inode(inode); |
| 2534 | } |
| 2535 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2536 | /* |
| 2537 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2538 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2539 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2540 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2541 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2542 | struct inode *inode) |
| 2543 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2544 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2545 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2546 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2547 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2548 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2549 | |
| 2550 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2551 | inode->i_atime.tv_sec); |
| 2552 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2553 | inode->i_atime.tv_nsec); |
| 2554 | |
| 2555 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2556 | inode->i_mtime.tv_sec); |
| 2557 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2558 | inode->i_mtime.tv_nsec); |
| 2559 | |
| 2560 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2561 | inode->i_ctime.tv_sec); |
| 2562 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2563 | inode->i_ctime.tv_nsec); |
| 2564 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2565 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2566 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2567 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2568 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2569 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2570 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 2571 | btrfs_set_inode_block_group(leaf, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2572 | } |
| 2573 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2574 | /* |
| 2575 | * copy everything in the in-memory inode into the btree. |
| 2576 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2577 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2578 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2579 | { |
| 2580 | struct btrfs_inode_item *inode_item; |
| 2581 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2582 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2583 | int ret; |
| 2584 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2585 | /* |
Miao Xie | 149e2d7 | 2011-07-06 18:51:53 -0400 | [diff] [blame] | 2586 | * If the inode is a free space inode, we can deadlock during commit |
| 2587 | * if we put it into the delayed code. |
| 2588 | * |
| 2589 | * The data relocation inode should also be directly updated |
| 2590 | * without delay |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2591 | */ |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 2592 | if (!btrfs_is_free_space_inode(root, inode) |
Miao Xie | 149e2d7 | 2011-07-06 18:51:53 -0400 | [diff] [blame] | 2593 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2594 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 2595 | if (!ret) |
| 2596 | btrfs_set_inode_last_trans(trans, inode); |
| 2597 | return ret; |
| 2598 | } |
| 2599 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2600 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2601 | if (!path) |
| 2602 | return -ENOMEM; |
| 2603 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2604 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2605 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 2606 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 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], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2616 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 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 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2627 | /* |
| 2628 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2629 | * recovery code. It remove a link in a directory with a given name, and |
| 2630 | * also drops the back refs in the inode to the directory |
| 2631 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2632 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2633 | struct btrfs_root *root, |
| 2634 | struct inode *dir, struct inode *inode, |
| 2635 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2636 | { |
| 2637 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2638 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2639 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2640 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2641 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2642 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2643 | u64 ino = btrfs_ino(inode); |
| 2644 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2645 | |
| 2646 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2647 | if (!path) { |
| 2648 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 2649 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2650 | } |
| 2651 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2652 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2653 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2654 | name, name_len, -1); |
| 2655 | if (IS_ERR(di)) { |
| 2656 | ret = PTR_ERR(di); |
| 2657 | goto err; |
| 2658 | } |
| 2659 | if (!di) { |
| 2660 | ret = -ENOENT; |
| 2661 | goto err; |
| 2662 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2663 | leaf = path->nodes[0]; |
| 2664 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2665 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2666 | if (ret) |
| 2667 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2668 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2669 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2670 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 2671 | dir_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, " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2674 | "inode %llu parent %llu\n", name_len, name, |
| 2675 | (unsigned long long)ino, (unsigned long long)dir_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2676 | goto err; |
| 2677 | } |
| 2678 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2679 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
| 2680 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2681 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2682 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2683 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2684 | inode, dir_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2685 | BUG_ON(ret != 0 && ret != -ENOENT); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2686 | |
| 2687 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2688 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 2689 | if (ret == -ENOENT) |
| 2690 | ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2691 | err: |
| 2692 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2693 | if (ret) |
| 2694 | goto out; |
| 2695 | |
| 2696 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2697 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2698 | btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2699 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2700 | return ret; |
| 2701 | } |
| 2702 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2703 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2704 | struct btrfs_root *root, |
| 2705 | struct inode *dir, struct inode *inode, |
| 2706 | const char *name, int name_len) |
| 2707 | { |
| 2708 | int ret; |
| 2709 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 2710 | if (!ret) { |
| 2711 | btrfs_drop_nlink(inode); |
| 2712 | ret = btrfs_update_inode(trans, root, inode); |
| 2713 | } |
| 2714 | return ret; |
| 2715 | } |
| 2716 | |
| 2717 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2718 | /* helper to check if there is any shared block in the path */ |
| 2719 | static int check_path_shared(struct btrfs_root *root, |
| 2720 | struct btrfs_path *path) |
| 2721 | { |
| 2722 | struct extent_buffer *eb; |
| 2723 | int level; |
Dan Carpenter | 0e4dcbe | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 2724 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2725 | |
| 2726 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2727 | int ret; |
| 2728 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2729 | if (!path->nodes[level]) |
| 2730 | break; |
| 2731 | eb = path->nodes[level]; |
| 2732 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2733 | continue; |
| 2734 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2735 | &refs, NULL); |
| 2736 | if (refs > 1) |
| 2737 | return 1; |
| 2738 | } |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2739 | return 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | /* |
| 2743 | * helper to start transaction for unlink and rmdir. |
| 2744 | * |
| 2745 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2746 | * so in enospc case, we should make sure they will free space before |
| 2747 | * allowing them to use the global metadata reservation. |
| 2748 | */ |
| 2749 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2750 | struct dentry *dentry) |
| 2751 | { |
| 2752 | struct btrfs_trans_handle *trans; |
| 2753 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2754 | struct btrfs_path *path; |
| 2755 | struct btrfs_inode_ref *ref; |
| 2756 | struct btrfs_dir_item *di; |
| 2757 | struct inode *inode = dentry->d_inode; |
| 2758 | u64 index; |
| 2759 | int check_link = 1; |
| 2760 | int err = -ENOSPC; |
| 2761 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2762 | u64 ino = btrfs_ino(inode); |
| 2763 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2764 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 2765 | /* |
| 2766 | * 1 for the possible orphan item |
| 2767 | * 1 for the dir item |
| 2768 | * 1 for the dir index |
| 2769 | * 1 for the inode ref |
| 2770 | * 1 for the inode ref in the tree log |
| 2771 | * 2 for the dir entries in the log |
| 2772 | * 1 for the inode |
| 2773 | */ |
| 2774 | trans = btrfs_start_transaction(root, 8); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2775 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 2776 | return trans; |
| 2777 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2778 | if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2779 | return ERR_PTR(-ENOSPC); |
| 2780 | |
| 2781 | /* check if there is someone else holds reference */ |
| 2782 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 2783 | return ERR_PTR(-ENOSPC); |
| 2784 | |
| 2785 | if (atomic_read(&inode->i_count) > 2) |
| 2786 | return ERR_PTR(-ENOSPC); |
| 2787 | |
| 2788 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 2789 | return ERR_PTR(-ENOSPC); |
| 2790 | |
| 2791 | path = btrfs_alloc_path(); |
| 2792 | if (!path) { |
| 2793 | root->fs_info->enospc_unlink = 0; |
| 2794 | return ERR_PTR(-ENOMEM); |
| 2795 | } |
| 2796 | |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 2797 | /* 1 for the orphan item */ |
| 2798 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2799 | if (IS_ERR(trans)) { |
| 2800 | btrfs_free_path(path); |
| 2801 | root->fs_info->enospc_unlink = 0; |
| 2802 | return trans; |
| 2803 | } |
| 2804 | |
| 2805 | path->skip_locking = 1; |
| 2806 | path->search_commit_root = 1; |
| 2807 | |
| 2808 | ret = btrfs_lookup_inode(trans, root, path, |
| 2809 | &BTRFS_I(dir)->location, 0); |
| 2810 | if (ret < 0) { |
| 2811 | err = ret; |
| 2812 | goto out; |
| 2813 | } |
| 2814 | if (ret == 0) { |
| 2815 | if (check_path_shared(root, path)) |
| 2816 | goto out; |
| 2817 | } else { |
| 2818 | check_link = 0; |
| 2819 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2820 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2821 | |
| 2822 | ret = btrfs_lookup_inode(trans, root, path, |
| 2823 | &BTRFS_I(inode)->location, 0); |
| 2824 | if (ret < 0) { |
| 2825 | err = ret; |
| 2826 | goto out; |
| 2827 | } |
| 2828 | if (ret == 0) { |
| 2829 | if (check_path_shared(root, path)) |
| 2830 | goto out; |
| 2831 | } else { |
| 2832 | check_link = 0; |
| 2833 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2834 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2835 | |
| 2836 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 2837 | ret = btrfs_lookup_file_extent(trans, root, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2838 | ino, (u64)-1, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2839 | if (ret < 0) { |
| 2840 | err = ret; |
| 2841 | goto out; |
| 2842 | } |
| 2843 | BUG_ON(ret == 0); |
| 2844 | if (check_path_shared(root, path)) |
| 2845 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2846 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2847 | } |
| 2848 | |
| 2849 | if (!check_link) { |
| 2850 | err = 0; |
| 2851 | goto out; |
| 2852 | } |
| 2853 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2854 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2855 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2856 | if (IS_ERR(di)) { |
| 2857 | err = PTR_ERR(di); |
| 2858 | goto out; |
| 2859 | } |
| 2860 | if (di) { |
| 2861 | if (check_path_shared(root, path)) |
| 2862 | goto out; |
| 2863 | } else { |
| 2864 | err = 0; |
| 2865 | goto out; |
| 2866 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2867 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2868 | |
| 2869 | ref = btrfs_lookup_inode_ref(trans, root, path, |
| 2870 | dentry->d_name.name, dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2871 | ino, dir_ino, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2872 | if (IS_ERR(ref)) { |
| 2873 | err = PTR_ERR(ref); |
| 2874 | goto out; |
| 2875 | } |
| 2876 | BUG_ON(!ref); |
| 2877 | if (check_path_shared(root, path)) |
| 2878 | goto out; |
| 2879 | index = btrfs_inode_ref_index(path->nodes[0], ref); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2880 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2881 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2882 | /* |
| 2883 | * This is a commit root search, if we can lookup inode item and other |
| 2884 | * relative items in the commit root, it means the transaction of |
| 2885 | * dir/file creation has been committed, and the dir index item that we |
| 2886 | * delay to insert has also been inserted into the commit root. So |
| 2887 | * we needn't worry about the delayed insertion of the dir index item |
| 2888 | * here. |
| 2889 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2890 | di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2891 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2892 | if (IS_ERR(di)) { |
| 2893 | err = PTR_ERR(di); |
| 2894 | goto out; |
| 2895 | } |
| 2896 | BUG_ON(ret == -ENOENT); |
| 2897 | if (check_path_shared(root, path)) |
| 2898 | goto out; |
| 2899 | |
| 2900 | err = 0; |
| 2901 | out: |
| 2902 | btrfs_free_path(path); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 2903 | /* Migrate the orphan reservation over */ |
| 2904 | if (!err) |
| 2905 | err = btrfs_block_rsv_migrate(trans->block_rsv, |
| 2906 | &root->fs_info->global_block_rsv, |
| 2907 | btrfs_calc_trans_metadata_size(root, 1)); |
| 2908 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2909 | if (err) { |
| 2910 | btrfs_end_transaction(trans, root); |
| 2911 | root->fs_info->enospc_unlink = 0; |
| 2912 | return ERR_PTR(err); |
| 2913 | } |
| 2914 | |
| 2915 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 2916 | return trans; |
| 2917 | } |
| 2918 | |
| 2919 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 2920 | struct btrfs_root *root) |
| 2921 | { |
| 2922 | if (trans->block_rsv == &root->fs_info->global_block_rsv) { |
| 2923 | BUG_ON(!root->fs_info->enospc_unlink); |
| 2924 | root->fs_info->enospc_unlink = 0; |
| 2925 | } |
| 2926 | btrfs_end_transaction_throttle(trans, root); |
| 2927 | } |
| 2928 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2929 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2930 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2931 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2932 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2933 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2934 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2935 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2936 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2937 | trans = __unlink_start_trans(dir, dentry); |
| 2938 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 2939 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2940 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 2941 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 2942 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2943 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 2944 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2945 | if (ret) |
| 2946 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2947 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2948 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2949 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2950 | if (ret) |
| 2951 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2952 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2953 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2954 | out: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2955 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2956 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2957 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2958 | return ret; |
| 2959 | } |
| 2960 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2961 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 2962 | struct btrfs_root *root, |
| 2963 | struct inode *dir, u64 objectid, |
| 2964 | const char *name, int name_len) |
| 2965 | { |
| 2966 | struct btrfs_path *path; |
| 2967 | struct extent_buffer *leaf; |
| 2968 | struct btrfs_dir_item *di; |
| 2969 | struct btrfs_key key; |
| 2970 | u64 index; |
| 2971 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2972 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2973 | |
| 2974 | path = btrfs_alloc_path(); |
| 2975 | if (!path) |
| 2976 | return -ENOMEM; |
| 2977 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2978 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2979 | name, name_len, -1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2980 | BUG_ON(IS_ERR_OR_NULL(di)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2981 | |
| 2982 | leaf = path->nodes[0]; |
| 2983 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 2984 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 2985 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
| 2986 | BUG_ON(ret); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2987 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2988 | |
| 2989 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 2990 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2991 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2992 | if (ret < 0) { |
| 2993 | BUG_ON(ret != -ENOENT); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2994 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2995 | name, name_len); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2996 | BUG_ON(IS_ERR_OR_NULL(di)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 2997 | |
| 2998 | leaf = path->nodes[0]; |
| 2999 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3000 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3001 | index = key.offset; |
| 3002 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3003 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3004 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3005 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
| 3006 | BUG_ON(ret); |
| 3007 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3008 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 3009 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 3010 | ret = btrfs_update_inode(trans, root, dir); |
| 3011 | BUG_ON(ret); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3012 | |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3013 | btrfs_free_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3014 | return 0; |
| 3015 | } |
| 3016 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3017 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3018 | { |
| 3019 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3020 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3021 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3022 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3023 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3024 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 3025 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3026 | btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3027 | return -ENOTEMPTY; |
| 3028 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3029 | trans = __unlink_start_trans(dir, dentry); |
| 3030 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3031 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3032 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3033 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3034 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3035 | BTRFS_I(inode)->location.objectid, |
| 3036 | dentry->d_name.name, |
| 3037 | dentry->d_name.len); |
| 3038 | goto out; |
| 3039 | } |
| 3040 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3041 | err = btrfs_orphan_add(trans, inode); |
| 3042 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3043 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3044 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3045 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3046 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3047 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3048 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3049 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3050 | out: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3051 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3052 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3053 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3054 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3055 | return err; |
| 3056 | } |
| 3057 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3058 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3059 | * this can truncate away extent items, csum items and directory items. |
| 3060 | * 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] | 3061 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3062 | * |
| 3063 | * csum items that cross the new i_size are truncated to the new size |
| 3064 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3065 | * |
| 3066 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3067 | * 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] | 3068 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3069 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3070 | struct btrfs_root *root, |
| 3071 | struct inode *inode, |
| 3072 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3073 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3074 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3075 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3076 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3077 | struct btrfs_key key; |
| 3078 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3079 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3080 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3081 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3082 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3083 | u64 mask = root->sectorsize - 1; |
| 3084 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3085 | int found_extent; |
| 3086 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3087 | int pending_del_nr = 0; |
| 3088 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3089 | int extent_type = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3090 | int encoding; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3091 | int ret; |
| 3092 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3093 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3094 | |
| 3095 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3096 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 3097 | path = btrfs_alloc_path(); |
| 3098 | if (!path) |
| 3099 | return -ENOMEM; |
| 3100 | path->reada = -1; |
| 3101 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3102 | if (root->ref_cows || root == root->fs_info->tree_root) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3103 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3104 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3105 | /* |
| 3106 | * This function is also used to drop the items in the log tree before |
| 3107 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 3108 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 3109 | * items. |
| 3110 | */ |
| 3111 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 3112 | btrfs_kill_delayed_inode_items(inode); |
| 3113 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3114 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3115 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3116 | key.type = (u8)-1; |
| 3117 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3118 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3119 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3120 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3121 | if (ret < 0) { |
| 3122 | err = ret; |
| 3123 | goto out; |
| 3124 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3125 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3126 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3127 | /* there are no items in the tree for us to truncate, we're |
| 3128 | * done |
| 3129 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3130 | if (path->slots[0] == 0) |
| 3131 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3132 | path->slots[0]--; |
| 3133 | } |
| 3134 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3135 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3136 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3137 | leaf = path->nodes[0]; |
| 3138 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3139 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3140 | encoding = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3141 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3142 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3143 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3144 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3145 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3146 | break; |
| 3147 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3148 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3149 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3150 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3151 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3152 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3153 | encoding = btrfs_file_extent_compression(leaf, fi); |
| 3154 | encoding |= btrfs_file_extent_encryption(leaf, fi); |
| 3155 | encoding |= btrfs_file_extent_other_encoding(leaf, fi); |
| 3156 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3157 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3158 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3159 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3160 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3161 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3162 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3163 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3164 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3165 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3166 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3167 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3168 | } else { |
| 3169 | if (item_end < new_size) |
| 3170 | break; |
| 3171 | if (found_key.offset >= new_size) |
| 3172 | del_item = 1; |
| 3173 | else |
| 3174 | del_item = 0; |
| 3175 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3176 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3177 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3178 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3179 | goto delete; |
| 3180 | |
| 3181 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3182 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3183 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3184 | if (!del_item && !encoding) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3185 | u64 orig_num_bytes = |
| 3186 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3187 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3188 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3189 | extent_num_bytes = extent_num_bytes & |
| 3190 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3191 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3192 | extent_num_bytes); |
| 3193 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3194 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3195 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3196 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3197 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3198 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3199 | extent_num_bytes = |
| 3200 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3201 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3202 | extent_offset = found_key.offset - |
| 3203 | btrfs_file_extent_offset(leaf, fi); |
| 3204 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3205 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3206 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3207 | if (extent_start != 0) { |
| 3208 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3209 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3210 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3211 | } |
| 3212 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3213 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3214 | /* |
| 3215 | * we can't truncate inline items that have had |
| 3216 | * special encodings |
| 3217 | */ |
| 3218 | if (!del_item && |
| 3219 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3220 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3221 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3222 | u32 size = new_size - found_key.offset; |
| 3223 | |
| 3224 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3225 | inode_sub_bytes(inode, item_end + 1 - |
| 3226 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3227 | } |
| 3228 | size = |
| 3229 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3230 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3231 | size, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3232 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3233 | inode_sub_bytes(inode, item_end + 1 - |
| 3234 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3235 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3236 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3237 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3238 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3239 | if (!pending_del_nr) { |
| 3240 | /* no pending yet, add ourselves */ |
| 3241 | pending_del_slot = path->slots[0]; |
| 3242 | pending_del_nr = 1; |
| 3243 | } else if (pending_del_nr && |
| 3244 | path->slots[0] + 1 == pending_del_slot) { |
| 3245 | /* hop on the pending chunk */ |
| 3246 | pending_del_nr++; |
| 3247 | pending_del_slot = path->slots[0]; |
| 3248 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3249 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3250 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3251 | } else { |
| 3252 | break; |
| 3253 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3254 | if (found_extent && (root->ref_cows || |
| 3255 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3256 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3257 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3258 | extent_num_bytes, 0, |
| 3259 | btrfs_header_owner(leaf), |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3260 | ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3261 | BUG_ON(ret); |
| 3262 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3263 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3264 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3265 | break; |
| 3266 | |
| 3267 | if (path->slots[0] == 0 || |
| 3268 | path->slots[0] != pending_del_slot) { |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 3269 | if (root->ref_cows && |
| 3270 | BTRFS_I(inode)->location.objectid != |
| 3271 | BTRFS_FREE_INO_OBJECTID) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3272 | err = -EAGAIN; |
| 3273 | goto out; |
| 3274 | } |
| 3275 | if (pending_del_nr) { |
| 3276 | ret = btrfs_del_items(trans, root, path, |
| 3277 | pending_del_slot, |
| 3278 | pending_del_nr); |
| 3279 | BUG_ON(ret); |
| 3280 | pending_del_nr = 0; |
| 3281 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3282 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3283 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3284 | } else { |
| 3285 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3286 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3287 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3288 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3289 | if (pending_del_nr) { |
| 3290 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3291 | pending_del_nr); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3292 | BUG_ON(ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3293 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3294 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3295 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3296 | } |
| 3297 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3298 | /* |
| 3299 | * taken from block_truncate_page, but does cow as it zeros out |
| 3300 | * any bytes left in the last page in the file. |
| 3301 | */ |
| 3302 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3303 | { |
| 3304 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3305 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3306 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3307 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3308 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3309 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3310 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3311 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3312 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3313 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 3314 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3315 | int ret = 0; |
| 3316 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3317 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3318 | |
| 3319 | if ((offset & (blocksize - 1)) == 0) |
| 3320 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3321 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3322 | if (ret) |
| 3323 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3324 | |
| 3325 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3326 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 3327 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3328 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3329 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3330 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3331 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3332 | |
| 3333 | page_start = page_offset(page); |
| 3334 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3335 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3336 | if (!PageUptodate(page)) { |
| 3337 | ret = btrfs_readpage(NULL, page); |
| 3338 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3339 | if (page->mapping != mapping) { |
| 3340 | unlock_page(page); |
| 3341 | page_cache_release(page); |
| 3342 | goto again; |
| 3343 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3344 | if (!PageUptodate(page)) { |
| 3345 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3346 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3347 | } |
| 3348 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3349 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3350 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3351 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 3352 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3353 | set_page_extent_mapped(page); |
| 3354 | |
| 3355 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3356 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3357 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3358 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3359 | unlock_page(page); |
| 3360 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3361 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3362 | btrfs_put_ordered_extent(ordered); |
| 3363 | goto again; |
| 3364 | } |
| 3365 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3366 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3367 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3368 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3369 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3370 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3371 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3372 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3373 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3374 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3375 | goto out_unlock; |
| 3376 | } |
| 3377 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3378 | ret = 0; |
| 3379 | if (offset != PAGE_CACHE_SIZE) { |
| 3380 | kaddr = kmap(page); |
| 3381 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3382 | flush_dcache_page(page); |
| 3383 | kunmap(page); |
| 3384 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3385 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3386 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3387 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3388 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3389 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3390 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3391 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3392 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3393 | unlock_page(page); |
| 3394 | page_cache_release(page); |
| 3395 | out: |
| 3396 | return ret; |
| 3397 | } |
| 3398 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 3399 | /* |
| 3400 | * This function puts in dummy file extents for the area we're creating a hole |
| 3401 | * for. So if we are truncating this file to a larger size we need to insert |
| 3402 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 3403 | * the range between oldsize and size |
| 3404 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3405 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3406 | { |
| 3407 | struct btrfs_trans_handle *trans; |
| 3408 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3409 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3410 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3411 | struct extent_state *cached_state = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3412 | u64 mask = root->sectorsize - 1; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3413 | u64 hole_start = (oldsize + mask) & ~mask; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3414 | u64 block_end = (size + mask) & ~mask; |
| 3415 | u64 last_byte; |
| 3416 | u64 cur_offset; |
| 3417 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3418 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3419 | |
| 3420 | if (size <= hole_start) |
| 3421 | return 0; |
| 3422 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3423 | while (1) { |
| 3424 | struct btrfs_ordered_extent *ordered; |
| 3425 | btrfs_wait_ordered_range(inode, hole_start, |
| 3426 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3427 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
| 3428 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3429 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3430 | if (!ordered) |
| 3431 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3432 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3433 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3434 | btrfs_put_ordered_extent(ordered); |
| 3435 | } |
| 3436 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3437 | cur_offset = hole_start; |
| 3438 | while (1) { |
| 3439 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3440 | block_end - cur_offset, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3441 | BUG_ON(IS_ERR_OR_NULL(em)); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3442 | last_byte = min(extent_map_end(em), block_end); |
| 3443 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3444 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3445 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3446 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3447 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3448 | trans = btrfs_start_transaction(root, 2); |
| 3449 | if (IS_ERR(trans)) { |
| 3450 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3451 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3452 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3453 | |
| 3454 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3455 | cur_offset + hole_size, |
| 3456 | &hint_byte, 1); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3457 | if (err) { |
| 3458 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3459 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3460 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3461 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3462 | err = btrfs_insert_file_extent(trans, root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3463 | btrfs_ino(inode), cur_offset, 0, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3464 | 0, hole_size, 0, hole_size, |
| 3465 | 0, 0, 0); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3466 | if (err) { |
| 3467 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3468 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3469 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3470 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3471 | btrfs_drop_extent_cache(inode, hole_start, |
| 3472 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3473 | |
| 3474 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3475 | } |
| 3476 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3477 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3478 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3479 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3480 | break; |
| 3481 | } |
| 3482 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3483 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3484 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3485 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3486 | return err; |
| 3487 | } |
| 3488 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3489 | static int btrfs_setsize(struct inode *inode, loff_t newsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3490 | { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3491 | loff_t oldsize = i_size_read(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3492 | int ret; |
| 3493 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3494 | if (newsize == oldsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3495 | return 0; |
| 3496 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3497 | if (newsize > oldsize) { |
| 3498 | i_size_write(inode, newsize); |
| 3499 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 3500 | truncate_pagecache(inode, oldsize, newsize); |
| 3501 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3502 | if (ret) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3503 | btrfs_setsize(inode, oldsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3504 | return ret; |
| 3505 | } |
| 3506 | |
Josef Bacik | 930f028 | 2011-03-04 14:41:41 -0500 | [diff] [blame] | 3507 | mark_inode_dirty(inode); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3508 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3509 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3510 | /* |
| 3511 | * We're truncating a file that used to have good data down to |
| 3512 | * zero. Make sure it gets into the ordered flush list so that |
| 3513 | * any new writes get down to disk quickly. |
| 3514 | */ |
| 3515 | if (newsize == 0) |
| 3516 | BTRFS_I(inode)->ordered_data_close = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3517 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3518 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3519 | truncate_setsize(inode, newsize); |
| 3520 | ret = btrfs_truncate(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3523 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3526 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3527 | { |
| 3528 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3529 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3530 | int err; |
| 3531 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3532 | if (btrfs_root_readonly(root)) |
| 3533 | return -EROFS; |
| 3534 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3535 | err = inode_change_ok(inode, attr); |
| 3536 | if (err) |
| 3537 | return err; |
| 3538 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3539 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3540 | err = btrfs_setsize(inode, attr->ia_size); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3541 | if (err) |
| 3542 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3543 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3544 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3545 | if (attr->ia_valid) { |
| 3546 | setattr_copy(inode, attr); |
| 3547 | mark_inode_dirty(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3548 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3549 | if (attr->ia_valid & ATTR_MODE) |
| 3550 | err = btrfs_acl_chmod(inode); |
| 3551 | } |
| 3552 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3553 | return err; |
| 3554 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3555 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3556 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3557 | { |
| 3558 | struct btrfs_trans_handle *trans; |
| 3559 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3560 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 3561 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3562 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3563 | int ret; |
| 3564 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3565 | trace_btrfs_inode_evict(inode); |
| 3566 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3567 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3568 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 3569 | btrfs_is_free_space_inode(root, inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3570 | goto no_delete; |
| 3571 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3572 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3573 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3574 | goto no_delete; |
| 3575 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3576 | /* 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] | 3577 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3578 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3579 | if (root->fs_info->log_root_recovering) { |
| 3580 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3581 | goto no_delete; |
| 3582 | } |
| 3583 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3584 | if (inode->i_nlink > 0) { |
| 3585 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3586 | goto no_delete; |
| 3587 | } |
| 3588 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3589 | rsv = btrfs_alloc_block_rsv(root); |
| 3590 | if (!rsv) { |
| 3591 | btrfs_orphan_del(NULL, inode); |
| 3592 | goto no_delete; |
| 3593 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 3594 | rsv->size = min_size; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3595 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3596 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3597 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3598 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3599 | /* |
| 3600 | * This is a bit simpler than btrfs_truncate since |
| 3601 | * |
| 3602 | * 1) We've already reserved our space for our orphan item in the |
| 3603 | * unlink. |
| 3604 | * 2) We're going to delete the inode item, so we don't need to update |
| 3605 | * it at all. |
| 3606 | * |
| 3607 | * So we just need to reserve some slack space in case we add bytes when |
| 3608 | * doing the truncate. |
| 3609 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3610 | while (1) { |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 3611 | ret = btrfs_block_rsv_refill(root, rsv, min_size); |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3612 | |
| 3613 | /* |
| 3614 | * Try and steal from the global reserve since we will |
| 3615 | * likely not use this space anyway, we want to try as |
| 3616 | * hard as possible to get this to work. |
| 3617 | */ |
| 3618 | if (ret) |
| 3619 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 3620 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3621 | if (ret) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3622 | printk(KERN_WARNING "Could not get space for a " |
Josef Bacik | 482e6dc | 2011-08-19 10:31:56 -0400 | [diff] [blame] | 3623 | "delete, will truncate on mount %d\n", ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3624 | btrfs_orphan_del(NULL, inode); |
| 3625 | btrfs_free_block_rsv(root, rsv); |
| 3626 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3627 | } |
| 3628 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3629 | trans = btrfs_start_transaction(root, 0); |
| 3630 | if (IS_ERR(trans)) { |
| 3631 | btrfs_orphan_del(NULL, inode); |
| 3632 | btrfs_free_block_rsv(root, rsv); |
| 3633 | goto no_delete; |
| 3634 | } |
| 3635 | |
| 3636 | trans->block_rsv = rsv; |
| 3637 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3638 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3639 | if (ret != -EAGAIN) |
| 3640 | break; |
| 3641 | |
| 3642 | nr = trans->blocks_used; |
| 3643 | btrfs_end_transaction(trans, root); |
| 3644 | trans = NULL; |
| 3645 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3646 | } |
| 3647 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3648 | btrfs_free_block_rsv(root, rsv); |
| 3649 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3650 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3651 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3652 | ret = btrfs_orphan_del(trans, inode); |
| 3653 | BUG_ON(ret); |
| 3654 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3655 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3656 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3657 | if (!(root == root->fs_info->tree_root || |
| 3658 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3659 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3660 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3661 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3662 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3663 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3664 | no_delete: |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3665 | end_writeback(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3666 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | /* |
| 3670 | * this returns the key found in the dir entry in the location pointer. |
| 3671 | * If no dir entries were found, location->objectid is 0. |
| 3672 | */ |
| 3673 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3674 | struct btrfs_key *location) |
| 3675 | { |
| 3676 | const char *name = dentry->d_name.name; |
| 3677 | int namelen = dentry->d_name.len; |
| 3678 | struct btrfs_dir_item *di; |
| 3679 | struct btrfs_path *path; |
| 3680 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3681 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3682 | |
| 3683 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 3684 | if (!path) |
| 3685 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3686 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3687 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3688 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3689 | if (IS_ERR(di)) |
| 3690 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3691 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3692 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3693 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3694 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3695 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3696 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3697 | btrfs_free_path(path); |
| 3698 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3699 | out_err: |
| 3700 | location->objectid = 0; |
| 3701 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | /* |
| 3705 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3706 | * needs to be changed to reflect the root directory of the tree root. This |
| 3707 | * is kind of like crossing a mount point. |
| 3708 | */ |
| 3709 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3710 | struct inode *dir, |
| 3711 | struct dentry *dentry, |
| 3712 | struct btrfs_key *location, |
| 3713 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3714 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3715 | struct btrfs_path *path; |
| 3716 | struct btrfs_root *new_root; |
| 3717 | struct btrfs_root_ref *ref; |
| 3718 | struct extent_buffer *leaf; |
| 3719 | int ret; |
| 3720 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3721 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3722 | path = btrfs_alloc_path(); |
| 3723 | if (!path) { |
| 3724 | err = -ENOMEM; |
| 3725 | goto out; |
| 3726 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3727 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3728 | err = -ENOENT; |
| 3729 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3730 | BTRFS_I(dir)->root->root_key.objectid, |
| 3731 | location->objectid); |
| 3732 | if (ret) { |
| 3733 | if (ret < 0) |
| 3734 | err = ret; |
| 3735 | goto out; |
| 3736 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3737 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3738 | leaf = path->nodes[0]; |
| 3739 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3740 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3741 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3742 | goto out; |
| 3743 | |
| 3744 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3745 | (unsigned long)(ref + 1), |
| 3746 | dentry->d_name.len); |
| 3747 | if (ret) |
| 3748 | goto out; |
| 3749 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3750 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3751 | |
| 3752 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3753 | if (IS_ERR(new_root)) { |
| 3754 | err = PTR_ERR(new_root); |
| 3755 | goto out; |
| 3756 | } |
| 3757 | |
| 3758 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3759 | err = -ENOENT; |
| 3760 | goto out; |
| 3761 | } |
| 3762 | |
| 3763 | *sub_root = new_root; |
| 3764 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3765 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3766 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3767 | err = 0; |
| 3768 | out: |
| 3769 | btrfs_free_path(path); |
| 3770 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3771 | } |
| 3772 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3773 | static void inode_tree_add(struct inode *inode) |
| 3774 | { |
| 3775 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3776 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3777 | struct rb_node **p; |
| 3778 | struct rb_node *parent; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3779 | u64 ino = btrfs_ino(inode); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3780 | again: |
| 3781 | p = &root->inode_tree.rb_node; |
| 3782 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3783 | |
Al Viro | 1d3382c | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 3784 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3785 | return; |
| 3786 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3787 | spin_lock(&root->inode_lock); |
| 3788 | while (*p) { |
| 3789 | parent = *p; |
| 3790 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3791 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3792 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3793 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3794 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3795 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3796 | else { |
| 3797 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 3798 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3799 | rb_erase(parent, &root->inode_tree); |
| 3800 | RB_CLEAR_NODE(parent); |
| 3801 | spin_unlock(&root->inode_lock); |
| 3802 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3803 | } |
| 3804 | } |
| 3805 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3806 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3807 | spin_unlock(&root->inode_lock); |
| 3808 | } |
| 3809 | |
| 3810 | static void inode_tree_del(struct inode *inode) |
| 3811 | { |
| 3812 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3813 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3814 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3815 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3816 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3817 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3818 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3819 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3820 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3821 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3822 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3823 | /* |
| 3824 | * Free space cache has inodes in the tree root, but the tree root has a |
| 3825 | * root_refs of 0, so this could end up dropping the tree root as a |
| 3826 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 3827 | * make sure we don't drop it. |
| 3828 | */ |
| 3829 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 3830 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3831 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3832 | spin_lock(&root->inode_lock); |
| 3833 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3834 | spin_unlock(&root->inode_lock); |
| 3835 | if (empty) |
| 3836 | btrfs_add_dead_root(root); |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | int btrfs_invalidate_inodes(struct btrfs_root *root) |
| 3841 | { |
| 3842 | struct rb_node *node; |
| 3843 | struct rb_node *prev; |
| 3844 | struct btrfs_inode *entry; |
| 3845 | struct inode *inode; |
| 3846 | u64 objectid = 0; |
| 3847 | |
| 3848 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3849 | |
| 3850 | spin_lock(&root->inode_lock); |
| 3851 | again: |
| 3852 | node = root->inode_tree.rb_node; |
| 3853 | prev = NULL; |
| 3854 | while (node) { |
| 3855 | prev = node; |
| 3856 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3857 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3858 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3859 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3860 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3861 | node = node->rb_right; |
| 3862 | else |
| 3863 | break; |
| 3864 | } |
| 3865 | if (!node) { |
| 3866 | while (prev) { |
| 3867 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3868 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3869 | node = prev; |
| 3870 | break; |
| 3871 | } |
| 3872 | prev = rb_next(prev); |
| 3873 | } |
| 3874 | } |
| 3875 | while (node) { |
| 3876 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3877 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3878 | inode = igrab(&entry->vfs_inode); |
| 3879 | if (inode) { |
| 3880 | spin_unlock(&root->inode_lock); |
| 3881 | if (atomic_read(&inode->i_count) > 1) |
| 3882 | d_prune_aliases(inode); |
| 3883 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 3884 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3885 | * the inode cache when its usage count |
| 3886 | * hits zero. |
| 3887 | */ |
| 3888 | iput(inode); |
| 3889 | cond_resched(); |
| 3890 | spin_lock(&root->inode_lock); |
| 3891 | goto again; |
| 3892 | } |
| 3893 | |
| 3894 | if (cond_resched_lock(&root->inode_lock)) |
| 3895 | goto again; |
| 3896 | |
| 3897 | node = rb_next(node); |
| 3898 | } |
| 3899 | spin_unlock(&root->inode_lock); |
| 3900 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3901 | } |
| 3902 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3903 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 3904 | { |
| 3905 | struct btrfs_iget_args *args = p; |
| 3906 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3907 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3908 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3909 | return 0; |
| 3910 | } |
| 3911 | |
| 3912 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 3913 | { |
| 3914 | struct btrfs_iget_args *args = opaque; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3915 | return args->ino == btrfs_ino(inode) && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3916 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3917 | } |
| 3918 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3919 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 3920 | u64 objectid, |
| 3921 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3922 | { |
| 3923 | struct inode *inode; |
| 3924 | struct btrfs_iget_args args; |
| 3925 | args.ino = objectid; |
| 3926 | args.root = root; |
| 3927 | |
| 3928 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 3929 | btrfs_init_locked_inode, |
| 3930 | (void *)&args); |
| 3931 | return inode; |
| 3932 | } |
| 3933 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3934 | /* Get an inode object given its location and corresponding root. |
| 3935 | * Returns in *is_new if the inode was read from disk |
| 3936 | */ |
| 3937 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3938 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3939 | { |
| 3940 | struct inode *inode; |
| 3941 | |
| 3942 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 3943 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3944 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3945 | |
| 3946 | if (inode->i_state & I_NEW) { |
| 3947 | BTRFS_I(inode)->root = root; |
| 3948 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 3949 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3950 | if (!is_bad_inode(inode)) { |
| 3951 | inode_tree_add(inode); |
| 3952 | unlock_new_inode(inode); |
| 3953 | if (new) |
| 3954 | *new = 1; |
| 3955 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3956 | unlock_new_inode(inode); |
| 3957 | iput(inode); |
| 3958 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3959 | } |
| 3960 | } |
| 3961 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 3962 | return inode; |
| 3963 | } |
| 3964 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3965 | static struct inode *new_simple_dir(struct super_block *s, |
| 3966 | struct btrfs_key *key, |
| 3967 | struct btrfs_root *root) |
| 3968 | { |
| 3969 | struct inode *inode = new_inode(s); |
| 3970 | |
| 3971 | if (!inode) |
| 3972 | return ERR_PTR(-ENOMEM); |
| 3973 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3974 | BTRFS_I(inode)->root = root; |
| 3975 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 3976 | BTRFS_I(inode)->dummy_inode = 1; |
| 3977 | |
| 3978 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
| 3979 | inode->i_op = &simple_dir_inode_operations; |
| 3980 | inode->i_fop = &simple_dir_operations; |
| 3981 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 3982 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 3983 | |
| 3984 | return inode; |
| 3985 | } |
| 3986 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 3987 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3988 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3989 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3990 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3991 | struct btrfs_root *sub_root = root; |
| 3992 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3993 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 3994 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3995 | |
| 3996 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 3997 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3998 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 3999 | if (unlikely(d_need_lookup(dentry))) { |
| 4000 | memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key)); |
| 4001 | kfree(dentry->d_fsdata); |
| 4002 | dentry->d_fsdata = NULL; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 4003 | /* This thing is hashed, drop it for now */ |
| 4004 | d_drop(dentry); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4005 | } else { |
| 4006 | ret = btrfs_inode_by_name(dir, dentry, &location); |
| 4007 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4008 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4009 | if (ret < 0) |
| 4010 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4011 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4012 | if (location.objectid == 0) |
| 4013 | return NULL; |
| 4014 | |
| 4015 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4016 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4017 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4018 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4019 | |
| 4020 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4021 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4022 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4023 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4024 | &location, &sub_root); |
| 4025 | if (ret < 0) { |
| 4026 | if (ret != -ENOENT) |
| 4027 | inode = ERR_PTR(ret); |
| 4028 | else |
| 4029 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4030 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4031 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4032 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4033 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4034 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 4035 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4036 | down_read(&root->fs_info->cleanup_work_sem); |
| 4037 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4038 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4039 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4040 | if (ret) |
| 4041 | inode = ERR_PTR(ret); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4044 | return inode; |
| 4045 | } |
| 4046 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 4047 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4048 | { |
| 4049 | struct btrfs_root *root; |
| 4050 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4051 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 4052 | dentry = dentry->d_parent; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4053 | |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4054 | if (dentry->d_inode) { |
| 4055 | root = BTRFS_I(dentry->d_inode)->root; |
| 4056 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4057 | return 1; |
| 4058 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4059 | return 0; |
| 4060 | } |
| 4061 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4062 | static void btrfs_dentry_release(struct dentry *dentry) |
| 4063 | { |
| 4064 | if (dentry->d_fsdata) |
| 4065 | kfree(dentry->d_fsdata); |
| 4066 | } |
| 4067 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4068 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 4069 | struct nameidata *nd) |
| 4070 | { |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 4071 | struct dentry *ret; |
| 4072 | |
| 4073 | ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); |
| 4074 | if (unlikely(d_need_lookup(dentry))) { |
| 4075 | spin_lock(&dentry->d_lock); |
| 4076 | dentry->d_flags &= ~DCACHE_NEED_LOOKUP; |
| 4077 | spin_unlock(&dentry->d_lock); |
| 4078 | } |
| 4079 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4080 | } |
| 4081 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4082 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4083 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 4084 | }; |
| 4085 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 4086 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 4087 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4088 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4089 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4090 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4091 | struct btrfs_item *item; |
| 4092 | struct btrfs_dir_item *di; |
| 4093 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4094 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4095 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4096 | struct list_head ins_list; |
| 4097 | struct list_head del_list; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4098 | struct qstr q; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4099 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4100 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4101 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4102 | unsigned char d_type; |
| 4103 | int over = 0; |
| 4104 | u32 di_cur; |
| 4105 | u32 di_total; |
| 4106 | u32 di_len; |
| 4107 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4108 | char tmp_name[32]; |
| 4109 | char *name_ptr; |
| 4110 | int name_len; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4111 | int is_curr = 0; /* filp->f_pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4112 | |
| 4113 | /* FIXME, use a real flag for deciding about the key type */ |
| 4114 | if (root->fs_info->tree_root == root) |
| 4115 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4116 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4117 | /* special case for "." */ |
| 4118 | if (filp->f_pos == 0) { |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 4119 | over = filldir(dirent, ".", 1, |
| 4120 | filp->f_pos, btrfs_ino(inode), DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4121 | if (over) |
| 4122 | return 0; |
| 4123 | filp->f_pos = 1; |
| 4124 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4125 | /* special case for .., just use the back ref */ |
| 4126 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4127 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4128 | over = filldir(dirent, "..", 2, |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 4129 | filp->f_pos, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4130 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4131 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4132 | filp->f_pos = 2; |
| 4133 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4134 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4135 | if (!path) |
| 4136 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4137 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 4138 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4139 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4140 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4141 | INIT_LIST_HEAD(&ins_list); |
| 4142 | INIT_LIST_HEAD(&del_list); |
| 4143 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 4144 | } |
| 4145 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4146 | btrfs_set_key_type(&key, key_type); |
| 4147 | key.offset = filp->f_pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4148 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4149 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4150 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4151 | if (ret < 0) |
| 4152 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4153 | |
| 4154 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4155 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4156 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4157 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4158 | ret = btrfs_next_leaf(root, path); |
| 4159 | if (ret < 0) |
| 4160 | goto err; |
| 4161 | else if (ret > 0) |
| 4162 | break; |
| 4163 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4164 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4165 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4166 | item = btrfs_item_nr(leaf, slot); |
| 4167 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4168 | |
| 4169 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4170 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4171 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4172 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4173 | if (found_key.offset < filp->f_pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4174 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4175 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 4176 | btrfs_should_delete_dir_index(&del_list, |
| 4177 | found_key.offset)) |
| 4178 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4179 | |
| 4180 | filp->f_pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4181 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4182 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4183 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 4184 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4185 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4186 | |
| 4187 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4188 | struct btrfs_key location; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4189 | struct dentry *tmp; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4190 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 4191 | if (verify_dir_item(root, leaf, di)) |
| 4192 | break; |
| 4193 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4194 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4195 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4196 | name_ptr = tmp_name; |
| 4197 | } else { |
| 4198 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4199 | if (!name_ptr) { |
| 4200 | ret = -ENOMEM; |
| 4201 | goto err; |
| 4202 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4203 | } |
| 4204 | read_extent_buffer(leaf, name_ptr, |
| 4205 | (unsigned long)(di + 1), name_len); |
| 4206 | |
| 4207 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4208 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4209 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4210 | q.name = name_ptr; |
| 4211 | q.len = name_len; |
| 4212 | q.hash = full_name_hash(q.name, q.len); |
| 4213 | tmp = d_lookup(filp->f_dentry, &q); |
| 4214 | if (!tmp) { |
| 4215 | struct btrfs_key *newkey; |
| 4216 | |
| 4217 | newkey = kzalloc(sizeof(struct btrfs_key), |
| 4218 | GFP_NOFS); |
| 4219 | if (!newkey) |
| 4220 | goto no_dentry; |
| 4221 | tmp = d_alloc(filp->f_dentry, &q); |
| 4222 | if (!tmp) { |
| 4223 | kfree(newkey); |
| 4224 | dput(tmp); |
| 4225 | goto no_dentry; |
| 4226 | } |
| 4227 | memcpy(newkey, &location, |
| 4228 | sizeof(struct btrfs_key)); |
| 4229 | tmp->d_fsdata = newkey; |
| 4230 | tmp->d_flags |= DCACHE_NEED_LOOKUP; |
| 4231 | d_rehash(tmp); |
| 4232 | dput(tmp); |
| 4233 | } else { |
| 4234 | dput(tmp); |
| 4235 | } |
| 4236 | no_dentry: |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4237 | /* is this a reference to our own snapshot? If so |
| 4238 | * skip it |
| 4239 | */ |
| 4240 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4241 | location.objectid == root->root_key.objectid) { |
| 4242 | over = 0; |
| 4243 | goto skip; |
| 4244 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4245 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4246 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4247 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4248 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4249 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4250 | if (name_ptr != tmp_name) |
| 4251 | kfree(name_ptr); |
| 4252 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4253 | if (over) |
| 4254 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4255 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4256 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4257 | di_cur += di_len; |
| 4258 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4259 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4260 | next: |
| 4261 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4262 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4263 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4264 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4265 | if (is_curr) |
| 4266 | filp->f_pos++; |
| 4267 | ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir, |
| 4268 | &ins_list); |
| 4269 | if (ret) |
| 4270 | goto nopos; |
| 4271 | } |
| 4272 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4273 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4274 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4275 | /* |
| 4276 | * 32-bit glibc will use getdents64, but then strtol - |
| 4277 | * so the last number we can serve is this. |
| 4278 | */ |
| 4279 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4280 | else |
| 4281 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4282 | nopos: |
| 4283 | ret = 0; |
| 4284 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4285 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 4286 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4287 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4288 | return ret; |
| 4289 | } |
| 4290 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4291 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4292 | { |
| 4293 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4294 | struct btrfs_trans_handle *trans; |
| 4295 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4296 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4297 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4298 | if (BTRFS_I(inode)->dummy_inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4299 | return 0; |
| 4300 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 4301 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(root, inode)) |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 4302 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4303 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4304 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4305 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4306 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4307 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4308 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4309 | if (IS_ERR(trans)) |
| 4310 | return PTR_ERR(trans); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4311 | if (nolock) |
| 4312 | ret = btrfs_end_transaction_nolock(trans, root); |
| 4313 | else |
| 4314 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4315 | } |
| 4316 | return ret; |
| 4317 | } |
| 4318 | |
| 4319 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4320 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4321 | * inode changes. But, it is most likely to find the inode in cache. |
| 4322 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4323 | * to keep or drop this code. |
| 4324 | */ |
Christoph Hellwig | aa38572 | 2011-05-27 06:53:02 -0400 | [diff] [blame] | 4325 | void btrfs_dirty_inode(struct inode *inode, int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4326 | { |
| 4327 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4328 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4329 | int ret; |
| 4330 | |
| 4331 | if (BTRFS_I(inode)->dummy_inode) |
| 4332 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4333 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4334 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4335 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4336 | |
| 4337 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4338 | if (ret && ret == -ENOSPC) { |
| 4339 | /* whoops, lets try again with the full transaction */ |
| 4340 | btrfs_end_transaction(trans, root); |
| 4341 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4342 | if (IS_ERR(trans)) { |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 4343 | printk_ratelimited(KERN_ERR "btrfs: fail to " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4344 | "dirty inode %llu error %ld\n", |
| 4345 | (unsigned long long)btrfs_ino(inode), |
| 4346 | PTR_ERR(trans)); |
Chris Mason | 9aeead7 | 2010-05-27 10:23:00 -0400 | [diff] [blame] | 4347 | return; |
| 4348 | } |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4349 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4350 | ret = btrfs_update_inode(trans, root, inode); |
| 4351 | if (ret) { |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 4352 | printk_ratelimited(KERN_ERR "btrfs: fail to " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4353 | "dirty inode %llu error %d\n", |
| 4354 | (unsigned long long)btrfs_ino(inode), |
| 4355 | ret); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4356 | } |
| 4357 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4358 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4359 | if (BTRFS_I(inode)->delayed_node) |
| 4360 | btrfs_balance_delayed_items(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4361 | } |
| 4362 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4363 | /* |
| 4364 | * find the highest existing sequence number in a directory |
| 4365 | * and then set the in-memory index_cnt variable to reflect |
| 4366 | * free sequence numbers |
| 4367 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4368 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4369 | { |
| 4370 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4371 | struct btrfs_key key, found_key; |
| 4372 | struct btrfs_path *path; |
| 4373 | struct extent_buffer *leaf; |
| 4374 | int ret; |
| 4375 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4376 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4377 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4378 | key.offset = (u64)-1; |
| 4379 | |
| 4380 | path = btrfs_alloc_path(); |
| 4381 | if (!path) |
| 4382 | return -ENOMEM; |
| 4383 | |
| 4384 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4385 | if (ret < 0) |
| 4386 | goto out; |
| 4387 | /* FIXME: we should be able to handle this */ |
| 4388 | if (ret == 0) |
| 4389 | goto out; |
| 4390 | ret = 0; |
| 4391 | |
| 4392 | /* |
| 4393 | * MAGIC NUMBER EXPLANATION: |
| 4394 | * since we search a directory based on f_pos we have to start at 2 |
| 4395 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4396 | * else has to start at 2 |
| 4397 | */ |
| 4398 | if (path->slots[0] == 0) { |
| 4399 | BTRFS_I(inode)->index_cnt = 2; |
| 4400 | goto out; |
| 4401 | } |
| 4402 | |
| 4403 | path->slots[0]--; |
| 4404 | |
| 4405 | leaf = path->nodes[0]; |
| 4406 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4407 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4408 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4409 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4410 | BTRFS_I(inode)->index_cnt = 2; |
| 4411 | goto out; |
| 4412 | } |
| 4413 | |
| 4414 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4415 | out: |
| 4416 | btrfs_free_path(path); |
| 4417 | return ret; |
| 4418 | } |
| 4419 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4420 | /* |
| 4421 | * helper to find a free sequence number in a given directory. This current |
| 4422 | * code is very simple, later versions will do smarter things in the btree |
| 4423 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4424 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4425 | { |
| 4426 | int ret = 0; |
| 4427 | |
| 4428 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4429 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 4430 | if (ret) { |
| 4431 | ret = btrfs_set_inode_index_count(dir); |
| 4432 | if (ret) |
| 4433 | return ret; |
| 4434 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4435 | } |
| 4436 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4437 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4438 | BTRFS_I(dir)->index_cnt++; |
| 4439 | |
| 4440 | return ret; |
| 4441 | } |
| 4442 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4443 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4444 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4445 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4446 | const char *name, int name_len, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4447 | u64 ref_objectid, u64 objectid, int mode, |
| 4448 | u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4449 | { |
| 4450 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4451 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4452 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4453 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4454 | struct btrfs_inode_ref *ref; |
| 4455 | struct btrfs_key key[2]; |
| 4456 | u32 sizes[2]; |
| 4457 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4458 | int ret; |
| 4459 | int owner; |
| 4460 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4461 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4462 | if (!path) |
| 4463 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4464 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4465 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4466 | if (!inode) { |
| 4467 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4468 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4469 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4470 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4471 | /* |
| 4472 | * we have to initialize this early, so we can reclaim the inode |
| 4473 | * number if we fail afterwards in this function. |
| 4474 | */ |
| 4475 | inode->i_ino = objectid; |
| 4476 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4477 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4478 | trace_btrfs_inode_request(dir); |
| 4479 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4480 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4481 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4482 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4483 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4484 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4485 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4486 | } |
| 4487 | /* |
| 4488 | * index_cnt is ignored for everything but a dir, |
| 4489 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4490 | * number |
| 4491 | */ |
| 4492 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4493 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4494 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 4495 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4496 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4497 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 4498 | if (S_ISDIR(mode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4499 | owner = 0; |
| 4500 | else |
| 4501 | owner = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4502 | |
| 4503 | key[0].objectid = objectid; |
| 4504 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4505 | key[0].offset = 0; |
| 4506 | |
| 4507 | key[1].objectid = objectid; |
| 4508 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4509 | key[1].offset = ref_objectid; |
| 4510 | |
| 4511 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4512 | sizes[1] = name_len + sizeof(*ref); |
| 4513 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4514 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4515 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4516 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4517 | goto fail; |
| 4518 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 4519 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4520 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4521 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4522 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4523 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4524 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4525 | |
| 4526 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4527 | struct btrfs_inode_ref); |
| 4528 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4529 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4530 | ptr = (unsigned long)(ref + 1); |
| 4531 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4532 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4533 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4534 | btrfs_free_path(path); |
| 4535 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4536 | location = &BTRFS_I(inode)->location; |
| 4537 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4538 | location->offset = 0; |
| 4539 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4540 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4541 | btrfs_inherit_iflags(inode, dir); |
| 4542 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 4543 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4544 | if (btrfs_test_opt(root, NODATASUM)) |
| 4545 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 4546 | if (btrfs_test_opt(root, NODATACOW) || |
| 4547 | (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4548 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4549 | } |
| 4550 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4551 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4552 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4553 | |
| 4554 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 4555 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4556 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4557 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4558 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4559 | if (dir) |
| 4560 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4561 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4562 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4563 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4567 | { |
| 4568 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4569 | } |
| 4570 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4571 | /* |
| 4572 | * utility function to add 'inode' into 'parent_inode' with |
| 4573 | * a give name and a given sequence number. |
| 4574 | * if 'add_backref' is true, also insert a backref from the |
| 4575 | * inode to the parent directory. |
| 4576 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4577 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4578 | struct inode *parent_inode, struct inode *inode, |
| 4579 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4580 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4581 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4582 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4583 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4584 | u64 ino = btrfs_ino(inode); |
| 4585 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4586 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4587 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4588 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4589 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4590 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4591 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4592 | key.offset = 0; |
| 4593 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4594 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4595 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4596 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4597 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4598 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4599 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4600 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 4601 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4602 | } |
| 4603 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4604 | if (ret == 0) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4605 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4606 | parent_inode, &key, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4607 | btrfs_inode_type(inode), index); |
| 4608 | BUG_ON(ret); |
| 4609 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4610 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4611 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 4612 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4613 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4614 | } |
| 4615 | return ret; |
| 4616 | } |
| 4617 | |
| 4618 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4619 | struct inode *dir, struct dentry *dentry, |
| 4620 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4621 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4622 | int err = btrfs_add_link(trans, dir, inode, |
| 4623 | dentry->d_name.name, dentry->d_name.len, |
| 4624 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4625 | if (!err) { |
| 4626 | d_instantiate(dentry, inode); |
| 4627 | return 0; |
| 4628 | } |
| 4629 | if (err > 0) |
| 4630 | err = -EEXIST; |
| 4631 | return err; |
| 4632 | } |
| 4633 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4634 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 4635 | int mode, dev_t rdev) |
| 4636 | { |
| 4637 | struct btrfs_trans_handle *trans; |
| 4638 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4639 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4640 | int err; |
| 4641 | int drop_inode = 0; |
| 4642 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4643 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4644 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4645 | |
| 4646 | if (!new_valid_dev(rdev)) |
| 4647 | return -EINVAL; |
| 4648 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4649 | /* |
| 4650 | * 2 for inode item and ref |
| 4651 | * 2 for dir items |
| 4652 | * 1 for xattr if selinux is on |
| 4653 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4654 | trans = btrfs_start_transaction(root, 5); |
| 4655 | if (IS_ERR(trans)) |
| 4656 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4657 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4658 | err = btrfs_find_free_ino(root, &objectid); |
| 4659 | if (err) |
| 4660 | goto out_unlock; |
| 4661 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4662 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4663 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4664 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4665 | if (IS_ERR(inode)) { |
| 4666 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4667 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4668 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4669 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4670 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4671 | if (err) { |
| 4672 | drop_inode = 1; |
| 4673 | goto out_unlock; |
| 4674 | } |
| 4675 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4676 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4677 | if (err) |
| 4678 | drop_inode = 1; |
| 4679 | else { |
| 4680 | inode->i_op = &btrfs_special_inode_operations; |
| 4681 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4682 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4683 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4684 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4685 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4686 | btrfs_end_transaction_throttle(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4687 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4688 | if (drop_inode) { |
| 4689 | inode_dec_link_count(inode); |
| 4690 | iput(inode); |
| 4691 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4692 | return err; |
| 4693 | } |
| 4694 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4695 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 4696 | int mode, struct nameidata *nd) |
| 4697 | { |
| 4698 | struct btrfs_trans_handle *trans; |
| 4699 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4700 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4701 | int drop_inode = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4702 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4703 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4704 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4705 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4706 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4707 | /* |
| 4708 | * 2 for inode item and ref |
| 4709 | * 2 for dir items |
| 4710 | * 1 for xattr if selinux is on |
| 4711 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4712 | trans = btrfs_start_transaction(root, 5); |
| 4713 | if (IS_ERR(trans)) |
| 4714 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4715 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4716 | err = btrfs_find_free_ino(root, &objectid); |
| 4717 | if (err) |
| 4718 | goto out_unlock; |
| 4719 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4720 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4721 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4722 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4723 | if (IS_ERR(inode)) { |
| 4724 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4725 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4726 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4727 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4728 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4729 | if (err) { |
| 4730 | drop_inode = 1; |
| 4731 | goto out_unlock; |
| 4732 | } |
| 4733 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4734 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4735 | if (err) |
| 4736 | drop_inode = 1; |
| 4737 | else { |
| 4738 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4739 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4740 | inode->i_fop = &btrfs_file_operations; |
| 4741 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4742 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4743 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4744 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4745 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4746 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4747 | if (drop_inode) { |
| 4748 | inode_dec_link_count(inode); |
| 4749 | iput(inode); |
| 4750 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4751 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4752 | return err; |
| 4753 | } |
| 4754 | |
| 4755 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4756 | struct dentry *dentry) |
| 4757 | { |
| 4758 | struct btrfs_trans_handle *trans; |
| 4759 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4760 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4761 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4762 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4763 | int err; |
| 4764 | int drop_inode = 0; |
| 4765 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4766 | /* do not allow sys_link's with other subvols of the same device */ |
| 4767 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 4768 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4769 | |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 4770 | if (inode->i_nlink == ~0U) |
| 4771 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4772 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4773 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4774 | if (err) |
| 4775 | goto fail; |
| 4776 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4777 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4778 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4779 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4780 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4781 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4782 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4783 | if (IS_ERR(trans)) { |
| 4784 | err = PTR_ERR(trans); |
| 4785 | goto fail; |
| 4786 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4787 | |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 4788 | btrfs_inc_nlink(inode); |
| 4789 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6e | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 4790 | ihold(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4791 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4792 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4793 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4794 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4795 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4796 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 4797 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4798 | err = btrfs_update_inode(trans, root, inode); |
| 4799 | BUG_ON(err); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4800 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4801 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4802 | |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4803 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4804 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4805 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4806 | if (drop_inode) { |
| 4807 | inode_dec_link_count(inode); |
| 4808 | iput(inode); |
| 4809 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4810 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4811 | return err; |
| 4812 | } |
| 4813 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4814 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 4815 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4816 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4817 | struct btrfs_trans_handle *trans; |
| 4818 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4819 | int err = 0; |
| 4820 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4821 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4822 | u64 index = 0; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4823 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4824 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4825 | /* |
| 4826 | * 2 items for inode and ref |
| 4827 | * 2 items for dir items |
| 4828 | * 1 for xattr if selinux is on |
| 4829 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4830 | trans = btrfs_start_transaction(root, 5); |
| 4831 | if (IS_ERR(trans)) |
| 4832 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4833 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4834 | err = btrfs_find_free_ino(root, &objectid); |
| 4835 | if (err) |
| 4836 | goto out_fail; |
| 4837 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4838 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4839 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4840 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4841 | if (IS_ERR(inode)) { |
| 4842 | err = PTR_ERR(inode); |
| 4843 | goto out_fail; |
| 4844 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4845 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4846 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4847 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4848 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4849 | if (err) |
| 4850 | goto out_fail; |
| 4851 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4852 | inode->i_op = &btrfs_dir_inode_operations; |
| 4853 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4854 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4855 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4856 | err = btrfs_update_inode(trans, root, inode); |
| 4857 | if (err) |
| 4858 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4859 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4860 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 4861 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4862 | if (err) |
| 4863 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4864 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4865 | d_instantiate(dentry, inode); |
| 4866 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4867 | |
| 4868 | out_fail: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4869 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 4870 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4871 | if (drop_on_err) |
| 4872 | iput(inode); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4873 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4874 | return err; |
| 4875 | } |
| 4876 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4877 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 4878 | * and an extent that you want to insert, deal with overlap and insert |
| 4879 | * the new extent into the tree. |
| 4880 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4881 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 4882 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4883 | struct extent_map *em, |
| 4884 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4885 | { |
| 4886 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4887 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4888 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 4889 | start_diff = map_start - em->start; |
| 4890 | em->start = map_start; |
| 4891 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4892 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 4893 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4894 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4895 | em->block_len -= start_diff; |
| 4896 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4897 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4898 | } |
| 4899 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4900 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 4901 | struct inode *inode, struct page *page, |
| 4902 | size_t pg_offset, u64 extent_offset, |
| 4903 | struct btrfs_file_extent_item *item) |
| 4904 | { |
| 4905 | int ret; |
| 4906 | struct extent_buffer *leaf = path->nodes[0]; |
| 4907 | char *tmp; |
| 4908 | size_t max_size; |
| 4909 | unsigned long inline_size; |
| 4910 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4911 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4912 | |
| 4913 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4914 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4915 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 4916 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 4917 | btrfs_item_nr(leaf, path->slots[0])); |
| 4918 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 4919 | if (!tmp) |
| 4920 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4921 | ptr = btrfs_file_extent_inline_start(item); |
| 4922 | |
| 4923 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 4924 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 4925 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4926 | ret = btrfs_decompress(compress_type, tmp, page, |
| 4927 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4928 | if (ret) { |
| 4929 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 4930 | unsigned long copy_size = min_t(u64, |
| 4931 | PAGE_CACHE_SIZE - pg_offset, |
| 4932 | max_size - extent_offset); |
| 4933 | memset(kaddr + pg_offset, 0, copy_size); |
| 4934 | kunmap_atomic(kaddr, KM_USER0); |
| 4935 | } |
| 4936 | kfree(tmp); |
| 4937 | return 0; |
| 4938 | } |
| 4939 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4940 | /* |
| 4941 | * 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] | 4942 | * the ugly parts come from merging extents from the disk with the in-ram |
| 4943 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4944 | * where the in-ram extents might be locked pending data=ordered completion. |
| 4945 | * |
| 4946 | * This also copies inline extents directly into the page. |
| 4947 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4948 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4949 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4950 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4951 | int create) |
| 4952 | { |
| 4953 | int ret; |
| 4954 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4955 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4956 | u64 extent_start = 0; |
| 4957 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4958 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4959 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4960 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4961 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4962 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4963 | struct extent_buffer *leaf; |
| 4964 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4965 | struct extent_map *em = NULL; |
| 4966 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4967 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4968 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 4969 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4970 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4971 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4972 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4973 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4974 | if (em) |
| 4975 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4976 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4977 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4978 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 4979 | if (em->start > start || em->start + em->len <= start) |
| 4980 | free_extent_map(em); |
| 4981 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4982 | free_extent_map(em); |
| 4983 | else |
| 4984 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4985 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4986 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4987 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4988 | err = -ENOMEM; |
| 4989 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4990 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4991 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4992 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 4993 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4994 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4995 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 4996 | |
| 4997 | if (!path) { |
| 4998 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 4999 | if (!path) { |
| 5000 | err = -ENOMEM; |
| 5001 | goto out; |
| 5002 | } |
| 5003 | /* |
| 5004 | * Chances are we'll be called again, so go ahead and do |
| 5005 | * readahead |
| 5006 | */ |
| 5007 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5008 | } |
| 5009 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5010 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 5011 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5012 | if (ret < 0) { |
| 5013 | err = ret; |
| 5014 | goto out; |
| 5015 | } |
| 5016 | |
| 5017 | if (ret != 0) { |
| 5018 | if (path->slots[0] == 0) |
| 5019 | goto not_found; |
| 5020 | path->slots[0]--; |
| 5021 | } |
| 5022 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5023 | leaf = path->nodes[0]; |
| 5024 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5025 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5026 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5027 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5028 | found_type = btrfs_key_type(&found_key); |
| 5029 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5030 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 5031 | goto not_found; |
| 5032 | } |
| 5033 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5034 | found_type = btrfs_file_extent_type(leaf, item); |
| 5035 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5036 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5037 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5038 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5039 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5040 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5041 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5042 | size_t size; |
| 5043 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5044 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 5045 | ~((u64)root->sectorsize - 1); |
| 5046 | } |
| 5047 | |
| 5048 | if (start >= extent_end) { |
| 5049 | path->slots[0]++; |
| 5050 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 5051 | ret = btrfs_next_leaf(root, path); |
| 5052 | if (ret < 0) { |
| 5053 | err = ret; |
| 5054 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5055 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5056 | if (ret > 0) |
| 5057 | goto not_found; |
| 5058 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5059 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5060 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5061 | if (found_key.objectid != objectid || |
| 5062 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 5063 | goto not_found; |
| 5064 | if (start + len <= found_key.offset) |
| 5065 | goto not_found; |
| 5066 | em->start = start; |
| 5067 | em->len = found_key.offset - start; |
| 5068 | goto not_found_em; |
| 5069 | } |
| 5070 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5071 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5072 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5073 | em->start = extent_start; |
| 5074 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5075 | em->orig_start = extent_start - |
| 5076 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5077 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 5078 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5079 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5080 | goto insert; |
| 5081 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5082 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5083 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5084 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5085 | em->block_start = bytenr; |
| 5086 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 5087 | item); |
| 5088 | } else { |
| 5089 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 5090 | em->block_start = bytenr; |
| 5091 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5092 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 5093 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5094 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5095 | goto insert; |
| 5096 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5097 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5098 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5099 | size_t size; |
| 5100 | size_t extent_offset; |
| 5101 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5102 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5103 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5104 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5105 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5106 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5107 | goto out; |
| 5108 | } |
| 5109 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5110 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5111 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5112 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5113 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5114 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5115 | em->len = (copy_size + root->sectorsize - 1) & |
| 5116 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5117 | em->orig_start = EXTENT_MAP_INLINE; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5118 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5119 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5120 | em->compress_type = compress_type; |
| 5121 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5122 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5123 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5124 | if (btrfs_file_extent_compression(leaf, item) != |
| 5125 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5126 | ret = uncompress_inline(path, inode, page, |
| 5127 | pg_offset, |
| 5128 | extent_offset, item); |
| 5129 | BUG_ON(ret); |
| 5130 | } else { |
| 5131 | map = kmap(page); |
| 5132 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 5133 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 5134 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 5135 | memset(map + pg_offset + copy_size, 0, |
| 5136 | PAGE_CACHE_SIZE - pg_offset - |
| 5137 | copy_size); |
| 5138 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5139 | kunmap(page); |
| 5140 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5141 | flush_dcache_page(page); |
| 5142 | } else if (create && PageUptodate(page)) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5143 | WARN_ON(1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5144 | if (!trans) { |
| 5145 | kunmap(page); |
| 5146 | free_extent_map(em); |
| 5147 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5148 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5149 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5150 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5151 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5152 | if (IS_ERR(trans)) |
| 5153 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5154 | goto again; |
| 5155 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5156 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5157 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5158 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5159 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5160 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5161 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5162 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 5163 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5164 | goto insert; |
| 5165 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5166 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5167 | WARN_ON(1); |
| 5168 | } |
| 5169 | not_found: |
| 5170 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5171 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5172 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5173 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5174 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5175 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5176 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5177 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5178 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 5179 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 5180 | (unsigned long long)em->len, |
| 5181 | (unsigned long long)start, |
| 5182 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5183 | err = -EIO; |
| 5184 | goto out; |
| 5185 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5186 | |
| 5187 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5188 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5189 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5190 | /* it is possible that someone inserted the extent into the tree |
| 5191 | * while we had the lock dropped. It is also possible that |
| 5192 | * an overlapping map exists in the tree |
| 5193 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5194 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5195 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5196 | |
| 5197 | ret = 0; |
| 5198 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5199 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5200 | if (existing && (existing->start > start || |
| 5201 | existing->start + existing->len <= start)) { |
| 5202 | free_extent_map(existing); |
| 5203 | existing = NULL; |
| 5204 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5205 | if (!existing) { |
| 5206 | existing = lookup_extent_mapping(em_tree, em->start, |
| 5207 | em->len); |
| 5208 | if (existing) { |
| 5209 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5210 | em, start, |
| 5211 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5212 | free_extent_map(existing); |
| 5213 | if (err) { |
| 5214 | free_extent_map(em); |
| 5215 | em = NULL; |
| 5216 | } |
| 5217 | } else { |
| 5218 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5219 | free_extent_map(em); |
| 5220 | em = NULL; |
| 5221 | } |
| 5222 | } else { |
| 5223 | free_extent_map(em); |
| 5224 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5225 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5226 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5227 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5228 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5229 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5230 | |
| 5231 | trace_btrfs_get_extent(root, em); |
| 5232 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5233 | if (path) |
| 5234 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5235 | if (trans) { |
| 5236 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5237 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5238 | err = ret; |
| 5239 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5240 | if (err) { |
| 5241 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5242 | return ERR_PTR(err); |
| 5243 | } |
| 5244 | return em; |
| 5245 | } |
| 5246 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5247 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 5248 | size_t pg_offset, u64 start, u64 len, |
| 5249 | int create) |
| 5250 | { |
| 5251 | struct extent_map *em; |
| 5252 | struct extent_map *hole_em = NULL; |
| 5253 | u64 range_start = start; |
| 5254 | u64 end; |
| 5255 | u64 found; |
| 5256 | u64 found_end; |
| 5257 | int err = 0; |
| 5258 | |
| 5259 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 5260 | if (IS_ERR(em)) |
| 5261 | return em; |
| 5262 | if (em) { |
| 5263 | /* |
| 5264 | * if our em maps to a hole, there might |
| 5265 | * actually be delalloc bytes behind it |
| 5266 | */ |
| 5267 | if (em->block_start != EXTENT_MAP_HOLE) |
| 5268 | return em; |
| 5269 | else |
| 5270 | hole_em = em; |
| 5271 | } |
| 5272 | |
| 5273 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 5274 | end = start + len; |
| 5275 | if (end < start) |
| 5276 | end = (u64)-1; |
| 5277 | else |
| 5278 | end -= 1; |
| 5279 | |
| 5280 | em = NULL; |
| 5281 | |
| 5282 | /* ok, we didn't find anything, lets look for delalloc */ |
| 5283 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 5284 | end, len, EXTENT_DELALLOC, 1); |
| 5285 | found_end = range_start + found; |
| 5286 | if (found_end < range_start) |
| 5287 | found_end = (u64)-1; |
| 5288 | |
| 5289 | /* |
| 5290 | * we didn't find anything useful, return |
| 5291 | * the original results from get_extent() |
| 5292 | */ |
| 5293 | if (range_start > end || found_end <= start) { |
| 5294 | em = hole_em; |
| 5295 | hole_em = NULL; |
| 5296 | goto out; |
| 5297 | } |
| 5298 | |
| 5299 | /* adjust the range_start to make sure it doesn't |
| 5300 | * go backwards from the start they passed in |
| 5301 | */ |
| 5302 | range_start = max(start,range_start); |
| 5303 | found = found_end - range_start; |
| 5304 | |
| 5305 | if (found > 0) { |
| 5306 | u64 hole_start = start; |
| 5307 | u64 hole_len = len; |
| 5308 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5309 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5310 | if (!em) { |
| 5311 | err = -ENOMEM; |
| 5312 | goto out; |
| 5313 | } |
| 5314 | /* |
| 5315 | * when btrfs_get_extent can't find anything it |
| 5316 | * returns one huge hole |
| 5317 | * |
| 5318 | * make sure what it found really fits our range, and |
| 5319 | * adjust to make sure it is based on the start from |
| 5320 | * the caller |
| 5321 | */ |
| 5322 | if (hole_em) { |
| 5323 | u64 calc_end = extent_map_end(hole_em); |
| 5324 | |
| 5325 | if (calc_end <= start || (hole_em->start > end)) { |
| 5326 | free_extent_map(hole_em); |
| 5327 | hole_em = NULL; |
| 5328 | } else { |
| 5329 | hole_start = max(hole_em->start, start); |
| 5330 | hole_len = calc_end - hole_start; |
| 5331 | } |
| 5332 | } |
| 5333 | em->bdev = NULL; |
| 5334 | if (hole_em && range_start > hole_start) { |
| 5335 | /* our hole starts before our delalloc, so we |
| 5336 | * have to return just the parts of the hole |
| 5337 | * that go until the delalloc starts |
| 5338 | */ |
| 5339 | em->len = min(hole_len, |
| 5340 | range_start - hole_start); |
| 5341 | em->start = hole_start; |
| 5342 | em->orig_start = hole_start; |
| 5343 | /* |
| 5344 | * don't adjust block start at all, |
| 5345 | * it is fixed at EXTENT_MAP_HOLE |
| 5346 | */ |
| 5347 | em->block_start = hole_em->block_start; |
| 5348 | em->block_len = hole_len; |
| 5349 | } else { |
| 5350 | em->start = range_start; |
| 5351 | em->len = found; |
| 5352 | em->orig_start = range_start; |
| 5353 | em->block_start = EXTENT_MAP_DELALLOC; |
| 5354 | em->block_len = found; |
| 5355 | } |
| 5356 | } else if (hole_em) { |
| 5357 | return hole_em; |
| 5358 | } |
| 5359 | out: |
| 5360 | |
| 5361 | free_extent_map(hole_em); |
| 5362 | if (err) { |
| 5363 | free_extent_map(em); |
| 5364 | return ERR_PTR(err); |
| 5365 | } |
| 5366 | return em; |
| 5367 | } |
| 5368 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5369 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5370 | struct extent_map *em, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5371 | u64 start, u64 len) |
| 5372 | { |
| 5373 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5374 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5375 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 5376 | struct btrfs_key ins; |
| 5377 | u64 alloc_hint; |
| 5378 | int ret; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5379 | bool insert = false; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5380 | |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5381 | /* |
| 5382 | * Ok if the extent map we looked up is a hole and is for the exact |
| 5383 | * range we want, there is no reason to allocate a new one, however if |
| 5384 | * it is not right then we need to free this one and drop the cache for |
| 5385 | * our range. |
| 5386 | */ |
| 5387 | if (em->block_start != EXTENT_MAP_HOLE || em->start != start || |
| 5388 | em->len != len) { |
| 5389 | free_extent_map(em); |
| 5390 | em = NULL; |
| 5391 | insert = true; |
| 5392 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
| 5393 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5394 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5395 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5396 | if (IS_ERR(trans)) |
| 5397 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5398 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 5399 | if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024) |
| 5400 | btrfs_add_inode_defrag(trans, inode); |
| 5401 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5402 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5403 | |
| 5404 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 5405 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
| 5406 | alloc_hint, (u64)-1, &ins, 1); |
| 5407 | if (ret) { |
| 5408 | em = ERR_PTR(ret); |
| 5409 | goto out; |
| 5410 | } |
| 5411 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5412 | if (!em) { |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5413 | em = alloc_extent_map(); |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5414 | if (!em) { |
| 5415 | em = ERR_PTR(-ENOMEM); |
| 5416 | goto out; |
| 5417 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5418 | } |
| 5419 | |
| 5420 | em->start = start; |
| 5421 | em->orig_start = em->start; |
| 5422 | em->len = ins.offset; |
| 5423 | |
| 5424 | em->block_start = ins.objectid; |
| 5425 | em->block_len = ins.offset; |
| 5426 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5427 | |
| 5428 | /* |
| 5429 | * We need to do this because if we're using the original em we searched |
| 5430 | * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that. |
| 5431 | */ |
| 5432 | em->flags = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5433 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5434 | |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5435 | while (insert) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5436 | write_lock(&em_tree->lock); |
| 5437 | ret = add_extent_mapping(em_tree, em); |
| 5438 | write_unlock(&em_tree->lock); |
| 5439 | if (ret != -EEXIST) |
| 5440 | break; |
| 5441 | btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0); |
| 5442 | } |
| 5443 | |
| 5444 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 5445 | ins.offset, ins.offset, 0); |
| 5446 | if (ret) { |
| 5447 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 5448 | em = ERR_PTR(ret); |
| 5449 | } |
| 5450 | out: |
| 5451 | btrfs_end_transaction(trans, root); |
| 5452 | return em; |
| 5453 | } |
| 5454 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5455 | /* |
| 5456 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 5457 | * block must be cow'd |
| 5458 | */ |
| 5459 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 5460 | struct inode *inode, u64 offset, u64 len) |
| 5461 | { |
| 5462 | struct btrfs_path *path; |
| 5463 | int ret; |
| 5464 | struct extent_buffer *leaf; |
| 5465 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5466 | struct btrfs_file_extent_item *fi; |
| 5467 | struct btrfs_key key; |
| 5468 | u64 disk_bytenr; |
| 5469 | u64 backref_offset; |
| 5470 | u64 extent_end; |
| 5471 | u64 num_bytes; |
| 5472 | int slot; |
| 5473 | int found_type; |
| 5474 | |
| 5475 | path = btrfs_alloc_path(); |
| 5476 | if (!path) |
| 5477 | return -ENOMEM; |
| 5478 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5479 | ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5480 | offset, 0); |
| 5481 | if (ret < 0) |
| 5482 | goto out; |
| 5483 | |
| 5484 | slot = path->slots[0]; |
| 5485 | if (ret == 1) { |
| 5486 | if (slot == 0) { |
| 5487 | /* can't find the item, must cow */ |
| 5488 | ret = 0; |
| 5489 | goto out; |
| 5490 | } |
| 5491 | slot--; |
| 5492 | } |
| 5493 | ret = 0; |
| 5494 | leaf = path->nodes[0]; |
| 5495 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5496 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5497 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5498 | /* not our file or wrong item type, must cow */ |
| 5499 | goto out; |
| 5500 | } |
| 5501 | |
| 5502 | if (key.offset > offset) { |
| 5503 | /* Wrong offset, must cow */ |
| 5504 | goto out; |
| 5505 | } |
| 5506 | |
| 5507 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5508 | found_type = btrfs_file_extent_type(leaf, fi); |
| 5509 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 5510 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 5511 | /* not a regular extent, must cow */ |
| 5512 | goto out; |
| 5513 | } |
| 5514 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5515 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 5516 | |
| 5517 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 5518 | if (extent_end < offset + len) { |
| 5519 | /* extent doesn't include our full range, must cow */ |
| 5520 | goto out; |
| 5521 | } |
| 5522 | |
| 5523 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 5524 | goto out; |
| 5525 | |
| 5526 | /* |
| 5527 | * look for other files referencing this extent, if we |
| 5528 | * find any we must cow |
| 5529 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5530 | if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5531 | key.offset - backref_offset, disk_bytenr)) |
| 5532 | goto out; |
| 5533 | |
| 5534 | /* |
| 5535 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 5536 | * in this extent we are about to write. If there |
| 5537 | * are any csums in that range we have to cow in order |
| 5538 | * to keep the csums correct |
| 5539 | */ |
| 5540 | disk_bytenr += backref_offset; |
| 5541 | disk_bytenr += offset - key.offset; |
| 5542 | num_bytes = min(offset + len, extent_end) - offset; |
| 5543 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 5544 | goto out; |
| 5545 | /* |
| 5546 | * all of the above have passed, it is safe to overwrite this extent |
| 5547 | * without cow |
| 5548 | */ |
| 5549 | ret = 1; |
| 5550 | out: |
| 5551 | btrfs_free_path(path); |
| 5552 | return ret; |
| 5553 | } |
| 5554 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5555 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 5556 | struct buffer_head *bh_result, int create) |
| 5557 | { |
| 5558 | struct extent_map *em; |
| 5559 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5560 | u64 start = iblock << inode->i_blkbits; |
| 5561 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5562 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5563 | |
| 5564 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
| 5565 | if (IS_ERR(em)) |
| 5566 | return PTR_ERR(em); |
| 5567 | |
| 5568 | /* |
| 5569 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 5570 | * io. INLINE is special, and we could probably kludge it in here, but |
| 5571 | * it's still buffered so for safety lets just fall back to the generic |
| 5572 | * buffered path. |
| 5573 | * |
| 5574 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 5575 | * decompress it, so there will be buffering required no matter what we |
| 5576 | * do, so go ahead and fallback to buffered. |
| 5577 | * |
| 5578 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 5579 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 5580 | * the generic code. |
| 5581 | */ |
| 5582 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 5583 | em->block_start == EXTENT_MAP_INLINE) { |
| 5584 | free_extent_map(em); |
| 5585 | return -ENOTBLK; |
| 5586 | } |
| 5587 | |
| 5588 | /* Just a good old fashioned hole, return */ |
| 5589 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 5590 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5591 | free_extent_map(em); |
| 5592 | /* DIO will do one hole at a time, so just unlock a sector */ |
| 5593 | unlock_extent(&BTRFS_I(inode)->io_tree, start, |
| 5594 | start + root->sectorsize - 1, GFP_NOFS); |
| 5595 | return 0; |
| 5596 | } |
| 5597 | |
| 5598 | /* |
| 5599 | * We don't allocate a new extent in the following cases |
| 5600 | * |
| 5601 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 5602 | * existing extent. |
| 5603 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 5604 | * just use the extent. |
| 5605 | * |
| 5606 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5607 | if (!create) { |
| 5608 | len = em->len - (start - em->start); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5609 | goto map; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5610 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5611 | |
| 5612 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 5613 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 5614 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5615 | int type; |
| 5616 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5617 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5618 | |
| 5619 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5620 | type = BTRFS_ORDERED_PREALLOC; |
| 5621 | else |
| 5622 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5623 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5624 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5625 | |
| 5626 | /* |
| 5627 | * we're not going to log anything, but we do need |
| 5628 | * to make sure the current transaction stays open |
| 5629 | * while we look for nocow cross refs |
| 5630 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5631 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5632 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5633 | goto must_cow; |
| 5634 | |
| 5635 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
| 5636 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 5637 | block_start, len, len, type); |
| 5638 | btrfs_end_transaction(trans, root); |
| 5639 | if (ret) { |
| 5640 | free_extent_map(em); |
| 5641 | return ret; |
| 5642 | } |
| 5643 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5644 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5645 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5646 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5647 | must_cow: |
| 5648 | /* |
| 5649 | * this will cow the extent, reset the len in case we changed |
| 5650 | * it above |
| 5651 | */ |
| 5652 | len = bh_result->b_size; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5653 | em = btrfs_new_extent_direct(inode, em, start, len); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5654 | if (IS_ERR(em)) |
| 5655 | return PTR_ERR(em); |
| 5656 | len = min(len, em->len - (start - em->start)); |
| 5657 | unlock: |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5658 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
| 5659 | EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1, |
| 5660 | 0, NULL, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5661 | map: |
| 5662 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 5663 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5664 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5665 | bh_result->b_bdev = em->bdev; |
| 5666 | set_buffer_mapped(bh_result); |
| 5667 | if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5668 | set_buffer_new(bh_result); |
| 5669 | |
| 5670 | free_extent_map(em); |
| 5671 | |
| 5672 | return 0; |
| 5673 | } |
| 5674 | |
| 5675 | struct btrfs_dio_private { |
| 5676 | struct inode *inode; |
| 5677 | u64 logical_offset; |
| 5678 | u64 disk_bytenr; |
| 5679 | u64 bytes; |
| 5680 | u32 *csums; |
| 5681 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5682 | |
| 5683 | /* number of bios pending for this dio */ |
| 5684 | atomic_t pending_bios; |
| 5685 | |
| 5686 | /* IO errors */ |
| 5687 | int errors; |
| 5688 | |
| 5689 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5690 | }; |
| 5691 | |
| 5692 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 5693 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5694 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5695 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 5696 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5697 | struct inode *inode = dip->inode; |
| 5698 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5699 | u64 start; |
| 5700 | u32 *private = dip->csums; |
| 5701 | |
| 5702 | start = dip->logical_offset; |
| 5703 | do { |
| 5704 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 5705 | struct page *page = bvec->bv_page; |
| 5706 | char *kaddr; |
| 5707 | u32 csum = ~(u32)0; |
| 5708 | unsigned long flags; |
| 5709 | |
| 5710 | local_irq_save(flags); |
| 5711 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 5712 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 5713 | csum, bvec->bv_len); |
| 5714 | btrfs_csum_final(csum, (char *)&csum); |
| 5715 | kunmap_atomic(kaddr, KM_IRQ0); |
| 5716 | local_irq_restore(flags); |
| 5717 | |
| 5718 | flush_dcache_page(bvec->bv_page); |
| 5719 | if (csum != *private) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5720 | printk(KERN_ERR "btrfs csum failed ino %llu off" |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5721 | " %llu csum %u private %u\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5722 | (unsigned long long)btrfs_ino(inode), |
| 5723 | (unsigned long long)start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5724 | csum, *private); |
| 5725 | err = -EIO; |
| 5726 | } |
| 5727 | } |
| 5728 | |
| 5729 | start += bvec->bv_len; |
| 5730 | private++; |
| 5731 | bvec++; |
| 5732 | } while (bvec <= bvec_end); |
| 5733 | |
| 5734 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
| 5735 | dip->logical_offset + dip->bytes - 1, GFP_NOFS); |
| 5736 | bio->bi_private = dip->private; |
| 5737 | |
| 5738 | kfree(dip->csums); |
| 5739 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 5740 | |
| 5741 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 5742 | if (err) |
| 5743 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5744 | dio_end_io(bio, err); |
| 5745 | } |
| 5746 | |
| 5747 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 5748 | { |
| 5749 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5750 | struct inode *inode = dip->inode; |
| 5751 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5752 | struct btrfs_trans_handle *trans; |
| 5753 | struct btrfs_ordered_extent *ordered = NULL; |
| 5754 | struct extent_state *cached_state = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5755 | u64 ordered_offset = dip->logical_offset; |
| 5756 | u64 ordered_bytes = dip->bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5757 | int ret; |
| 5758 | |
| 5759 | if (err) |
| 5760 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5761 | again: |
| 5762 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 5763 | &ordered_offset, |
| 5764 | ordered_bytes); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5765 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5766 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5767 | |
| 5768 | BUG_ON(!ordered); |
| 5769 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5770 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5771 | if (IS_ERR(trans)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5772 | err = -ENOMEM; |
| 5773 | goto out; |
| 5774 | } |
| 5775 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5776 | |
| 5777 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) { |
| 5778 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5779 | if (!ret) |
Li Zefan | f0dd959 | 2011-09-08 10:26:51 +0800 | [diff] [blame] | 5780 | err = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5781 | goto out; |
| 5782 | } |
| 5783 | |
| 5784 | lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5785 | ordered->file_offset + ordered->len - 1, 0, |
| 5786 | &cached_state, GFP_NOFS); |
| 5787 | |
| 5788 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) { |
| 5789 | ret = btrfs_mark_extent_written(trans, inode, |
| 5790 | ordered->file_offset, |
| 5791 | ordered->file_offset + |
| 5792 | ordered->len); |
| 5793 | if (ret) { |
| 5794 | err = ret; |
| 5795 | goto out_unlock; |
| 5796 | } |
| 5797 | } else { |
| 5798 | ret = insert_reserved_file_extent(trans, inode, |
| 5799 | ordered->file_offset, |
| 5800 | ordered->start, |
| 5801 | ordered->disk_len, |
| 5802 | ordered->len, |
| 5803 | ordered->len, |
| 5804 | 0, 0, 0, |
| 5805 | BTRFS_FILE_EXTENT_REG); |
| 5806 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 5807 | ordered->file_offset, ordered->len); |
| 5808 | if (ret) { |
| 5809 | err = ret; |
| 5810 | WARN_ON(1); |
| 5811 | goto out_unlock; |
| 5812 | } |
| 5813 | } |
| 5814 | |
| 5815 | add_pending_csums(trans, inode, ordered->file_offset, &ordered->list); |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 5816 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
Miao Xie | a39f752 | 2011-09-11 10:52:25 -0400 | [diff] [blame] | 5817 | if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 5818 | btrfs_update_inode(trans, root, inode); |
| 5819 | ret = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5820 | out_unlock: |
| 5821 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5822 | ordered->file_offset + ordered->len - 1, |
| 5823 | &cached_state, GFP_NOFS); |
| 5824 | out: |
| 5825 | btrfs_delalloc_release_metadata(inode, ordered->len); |
| 5826 | btrfs_end_transaction(trans, root); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5827 | ordered_offset = ordered->file_offset + ordered->len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5828 | btrfs_put_ordered_extent(ordered); |
| 5829 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5830 | |
| 5831 | out_test: |
| 5832 | /* |
| 5833 | * our bio might span multiple ordered extents. If we haven't |
| 5834 | * completed the accounting for the whole dio, go back and try again |
| 5835 | */ |
| 5836 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 5837 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 5838 | ordered_offset; |
| 5839 | goto again; |
| 5840 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5841 | out_done: |
| 5842 | bio->bi_private = dip->private; |
| 5843 | |
| 5844 | kfree(dip->csums); |
| 5845 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 5846 | |
| 5847 | /* If we had an error make sure to clear the uptodate flag */ |
| 5848 | if (err) |
| 5849 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5850 | dio_end_io(bio, err); |
| 5851 | } |
| 5852 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 5853 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 5854 | struct bio *bio, int mirror_num, |
| 5855 | unsigned long bio_flags, u64 offset) |
| 5856 | { |
| 5857 | int ret; |
| 5858 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5859 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
| 5860 | BUG_ON(ret); |
| 5861 | return 0; |
| 5862 | } |
| 5863 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5864 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 5865 | { |
| 5866 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5867 | |
| 5868 | if (err) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5869 | printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 5870 | "sector %#Lx len %u err no %d\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5871 | (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw, |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 5872 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5873 | dip->errors = 1; |
| 5874 | |
| 5875 | /* |
| 5876 | * before atomic variable goto zero, we must make sure |
| 5877 | * dip->errors is perceived to be set. |
| 5878 | */ |
| 5879 | smp_mb__before_atomic_dec(); |
| 5880 | } |
| 5881 | |
| 5882 | /* if there are more bios still pending for this dio, just exit */ |
| 5883 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 5884 | goto out; |
| 5885 | |
| 5886 | if (dip->errors) |
| 5887 | bio_io_error(dip->orig_bio); |
| 5888 | else { |
| 5889 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 5890 | bio_endio(dip->orig_bio, 0); |
| 5891 | } |
| 5892 | out: |
| 5893 | bio_put(bio); |
| 5894 | } |
| 5895 | |
| 5896 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 5897 | u64 first_sector, gfp_t gfp_flags) |
| 5898 | { |
| 5899 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 5900 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 5901 | } |
| 5902 | |
| 5903 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 5904 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5905 | u32 *csums, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5906 | { |
| 5907 | int write = rw & REQ_WRITE; |
| 5908 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5909 | int ret; |
| 5910 | |
| 5911 | bio_get(bio); |
| 5912 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 5913 | if (ret) |
| 5914 | goto err; |
| 5915 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5916 | if (skip_sum) |
| 5917 | goto map; |
| 5918 | |
| 5919 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5920 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 5921 | inode, rw, bio, 0, 0, |
| 5922 | file_offset, |
| 5923 | __btrfs_submit_bio_start_direct_io, |
| 5924 | __btrfs_submit_bio_done); |
| 5925 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5926 | } else if (write) { |
| 5927 | /* |
| 5928 | * If we aren't doing async submit, calculate the csum of the |
| 5929 | * bio now. |
| 5930 | */ |
| 5931 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 5932 | if (ret) |
| 5933 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 5934 | } else if (!skip_sum) { |
| 5935 | ret = btrfs_lookup_bio_sums_dio(root, inode, bio, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5936 | file_offset, csums); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 5937 | if (ret) |
| 5938 | goto err; |
| 5939 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5940 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5941 | map: |
| 5942 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5943 | err: |
| 5944 | bio_put(bio); |
| 5945 | return ret; |
| 5946 | } |
| 5947 | |
| 5948 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 5949 | int skip_sum) |
| 5950 | { |
| 5951 | struct inode *inode = dip->inode; |
| 5952 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5953 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5954 | struct bio *bio; |
| 5955 | struct bio *orig_bio = dip->orig_bio; |
| 5956 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 5957 | u64 start_sector = orig_bio->bi_sector; |
| 5958 | u64 file_offset = dip->logical_offset; |
| 5959 | u64 submit_len = 0; |
| 5960 | u64 map_length; |
| 5961 | int nr_pages = 0; |
| 5962 | u32 *csums = dip->csums; |
| 5963 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5964 | int async_submit = 0; |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 5965 | int write = rw & REQ_WRITE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5966 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5967 | map_length = orig_bio->bi_size; |
| 5968 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 5969 | &map_length, NULL, 0); |
| 5970 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 5971 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5972 | return -EIO; |
| 5973 | } |
| 5974 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 5975 | if (map_length >= orig_bio->bi_size) { |
| 5976 | bio = orig_bio; |
| 5977 | goto submit; |
| 5978 | } |
| 5979 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 5980 | async_submit = 1; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 5981 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 5982 | if (!bio) |
| 5983 | return -ENOMEM; |
| 5984 | bio->bi_private = dip; |
| 5985 | bio->bi_end_io = btrfs_end_dio_bio; |
| 5986 | atomic_inc(&dip->pending_bios); |
| 5987 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5988 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 5989 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 5990 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5991 | bvec->bv_offset) < bvec->bv_len)) { |
| 5992 | /* |
| 5993 | * inc the count before we submit the bio so |
| 5994 | * we know the end IO handler won't happen before |
| 5995 | * we inc the count. Otherwise, the dip might get freed |
| 5996 | * before we're done setting it up |
| 5997 | */ |
| 5998 | atomic_inc(&dip->pending_bios); |
| 5999 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 6000 | file_offset, skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6001 | csums, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6002 | if (ret) { |
| 6003 | bio_put(bio); |
| 6004 | atomic_dec(&dip->pending_bios); |
| 6005 | goto out_err; |
| 6006 | } |
| 6007 | |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 6008 | /* Write's use the ordered csums */ |
| 6009 | if (!write && !skip_sum) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6010 | csums = csums + nr_pages; |
| 6011 | start_sector += submit_len >> 9; |
| 6012 | file_offset += submit_len; |
| 6013 | |
| 6014 | submit_len = 0; |
| 6015 | nr_pages = 0; |
| 6016 | |
| 6017 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 6018 | start_sector, GFP_NOFS); |
| 6019 | if (!bio) |
| 6020 | goto out_err; |
| 6021 | bio->bi_private = dip; |
| 6022 | bio->bi_end_io = btrfs_end_dio_bio; |
| 6023 | |
| 6024 | map_length = orig_bio->bi_size; |
| 6025 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 6026 | &map_length, NULL, 0); |
| 6027 | if (ret) { |
| 6028 | bio_put(bio); |
| 6029 | goto out_err; |
| 6030 | } |
| 6031 | } else { |
| 6032 | submit_len += bvec->bv_len; |
| 6033 | nr_pages ++; |
| 6034 | bvec++; |
| 6035 | } |
| 6036 | } |
| 6037 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6038 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6039 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6040 | csums, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6041 | if (!ret) |
| 6042 | return 0; |
| 6043 | |
| 6044 | bio_put(bio); |
| 6045 | out_err: |
| 6046 | dip->errors = 1; |
| 6047 | /* |
| 6048 | * before atomic variable goto zero, we must |
| 6049 | * make sure dip->errors is perceived to be set. |
| 6050 | */ |
| 6051 | smp_mb__before_atomic_dec(); |
| 6052 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 6053 | bio_io_error(dip->orig_bio); |
| 6054 | |
| 6055 | /* bio_end_io() will handle error, so we needn't return it */ |
| 6056 | return 0; |
| 6057 | } |
| 6058 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6059 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 6060 | loff_t file_offset) |
| 6061 | { |
| 6062 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6063 | struct btrfs_dio_private *dip; |
| 6064 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6065 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 6066 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6067 | int ret = 0; |
| 6068 | |
| 6069 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 6070 | |
| 6071 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 6072 | if (!dip) { |
| 6073 | ret = -ENOMEM; |
| 6074 | goto free_ordered; |
| 6075 | } |
| 6076 | dip->csums = NULL; |
| 6077 | |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 6078 | /* Write's use the ordered csum stuff, so we don't need dip->csums */ |
| 6079 | if (!write && !skip_sum) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6080 | dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); |
| 6081 | if (!dip->csums) { |
Daniel J Blueman | b4966b7 | 2011-03-09 16:46:42 +0000 | [diff] [blame] | 6082 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6083 | ret = -ENOMEM; |
| 6084 | goto free_ordered; |
| 6085 | } |
| 6086 | } |
| 6087 | |
| 6088 | dip->private = bio->bi_private; |
| 6089 | dip->inode = inode; |
| 6090 | dip->logical_offset = file_offset; |
| 6091 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6092 | dip->bytes = 0; |
| 6093 | do { |
| 6094 | dip->bytes += bvec->bv_len; |
| 6095 | bvec++; |
| 6096 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 6097 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6098 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6099 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6100 | dip->errors = 0; |
| 6101 | dip->orig_bio = bio; |
| 6102 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6103 | |
| 6104 | if (write) |
| 6105 | bio->bi_end_io = btrfs_endio_direct_write; |
| 6106 | else |
| 6107 | bio->bi_end_io = btrfs_endio_direct_read; |
| 6108 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6109 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 6110 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6111 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6112 | free_ordered: |
| 6113 | /* |
| 6114 | * If this is a write, we need to clean up the reserved space and kill |
| 6115 | * the ordered extent. |
| 6116 | */ |
| 6117 | if (write) { |
| 6118 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 6119 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6120 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 6121 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 6122 | btrfs_free_reserved_extent(root, ordered->start, |
| 6123 | ordered->disk_len); |
| 6124 | btrfs_put_ordered_extent(ordered); |
| 6125 | btrfs_put_ordered_extent(ordered); |
| 6126 | } |
| 6127 | bio_endio(bio, ret); |
| 6128 | } |
| 6129 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6130 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 6131 | const struct iovec *iov, loff_t offset, |
| 6132 | unsigned long nr_segs) |
| 6133 | { |
| 6134 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6135 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6136 | size_t size; |
| 6137 | unsigned long addr; |
| 6138 | unsigned blocksize_mask = root->sectorsize - 1; |
| 6139 | ssize_t retval = -EINVAL; |
| 6140 | loff_t end = offset; |
| 6141 | |
| 6142 | if (offset & blocksize_mask) |
| 6143 | goto out; |
| 6144 | |
| 6145 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 6146 | for (seg = 0; seg < nr_segs; seg++) { |
| 6147 | addr = (unsigned long)iov[seg].iov_base; |
| 6148 | size = iov[seg].iov_len; |
| 6149 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6150 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6151 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6152 | |
| 6153 | /* If this is a write we don't need to check anymore */ |
| 6154 | if (rw & WRITE) |
| 6155 | continue; |
| 6156 | |
| 6157 | /* |
| 6158 | * Check to make sure we don't have duplicate iov_base's in this |
| 6159 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 6160 | * when reading back. |
| 6161 | */ |
| 6162 | for (i = seg + 1; i < nr_segs; i++) { |
| 6163 | if (iov[seg].iov_base == iov[i].iov_base) |
| 6164 | goto out; |
| 6165 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6166 | } |
| 6167 | retval = 0; |
| 6168 | out: |
| 6169 | return retval; |
| 6170 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6171 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 6172 | const struct iovec *iov, loff_t offset, |
| 6173 | unsigned long nr_segs) |
| 6174 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6175 | struct file *file = iocb->ki_filp; |
| 6176 | struct inode *inode = file->f_mapping->host; |
| 6177 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6178 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6179 | u64 lockstart, lockend; |
| 6180 | ssize_t ret; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6181 | int writing = rw & WRITE; |
| 6182 | int write_bits = 0; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6183 | size_t count = iov_length(iov, nr_segs); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6184 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6185 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
| 6186 | offset, nr_segs)) { |
| 6187 | return 0; |
| 6188 | } |
| 6189 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6190 | lockstart = offset; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6191 | lockend = offset + count - 1; |
| 6192 | |
| 6193 | if (writing) { |
| 6194 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 6195 | if (ret) |
| 6196 | goto out; |
| 6197 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6198 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6199 | while (1) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6200 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6201 | 0, &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6202 | /* |
| 6203 | * We're concerned with the entire range that we're going to be |
| 6204 | * doing DIO to, so we need to make sure theres no ordered |
| 6205 | * extents in this range. |
| 6206 | */ |
| 6207 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6208 | lockend - lockstart + 1); |
| 6209 | if (!ordered) |
| 6210 | break; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6211 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6212 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6213 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6214 | btrfs_put_ordered_extent(ordered); |
| 6215 | cond_resched(); |
| 6216 | } |
| 6217 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6218 | /* |
| 6219 | * we don't use btrfs_set_extent_delalloc because we don't want |
| 6220 | * the dirty or uptodate bits |
| 6221 | */ |
| 6222 | if (writing) { |
| 6223 | write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING; |
| 6224 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6225 | EXTENT_DELALLOC, 0, NULL, &cached_state, |
| 6226 | GFP_NOFS); |
| 6227 | if (ret) { |
| 6228 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6229 | lockend, EXTENT_LOCKED | write_bits, |
| 6230 | 1, 0, &cached_state, GFP_NOFS); |
| 6231 | goto out; |
| 6232 | } |
| 6233 | } |
| 6234 | |
| 6235 | free_extent_state(cached_state); |
| 6236 | cached_state = NULL; |
| 6237 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6238 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 6239 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 6240 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
| 6241 | btrfs_submit_direct, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6242 | |
| 6243 | if (ret < 0 && ret != -EIOCBQUEUED) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6244 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset, |
| 6245 | offset + iov_length(iov, nr_segs) - 1, |
| 6246 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6247 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6248 | } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) { |
| 6249 | /* |
| 6250 | * We're falling back to buffered, unlock the section we didn't |
| 6251 | * do IO on. |
| 6252 | */ |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6253 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret, |
| 6254 | offset + iov_length(iov, nr_segs) - 1, |
| 6255 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6256 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6257 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6258 | out: |
| 6259 | free_extent_state(cached_state); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6260 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6261 | } |
| 6262 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6263 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 6264 | __u64 start, __u64 len) |
| 6265 | { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6266 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6267 | } |
| 6268 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6269 | int btrfs_readpage(struct file *file, struct page *page) |
| 6270 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6271 | struct extent_io_tree *tree; |
| 6272 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6273 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6274 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6275 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6276 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 6277 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6278 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6279 | |
| 6280 | |
| 6281 | if (current->flags & PF_MEMALLOC) { |
| 6282 | redirty_page_for_writepage(wbc, page); |
| 6283 | unlock_page(page); |
| 6284 | return 0; |
| 6285 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6286 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6287 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 6288 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6289 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6290 | int btrfs_writepages(struct address_space *mapping, |
| 6291 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6292 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6293 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6294 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6295 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6296 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 6297 | } |
| 6298 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6299 | static int |
| 6300 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 6301 | struct list_head *pages, unsigned nr_pages) |
| 6302 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6303 | struct extent_io_tree *tree; |
| 6304 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6305 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 6306 | btrfs_get_extent); |
| 6307 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6308 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6309 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6310 | struct extent_io_tree *tree; |
| 6311 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6312 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6313 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6314 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 6315 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6316 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6317 | if (ret == 1) { |
| 6318 | ClearPagePrivate(page); |
| 6319 | set_page_private(page, 0); |
| 6320 | page_cache_release(page); |
| 6321 | } |
| 6322 | return ret; |
| 6323 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6324 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6325 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 6326 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 6327 | if (PageWriteback(page) || PageDirty(page)) |
| 6328 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 6329 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6330 | } |
| 6331 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6332 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 6333 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6334 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6335 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6336 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6337 | u64 page_start = page_offset(page); |
| 6338 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6339 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6340 | |
| 6341 | /* |
| 6342 | * we have the page locked, so new writeback can't start, |
| 6343 | * and the dirty bit won't be cleared while we are here. |
| 6344 | * |
| 6345 | * Wait for IO on this page so that we can safely clear |
| 6346 | * the PagePrivate2 bit and do ordered accounting |
| 6347 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6348 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6349 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6350 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6351 | if (offset) { |
| 6352 | btrfs_releasepage(page, GFP_NOFS); |
| 6353 | return; |
| 6354 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6355 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6356 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6357 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 6358 | page_offset(page)); |
| 6359 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6360 | /* |
| 6361 | * IO on this page will never be started, so we need |
| 6362 | * to account for any ordered extents now |
| 6363 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6364 | clear_extent_bit(tree, page_start, page_end, |
| 6365 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6366 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6367 | &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6368 | /* |
| 6369 | * whoever cleared the private bit is responsible |
| 6370 | * for the finish_ordered_io |
| 6371 | */ |
| 6372 | if (TestClearPagePrivate2(page)) { |
| 6373 | btrfs_finish_ordered_io(page->mapping->host, |
| 6374 | page_start, page_end); |
| 6375 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6376 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6377 | cached_state = NULL; |
| 6378 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state, |
| 6379 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6380 | } |
| 6381 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6382 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6383 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6384 | __btrfs_releasepage(page, GFP_NOFS); |
| 6385 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6386 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6387 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6388 | ClearPagePrivate(page); |
| 6389 | set_page_private(page, 0); |
| 6390 | page_cache_release(page); |
| 6391 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6392 | } |
| 6393 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6394 | /* |
| 6395 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 6396 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 6397 | * be careful to check for EOF conditions here. We set the page up correctly |
| 6398 | * for a written page which means we get ENOSPC checking when writing into |
| 6399 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 6400 | * support these features. |
| 6401 | * |
| 6402 | * We are not allowed to take the i_mutex here so we have to play games to |
| 6403 | * protect against truncate races as the page could now be beyond EOF. Because |
| 6404 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 6405 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 6406 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 6407 | * unlock the page. |
| 6408 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6409 | 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] | 6410 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6411 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 6412 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6413 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6414 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 6415 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6416 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6417 | char *kaddr; |
| 6418 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6419 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6420 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6421 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6422 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6423 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6424 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6425 | if (ret) { |
| 6426 | if (ret == -ENOMEM) |
| 6427 | ret = VM_FAULT_OOM; |
| 6428 | else /* -ENOSPC, -EIO, etc */ |
| 6429 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6430 | goto out; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6431 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6432 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6433 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6434 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6435 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6436 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6437 | page_start = page_offset(page); |
| 6438 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6439 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6440 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6441 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6442 | /* page got truncated out from underneath us */ |
| 6443 | goto out_unlock; |
| 6444 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6445 | wait_on_page_writeback(page); |
| 6446 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6447 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, |
| 6448 | GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6449 | set_page_extent_mapped(page); |
| 6450 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6451 | /* |
| 6452 | * we can't set the delalloc bits if there are pending ordered |
| 6453 | * extents. Drop our locks and wait for them to finish |
| 6454 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6455 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6456 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6457 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6458 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6459 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6460 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6461 | btrfs_put_ordered_extent(ordered); |
| 6462 | goto again; |
| 6463 | } |
| 6464 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6465 | /* |
| 6466 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 6467 | * if it was already dirty, so for space accounting reasons we need to |
| 6468 | * clear any delalloc bits for the range we are fixing to save. There |
| 6469 | * is probably a better way to do this, but for now keep consistent with |
| 6470 | * prepare_pages in the normal write path. |
| 6471 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6472 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6473 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6474 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6475 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6476 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 6477 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6478 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6479 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6480 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6481 | ret = VM_FAULT_SIGBUS; |
| 6482 | goto out_unlock; |
| 6483 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6484 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6485 | |
| 6486 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6487 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6488 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6489 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6490 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6491 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6492 | if (zero_start != PAGE_CACHE_SIZE) { |
| 6493 | kaddr = kmap(page); |
| 6494 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 6495 | flush_dcache_page(page); |
| 6496 | kunmap(page); |
| 6497 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6498 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6499 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6500 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6501 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6502 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 6503 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 6504 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6505 | 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] | 6506 | |
| 6507 | out_unlock: |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6508 | if (!ret) |
| 6509 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6510 | unlock_page(page); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6511 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6512 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6513 | return ret; |
| 6514 | } |
| 6515 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6516 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6517 | { |
| 6518 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6519 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6520 | int ret; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6521 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6522 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6523 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6524 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 6525 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6526 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 6527 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 6528 | if (ret) |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6529 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6530 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6531 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6532 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6533 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6534 | /* |
| 6535 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 6536 | * 3 things going on here |
| 6537 | * |
| 6538 | * 1) We need to reserve space for our orphan item and the space to |
| 6539 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 6540 | * orphan item because we didn't reserve space to remove it. |
| 6541 | * |
| 6542 | * 2) We need to reserve space to update our inode. |
| 6543 | * |
| 6544 | * 3) We need to have something to cache all the space that is going to |
| 6545 | * be free'd up by the truncate operation, but also have some slack |
| 6546 | * space reserved in case it uses space during the truncate (thank you |
| 6547 | * very much snapshotting). |
| 6548 | * |
| 6549 | * And we need these to all be seperate. The fact is we can use alot of |
| 6550 | * space doing the truncate, and we have no earthly idea how much space |
| 6551 | * we will use, so we need the truncate reservation to be seperate so it |
| 6552 | * doesn't end up using space reserved for updating the inode or |
| 6553 | * removing the orphan item. We also need to be able to stop the |
| 6554 | * transaction and start a new one, which means we need to be able to |
| 6555 | * update the inode several times, and we have no idea of knowing how |
| 6556 | * many times that will be, so we can't just reserve 1 item for the |
| 6557 | * entirety of the opration, so that has to be done seperately as well. |
| 6558 | * Then there is the orphan item, which does indeed need to be held on |
| 6559 | * to for the whole operation, and we need nobody to touch this reserved |
| 6560 | * space except the orphan code. |
| 6561 | * |
| 6562 | * So that leaves us with |
| 6563 | * |
| 6564 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 6565 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 6566 | * transaction reservation. |
| 6567 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 6568 | * updating the inode. |
| 6569 | */ |
| 6570 | rsv = btrfs_alloc_block_rsv(root); |
| 6571 | if (!rsv) |
| 6572 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 6573 | rsv->size = min_size; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6574 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6575 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 6576 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6577 | * 1 for the orphan item we're going to add |
| 6578 | * 1 for the orphan item deletion |
| 6579 | * 1 for updating the inode. |
| 6580 | */ |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 6581 | trans = btrfs_start_transaction(root, 4); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6582 | if (IS_ERR(trans)) { |
| 6583 | err = PTR_ERR(trans); |
| 6584 | goto out; |
| 6585 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6586 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6587 | /* Migrate the slack space for the truncate to our reserve */ |
| 6588 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 6589 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6590 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6591 | |
| 6592 | ret = btrfs_orphan_add(trans, inode); |
| 6593 | if (ret) { |
| 6594 | btrfs_end_transaction(trans, root); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6595 | goto out; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6596 | } |
| 6597 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6598 | /* |
| 6599 | * setattr is responsible for setting the ordered_data_close flag, |
| 6600 | * but that is only tested during the last file release. That |
| 6601 | * could happen well after the next commit, leaving a great big |
| 6602 | * window where new writes may get lost if someone chooses to write |
| 6603 | * to this file after truncating to zero |
| 6604 | * |
| 6605 | * The inode doesn't have any dirty data here, and so if we commit |
| 6606 | * this is a noop. If someone immediately starts writing to the inode |
| 6607 | * it is very likely we'll catch some of their writes in this |
| 6608 | * transaction, and the commit will find this file on the ordered |
| 6609 | * data list with good things to send down. |
| 6610 | * |
| 6611 | * This is a best effort solution, there is still a window where |
| 6612 | * using truncate to replace the contents of the file will |
| 6613 | * end up with a zero length file after a crash. |
| 6614 | */ |
| 6615 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 6616 | btrfs_add_ordered_operation(trans, root, inode); |
| 6617 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6618 | while (1) { |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 6619 | ret = btrfs_block_rsv_refill(root, rsv, min_size); |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6620 | if (ret) { |
| 6621 | /* |
| 6622 | * This can only happen with the original transaction we |
| 6623 | * started above, every other time we shouldn't have a |
| 6624 | * transaction started yet. |
| 6625 | */ |
| 6626 | if (ret == -EAGAIN) |
| 6627 | goto end_trans; |
| 6628 | err = ret; |
| 6629 | break; |
| 6630 | } |
| 6631 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6632 | if (!trans) { |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6633 | /* Just need the 1 for updating the inode */ |
| 6634 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6635 | if (IS_ERR(trans)) { |
| 6636 | err = PTR_ERR(trans); |
| 6637 | goto out; |
| 6638 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6639 | } |
| 6640 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6641 | trans->block_rsv = rsv; |
| 6642 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6643 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 6644 | inode->i_size, |
| 6645 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6646 | if (ret != -EAGAIN) { |
| 6647 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6648 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6649 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6650 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6651 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6652 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6653 | if (ret) { |
| 6654 | err = ret; |
| 6655 | break; |
| 6656 | } |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6657 | end_trans: |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6658 | nr = trans->blocks_used; |
| 6659 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6660 | trans = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6661 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
| 6664 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6665 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6666 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6667 | if (ret) |
| 6668 | err = ret; |
Josef Bacik | ded5db9 | 2011-03-04 14:09:46 -0500 | [diff] [blame] | 6669 | } else if (ret && inode->i_nlink > 0) { |
| 6670 | /* |
| 6671 | * Failed to do the truncate, remove us from the in memory |
| 6672 | * orphan list. |
| 6673 | */ |
| 6674 | ret = btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6675 | } |
| 6676 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6677 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6678 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6679 | if (ret && !err) |
| 6680 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6681 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6682 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6683 | ret = btrfs_end_transaction_throttle(trans, root); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6684 | btrfs_btree_balance_dirty(root, nr); |
| 6685 | |
| 6686 | out: |
| 6687 | btrfs_free_block_rsv(root, rsv); |
| 6688 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6689 | if (ret && !err) |
| 6690 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6691 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6692 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6693 | } |
| 6694 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6695 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6696 | * create a new subvolume directory/inode (helper for the ioctl). |
| 6697 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6698 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6699 | struct btrfs_root *new_root, u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6700 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6701 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6702 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6703 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6704 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6705 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6706 | new_dirid, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6707 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 6708 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6709 | inode->i_op = &btrfs_dir_inode_operations; |
| 6710 | inode->i_fop = &btrfs_dir_file_operations; |
| 6711 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6712 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6713 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6714 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6715 | err = btrfs_update_inode(trans, new_root, inode); |
| 6716 | BUG_ON(err); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6717 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6718 | iput(inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6719 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6720 | } |
| 6721 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6722 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 6723 | { |
| 6724 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6725 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6726 | |
| 6727 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 6728 | if (!ei) |
| 6729 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6730 | |
| 6731 | ei->root = NULL; |
| 6732 | ei->space_info = NULL; |
| 6733 | ei->generation = 0; |
| 6734 | ei->sequence = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 6735 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6736 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6737 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6738 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6739 | ei->disk_i_size = 0; |
| 6740 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6741 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6742 | ei->index_cnt = (u64)-1; |
| 6743 | ei->last_unlink_trans = 0; |
| 6744 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 6745 | spin_lock_init(&ei->lock); |
| 6746 | ei->outstanding_extents = 0; |
| 6747 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6748 | |
| 6749 | ei->ordered_data_close = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6750 | ei->orphan_meta_reserved = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6751 | ei->dummy_inode = 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 6752 | ei->in_defrag = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6753 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6754 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6755 | ei->delayed_node = NULL; |
| 6756 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6757 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 6758 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 6759 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 6760 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6761 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6762 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6763 | INIT_LIST_HEAD(&ei->i_orphan); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6764 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6765 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6766 | RB_CLEAR_NODE(&ei->rb_node); |
| 6767 | |
| 6768 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6769 | } |
| 6770 | |
Nick Piggin | fa0d7e3 | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6771 | static void btrfs_i_callback(struct rcu_head *head) |
| 6772 | { |
| 6773 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 6774 | INIT_LIST_HEAD(&inode->i_dentry); |
| 6775 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 6776 | } |
| 6777 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6778 | void btrfs_destroy_inode(struct inode *inode) |
| 6779 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6780 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6781 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6782 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6783 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 6784 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 6785 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 6786 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6787 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 6788 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6789 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6790 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6791 | * This can happen where we create an inode, but somebody else also |
| 6792 | * created the same inode and we need to destroy the one we already |
| 6793 | * created. |
| 6794 | */ |
| 6795 | if (!root) |
| 6796 | goto free; |
| 6797 | |
| 6798 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6799 | * Make sure we're properly removed from the ordered operation |
| 6800 | * lists. |
| 6801 | */ |
| 6802 | smp_mb(); |
| 6803 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 6804 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 6805 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 6806 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 6807 | } |
| 6808 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6809 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6810 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6811 | printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n", |
| 6812 | (unsigned long long)btrfs_ino(inode)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6813 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6814 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6815 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6816 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6817 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6818 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 6819 | if (!ordered) |
| 6820 | break; |
| 6821 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6822 | printk(KERN_ERR "btrfs found ordered " |
| 6823 | "extent %llu %llu on inode cleanup\n", |
| 6824 | (unsigned long long)ordered->file_offset, |
| 6825 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6826 | btrfs_remove_ordered_extent(inode, ordered); |
| 6827 | btrfs_put_ordered_extent(ordered); |
| 6828 | btrfs_put_ordered_extent(ordered); |
| 6829 | } |
| 6830 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6831 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 6832 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6833 | free: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6834 | btrfs_remove_delayed_node(inode); |
Nick Piggin | fa0d7e3 | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6835 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6836 | } |
| 6837 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6838 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6839 | { |
| 6840 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6841 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6842 | if (btrfs_root_refs(&root->root_item) == 0 && |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 6843 | !btrfs_is_free_space_inode(root, inode)) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6844 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6845 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 6846 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6847 | } |
| 6848 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 6849 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6850 | { |
| 6851 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 6852 | |
| 6853 | inode_init_once(&ei->vfs_inode); |
| 6854 | } |
| 6855 | |
| 6856 | void btrfs_destroy_cachep(void) |
| 6857 | { |
| 6858 | if (btrfs_inode_cachep) |
| 6859 | kmem_cache_destroy(btrfs_inode_cachep); |
| 6860 | if (btrfs_trans_handle_cachep) |
| 6861 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 6862 | if (btrfs_transaction_cachep) |
| 6863 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6864 | if (btrfs_path_cachep) |
| 6865 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 6866 | if (btrfs_free_space_cachep) |
| 6867 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6868 | } |
| 6869 | |
| 6870 | int btrfs_init_cachep(void) |
| 6871 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6872 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 6873 | sizeof(struct btrfs_inode), 0, |
| 6874 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6875 | if (!btrfs_inode_cachep) |
| 6876 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6877 | |
| 6878 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 6879 | sizeof(struct btrfs_trans_handle), 0, |
| 6880 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6881 | if (!btrfs_trans_handle_cachep) |
| 6882 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6883 | |
| 6884 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 6885 | sizeof(struct btrfs_transaction), 0, |
| 6886 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6887 | if (!btrfs_transaction_cachep) |
| 6888 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6889 | |
| 6890 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 6891 | sizeof(struct btrfs_path), 0, |
| 6892 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6893 | if (!btrfs_path_cachep) |
| 6894 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 6895 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 6896 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache", |
| 6897 | sizeof(struct btrfs_free_space), 0, |
| 6898 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 6899 | if (!btrfs_free_space_cachep) |
| 6900 | goto fail; |
| 6901 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6902 | return 0; |
| 6903 | fail: |
| 6904 | btrfs_destroy_cachep(); |
| 6905 | return -ENOMEM; |
| 6906 | } |
| 6907 | |
| 6908 | static int btrfs_getattr(struct vfsmount *mnt, |
| 6909 | struct dentry *dentry, struct kstat *stat) |
| 6910 | { |
| 6911 | struct inode *inode = dentry->d_inode; |
| 6912 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 6913 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 6914 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6915 | stat->blocks = (inode_get_bytes(inode) + |
| 6916 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6917 | return 0; |
| 6918 | } |
| 6919 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 6920 | /* |
| 6921 | * If a file is moved, it will inherit the cow and compression flags of the new |
| 6922 | * directory. |
| 6923 | */ |
| 6924 | static void fixup_inode_flags(struct inode *dir, struct inode *inode) |
| 6925 | { |
| 6926 | struct btrfs_inode *b_dir = BTRFS_I(dir); |
| 6927 | struct btrfs_inode *b_inode = BTRFS_I(inode); |
| 6928 | |
| 6929 | if (b_dir->flags & BTRFS_INODE_NODATACOW) |
| 6930 | b_inode->flags |= BTRFS_INODE_NODATACOW; |
| 6931 | else |
| 6932 | b_inode->flags &= ~BTRFS_INODE_NODATACOW; |
| 6933 | |
| 6934 | if (b_dir->flags & BTRFS_INODE_COMPRESS) |
| 6935 | b_inode->flags |= BTRFS_INODE_COMPRESS; |
| 6936 | else |
| 6937 | b_inode->flags &= ~BTRFS_INODE_COMPRESS; |
| 6938 | } |
| 6939 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6940 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 6941 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6942 | { |
| 6943 | struct btrfs_trans_handle *trans; |
| 6944 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6945 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6946 | struct inode *new_inode = new_dentry->d_inode; |
| 6947 | struct inode *old_inode = old_dentry->d_inode; |
| 6948 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6949 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6950 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6951 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6952 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6953 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6954 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6955 | return -EPERM; |
| 6956 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6957 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6958 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 6959 | return -EXDEV; |
| 6960 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6961 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 6962 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6963 | return -ENOTEMPTY; |
| 6964 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6965 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6966 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6967 | return -ENOTEMPTY; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6968 | /* |
| 6969 | * we're using rename to replace one file with another. |
| 6970 | * and the replacement file is large. Start IO on it now so |
| 6971 | * we don't add too much work to the end of the transaction |
| 6972 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 6973 | 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] | 6974 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 6975 | filemap_flush(old_inode->i_mapping); |
| 6976 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6977 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6978 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6979 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6980 | /* |
| 6981 | * We want to reserve the absolute worst case amount of items. So if |
| 6982 | * both inodes are subvols and we need to unlink them then that would |
| 6983 | * require 4 item modifications, but if they are both normal inodes it |
| 6984 | * would require 5 item modifications, so we'll assume their normal |
| 6985 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 6986 | * should cover the worst case number of items we'll modify. |
| 6987 | */ |
| 6988 | trans = btrfs_start_transaction(root, 20); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 6989 | if (IS_ERR(trans)) { |
| 6990 | ret = PTR_ERR(trans); |
| 6991 | goto out_notrans; |
| 6992 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6993 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6994 | if (dest != root) |
| 6995 | btrfs_record_root_in_trans(trans, dest); |
| 6996 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6997 | ret = btrfs_set_inode_index(new_dir, &index); |
| 6998 | if (ret) |
| 6999 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7000 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7001 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7002 | /* force full log commit if subvolume involved. */ |
| 7003 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 7004 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7005 | ret = btrfs_insert_inode_ref(trans, dest, |
| 7006 | new_dentry->d_name.name, |
| 7007 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7008 | old_ino, |
| 7009 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7010 | if (ret) |
| 7011 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7012 | /* |
| 7013 | * this is an ugly little race, but the rename is required |
| 7014 | * to make sure that if we crash, the inode is either at the |
| 7015 | * old name or the new one. pinning the log transaction lets |
| 7016 | * us make sure we don't allow a log commit to come in after |
| 7017 | * we unlink the name but before we add the new name back in. |
| 7018 | */ |
| 7019 | btrfs_pin_log_trans(root); |
| 7020 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7021 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7022 | * make sure the inode gets flushed if it is replacing |
| 7023 | * something. |
| 7024 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7025 | if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode)) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7026 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7027 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7028 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 7029 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 7030 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7031 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7032 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 7033 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 7034 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7035 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7036 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 7037 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 7038 | old_dentry->d_name.name, |
| 7039 | old_dentry->d_name.len); |
| 7040 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 7041 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 7042 | old_dentry->d_inode, |
| 7043 | old_dentry->d_name.name, |
| 7044 | old_dentry->d_name.len); |
| 7045 | if (!ret) |
| 7046 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7047 | } |
| 7048 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7049 | |
| 7050 | if (new_inode) { |
| 7051 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7052 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7053 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 7054 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 7055 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 7056 | root_objectid, |
| 7057 | new_dentry->d_name.name, |
| 7058 | new_dentry->d_name.len); |
| 7059 | BUG_ON(new_inode->i_nlink == 0); |
| 7060 | } else { |
| 7061 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 7062 | new_dentry->d_inode, |
| 7063 | new_dentry->d_name.name, |
| 7064 | new_dentry->d_name.len); |
| 7065 | } |
| 7066 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7067 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7068 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7069 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7070 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7071 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7072 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7073 | fixup_inode_flags(new_dir, old_inode); |
| 7074 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7075 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 7076 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7077 | new_dentry->d_name.len, 0, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7078 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7079 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7080 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 7081 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 7082 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7083 | btrfs_end_log_trans(root); |
| 7084 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7085 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 7086 | btrfs_end_transaction_throttle(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 7087 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7088 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7089 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7090 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7091 | return ret; |
| 7092 | } |
| 7093 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7094 | /* |
| 7095 | * some fairly slow code that needs optimization. This walks the list |
| 7096 | * of all the inodes with pending delalloc and forces them to disk. |
| 7097 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7098 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7099 | { |
| 7100 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 7101 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7102 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7103 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7104 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 7105 | return -EROFS; |
| 7106 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7107 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7108 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7109 | binode = list_entry(head->next, struct btrfs_inode, |
| 7110 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7111 | inode = igrab(&binode->vfs_inode); |
| 7112 | if (!inode) |
| 7113 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7114 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7115 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7116 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7117 | if (delay_iput) |
| 7118 | btrfs_add_delayed_iput(inode); |
| 7119 | else |
| 7120 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7121 | } |
| 7122 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7123 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7124 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7125 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7126 | |
| 7127 | /* the filemap_flush will queue IO into the worker threads, but |
| 7128 | * we have to make sure the IO is actually started and that |
| 7129 | * ordered extents get created before we return |
| 7130 | */ |
| 7131 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7132 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7133 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7134 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7135 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 7136 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7137 | } |
| 7138 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7139 | return 0; |
| 7140 | } |
| 7141 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7142 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 7143 | const char *symname) |
| 7144 | { |
| 7145 | struct btrfs_trans_handle *trans; |
| 7146 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 7147 | struct btrfs_path *path; |
| 7148 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7149 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7150 | int err; |
| 7151 | int drop_inode = 0; |
| 7152 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7153 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7154 | int name_len; |
| 7155 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7156 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7157 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7158 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7159 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7160 | |
| 7161 | name_len = strlen(symname) + 1; |
| 7162 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 7163 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7164 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7165 | /* |
| 7166 | * 2 items for inode item and ref |
| 7167 | * 2 items for dir items |
| 7168 | * 1 item for xattr if selinux is on |
| 7169 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7170 | trans = btrfs_start_transaction(root, 5); |
| 7171 | if (IS_ERR(trans)) |
| 7172 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7173 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 7174 | err = btrfs_find_free_ino(root, &objectid); |
| 7175 | if (err) |
| 7176 | goto out_unlock; |
| 7177 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7178 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7179 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7180 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7181 | if (IS_ERR(inode)) { |
| 7182 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7183 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7184 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7185 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 7186 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7187 | if (err) { |
| 7188 | drop_inode = 1; |
| 7189 | goto out_unlock; |
| 7190 | } |
| 7191 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 7192 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7193 | if (err) |
| 7194 | drop_inode = 1; |
| 7195 | else { |
| 7196 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7197 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7198 | inode->i_fop = &btrfs_file_operations; |
| 7199 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7200 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7201 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7202 | if (drop_inode) |
| 7203 | goto out_unlock; |
| 7204 | |
| 7205 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 7206 | if (!path) { |
| 7207 | err = -ENOMEM; |
| 7208 | drop_inode = 1; |
| 7209 | goto out_unlock; |
| 7210 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7211 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7212 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7213 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 7214 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 7215 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 7216 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7217 | if (err) { |
| 7218 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 7219 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7220 | goto out_unlock; |
| 7221 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7222 | leaf = path->nodes[0]; |
| 7223 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 7224 | struct btrfs_file_extent_item); |
| 7225 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 7226 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7227 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7228 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 7229 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 7230 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 7231 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 7232 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7233 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7234 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 7235 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7236 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7237 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7238 | inode->i_op = &btrfs_symlink_inode_operations; |
| 7239 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7240 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7241 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7242 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7243 | err = btrfs_update_inode(trans, root, inode); |
| 7244 | if (err) |
| 7245 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7246 | |
| 7247 | out_unlock: |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7248 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 7249 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7250 | if (drop_inode) { |
| 7251 | inode_dec_link_count(inode); |
| 7252 | iput(inode); |
| 7253 | } |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7254 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7255 | return err; |
| 7256 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7257 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7258 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7259 | u64 start, u64 num_bytes, u64 min_size, |
| 7260 | loff_t actual_len, u64 *alloc_hint, |
| 7261 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7262 | { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7263 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7264 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7265 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7266 | u64 i_size; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7267 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7268 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7269 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7270 | if (trans) |
| 7271 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7272 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7273 | if (own_trans) { |
| 7274 | trans = btrfs_start_transaction(root, 3); |
| 7275 | if (IS_ERR(trans)) { |
| 7276 | ret = PTR_ERR(trans); |
| 7277 | break; |
| 7278 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7279 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7280 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7281 | ret = btrfs_reserve_extent(trans, root, num_bytes, min_size, |
| 7282 | 0, *alloc_hint, (u64)-1, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7283 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7284 | if (own_trans) |
| 7285 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7286 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7287 | } |
| 7288 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7289 | ret = insert_reserved_file_extent(trans, inode, |
| 7290 | cur_offset, ins.objectid, |
| 7291 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 7292 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7293 | BTRFS_FILE_EXTENT_PREALLOC); |
| 7294 | BUG_ON(ret); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 7295 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7296 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7297 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7298 | num_bytes -= ins.offset; |
| 7299 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7300 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7301 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7302 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7303 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7304 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7305 | (actual_len > inode->i_size) && |
| 7306 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7307 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7308 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7309 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7310 | i_size = cur_offset; |
| 7311 | i_size_write(inode, i_size); |
| 7312 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7313 | } |
| 7314 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7315 | ret = btrfs_update_inode(trans, root, inode); |
| 7316 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7317 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7318 | if (own_trans) |
| 7319 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7320 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7321 | return ret; |
| 7322 | } |
| 7323 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7324 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7325 | u64 start, u64 num_bytes, u64 min_size, |
| 7326 | loff_t actual_len, u64 *alloc_hint) |
| 7327 | { |
| 7328 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7329 | min_size, actual_len, alloc_hint, |
| 7330 | NULL); |
| 7331 | } |
| 7332 | |
| 7333 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 7334 | struct btrfs_trans_handle *trans, int mode, |
| 7335 | u64 start, u64 num_bytes, u64 min_size, |
| 7336 | loff_t actual_len, u64 *alloc_hint) |
| 7337 | { |
| 7338 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7339 | min_size, actual_len, alloc_hint, trans); |
| 7340 | } |
| 7341 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7342 | static int btrfs_set_page_dirty(struct page *page) |
| 7343 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7344 | return __set_page_dirty_nobuffers(page); |
| 7345 | } |
| 7346 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 7347 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7348 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7349 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 7350 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7351 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 7352 | if (mask & MAY_WRITE && |
| 7353 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 7354 | if (btrfs_root_readonly(root)) |
| 7355 | return -EROFS; |
| 7356 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 7357 | return -EACCES; |
| 7358 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 7359 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7360 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7361 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7362 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7363 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7364 | .lookup = btrfs_lookup, |
| 7365 | .create = btrfs_create, |
| 7366 | .unlink = btrfs_unlink, |
| 7367 | .link = btrfs_link, |
| 7368 | .mkdir = btrfs_mkdir, |
| 7369 | .rmdir = btrfs_rmdir, |
| 7370 | .rename = btrfs_rename, |
| 7371 | .symlink = btrfs_symlink, |
| 7372 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7373 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7374 | .setxattr = btrfs_setxattr, |
| 7375 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7376 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7377 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7378 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7379 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7380 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7381 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7382 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7383 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7384 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7385 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7386 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 7387 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7388 | .llseek = generic_file_llseek, |
| 7389 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 7390 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7391 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7392 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7393 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7394 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 7395 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7396 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7397 | }; |
| 7398 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7399 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7400 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 7401 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7402 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7403 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7404 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7405 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 7406 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 7407 | .set_bit_hook = btrfs_set_bit_hook, |
| 7408 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7409 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 7410 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7411 | }; |
| 7412 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 7413 | /* |
| 7414 | * btrfs doesn't support the bmap operation because swapfiles |
| 7415 | * use bmap to make a mapping of extents in the file. They assume |
| 7416 | * these extents won't change over the life of the file and they |
| 7417 | * use the bmap result to do IO directly to the drive. |
| 7418 | * |
| 7419 | * the btrfs bmap call would return logical addresses that aren't |
| 7420 | * suitable for IO and they also will change frequently as COW |
| 7421 | * operations happen. So, swapfile + btrfs == corruption. |
| 7422 | * |
| 7423 | * For now we're avoiding this by dropping bmap. |
| 7424 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7425 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7426 | .readpage = btrfs_readpage, |
| 7427 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7428 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7429 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7430 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7431 | .invalidatepage = btrfs_invalidatepage, |
| 7432 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7433 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 7434 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7435 | }; |
| 7436 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7437 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7438 | .readpage = btrfs_readpage, |
| 7439 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 7440 | .invalidatepage = btrfs_invalidatepage, |
| 7441 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7442 | }; |
| 7443 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7444 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7445 | .getattr = btrfs_getattr, |
| 7446 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7447 | .setxattr = btrfs_setxattr, |
| 7448 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7449 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7450 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7451 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7452 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7453 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7454 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7455 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7456 | .getattr = btrfs_getattr, |
| 7457 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7458 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7459 | .setxattr = btrfs_setxattr, |
| 7460 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7461 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7462 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7463 | .get_acl = btrfs_get_acl, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7464 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7465 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7466 | .readlink = generic_readlink, |
| 7467 | .follow_link = page_follow_link_light, |
| 7468 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 7469 | .getattr = btrfs_getattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7470 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 7471 | .setxattr = btrfs_setxattr, |
| 7472 | .getxattr = btrfs_getxattr, |
| 7473 | .listxattr = btrfs_listxattr, |
| 7474 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7475 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7476 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7477 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 7478 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7479 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 7480 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7481 | }; |